xhPayToolService.php 16 KB

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