xhPayToolService.php 14 KB

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