xhPayToolService.php 11 KB

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