xhPayToolService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace common\services;
  3. use biz\order\services\OrderService;
  4. use biz\tool\services\CouponService;
  5. use biz\user\services\UserAssetService;
  6. use biz\user\services\UserService;
  7. use common\components\stringUtil;
  8. use Yii;
  9. use common\components\configDict;
  10. use linslin\yii2\curl;
  11. use common\components\weixinUtil;
  12. /**
  13. * 支付工具
  14. * @author sofashi
  15. */
  16. class xhPayToolService
  17. {
  18. /**
  19. * 余额支付
  20. */
  21. public static function balancePay($orderId, $totalFee, $capitalType, $couponId)
  22. {
  23. $payWay = configDict::getConfig('payWay', 'balancePay');
  24. return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay);
  25. }
  26. /**
  27. * 微信支付
  28. */
  29. public static function weixinPay($orderId, $totalFee, $capitalType, $couponId)
  30. {
  31. $payWay = configDict::getConfig('payWay', 'weixinPay');
  32. return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay);
  33. }
  34. /**
  35. * 支付宝支付
  36. */
  37. public static function alipay($orderId, $totalFee, $capitalType, $couponId, $alipayParams)
  38. {
  39. $payWay = configDict::getConfig('payWay', 'alipay');
  40. return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
  41. }
  42. /**
  43. * 现金支付,转店主微信,转店主支付宝
  44. */
  45. public static function cashPay($orderId, $totalFee, $capitalType, $couponId)
  46. {
  47. $payWay = configDict::getConfig('payWay', 'cashPay');
  48. return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay);
  49. }
  50. /**
  51. * 基础支付
  52. * $capitalType 需要支付订单来源:xhOrder xhActiveOrder xhApplyOrder
  53. * $payWay 支付方式:weixinPay alipay balancePay cashPay
  54. */
  55. public static function basePay($orderId, $totalFee, $capitalType, $couponId, $payWay, $alipayParams = [])
  56. {
  57. $connection = Yii::$app->db;//事务处理
  58. $transaction = $connection->beginTransaction();
  59. try {
  60. $typeList = configDict::getConfig('capitalType');//流水类型列表
  61. switch ($capitalType) {
  62. case $typeList['xhOrder']['id']://购买商品
  63. $order = xhOrderService::getById($orderId);
  64. break;
  65. case $typeList['xhActiveOrder']['id']://活动报名
  66. $order = xhActiveOrderService::getById($orderId);
  67. break;
  68. case $typeList['xhApplyOrder']['id']:
  69. $order = xhApplyOrderService::getById($orderId);
  70. break;
  71. default:
  72. Yii::warning('获取订单信息失败,回调传过来的 capitalType 不符合要求,capitalType:' . $capitalType . ' orderId:' . $orderId);
  73. return ['code' => 'A0002', 'msg' => '订单流水类型不明确'];
  74. }
  75. if (empty($order)) {
  76. Yii::warning('order info empty,capitalType:' . $capitalType . ' orderId:' . $orderId);
  77. return ['code' => 'A0002', 'msg' => '订单信息为空'];
  78. }
  79. if ($order['payStatus'] == 1) {
  80. Yii::warning('already pay,capitalType:' . $capitalType . ' orderId:' . $orderId);
  81. return ['code' => 'A0002', 'msg' => '已经付款过了'];
  82. }
  83. if ($totalFee != $order['actPrice']) {
  84. Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderId:' . $orderId);
  85. return ['code' => 'A0002', 'msg' => '订单出错了'];
  86. }
  87. $orderId = $order['id'];
  88. $userId = $order['userId'];
  89. $totalFee = $order['actPrice'];
  90. $merchantId = $order['merchantId'];
  91. Yii::$app->params['merchantId'] = $merchantId;
  92. $alipayId = isset($alipayParams['alipayId']) ? $alipayParams['alipayId'] : '';
  93. $alipayAccount = isset($alipayParams['alipayAccount']) ? $alipayParams['alipayAccount'] : '';
  94. $addPoint = floor($totalFee);
  95. $addIntegral = $addPoint;
  96. $userIntegral = $addPoint;
  97. $userPoint = $addPoint;
  98. $totalExpend = $totalFee;
  99. $userTotalExpend = $totalExpend;
  100. $merchant = xhMerchantService::getById($merchantId);
  101. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  102. $now = time();
  103. $date = date("Y-m-d H:i:s", $now);
  104. //余额支付
  105. $balancePay = configDict::getConfig('payWay', 'balancePay');//余额支付
  106. if ($payWay == $balancePay && !empty($order['deadline']) && $now > $order['deadline']) {
  107. return ['code' => 'A0002', 'msg' => '订单已经过期'];
  108. }
  109. if (empty($userId) && $payWay == $balancePay) {
  110. $transaction->rollBack();
  111. Yii::warning('未登陆用户无法使用余额支付,capitalType:' . $capitalType . ' orderId:' . $orderId);
  112. return ['code' => 'A0002', 'msg' => '未登陆用户无法使用余额支付'];
  113. }
  114. $updateData = [];
  115. $updateData['alipayId'] = $alipayId;
  116. //支付宝支付
  117. $alipayWay = configDict::getConfig('payWay', 'alipay');
  118. if (empty($userId)) {
  119. if ($payWay == $alipayWay) {
  120. $source = UserService::$userSourceId['alipay']['name'];
  121. $info = ['alipayId' => $alipayId, 'merchantId' => $merchantId];
  122. $user = UserService::replaceUser($info, $source, $merchantId);
  123. $userId = $user['id'];
  124. $userAsset = UserAssetService::getByUserId($userId);
  125. $updateData['userId'] = $userId;
  126. //更新访问时间
  127. Yii::$app->params['merchantId'] = $merchantId;
  128. Yii::$app->params['userId'] = $userId;
  129. UserService::updateVisitTime();
  130. }
  131. } else {
  132. $user = xhUserService::getById($userId);
  133. $userAsset = xhUserAssetService::getByUserId($userId);
  134. }
  135. if (empty($user)) {
  136. return ['code' => 'A0002', 'msg' => '没有用户信息'];
  137. }
  138. $balance = $userAsset['balance'];
  139. if ($payWay == $balancePay && $balance < $totalFee) {
  140. Yii::warning('余额不足,capitalType:' . $capitalType . ' orderId:' . $orderId);
  141. return ['code' => 'A0002', 'msg' => '余额不足'];
  142. }
  143. $mAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  144. $mBalance = $mAsset['balance'];
  145. $mExpend = $mAsset['totalExpend'];
  146. $mIncome = stringUtil::calcAdd($mAsset['totalIncome'], $totalFee);
  147. $mUseRecharge = stringUtil::calcAdd($mAsset['totalUseRecharge'], $totalFee);
  148. $mTotalDeal = $mAsset['totalDeal'] + 1;//总交易数+1
  149. $mUpdateData = ['totalIncome' => $mIncome, 'totalUseRecharge' => $mUseRecharge, 'totalDeal' => $mTotalDeal];
  150. //支付方式要转到商家资产变更里去
  151. $order['payWay'] = $payWay;
  152. xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $mUpdateData, $order, $capitalType);//商家资产改变
  153. $userName = isset($user['userName']) ? $user['userName'] : '游客';
  154. switch ($capitalType) {
  155. case $typeList['xhOrder']['id']://购买商品
  156. $mCapitalEvent = '购买商品';
  157. $uCapitalEvent = '购买商品';
  158. $uIntegralEvent = '购买商品';
  159. break;
  160. case $typeList['xhActiveOrder']['id']://活动报名
  161. $mCapitalEvent = '活动报名';
  162. $uCapitalEvent = '活动报名';
  163. $uIntegralEvent = '活动报名';
  164. break;
  165. case $typeList['xhApplyOrder']['id']://申请服务
  166. $mCapitalEvent = '申请服务';
  167. $uCapitalEvent = '申请服务';
  168. $uIntegralEvent = '申请服务';
  169. break;
  170. default:
  171. }
  172. $capitalData = [
  173. 'relateId' => (string)$orderId,
  174. 'balance' => $mBalance,
  175. 'totalIncome' => $mIncome,
  176. 'totalExpend' => $mExpend,
  177. 'amount' => $totalFee,
  178. 'io' => 1,
  179. 'shopId' => $order['shopId'],
  180. 'payWay' => $payWay,
  181. 'event' => $mCapitalEvent,
  182. 'merchantId' => $merchantId,
  183. 'userId' => $userId,
  184. 'userName' => $userName,
  185. 'alipayId' => $alipayId,
  186. 'createTime' => $date,
  187. 'addTime' => $now,
  188. 'capitalType' => $capitalType,
  189. ];
  190. $merchantCapital = xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加
  191. $merchantCapitalId = $merchantCapital['id'];
  192. $updateData['capitalId'] = $merchantCapitalId;
  193. $updateData['payStatus'] = 1;//支付成功
  194. $updateData['couponId'] = $couponId;
  195. $updateData['payWay'] = $payWay;
  196. $updateData['payTime'] = $now;
  197. switch ($capitalType) {
  198. case $typeList['xhOrder']['id']://购买商品
  199. xhOrderService::updateById($orderId, $updateData);
  200. break;
  201. case $typeList['xhActiveOrder']['id']://活动报名
  202. xhActiveOrderService::updateById($orderId, $updateData);
  203. break;
  204. case $typeList['xhApplyOrder']['id']://申请服务
  205. $updateData['inviteCodeStatus'] = 1;
  206. xhApplyOrderService::updateById($orderId, $updateData);
  207. break;
  208. default:
  209. Yii::warning('更新订单失败,回调传过来的 capitalType 不符合要求,即订单流水类型不明确,capitalType:' . $capitalType . ' orderId:' . $orderId);
  210. return ['code' => 'A0002', 'msg' => '订单流水类型不明确'];
  211. }
  212. //抵销优惠券并分成
  213. CouponService::destroyToGetReward(['id' => $couponId]);
  214. if (isset($order['inviteCode']) && !empty($order['inviteCode'])) {
  215. $invitedMerchantId = isset($order['invitedMerchantId']) ? $order['invitedMerchantId'] : 0;
  216. xhInviteService::updateByCode($order['inviteCode'], ['takeStatus' => 1, 'inviteUseTo' => 0, 'targetId' => $orderId, 'invitedMerchantId' => $invitedMerchantId]);
  217. }
  218. $userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee);//累计消费
  219. $userIntegral = $userAsset['integral'] + floor($totalFee);//兑换型积分
  220. $userPoint = $userAsset['point'] + floor($totalFee);//升级型积点
  221. $userTotalBuyNum = $userAsset['totalBuyNum'] + 1;
  222. $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance;
  223. $upAssetData = [];
  224. $upAssetData['balance'] = $remainBalance;
  225. $upAssetData['integral'] = $userIntegral;
  226. $upAssetData['point'] = $userPoint;
  227. $upAssetData['totalExpend'] = $userTotalExpend;
  228. $upAssetData['totalBuyNum'] = $userTotalBuyNum;
  229. xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
  230. $integralData = [
  231. 'userId' => $userId,
  232. 'userName' => $userName,
  233. 'merchantId' => $merchantId,
  234. 'relateId' => (string)$orderId,
  235. 'integral' => $userIntegral,
  236. 'num' => $addIntegral,
  237. 'io' => 1,
  238. 'payWay' => $payWay,
  239. 'alipayId' => $alipayId,
  240. 'event' => $uIntegralEvent,
  241. 'operatorId' => $userId,
  242. 'createTime' => $date,
  243. 'capitalType' => $capitalType,
  244. 'addTime' => $now,
  245. ];
  246. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  247. $userCapitalData = [
  248. 'relateId' => (string)$orderId,
  249. 'balance' => $remainBalance,
  250. 'totalIncome' => $userAsset['totalIncome'],
  251. 'totalExpend' => $userTotalExpend,
  252. 'amount' => $totalFee,
  253. 'io' => 0,
  254. 'payWay' => $payWay,
  255. 'event' => $uCapitalEvent,
  256. 'merchantId' => $merchantId,
  257. 'userId' => $userId,
  258. 'alipayId' => $alipayId,
  259. 'userName' => $userName,
  260. 'operateId' => 0,
  261. 'createTime' => $date,
  262. 'addTime' => $now,
  263. 'capitalType' => $capitalType,
  264. ];
  265. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  266. $transaction->commit();
  267. //订单有选择分类和用途,则直接进行老带新的发放和资金分类登记 shish 2019.9.3
  268. if (isset($order['categoryId']) && !empty($order['categoryId']) && isset($order['usageId']) && !empty($order['usageId'])) {
  269. $setOrderData = ['id' => $orderId, 'categoryId' => $order['categoryId'], 'usageId' => $order['usageId']];
  270. OrderService::setOrderStyle($setOrderData,true);
  271. }
  272. return ['code' => 'A0001', 'msg' => '支付成功', 'data' => ['orderId' => $orderId]];
  273. } catch (Exception $e) {
  274. $transaction->rollBack();
  275. return ['code' => 'A0002', 'msg' => '支付没有成功'];
  276. }
  277. }
  278. }