xhPayToolService.php 18 KB

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