xhPayToolService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace common\services;
  3. use biz\order\classes\OrderSendClass;
  4. use biz\order\services\OrderService;
  5. use biz\user\services\UserAssetService;
  6. use biz\user\services\UserService;
  7. use common\components\stringUtil;
  8. use common\components\util;
  9. use Yii;
  10. use common\components\configDict;
  11. use linslin\yii2\curl;
  12. use common\components\weixinUtil;
  13. /**
  14. * 支付工具
  15. * @author sofashi
  16. */
  17. class xhPayToolService
  18. {
  19. /**
  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. $typeList = configDict::getConfig('capitalType');//流水类型列表
  62. switch ($capitalType) {
  63. case $typeList['xhOrder']['id']://购买商品
  64. $order = xhOrderService::getByOrderSn($orderSn);
  65. break;
  66. case $typeList['xhActiveOrder']['id']://活动报名
  67. $order = xhActiveOrderService::getById($orderSn);
  68. break;
  69. case $typeList['xhApplyOrder']['id']:
  70. $order = xhApplyOrderService::getById($orderSn);
  71. break;
  72. default:
  73. Yii::warning('获取订单信息失败,回调传过来的 capitalType 不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  74. util::fail('订单流水类型不明确');
  75. }
  76. if (empty($order)) {
  77. Yii::warning('order info empty,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  78. util::fail('订单信息为空');
  79. }
  80. if ($order['payStatus'] == 1) {
  81. Yii::warning('already pay,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  82. util::fail('已经付款过了');
  83. }
  84. if ($totalFee != $order['actPrice']) {
  85. Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  86. util::fail('订单出错了');
  87. }
  88. $orderId = $order['id'];
  89. $orderSn = $order['orderSn'];
  90. $userId = $order['userId'];
  91. $totalFee = $order['actPrice'];
  92. $merchantId = $order['merchantId'];
  93. $alipayId = isset($alipayParams['alipayId']) ? $alipayParams['alipayId'] : '';
  94. $alipayAccount = isset($alipayParams['alipayAccount']) ? $alipayParams['alipayAccount'] : '';
  95. $addPoint = floor($totalFee);
  96. $addIntegral = $addPoint;
  97. $userIntegral = $addPoint;
  98. $userPoint = $addPoint;
  99. $totalExpend = $totalFee;
  100. $userTotalExpend = $totalExpend;
  101. $merchant = xhMerchantService::getById($merchantId);
  102. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  103. $now = time();
  104. $date = date("Y-m-d H:i:s", $now);
  105. //余额支付
  106. $balancePay = configDict::getConfig('payWay', 'balancePay');//余额支付
  107. if ($payWay == $balancePay && !empty($order['deadline']) && $now > $order['deadline']) {
  108. util::fail('订单已经过期');
  109. }
  110. if (empty($userId) && $payWay == $balancePay) {
  111. $transaction->rollBack();
  112. Yii::warning('未登陆用户无法使用余额支付,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  113. util::fail('没有登陆不能使用余额支付');
  114. }
  115. $updateData = [];
  116. $updateData['alipayId'] = $alipayId;
  117. //支付宝支付
  118. $alipayWay = configDict::getConfig('payWay', 'alipay');
  119. if (empty($userId)) {
  120. if ($payWay == $alipayWay) {
  121. $source = UserService::$userSourceId['alipay']['name'];
  122. $info = ['alipayId' => $alipayId, 'merchantId' => $merchantId];
  123. $user = UserService::replaceUser($info, $source, $merchantId);
  124. $userId = $user['id'];
  125. $userAsset = UserAssetService::getByUserId($userId);
  126. $updateData['userId'] = $userId;
  127. //更新访问时间
  128. UserService::updateVisitTime($merchantId, $userId);
  129. }
  130. } else {
  131. $user = xhUserService::getById($userId);
  132. $userAsset = xhUserAssetService::getByUserId($userId);
  133. }
  134. if (empty($user)) {
  135. util::fail('没有客户信息');
  136. }
  137. $balance = $userAsset['balance'];
  138. if ($payWay == $balancePay && $balance < $totalFee) {
  139. Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  140. util::fail('余额不足');
  141. }
  142. $mAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  143. $mBalance = $mAsset['balance'];
  144. $mExpend = $mAsset['totalExpend'];
  145. $mIncome = stringUtil::calcAdd($mAsset['totalIncome'], $totalFee);
  146. $mUseRecharge = stringUtil::calcAdd($mAsset['usedRecharge'], $totalFee);
  147. $mTotalDeal = $mAsset['totalDeal'] + 1;//总交易数+1
  148. $mUpdateData = ['totalIncome' => $mIncome, 'usedRecharge' => $mUseRecharge, 'totalDeal' => $mTotalDeal];
  149. //支付方式要转到商家资产变更里去
  150. $order['payWay'] = $payWay;
  151. xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $mUpdateData, $order, $capitalType);//商家资产改变
  152. $userName = isset($user['userName']) ? $user['userName'] : '游客';
  153. switch ($capitalType) {
  154. case $typeList['xhOrder']['id']://购买商品
  155. $mCapitalEvent = '购买商品';
  156. $uCapitalEvent = '购买商品';
  157. $uIntegralEvent = '购买商品';
  158. break;
  159. case $typeList['xhActiveOrder']['id']://活动报名
  160. $mCapitalEvent = '活动报名';
  161. $uCapitalEvent = '活动报名';
  162. $uIntegralEvent = '活动报名';
  163. break;
  164. case $typeList['xhApplyOrder']['id']://申请服务
  165. $mCapitalEvent = '申请服务';
  166. $uCapitalEvent = '申请服务';
  167. $uIntegralEvent = '申请服务';
  168. break;
  169. default:
  170. }
  171. $capitalData = [
  172. 'relateId' => $orderId,
  173. 'balance' => $mBalance,
  174. 'totalIncome' => $mIncome,
  175. 'totalExpend' => $mExpend,
  176. 'amount' => $totalFee,
  177. 'io' => 1,
  178. 'shopId' => $order['shopId'],
  179. 'payWay' => $payWay,
  180. 'event' => $mCapitalEvent,
  181. 'merchantId' => $merchantId,
  182. 'userId' => $userId,
  183. 'userName' => $userName,
  184. 'alipayId' => $alipayId,
  185. 'createTime' => $date,
  186. 'addTime' => $now,
  187. 'capitalType' => $capitalType,
  188. ];
  189. $merchantCapital = xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加
  190. $merchantCapitalId = $merchantCapital['id'];
  191. $updateData['capitalId'] = $merchantCapitalId;
  192. $updateData['payStatus'] = 1;//支付成功
  193. $updateData['couponId'] = $couponId;
  194. $updateData['payWay'] = $payWay;
  195. $updateData['payTime'] = $now;
  196. switch ($capitalType) {
  197. case $typeList['xhOrder']['id']://购买商品
  198. xhOrderService::updateById($orderId, $updateData);
  199. //发货里的下单流程完成
  200. OrderSendClass::placeOrder(['orderId' => $orderId, 'payTime' => $now]);
  201. break;
  202. case $typeList['xhActiveOrder']['id']://活动报名
  203. xhActiveOrderService::updateById($orderId, $updateData);
  204. break;
  205. case $typeList['xhApplyOrder']['id']://申请服务
  206. $updateData['inviteCodeStatus'] = 1;
  207. xhApplyOrderService::updateById($orderId, $updateData);
  208. break;
  209. default:
  210. Yii::warning('更新订单失败,回调传过来的 capitalType 不符合要求,即订单流水类型不明确,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
  211. util::fail('订单流水类型不明确');
  212. }
  213. //累计消费
  214. $userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee);
  215. //成长值增加
  216. $userGrowth = $userAsset['growth'] + floor($totalFee);
  217. $userTotalBuyNum = $userAsset['totalBuyNum'] + 1;
  218. $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance;
  219. $upAssetData = [];
  220. $upAssetData['balance'] = $remainBalance;
  221. $upAssetData['growth'] = $userGrowth;
  222. $upAssetData['totalExpend'] = $userTotalExpend;
  223. $upAssetData['totalBuyNum'] = $userTotalBuyNum;
  224. xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
  225. $growData = [
  226. 'userId' => $userId,
  227. 'userName' => $userName,
  228. 'merchantId' => $merchantId,
  229. 'relateId' => $orderId,
  230. 'growth' => $userGrowth,
  231. 'num' => floor($totalFee),
  232. 'io' => 1,
  233. 'payWay' => $payWay,
  234. 'alipayId' => $alipayId,
  235. 'event' => $uIntegralEvent,
  236. 'operatorId' => $userId,
  237. 'createTime' => $date,
  238. 'capitalType' => $capitalType,
  239. 'addTime' => $now,
  240. ];
  241. xhUserGrowthService::add($growData);//用户兑换型积分流水增加
  242. $userCapitalData = [
  243. 'relateId' => $orderId,
  244. 'balance' => $remainBalance,
  245. 'totalIncome' => $userAsset['totalIncome'],
  246. 'totalExpend' => $userTotalExpend,
  247. 'amount' => $totalFee,
  248. 'io' => 0,
  249. 'payWay' => $payWay,
  250. 'event' => $uCapitalEvent,
  251. 'merchantId' => $merchantId,
  252. 'userId' => $userId,
  253. 'alipayId' => $alipayId,
  254. 'userName' => $userName,
  255. 'operateId' => 0,
  256. 'createTime' => $date,
  257. 'addTime' => $now,
  258. 'capitalType' => $capitalType,
  259. ];
  260. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  261. $transaction->commit();
  262. } catch (Exception $e) {
  263. $transaction->rollBack();
  264. util::fail('支付没有成功');
  265. }
  266. }
  267. }