CustomRechargeClass.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. namespace bizGhs\custom\classes;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\custom\classes\AccountMoneyClass;
  5. use bizGhs\custom\services\GhsRechargeSettleService;
  6. use bizGhs\ghs\classes\GhsRechargeClass;
  7. use bizGhs\shop\classes\MainClass;
  8. use bizGhs\shop\classes\ShopClass as GhsShopClass;
  9. use bizGhs\shop\classes\ShopMoneyClass;
  10. use bizHd\ghs\classes\GhsClass;
  11. use common\components\dict;
  12. use common\components\lakala\Lakala;
  13. use common\components\noticeUtil;
  14. use common\components\orderSn;
  15. use common\components\util;
  16. use bizHd\base\classes\BaseClass;
  17. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  18. use Yii;
  19. class CustomRechargeClass extends BaseClass
  20. {
  21. public static $baseFile = '\bizGhs\custom\models\CustomRecharge';
  22. public static function getRechargeList($where)
  23. {
  24. $data = self::getList('*', $where, 'addTime DESC,id DESC');
  25. return $data;
  26. }
  27. /**
  28. * 客户向供货商线上充值:支付渠道回调入账。
  29. *
  30. * 职责:校验订单与金额、加锁入账、写余额变动与 FIFO 销账。
  31. * 入参:payWay 支付方式;customRechargeSn 充值单号;totalFee 回调金额;attach/transactionId 渠道附加信息。
  32. * 返回:true 表示已处理或重复回调幂等成功;异常单仍 util::fail。
  33. * 关键边界:已 payStatus=1 的重复通知只记日志并 return,避免 util::fail 导致渠道收不到 SUCCESS 而反复重试。
  34. */
  35. public static function thirdPay($payWay, $customRechargeSn, $totalFee, $attach, $transactionId)
  36. {
  37. $customRecharge = self::getByCondition(['orderSn' => $customRechargeSn], true);
  38. if (empty($customRecharge)) {
  39. noticeUtil::push('充值订单,单号:' . $customRechargeSn . ',没有找到订单', '15280215347');
  40. $msg = "没有找到充值订单 orderSn:{$customRechargeSn}";
  41. Yii::info($msg);
  42. util::fail($msg);
  43. }
  44. if ($customRecharge->amount != $totalFee) {
  45. noticeUtil::push("充值订单,支付回调错误,零售订单金额与回调通知金额不一致 orderSn:{$customRechargeSn} {$customRecharge->amount} {$totalFee}", '15280215347');
  46. $msg = "充值订单金额与回调通知金额不一致 orderSn:{$customRechargeSn} {$customRecharge->amount} {$totalFee}";
  47. Yii::info($msg);
  48. util::fail($msg);
  49. }
  50. $amount = $totalFee;
  51. $id = $customRecharge->id;
  52. $customRecharge = self::getLockById($id);
  53. // 加锁后再判已付:防并发双入账,重复回调静默成功以便 NoticeController 回 SUCCESS
  54. if ((int)$customRecharge->payStatus === 1) {
  55. Yii::info("充值订单重复支付回调,已处理过,幂等跳过 orderSn:{$customRechargeSn}");
  56. return true;
  57. }
  58. $payDate = date("Y-m-d H:i:s");
  59. $customRecharge->payTime = $payDate;
  60. $customRecharge->payWay = $payWay;
  61. $customRecharge->payStatus = 1;
  62. $customRecharge->status = 1;
  63. $customRecharge->returnCode = $transactionId;
  64. $customRecharge->onlinePay = dict::getDict('onlinePay', 'yes');
  65. $customRecharge->save();
  66. $ghsRechargeId = $customRecharge->ghsRechargeId ?? 0;
  67. $ghsRecharge = GhsRechargeClass::getById($ghsRechargeId, true);
  68. if (empty($ghsRecharge)) {
  69. noticeUtil::push("充值订单,支付回调错误,没有找到对应单号 orderSn:{$customRechargeSn}", '15280215347');
  70. $msg = "充值订单,支付回调错误,没有找到对应单号 orderSn:{$customRechargeSn}";
  71. Yii::info($msg);
  72. util::fail($msg);
  73. }
  74. $ghsRecharge->payTime = $payDate;
  75. $ghsRecharge->payWay = $payWay;
  76. $ghsRecharge->payStatus = 1;
  77. $ghsRecharge->status = 1;
  78. $ghsRecharge->onlinePay = dict::getDict('onlinePay', 'yes');
  79. $ghsRecharge->save();
  80. // 充值金额,有多个地方要同步修改,请搜索关键词 custom_ghs_recharge、custom_recharge_ghs_clear_action
  81. // 合并后:入账后由 GhsRechargeSettleService::onlinePayFifoClear 按本次来款 FIFO 销账
  82. $customRechargeId = $customRecharge->id ?? 0;
  83. $customId = $customRecharge->customId ?? 0;
  84. $ghsId = $customRecharge->ghsId ?? 0;
  85. $custom = CustomClass::getLockById($customId);
  86. $ghsInfo = GhsClass::getLockById($ghsId);
  87. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  88. AccountMoneyClass::ensureGhsMoneyReady($ghsInfo, true);
  89. $rechargeEvent = '线上充值';
  90. $customMainId = $custom->mainId ?? 0;
  91. $customShopId = $custom->shopId ?? 0;
  92. $customName = $custom->name??'';
  93. $ghsMainId = $ghsInfo->mainId ?? 0;
  94. $ghsShopId = $ghsInfo->shopId ?? 0;
  95. $ghsName = $ghsInfo->name??'';
  96. $customStaffId = $ghsRecharge->staffId ?? 0;
  97. $customStaffName = $ghsRecharge->staffName ?? '';
  98. // 入账前净余额:用于判断结账单是否纯本次线上来款销账(有历史正余额则不标 onlinePay)
  99. $balanceBeforeRecharge = bcadd((string)($custom->balance ?? '0'), '0', 2);
  100. $custom->balance = bcadd($custom->balance, $amount, 2);
  101. $custom->save();
  102. $capitalType = dict::getDict('capitalType', 'customRechargeToGhs', 'id');
  103. $fromType = dict::getDict('fromType', 'shop');
  104. $cbData = [
  105. 'customId' => $customId,
  106. 'customName'=>$customName,
  107. 'relateId' => $customRechargeId,
  108. 'onlinePay' => 2,
  109. 'ptStyle' => 2,
  110. 'capitalType' => $capitalType,
  111. 'amount' => $amount,
  112. 'balance' => $custom->balance,
  113. 'staffId' => 0,
  114. 'staffName' => '',
  115. 'io' => 1,
  116. 'side' => 1,
  117. 'payWay' => $payWay,
  118. 'fromType' => $fromType,
  119. 'event' => $rechargeEvent,
  120. 'mainId' => $ghsMainId,
  121. 'shopId' => $ghsShopId,
  122. 'sjId' => 0,
  123. 'remark' => '',
  124. ];
  125. CustomBalanceChangeClass::add($cbData, true);
  126. $ghsInfo->balance = bcadd($ghsInfo->balance, $amount, 2);
  127. $ghsInfo->save();
  128. $ghsRechargeId = $ghsRecharge->id ?? 0;
  129. $gbData = [
  130. 'ghsId' => $ghsId,
  131. 'ghsName'=>$ghsName,
  132. 'relateId' => $ghsRechargeId,
  133. 'ptStyle' => 2,
  134. 'capitalType' => $capitalType,
  135. 'amount' => $amount,
  136. 'balance' => $ghsInfo->balance,
  137. 'staffId' => $customStaffId,
  138. 'staffName' => $customStaffName,
  139. 'io' => 1,
  140. 'side' => 1,
  141. 'onlinePay' => 2,
  142. 'payWay' => $payWay,
  143. 'fromType' => $fromType,
  144. 'event' => $rechargeEvent,
  145. 'sjId' => 0,
  146. 'mainId' => $customMainId,
  147. 'shopId' => $customShopId,
  148. 'remark' => '',
  149. ];
  150. GhsBalanceChangeClass::add($gbData, true);
  151. $ghsRecharge->balance = $ghsInfo->balance;
  152. $ghsRecharge->save();
  153. $customRecharge->balance = $custom->balance;
  154. $customRecharge->save();
  155. if ($customRecharge->onlinePay == dict::getDict('onlinePay', 'yes')) {
  156. //供货商门店余额增加
  157. $ghsShop = ShopClass::getById($ghsShopId, true);
  158. $ghsMain = MainClass::getLockById($ghsMainId);
  159. ShopClass::customRechargeAddBalance($ghsMain, $ghsShop, $customRecharge);
  160. }
  161. /**
  162. * 线上充值自动销账(详见 GhsRechargeSettleService::onlinePayFifoClear)。
  163. * 前置:balance 已 += amount;本步用「本次实付 amount」作资金池,FIFO 销欠款订单。
  164. * $balanceBeforeRecharge:入账前 balance,决定结账单标线上付还是余额付。
  165. */
  166. $ghsShop = ShopClass::getById($ghsShopId, true);
  167. if (!empty($ghsShop)) {
  168. GhsRechargeSettleService::onlinePayFifoClear(
  169. $custom,
  170. $ghsInfo,
  171. $ghsShop,
  172. $amount,
  173. $customRecharge,
  174. $ghsRecharge,
  175. $balanceBeforeRecharge
  176. );
  177. }
  178. return true;
  179. }
  180. /**
  181. * 供货商后台:对已付款充值单退款(线上原路退 / 线下扣减余额)。
  182. *
  183. * @param int $rechargeId xhCustomRecharge.id
  184. * @param object $shop 当前供货商门店
  185. * @param object $staff 操作员工(记余额变动 staffId/staffName)
  186. */
  187. public static function refundPaidRecharge($rechargeId, $shop, $staff)
  188. {
  189. $rechargeId = intval($rechargeId);
  190. if ($rechargeId <= 0) {
  191. util::fail('参数错误');
  192. }
  193. $customRecharge = self::getLockById($rechargeId);
  194. if (empty($customRecharge)) {
  195. util::fail('没有找到充值记录');
  196. }
  197. $shopMainId = intval($shop->mainId ?? 0);
  198. if (intval($customRecharge->mainId ?? 0) !== $shopMainId) {
  199. util::fail('没有权限操作该充值记录');
  200. }
  201. if (intval($customRecharge->payStatus ?? 0) !== 1) {
  202. util::fail('仅已付款的充值可退款');
  203. }
  204. if (self::isRechargeRefunded($customRecharge)) {
  205. util::fail('该充值已退款');
  206. }
  207. $amount = bcadd((string)($customRecharge->amount ?? '0'), '0', 2);
  208. if (bccomp($amount, '0', 2) <= 0) {
  209. util::fail('充值金额有误');
  210. }
  211. $customId = intval($customRecharge->customId ?? 0);
  212. $pair = GhsRechargeSettleService::lockAccountPair(['id' => $customId]);
  213. $custom = $pair['custom'];
  214. $ghs = $pair['ghs'];
  215. if (empty($custom)) {
  216. util::fail('没有找到客户');
  217. }
  218. $onlinePayYes = intval(dict::getDict('onlinePay', 'yes'));
  219. $isOnline = intval($customRecharge->onlinePay ?? 0) === $onlinePayYes;
  220. $payWay = intval($customRecharge->payWay ?? -1);
  221. $wxPay = intval(dict::getDict('payWay', 'wxPay'));
  222. $aliPay = intval(dict::getDict('payWay', 'alipay'));
  223. $cashPay = intval(dict::getDict('payWay', 'cash'));
  224. // 线上退款:先调支付渠道原路退回(拉卡拉商户取充值单所属门店)
  225. if ($isOnline) {
  226. if ($payWay !== $wxPay && $payWay !== $aliPay) {
  227. util::fail('暂不支持该支付方式原路退款');
  228. }
  229. $returnCode = trim((string)($customRecharge->returnCode ?? ''));
  230. if ($returnCode === '') {
  231. util::fail('缺少支付流水号,无法原路退款');
  232. }
  233. $payShopId = intval($customRecharge->shopId ?? 0);
  234. $payShop = $payShopId > 0 ? ShopClass::getById($payShopId, true) : $shop;
  235. if (empty($payShop)) {
  236. util::fail('没有找到收款门店');
  237. }
  238. self::refundOnlineByLakala($customRecharge, $payShop, $amount, $returnCode);
  239. }
  240. $staffId = intval($staff->id ?? 0);
  241. $staffName = (string)($staff->name ?? '');
  242. $newBalance = CustomClass::deductPairBalanceForRefund($custom, $ghs, $amount);
  243. $capitalType = dict::getDict('capitalType', 'customRechargeRefund', 'id');
  244. $fromType = dict::getDict('fromType', 'shop');
  245. $orderSn = (string)($customRecharge->orderSn ?? '');
  246. $customName = (string)($custom->name ?? '');
  247. $ghsName = (string)($ghs->name ?? ($customRecharge->ghsName ?? ''));
  248. $event = '充值退款(操作人:' . $staffName . ')单号:' . $orderSn;
  249. $shopId = intval($shop->id ?? 0);
  250. $mainId = intval($shop->mainId ?? 0);
  251. $sjId = intval($shop->sjId ?? 0);
  252. $onlinePayVal = $isOnline ? $onlinePayYes : intval(dict::getDict('onlinePay', 'not'));
  253. CustomBalanceChangeClass::add([
  254. 'customId' => $customId,
  255. 'customName' => $customName,
  256. 'relateId' => $rechargeId,
  257. 'onlinePay' => $onlinePayVal,
  258. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  259. 'capitalType' => $capitalType,
  260. 'amount' => $amount,
  261. 'balance' => $newBalance,
  262. 'staffId' => $staffId,
  263. 'staffName' => $staffName,
  264. 'io' => 0,
  265. 'side' => 0,
  266. 'payWay' => $payWay,
  267. 'fromType' => $fromType,
  268. 'event' => $event,
  269. 'mainId' => $mainId,
  270. 'shopId' => $shopId,
  271. 'sjId' => $sjId,
  272. 'remark' => '',
  273. ], true);
  274. $customShopId = intval($custom->shopId ?? 0);
  275. $customShop = GhsShopClass::getById($customShopId, true);
  276. $ghsRechargeId = intval($customRecharge->ghsRechargeId ?? 0);
  277. GhsBalanceChangeClass::add([
  278. 'ghsId' => intval($ghs->id ?? 0),
  279. 'ghsName' => $ghsName,
  280. 'relateId' => $ghsRechargeId,
  281. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  282. 'capitalType' => $capitalType,
  283. 'amount' => $amount,
  284. 'balance' => $newBalance,
  285. 'staffId' => $staffId,
  286. 'staffName' => $staffName,
  287. 'io' => 0,
  288. 'side' => 0,
  289. 'onlinePay' => $onlinePayVal,
  290. 'payWay' => $payWay,
  291. 'fromType' => $fromType,
  292. 'event' => $event,
  293. 'sjId' => $customShop->sjId ?? 0,
  294. 'mainId' => $customShop->mainId ?? 0,
  295. 'shopId' => $customShopId,
  296. 'remark' => '',
  297. ], true);
  298. // 线上充值曾增加门店 xhMain 余额,退款时对称扣减(不操作平台资产 PtAsset)
  299. if ($isOnline) {
  300. $ghsShopId = intval($customRecharge->shopId ?? 0);
  301. $ghsShop = ShopClass::getById($ghsShopId, true);
  302. if (empty($ghsShop)) {
  303. util::fail('没有找到供货商门店');
  304. }
  305. $ghsMain = MainClass::getLockById($ghsShop->mainId ?? 0);
  306. if (empty($ghsMain)) {
  307. util::fail('没有找到资产信息');
  308. }
  309. $originCapitalType = dict::getDict('capitalType', 'customRechargeToGhs', 'id');
  310. $rechargeType = intval($customRecharge->rechargeType ?? 0);
  311. if ($rechargeType === 1) {
  312. $originCapitalType = dict::getDict('capitalType', 'ghsHelpCustomRechargeReturn', 'id');
  313. }
  314. ShopClass::customRechargeRefundReduceBalance($ghsMain, $ghsShop, $customRecharge, $originCapitalType, $staffName);
  315. }
  316. // 线下现金充值:扣减 xhMain.money
  317. if (!$isOnline && $payWay === $cashPay) {
  318. $m = MainClass::getById($mainId, true, 'id, money');
  319. if (!empty($m)) {
  320. $moneyBefore = $m->money ?? 0;
  321. $m->money = bcsub((string)$moneyBefore, $amount, 2);
  322. $m->save(false, ['money']);
  323. ShopMoneyClass::addData([
  324. 'amount' => $amount,
  325. 'balance' => $m->money,
  326. 'io' => 0,
  327. 'mainId' => $mainId,
  328. 'capitalType' => $capitalType,
  329. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  330. 'event' => '客户(' . $customName . ')充值退款' . $amount . '元',
  331. 'remark' => '操作人:' . $staffName,
  332. ]);
  333. }
  334. }
  335. self::markRechargeRefunded($customRecharge);
  336. if ($ghsRechargeId > 0) {
  337. $ghsRecharge = GhsRechargeClass::getLockById($ghsRechargeId);
  338. if (!empty($ghsRecharge)) {
  339. self::markRechargeRefunded($ghsRecharge);
  340. $ghsRecharge->balance = $newBalance;
  341. $ghsRecharge->save(false);
  342. }
  343. }
  344. $customRecharge->balance = $newBalance;
  345. $customRecharge->save(false);
  346. return $customRecharge;
  347. }
  348. /** 充值记录是否已退款(兼容未加 refundStatus 列的环境) */
  349. protected static function isRechargeRefunded($recharge)
  350. {
  351. if (empty($recharge)) {
  352. return true;
  353. }
  354. if (self::modelHasAttr($recharge, 'refundStatus')) {
  355. return intval($recharge->refundStatus ?? 0) === 1;
  356. }
  357. return false;
  358. }
  359. /** 标记充值单已退款 */
  360. protected static function markRechargeRefunded($recharge)
  361. {
  362. if (empty($recharge) || !self::modelHasAttr($recharge, 'refundStatus')) {
  363. return;
  364. }
  365. $recharge->refundStatus = 1;
  366. $recharge->save(false, ['refundStatus']);
  367. }
  368. protected static function modelHasAttr($model, $attr)
  369. {
  370. return is_object($model) && method_exists($model, 'hasAttribute') && $model->hasAttribute($attr);
  371. }
  372. /**
  373. * 拉卡拉原路退款(微信/支付宝线上充值)。
  374. */
  375. protected static function refundOnlineByLakala($customRecharge, $shop, $amount, $returnCode)
  376. {
  377. $termNo = $shop->lklScanTermNo ?? '';
  378. $payWay = intval($customRecharge->payWay ?? 0);
  379. if ($payWay === intval(dict::getDict('payWay', 'alipay'))) {
  380. $termNo = $shop->lklB2BTermNo ?? '';
  381. }
  382. if (empty($shop->lklSjNo) || empty($termNo)) {
  383. util::fail('门店未配置拉卡拉商户信息');
  384. }
  385. $merchantPrivateKeyPath = Yii::getAlias('@vendor/lakala') . '/production/api_private_key.pem';
  386. $lklCertificatePath = Yii::getAlias('@vendor/lakala') . '/production/lkl-apigw-v1.cer';
  387. $params = [
  388. 'appid' => 'OP00002119',
  389. 'serial_no' => '018b08cfddbd',
  390. 'merchant_no' => $shop->lklSjNo,
  391. 'term_no' => $termNo,
  392. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  393. 'lklCertificatePath' => $lklCertificatePath,
  394. ];
  395. $laResource = new Lakala($params);
  396. $refundSn = orderSn::getGhsCustomRechargeSn();
  397. $refundFee = bcmul($amount, 100);
  398. $aliParams = [
  399. 'refundSn' => $refundSn,
  400. 'orderSn' => $customRecharge->orderSn ?? '',
  401. 'refundAmount' => $refundFee,
  402. 'refundReason' => '充值退款',
  403. 'thirdNo' => $returnCode,
  404. ];
  405. $response = $laResource->refund($aliParams);
  406. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  407. $errMsg = $response['msg'] ?? '退款失败';
  408. util::fail('原路退款失败:' . $errMsg);
  409. }
  410. }
  411. }