xhPayToolService.php 11 KB

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