xhPayToolService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace common\services;
  3. use biz\shop\classes\ShopAdminClass;
  4. use biz\shop\models\Shop;
  5. use bizHd\coupon\classes\CouponClass;
  6. use bizHd\merchant\classes\ShopClass;
  7. use bizHd\order\classes\OrderClass;
  8. use bizHd\purchase\classes\PurchaseClass;
  9. use bizHd\user\classes\UserClass;
  10. use bizHd\user\services\UserAssetService;
  11. use bizHd\user\services\UserService;
  12. use biz\recharge\classes\RechargeClass;
  13. use common\components\noticeUtil;
  14. use common\components\stringUtil;
  15. use common\components\util;
  16. use Yii;
  17. use common\components\dict;
  18. /**
  19. * 支付工具
  20. */
  21. class xhPayToolService
  22. {
  23. //余额支付
  24. public static function balancePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
  25. {
  26. $payWay = dict::getDict('payWay', 'balancePay');
  27. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
  28. }
  29. /**
  30. * 微信支付
  31. */
  32. public static function wxPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
  33. {
  34. $payWay = dict::getDict('payWay', 'wxPay');
  35. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
  36. }
  37. /**
  38. * 支付宝支付
  39. */
  40. public static function alipay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $alipayParams)
  41. {
  42. $payWay = dict::getDict('payWay', 'alipay');
  43. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
  44. }
  45. /**
  46. * 现金支付,转店主微信,转店主支付宝
  47. */
  48. public static function cashPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
  49. {
  50. $payWay = dict::getDict('payWay', 'cashPay');
  51. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
  52. }
  53. //供货商订单支付成功 ssh 2021.3.15
  54. public static function ghsOrderPay($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
  55. {
  56. $order = \bizGhs\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn]);
  57. if (empty($order)) {
  58. Yii::warning("支付回调通知,没有找到订单 编号{$orderSn} ");
  59. util::end();
  60. }
  61. if ($order['payStatus'] == 2) {
  62. Yii::warning("支付回调通知,订单已付款 编号{$orderSn} ");
  63. util::end();
  64. }
  65. if ($totalFee != $order['actPrice']) {
  66. Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  67. util::end();
  68. }
  69. $shopId = $order['shopId'] ?? 0;
  70. $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
  71. if (empty($shop)) {
  72. Yii::warning('支付第三方回调通知,capitalType:' . $capitalType . ' orderSn:' . $orderSn . " 没有找到门店64");
  73. util::end();
  74. }
  75. \bizGhs\order\classes\OrderClass::payOk($order, 3, $shop);
  76. $transaction->commit();
  77. }
  78. //充值回调处理 ssh 2021.2.21
  79. public static function recharge($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
  80. {
  81. $recharge = RechargeClass::getByCondition(['orderSn' => $orderSn], true);
  82. if (empty($recharge)) {
  83. noticeUtil::push('充值付款成功,收到回调,但没有找到充值记录,orderSn:' . $orderSn, '15280215347');
  84. return ['amount' => $totalFee];
  85. }
  86. if ($totalFee != $recharge->amount) {
  87. noticeUtil::push("充值付款成功,收到回调,但金额不一致 {$totalFee} {$recharge->amount},orderSn:" . $orderSn, '15280215347');
  88. return ['amount' => $totalFee];
  89. }
  90. if ($recharge->payStatus == RechargeClass::PAY_STATUS_HAS_PAY) {
  91. noticeUtil::push("充值付款成功,收到回调,但订单是已经支付过了,orderSn:" . $orderSn, '15280215347');
  92. return ['amount' => $totalFee];
  93. }
  94. $rechargeRenew = $callbackParams['rechargeRenew'] ?? 0;
  95. //处理充值成功的流程
  96. RechargeClass::complete($recharge, $rechargeRenew);
  97. $transaction->commit();
  98. return ['amount' => $totalFee];
  99. }
  100. //零售采购回调 ssh 2021.2.28
  101. public static function retailPurchase($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
  102. {
  103. $purchase = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  104. if (empty($purchase)) {
  105. return [];
  106. }
  107. PurchaseClass::complete($purchase);
  108. $transaction->commit();
  109. }
  110. /**
  111. * 基础支付
  112. * $capitalType 需要支付订单来源:xhOrder xhActiveOrder xhApplyOrder
  113. * $payWay 支付方式:wxPay alipay balancePay cashPay
  114. */
  115. public static function basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams = [])
  116. {
  117. $connection = Yii::$app->db;
  118. $transaction = $connection->beginTransaction();
  119. try {
  120. $capitalTypeList = dict::getDict('capitalType');//流水类型列表
  121. $order = [];
  122. switch ($capitalType) {
  123. case $capitalTypeList['xhRecharge']['id']:
  124. return self::recharge($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
  125. Yii::$app->end();
  126. break;
  127. case $capitalTypeList['xhOrder']['id']:
  128. //购买商品
  129. $order = xhOrderService::getByOrderSn($orderSn);
  130. break;
  131. case $capitalTypeList['xhActiveOrder']['id']:
  132. //活动报名
  133. $order = xhActiveOrderService::getById($orderSn);
  134. break;
  135. case $capitalTypeList['xhApplyOrder']['id']:
  136. $order = xhApplyOrderService::getById($orderSn);
  137. break;
  138. case $capitalTypeList['xhGhsOrder']['id']:
  139. //供货商订单
  140. return self::ghsOrderPay($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
  141. Yii::$app->end();
  142. break;
  143. case $capitalTypeList['xhGhsOrder']['id']:
  144. //零售店采购
  145. return self::retailPurchase($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
  146. Yii::$app->end();
  147. break;
  148. default:
  149. Yii::warning('获取订单信息失败,回调传过来的 capitalType 不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  150. util::fail('订单流水类型不明确');
  151. }
  152. if (empty($order)) {
  153. Yii::warning('order info empty,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  154. util::fail('订单信息为空');
  155. }
  156. if ($order['payStatus'] == 1) {
  157. Yii::warning('already pay,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  158. util::fail('已经付款过了');
  159. }
  160. if ($totalFee != $order['actPrice']) {
  161. Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  162. util::fail('订单出错了(第三方回调金额与订单金额不一致)');
  163. }
  164. $orderId = $order['id'];
  165. $orderSn = $order['orderSn'];
  166. $userId = $order['userId'] ?? 0;
  167. $totalFee = $order['actPrice'];
  168. $sjId = $order['sjId'];
  169. $alipayId = isset($alipayParams['alipayId']) ? $alipayParams['alipayId'] : '';
  170. $alipayAccount = isset($alipayParams['alipayAccount']) ? $alipayParams['alipayAccount'] : '';
  171. $addPoint = floor($totalFee);
  172. $addIntegral = $addPoint;
  173. $userIntegral = $addPoint;
  174. $userPoint = $addPoint;
  175. $totalExpend = $totalFee;
  176. $userTotalExpend = $totalExpend;
  177. $merchant = xhMerchantService::getById($sjId);
  178. $merchantExtend = xhMerchantExtendService::getBySjId($sjId);
  179. $shopId = isset($order['shopId']) ? $order['shopId'] : 0;
  180. if (empty($shopId)) {
  181. $shopId = ShopClass::getDefaultShopId($merchant);
  182. }
  183. $now = time();
  184. $date = date("Y-m-d H:i:s", $now);
  185. //余额支付
  186. $balancePay = dict::getDict('payWay', 'balancePay');//余额支付
  187. if ($payWay == $balancePay && !empty($order['deadline']) && $now > $order['deadline']) {
  188. util::fail('订单已经过期');
  189. }
  190. if (empty($userId) && $payWay == $balancePay) {
  191. $transaction->rollBack();
  192. Yii::warning('未登陆不能使用余额支付,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  193. util::fail('没有登陆不能使用余额支付');
  194. }
  195. //使用优惠券
  196. CouponClass::useCoupon($couponId, $orderId);
  197. $updateData = [];
  198. $updateData['alipayId'] = $alipayId;
  199. //支付宝支付
  200. $alipayWay = dict::getDict('payWay', 'alipay');
  201. $wxWay = dict::getDict('payWay', 'wxPay');
  202. //确认h5还是小程序支付
  203. $wxPayType = isset($callbackParams['wxPayType']) ? $callbackParams['wxPayType'] : 0;
  204. if (empty($userId)) {
  205. if ($payWay == $alipayWay) {
  206. $source = UserClass::$userSourceId['alipay']['name'];
  207. $info = ['alipayId' => $alipayId, 'sjId' => $sjId];
  208. $user = UserService::replaceUser($info, $source, $sjId);
  209. $userId = $user['id'];
  210. $userAsset = UserAssetService::getByUserId($userId);
  211. $updateData['userId'] = $userId;
  212. //更新访问时间
  213. UserService::updateVisitTime($sjId, $userId);
  214. }
  215. } else {
  216. $user = xhUserService::getById($userId);
  217. $userAsset = xhUserAssetService::getByUserId($userId);
  218. }
  219. if (empty($user)) {
  220. //util::fail('没有客户信息');
  221. }
  222. $balance = $userAsset['balance'] ?? 0;
  223. if ($payWay == $balancePay && $balance < $totalFee) {
  224. Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  225. util::fail('余额不足');
  226. }
  227. $mAsset = xhMerchantAssetService::getBySjId($sjId);
  228. $mBalance = $mAsset['balance'];
  229. $mExpend = $mAsset['totalExpend'];
  230. $mIncome = stringUtil::calcAdd($mAsset['totalIncome'], $totalFee);
  231. $mUseRecharge = stringUtil::calcAdd($mAsset['usedRecharge'], $totalFee);
  232. $mTotalDeal = $mAsset['totalDeal'] + 1;//付款成功成交量+1
  233. $mUnSendOrder = $mAsset['unSendOrder'] + 1;//待配送订单+1
  234. $mUnPayOrder = $mAsset['unPayOrder'] - 1;//待付款订单-1
  235. $mUpdateData = [
  236. 'totalIncome' => $mIncome,
  237. 'usedRecharge' => $mUseRecharge,
  238. 'totalDeal' => $mTotalDeal,
  239. 'unPayOrder' => $mUnPayOrder,
  240. 'unSendOrder' => $mUnSendOrder,
  241. ];
  242. //支付方式要转到商家资产变更里去
  243. $order['payWay'] = $payWay;
  244. $order['payTime'] = $now;
  245. //xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $shopId, $mUpdateData, $order, $capitalType);//商家资产改变
  246. $userName = isset($user['userName']) ? $user['userName'] : '游客';
  247. $mCapitalEvent = '';
  248. switch ($capitalType) {
  249. case $capitalTypeList['xhOrder']['id']://购买商品
  250. $mCapitalEvent = '购买商品';
  251. $uCapitalEvent = '购买商品';
  252. $uIntegralEvent = '购买商品';
  253. break;
  254. case $capitalTypeList['xhActiveOrder']['id']://活动报名
  255. $mCapitalEvent = '活动报名';
  256. $uCapitalEvent = '活动报名';
  257. $uIntegralEvent = '活动报名';
  258. break;
  259. case $capitalTypeList['xhApplyOrder']['id']://申请服务
  260. $mCapitalEvent = '申请服务';
  261. $uCapitalEvent = '申请服务';
  262. $uIntegralEvent = '申请服务';
  263. break;
  264. default:
  265. }
  266. $fromType = isset($order['fromType']) ? $order['fromType'] : 0;
  267. $capitalData = [
  268. 'relateId' => $orderId,
  269. 'balance' => $mBalance,
  270. 'totalIncome' => $mIncome,
  271. 'totalExpend' => $mExpend,
  272. 'amount' => $totalFee,
  273. 'io' => 1,
  274. 'shopId' => $order['shopId'],
  275. 'payWay' => $payWay,
  276. 'fromType' => $fromType,
  277. 'event' => $mCapitalEvent,
  278. 'sjId' => $sjId,
  279. 'userId' => $userId,
  280. 'userName' => $userName,
  281. 'alipayId' => $alipayId,
  282. 'createTime' => $date,
  283. 'addTime' => $now,
  284. 'capitalType' => $capitalType,
  285. ];
  286. // $merchantCapital = xhSjCapitalService::add($capitalData);//商家资金流水记录增加
  287. // $merchantCapitalId = $merchantCapital['id'];
  288. // $updateData['capitalId'] = $merchantCapitalId;
  289. $updateData['payStatus'] = 1;//支付成功
  290. $updateData['status'] = OrderClass::ORDER_STATUS_UN_SEND;
  291. $updateData['couponId'] = $couponId;
  292. $updateData['payWay'] = $payWay;
  293. $updateData['payTime'] = $now;
  294. //确认h5还是小程序支付
  295. $updateData['wxPayType'] = $wxPayType;
  296. switch ($capitalType) {
  297. case $capitalTypeList['xhOrder']['id']:
  298. break;
  299. case $capitalTypeList['xhActiveOrder']['id']://活动报名
  300. xhActiveOrderService::updateById($orderId, $updateData);
  301. break;
  302. case $capitalTypeList['xhApplyOrder']['id']://申请服务
  303. $updateData['inviteCodeStatus'] = 1;
  304. xhApplyOrderService::updateById($orderId, $updateData);
  305. break;
  306. default:
  307. Yii::warning('更新订单失败,回调的capitalType不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  308. util::fail('订单流水类型不明确');
  309. }
  310. //累计消费
  311. //$userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee);
  312. //成长值增加
  313. // $userGrowth = $userAsset['growth'] + floor($totalFee);
  314. // $userTotalBuyNum = $userAsset['totalBuyNum'] + 1;
  315. //
  316. // $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance;
  317. // $upAssetData = [];
  318. // $upAssetData['balance'] = $remainBalance;
  319. // $upAssetData['growth'] = $userGrowth;
  320. // $upAssetData['totalExpend'] = $userTotalExpend;
  321. // $upAssetData['totalBuyNum'] = $userTotalBuyNum;
  322. //xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
  323. $growData = [
  324. 'userId' => $userId,
  325. 'userName' => $userName,
  326. 'sjId' => $sjId,
  327. 'relateId' => $orderId,
  328. //'growth' => $userGrowth,
  329. 'num' => floor($totalFee),
  330. 'io' => 1,
  331. 'payWay' => $payWay,
  332. 'alipayId' => $alipayId,
  333. //'event' => $uIntegralEvent,
  334. 'operatorId' => $userId,
  335. 'createTime' => $date,
  336. 'gainType' => 1,
  337. 'addTime' => $now,
  338. ];
  339. //xhUserGrowthService::add($growData);//用户兑换型积分流水增加
  340. $userCapitalData = [
  341. 'relateId' => $orderId,
  342. //'balance' => $remainBalance,
  343. 'totalIncome' => $userAsset['totalIncome'] ?? 0,
  344. 'totalExpend' => $userTotalExpend,
  345. 'amount' => $totalFee,
  346. 'io' => 0,
  347. 'payWay' => $payWay,
  348. //'event' => $uCapitalEvent,
  349. 'sjId' => $sjId,
  350. 'userId' => $userId,
  351. 'alipayId' => $alipayId,
  352. 'userName' => $userName,
  353. 'operateId' => 0,
  354. 'createTime' => $date,
  355. 'addTime' => $now,
  356. 'capitalType' => $capitalType,
  357. ];
  358. //xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  359. $transaction->commit();
  360. $shopId = $order['shopId'] ?? 0;
  361. $money = $order['actPrice'] ?? 0.00;
  362. ShopAdminClass::incomeNotice($shopId, $money, 0);
  363. //如果是小程序支付可以获取unionId并进行更新关联
  364. if ($payWay == $wxWay && $wxPayType == 1) {
  365. //OrderClass::payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user);
  366. }
  367. return ['balance' => 0];
  368. //return ['balance' => $remainBalance];
  369. } catch (Exception $e) {
  370. $transaction->rollBack();
  371. util::fail('支付没有成功');
  372. }
  373. }
  374. }