xhPayToolService.php 14 KB

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