xhPayToolService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace common\services;
  3. use biz\coupon\classes\CouponClass;
  4. use biz\order\classes\OrderSendClass;
  5. use biz\order\services\OrderService;
  6. use biz\user\classes\UserClass;
  7. use biz\user\services\UserAssetService;
  8. use biz\user\services\UserService;
  9. use common\components\stringUtil;
  10. use common\components\util;
  11. use Yii;
  12. use common\components\configDict;
  13. use linslin\yii2\curl;
  14. use common\components\wxUtil;
  15. /**
  16. * 支付工具
  17. */
  18. class xhPayToolService
  19. {
  20. //余额支付
  21. public static function balancePay($orderSn, $totalFee, $capitalType, $couponId)
  22. {
  23. $payWay = configDict::getConfig('payWay', 'balancePay');
  24. return self::basePay($orderSn, $totalFee, $capitalType, $couponId, $payWay);
  25. }
  26. /**
  27. * 微信支付
  28. */
  29. public static function weixinPay($orderSn, $totalFee, $capitalType, $couponId)
  30. {
  31. $payWay = configDict::getConfig('payWay', 'weixinPay');
  32. return self::basePay($orderSn, $totalFee, $capitalType, $couponId, $payWay);
  33. }
  34. /**
  35. * 支付宝支付
  36. */
  37. public static function alipay($orderSn, $totalFee, $capitalType, $couponId, $alipayParams)
  38. {
  39. $payWay = configDict::getConfig('payWay', 'alipay');
  40. return self::basePay($orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
  41. }
  42. /**
  43. * 现金支付,转店主微信,转店主支付宝
  44. */
  45. public static function cashPay($orderSn, $totalFee, $capitalType, $couponId)
  46. {
  47. $payWay = configDict::getConfig('payWay', 'cashPay');
  48. return self::basePay($orderSn, $totalFee, $capitalType, $couponId, $payWay);
  49. }
  50. /**
  51. * 基础支付
  52. * $capitalType 需要支付订单来源:xhOrder xhActiveOrder xhApplyOrder
  53. * $payWay 支付方式:weixinPay alipay balancePay cashPay
  54. */
  55. public static function basePay($orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams = [])
  56. {
  57. $connection = Yii::$app->db;//事务处理
  58. $transaction = $connection->beginTransaction();
  59. try {
  60. //使用优惠券
  61. CouponClass::useCoupon($couponId);
  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. $now = time();
  105. $date = date("Y-m-d H:i:s", $now);
  106. //余额支付
  107. $balancePay = configDict::getConfig('payWay', 'balancePay');//余额支付
  108. if ($payWay == $balancePay && !empty($order['deadline']) && $now > $order['deadline']) {
  109. util::fail('订单已经过期');
  110. }
  111. if (empty($userId) && $payWay == $balancePay) {
  112. $transaction->rollBack();
  113. Yii::warning('未登陆用户无法使用余额支付,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  114. util::fail('没有登陆不能使用余额支付');
  115. }
  116. $updateData = [];
  117. $updateData['alipayId'] = $alipayId;
  118. //支付宝支付
  119. $alipayWay = configDict::getConfig('payWay', 'alipay');
  120. if (empty($userId)) {
  121. if ($payWay == $alipayWay) {
  122. $source = UserClass::$userSourceId['alipay']['name'];
  123. $info = ['alipayId' => $alipayId, 'merchantId' => $merchantId];
  124. $user = UserService::replaceUser($info, $source, $merchantId);
  125. $userId = $user['id'];
  126. $userAsset = UserAssetService::getByUserId($userId);
  127. $updateData['userId'] = $userId;
  128. //更新访问时间
  129. UserService::updateVisitTime($merchantId, $userId);
  130. }
  131. } else {
  132. $user = xhUserService::getById($userId);
  133. $userAsset = xhUserAssetService::getByUserId($userId);
  134. }
  135. if (empty($user)) {
  136. util::fail('没有客户信息');
  137. }
  138. $balance = $userAsset['balance'];
  139. if ($payWay == $balancePay && $balance < $totalFee) {
  140. Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  141. util::fail('余额不足');
  142. }
  143. $mAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  144. $mBalance = $mAsset['balance'];
  145. $mExpend = $mAsset['totalExpend'];
  146. $mIncome = stringUtil::calcAdd($mAsset['totalIncome'], $totalFee);
  147. $mUseRecharge = stringUtil::calcAdd($mAsset['usedRecharge'], $totalFee);
  148. $mTotalDeal = $mAsset['totalDeal'] + 1;//总交易数+1
  149. $mUpdateData = ['totalIncome' => $mIncome, 'usedRecharge' => $mUseRecharge, 'totalDeal' => $mTotalDeal];
  150. //支付方式要转到商家资产变更里去
  151. $order['payWay'] = $payWay;
  152. xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $mUpdateData, $order, $capitalType);//商家资产改变
  153. $userName = isset($user['userName']) ? $user['userName'] : '游客';
  154. switch ($capitalType) {
  155. case $typeList['xhOrder']['id']://购买商品
  156. $mCapitalEvent = '购买商品';
  157. $uCapitalEvent = '购买商品';
  158. $uIntegralEvent = '购买商品';
  159. break;
  160. case $typeList['xhActiveOrder']['id']://活动报名
  161. $mCapitalEvent = '活动报名';
  162. $uCapitalEvent = '活动报名';
  163. $uIntegralEvent = '活动报名';
  164. break;
  165. case $typeList['xhApplyOrder']['id']://申请服务
  166. $mCapitalEvent = '申请服务';
  167. $uCapitalEvent = '申请服务';
  168. $uIntegralEvent = '申请服务';
  169. break;
  170. default:
  171. }
  172. $capitalData = [
  173. 'relateId' => $orderId,
  174. 'balance' => $mBalance,
  175. 'totalIncome' => $mIncome,
  176. 'totalExpend' => $mExpend,
  177. 'amount' => $totalFee,
  178. 'io' => 1,
  179. 'shopId' => $order['shopId'],
  180. 'payWay' => $payWay,
  181. 'event' => $mCapitalEvent,
  182. 'merchantId' => $merchantId,
  183. 'userId' => $userId,
  184. 'userName' => $userName,
  185. 'alipayId' => $alipayId,
  186. 'createTime' => $date,
  187. 'addTime' => $now,
  188. 'capitalType' => $capitalType,
  189. ];
  190. $merchantCapital = xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加
  191. $merchantCapitalId = $merchantCapital['id'];
  192. $updateData['capitalId'] = $merchantCapitalId;
  193. $updateData['payStatus'] = 1;//支付成功
  194. $updateData['status'] = 1;//支付成功待配送
  195. $updateData['couponId'] = $couponId;
  196. $updateData['payWay'] = $payWay;
  197. $updateData['payTime'] = $now;
  198. Yii::info(json_encode($updateData));
  199. switch ($capitalType) {
  200. case $typeList['xhOrder']['id']://购买商品
  201. xhOrderService::updateById($orderId, $updateData);
  202. Yii::info('22222' . json_encode($updateData));
  203. //发货里的下单流程完成
  204. OrderSendClass::placeOrder(['orderId' => $orderId, 'payTime' => $now]);
  205. break;
  206. case $typeList['xhActiveOrder']['id']://活动报名
  207. xhActiveOrderService::updateById($orderId, $updateData);
  208. break;
  209. case $typeList['xhApplyOrder']['id']://申请服务
  210. $updateData['inviteCodeStatus'] = 1;
  211. xhApplyOrderService::updateById($orderId, $updateData);
  212. break;
  213. default:
  214. Yii::warning('更新订单失败,回调传过来的 capitalType 不符合要求,即订单流水类型不明确,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  215. util::fail('订单流水类型不明确');
  216. }
  217. //累计消费
  218. $userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee);
  219. //成长值增加
  220. $userGrowth = $userAsset['growth'] + floor($totalFee);
  221. $userTotalBuyNum = $userAsset['totalBuyNum'] + 1;
  222. $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance;
  223. $upAssetData = [];
  224. $upAssetData['balance'] = $remainBalance;
  225. $upAssetData['growth'] = $userGrowth;
  226. $upAssetData['totalExpend'] = $userTotalExpend;
  227. $upAssetData['totalBuyNum'] = $userTotalBuyNum;
  228. xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
  229. $growData = [
  230. 'userId' => $userId,
  231. 'userName' => $userName,
  232. 'merchantId' => $merchantId,
  233. 'relateId' => $orderId,
  234. 'growth' => $userGrowth,
  235. 'num' => floor($totalFee),
  236. 'io' => 1,
  237. 'payWay' => $payWay,
  238. 'alipayId' => $alipayId,
  239. 'event' => $uIntegralEvent,
  240. 'operatorId' => $userId,
  241. 'createTime' => $date,
  242. 'gainType' => 1,
  243. 'addTime' => $now,
  244. ];
  245. xhUserGrowthService::add($growData);//用户兑换型积分流水增加
  246. $userCapitalData = [
  247. 'relateId' => $orderId,
  248. 'balance' => $remainBalance,
  249. 'totalIncome' => $userAsset['totalIncome'],
  250. 'totalExpend' => $userTotalExpend,
  251. 'amount' => $totalFee,
  252. 'io' => 0,
  253. 'payWay' => $payWay,
  254. 'event' => $uCapitalEvent,
  255. 'merchantId' => $merchantId,
  256. 'userId' => $userId,
  257. 'alipayId' => $alipayId,
  258. 'userName' => $userName,
  259. 'operateId' => 0,
  260. 'createTime' => $date,
  261. 'addTime' => $now,
  262. 'capitalType' => $capitalType,
  263. ];
  264. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  265. $transaction->commit();
  266. return ['balance' => $remainBalance];
  267. } catch (Exception $e) {
  268. $transaction->rollBack();
  269. util::fail('支付没有成功');
  270. }
  271. }
  272. }