xhPayToolService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace common\services;
  3. use bizGhs\order\classes\OrderSendClass;
  4. use bizHd\coupon\classes\CouponClass;
  5. use bizHd\merchant\classes\ShopClass;
  6. use bizHd\order\classes\OrderClass;
  7. use bizHd\purchase\classes\PurchaseClass;
  8. use bizHd\user\classes\UserClass;
  9. use bizHd\user\services\UserAssetService;
  10. use bizHd\user\services\UserService;
  11. use biz\recharge\classes\RechargeClass;
  12. use common\components\noticeUtil;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use Yii;
  16. use common\components\dict;
  17. use linslin\yii2\curl;
  18. use common\components\wxUtil;
  19. /**
  20. * 支付工具
  21. */
  22. class xhPayToolService
  23. {
  24. //余额支付
  25. public static function balancePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
  26. {
  27. $payWay = dict::getConfig('payWay', 'balancePay');
  28. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
  29. }
  30. /**
  31. * 微信支付
  32. */
  33. public static function wxPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
  34. {
  35. $payWay = dict::getConfig('payWay', 'wxPay');
  36. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
  37. }
  38. /**
  39. * 支付宝支付
  40. */
  41. public static function alipay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $alipayParams)
  42. {
  43. $payWay = dict::getConfig('payWay', 'alipay');
  44. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
  45. }
  46. /**
  47. * 现金支付,转店主微信,转店主支付宝
  48. */
  49. public static function cashPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
  50. {
  51. $payWay = dict::getConfig('payWay', 'cashPay');
  52. return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
  53. }
  54. //供货商订单支付成功 shish 2021.3.15
  55. public static function ghsOrderPay($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
  56. {
  57. $order = \bizGhs\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn]);
  58. if (empty($order)) {
  59. Yii::warning("支付回调通知,没有找到订单 编号{$orderSn} ");
  60. util::end();
  61. }
  62. if ($order['payStatus'] == 2) {
  63. Yii::warning("支付回调通知,订单已付款 编号{$orderSn} ");
  64. util::end();
  65. }
  66. if ($totalFee != $order['actPrice']) {
  67. Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  68. util::end();
  69. }
  70. $shopId = $order['shopId'] ?? 0;
  71. $shop = \biz\shop\classes\ShopClass::getShopInfo($shopId);
  72. if (empty($shop)) {
  73. Yii::warning('支付第三方回调通知,capitalType:' . $capitalType . ' orderSn:' . $orderSn . " 没有找到门店");
  74. util::end();
  75. }
  76. \bizGhs\order\classes\OrderClass::payOk($order, 3, $shop);
  77. $transaction->commit();
  78. }
  79. //充值回调处理 shish 2021.2.21
  80. public static function recharge($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
  81. {
  82. $recharge = RechargeClass::getByCondition(['orderSn' => $orderSn], true);
  83. if (empty($recharge)) {
  84. noticeUtil::push('充值付款成功,收到回调,但没有找到充值记录,orderSn:' . $orderSn, '15280215347');
  85. return ['amount' => $totalFee];
  86. }
  87. if ($totalFee != $recharge->amount) {
  88. noticeUtil::push("充值付款成功,收到回调,但金额不一致 {$totalFee} {$recharge->amount},orderSn:" . $orderSn, '15280215347');
  89. return ['amount' => $totalFee];
  90. }
  91. if ($recharge->payStatus == RechargeClass::PAY_STATUS_HAS_PAY) {
  92. noticeUtil::push("充值付款成功,收到回调,但订单是已经支付过了,orderSn:" . $orderSn, '15280215347');
  93. return ['amount' => $totalFee];
  94. }
  95. //处理充值成功的流程
  96. RechargeClass::complete($recharge);
  97. $transaction->commit();
  98. return ['amount' => $totalFee];
  99. }
  100. //零售采购回调 shish 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::getConfig('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'];
  167. $totalFee = $order['actPrice'];
  168. $merchantId = $order['merchantId'];
  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($merchantId);
  178. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  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::getConfig('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::getConfig('payWay', 'alipay');
  201. $wxWay = dict::getConfig('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, 'merchantId' => $merchantId];
  208. $user = UserService::replaceUser($info, $source, $merchantId);
  209. $userId = $user['id'];
  210. $userAsset = UserAssetService::getByUserId($userId);
  211. $updateData['userId'] = $userId;
  212. //更新访问时间
  213. UserService::updateVisitTime($merchantId, $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'];
  223. if ($payWay == $balancePay && $balance < $totalFee) {
  224. Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  225. util::fail('余额不足');
  226. }
  227. $mAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  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. $mPayOrder = $mAsset['payOrder'] + 1;//付款订单+1
  236. $mUpdateData = [
  237. 'totalIncome' => $mIncome,
  238. 'usedRecharge' => $mUseRecharge,
  239. 'totalDeal' => $mTotalDeal,
  240. 'payOrder' => $mPayOrder,
  241. 'unPayOrder' => $mUnPayOrder,
  242. 'unSendOrder' => $mUnSendOrder,
  243. ];
  244. //支付方式要转到商家资产变更里去
  245. $order['payWay'] = $payWay;
  246. $order['payTime'] = $now;
  247. xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $shopId, $mUpdateData, $order, $capitalType);//商家资产改变
  248. $userName = isset($user['userName']) ? $user['userName'] : '游客';
  249. $mCapitalEvent = '';
  250. switch ($capitalType) {
  251. case $capitalTypeList['xhOrder']['id']://购买商品
  252. $mCapitalEvent = '购买商品';
  253. $uCapitalEvent = '购买商品';
  254. $uIntegralEvent = '购买商品';
  255. break;
  256. case $capitalTypeList['xhActiveOrder']['id']://活动报名
  257. $mCapitalEvent = '活动报名';
  258. $uCapitalEvent = '活动报名';
  259. $uIntegralEvent = '活动报名';
  260. break;
  261. case $capitalTypeList['xhApplyOrder']['id']://申请服务
  262. $mCapitalEvent = '申请服务';
  263. $uCapitalEvent = '申请服务';
  264. $uIntegralEvent = '申请服务';
  265. break;
  266. default:
  267. }
  268. $fromType = isset($order['fromType']) ? $order['fromType'] : 0;
  269. $capitalData = [
  270. 'relateId' => $orderId,
  271. 'balance' => $mBalance,
  272. 'totalIncome' => $mIncome,
  273. 'totalExpend' => $mExpend,
  274. 'amount' => $totalFee,
  275. 'io' => 1,
  276. 'shopId' => $order['shopId'],
  277. 'payWay' => $payWay,
  278. 'fromType' => $fromType,
  279. 'event' => $mCapitalEvent,
  280. 'merchantId' => $merchantId,
  281. 'userId' => $userId,
  282. 'userName' => $userName,
  283. 'alipayId' => $alipayId,
  284. 'createTime' => $date,
  285. 'addTime' => $now,
  286. 'capitalType' => $capitalType,
  287. ];
  288. $merchantCapital = xhSjCapitalService::add($capitalData);//商家资金流水记录增加
  289. $merchantCapitalId = $merchantCapital['id'];
  290. $updateData['capitalId'] = $merchantCapitalId;
  291. $updateData['payStatus'] = 1;//支付成功
  292. $updateData['status'] = 1;//支付成功待配送
  293. $updateData['couponId'] = $couponId;
  294. $updateData['payWay'] = $payWay;
  295. $updateData['payTime'] = $now;
  296. //确认h5还是小程序支付
  297. $updateData['wxPayType'] = $wxPayType;
  298. switch ($capitalType) {
  299. case $capitalTypeList['xhOrder']['id']:
  300. OrderClass::payAfter($order, $updateData);
  301. break;
  302. case $capitalTypeList['xhActiveOrder']['id']://活动报名
  303. xhActiveOrderService::updateById($orderId, $updateData);
  304. break;
  305. case $capitalTypeList['xhApplyOrder']['id']://申请服务
  306. $updateData['inviteCodeStatus'] = 1;
  307. xhApplyOrderService::updateById($orderId, $updateData);
  308. break;
  309. default:
  310. Yii::warning('更新订单失败,回调的capitalType不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  311. util::fail('订单流水类型不明确');
  312. }
  313. //累计消费
  314. $userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee);
  315. //成长值增加
  316. $userGrowth = $userAsset['growth'] + floor($totalFee);
  317. $userTotalBuyNum = $userAsset['totalBuyNum'] + 1;
  318. $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance;
  319. $upAssetData = [];
  320. $upAssetData['balance'] = $remainBalance;
  321. $upAssetData['growth'] = $userGrowth;
  322. $upAssetData['totalExpend'] = $userTotalExpend;
  323. $upAssetData['totalBuyNum'] = $userTotalBuyNum;
  324. xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
  325. $growData = [
  326. 'userId' => $userId,
  327. 'userName' => $userName,
  328. 'merchantId' => $merchantId,
  329. 'relateId' => $orderId,
  330. 'growth' => $userGrowth,
  331. 'num' => floor($totalFee),
  332. 'io' => 1,
  333. 'payWay' => $payWay,
  334. 'alipayId' => $alipayId,
  335. 'event' => $uIntegralEvent,
  336. 'operatorId' => $userId,
  337. 'createTime' => $date,
  338. 'gainType' => 1,
  339. 'addTime' => $now,
  340. ];
  341. xhUserGrowthService::add($growData);//用户兑换型积分流水增加
  342. $userCapitalData = [
  343. 'relateId' => $orderId,
  344. 'balance' => $remainBalance,
  345. 'totalIncome' => $userAsset['totalIncome'],
  346. 'totalExpend' => $userTotalExpend,
  347. 'amount' => $totalFee,
  348. 'io' => 0,
  349. 'payWay' => $payWay,
  350. 'event' => $uCapitalEvent,
  351. 'merchantId' => $merchantId,
  352. 'userId' => $userId,
  353. 'alipayId' => $alipayId,
  354. 'userName' => $userName,
  355. 'operateId' => 0,
  356. 'createTime' => $date,
  357. 'addTime' => $now,
  358. 'capitalType' => $capitalType,
  359. ];
  360. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  361. $transaction->commit();
  362. //如果是小程序支付可以获取unionId并进行更新关联
  363. if ($payWay == $wxWay && $wxPayType == 1) {
  364. OrderClass::payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user);
  365. }
  366. return ['balance' => $remainBalance];
  367. } catch (Exception $e) {
  368. $transaction->rollBack();
  369. util::fail('支付没有成功');
  370. }
  371. }
  372. }