xhPayToolService.php 17 KB

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