xhPayToolService.php 18 KB

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