xhPayToolService.php 10 KB

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