xhPayToolService.php 14 KB

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