| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace common\services;
- use biz\order\services\OrderService;
- use biz\tool\services\CouponService;
- use biz\user\services\UserAssetService;
- use biz\user\services\UserService;
- use common\components\stringUtil;
- use Yii;
- use common\components\configDict;
- use linslin\yii2\curl;
- use common\components\weixinUtil;
- /**
- * 支付工具
- * @author sofashi
- */
- class xhPayToolService
- {
-
- /**
- * 余额支付
- */
- public static function balancePay($orderId, $totalFee, $capitalType, $couponId)
- {
- $payWay = configDict::getConfig('payWay', 'balancePay');
- return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay);
- }
-
- /**
- * 微信支付
- */
- public static function weixinPay($orderId, $totalFee, $capitalType, $couponId)
- {
- $payWay = configDict::getConfig('payWay', 'weixinPay');
- return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay);
- }
-
- /**
- * 支付宝支付
- */
- public static function alipay($orderId, $totalFee, $capitalType, $couponId, $alipayParams)
- {
- $payWay = configDict::getConfig('payWay', 'alipay');
- return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
- }
-
- /**
- * 现金支付,转店主微信,转店主支付宝
- */
- public static function cashPay($orderId, $totalFee, $capitalType, $couponId)
- {
- $payWay = configDict::getConfig('payWay', 'cashPay');
- return self::basePay($orderId, $totalFee, $capitalType, $couponId, $payWay);
- }
-
- /**
- * 基础支付
- * $capitalType 需要支付订单来源:xhOrder xhActiveOrder xhApplyOrder
- * $payWay 支付方式:weixinPay alipay balancePay cashPay
- */
- public static function basePay($orderId, $totalFee, $capitalType, $couponId, $payWay, $alipayParams = [])
- {
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- $typeList = configDict::getConfig('capitalType');//流水类型列表
- switch ($capitalType) {
- case $typeList['xhOrder']['id']://购买商品
- $order = xhOrderService::getById($orderId);
- break;
- case $typeList['xhActiveOrder']['id']://活动报名
- $order = xhActiveOrderService::getById($orderId);
- break;
- case $typeList['xhApplyOrder']['id']:
- $order = xhApplyOrderService::getById($orderId);
- break;
- default:
- Yii::warning('获取订单信息失败,回调传过来的 capitalType 不符合要求,capitalType:' . $capitalType . ' orderId:' . $orderId);
- return ['code' => 'A0002', 'msg' => '订单流水类型不明确'];
- }
- if (empty($order)) {
- Yii::warning('order info empty,capitalType:' . $capitalType . ' orderId:' . $orderId);
- return ['code' => 'A0002', 'msg' => '订单信息为空'];
- }
- if ($order['payStatus'] == 1) {
- Yii::warning('already pay,capitalType:' . $capitalType . ' orderId:' . $orderId);
- return ['code' => 'A0002', 'msg' => '已经付款过了'];
- }
- if ($totalFee != $order['actPrice']) {
- Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderId:' . $orderId);
- return ['code' => 'A0002', 'msg' => '订单出错了'];
- }
- $orderId = $order['id'];
- $userId = $order['userId'];
- $totalFee = $order['actPrice'];
- $merchantId = $order['merchantId'];
- Yii::$app->params['merchantId'] = $merchantId;
- $alipayId = isset($alipayParams['alipayId']) ? $alipayParams['alipayId'] : '';
- $alipayAccount = isset($alipayParams['alipayAccount']) ? $alipayParams['alipayAccount'] : '';
- $addPoint = floor($totalFee);
- $addIntegral = $addPoint;
- $userIntegral = $addPoint;
- $userPoint = $addPoint;
- $totalExpend = $totalFee;
- $userTotalExpend = $totalExpend;
- $merchant = xhMerchantService::getById($merchantId);
- $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- $now = time();
- $date = date("Y-m-d H:i:s", $now);
-
- //余额支付
- $balancePay = configDict::getConfig('payWay', 'balancePay');//余额支付
- if ($payWay == $balancePay && !empty($order['deadline']) && $now > $order['deadline']) {
- return ['code' => 'A0002', 'msg' => '订单已经过期'];
- }
- if (empty($userId) && $payWay == $balancePay) {
- $transaction->rollBack();
- Yii::warning('未登陆用户无法使用余额支付,capitalType:' . $capitalType . ' orderId:' . $orderId);
- return ['code' => 'A0002', 'msg' => '未登陆用户无法使用余额支付'];
- }
- $updateData = [];
- $updateData['alipayId'] = $alipayId;
- //支付宝支付
- $alipayWay = configDict::getConfig('payWay', 'alipay');
- if (empty($userId)) {
- if ($payWay == $alipayWay) {
- $source = UserService::$userSourceId['alipay']['name'];
- $info = ['alipayId' => $alipayId, 'merchantId' => $merchantId];
- $user = UserService::replaceUser($info, $source, $merchantId);
- $userId = $user['id'];
- $userAsset = UserAssetService::getByUserId($userId);
- $updateData['userId'] = $userId;
-
- //更新访问时间
- Yii::$app->params['merchantId'] = $merchantId;
- Yii::$app->params['userId'] = $userId;
- UserService::updateVisitTime();
- }
- } else {
- $user = xhUserService::getById($userId);
- $userAsset = xhUserAssetService::getByUserId($userId);
- }
- if (empty($user)) {
- return ['code' => 'A0002', 'msg' => '没有用户信息'];
- }
-
- $balance = $userAsset['balance'];
- if ($payWay == $balancePay && $balance < $totalFee) {
- Yii::warning('余额不足,capitalType:' . $capitalType . ' orderId:' . $orderId);
- return ['code' => 'A0002', 'msg' => '余额不足'];
- }
-
- $mAsset = xhMerchantAssetService::getByMerchantId($merchantId);
- $mBalance = $mAsset['balance'];
- $mExpend = $mAsset['totalExpend'];
- $mIncome = stringUtil::calcAdd($mAsset['totalIncome'], $totalFee);
- $mUseRecharge = stringUtil::calcAdd($mAsset['totalUseRecharge'], $totalFee);
- $mTotalDeal = $mAsset['totalDeal'] + 1;//总交易数+1
- $mUpdateData = ['totalIncome' => $mIncome, 'totalUseRecharge' => $mUseRecharge, 'totalDeal' => $mTotalDeal];
- //支付方式要转到商家资产变更里去
- $order['payWay'] = $payWay;
- xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $mUpdateData, $order, $capitalType);//商家资产改变
-
- $userName = isset($user['userName']) ? $user['userName'] : '游客';
- switch ($capitalType) {
- case $typeList['xhOrder']['id']://购买商品
- $mCapitalEvent = '购买商品';
- $uCapitalEvent = '购买商品';
- $uIntegralEvent = '购买商品';
- break;
- case $typeList['xhActiveOrder']['id']://活动报名
- $mCapitalEvent = '活动报名';
- $uCapitalEvent = '活动报名';
- $uIntegralEvent = '活动报名';
- break;
- case $typeList['xhApplyOrder']['id']://申请服务
- $mCapitalEvent = '申请服务';
- $uCapitalEvent = '申请服务';
- $uIntegralEvent = '申请服务';
- break;
- default:
- }
- $capitalData = [
- 'relateId' => (string)$orderId,
- 'balance' => $mBalance,
- 'totalIncome' => $mIncome,
- 'totalExpend' => $mExpend,
- 'amount' => $totalFee,
- 'io' => 1,
- 'shopId' => $order['shopId'],
- 'payWay' => $payWay,
- 'event' => $mCapitalEvent,
- 'merchantId' => $merchantId,
- 'userId' => $userId,
- 'userName' => $userName,
- 'alipayId' => $alipayId,
- 'createTime' => $date,
- 'addTime' => $now,
- 'capitalType' => $capitalType,
- ];
- $merchantCapital = xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加
- $merchantCapitalId = $merchantCapital['id'];
- $updateData['capitalId'] = $merchantCapitalId;
- $updateData['payStatus'] = 1;//支付成功
- $updateData['couponId'] = $couponId;
- $updateData['payWay'] = $payWay;
- $updateData['payTime'] = $now;
- switch ($capitalType) {
- case $typeList['xhOrder']['id']://购买商品
- xhOrderService::updateById($orderId, $updateData);
- break;
- case $typeList['xhActiveOrder']['id']://活动报名
- xhActiveOrderService::updateById($orderId, $updateData);
- break;
- case $typeList['xhApplyOrder']['id']://申请服务
- $updateData['inviteCodeStatus'] = 1;
- xhApplyOrderService::updateById($orderId, $updateData);
- break;
- default:
- Yii::warning('更新订单失败,回调传过来的 capitalType 不符合要求,即订单流水类型不明确,capitalType:' . $capitalType . ' orderId:' . $orderId);
- return ['code' => 'A0002', 'msg' => '订单流水类型不明确'];
- }
-
- //抵销优惠券并分成
- CouponService::destroyToGetReward(['id' => $couponId]);
- if (isset($order['inviteCode']) && !empty($order['inviteCode'])) {
- $invitedMerchantId = isset($order['invitedMerchantId']) ? $order['invitedMerchantId'] : 0;
- xhInviteService::updateByCode($order['inviteCode'], ['takeStatus' => 1, 'inviteUseTo' => 0, 'targetId' => $orderId, 'invitedMerchantId' => $invitedMerchantId]);
- }
-
- $userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee);//累计消费
- $userIntegral = $userAsset['integral'] + floor($totalFee);//兑换型积分
- $userPoint = $userAsset['point'] + floor($totalFee);//升级型积点
- $userTotalBuyNum = $userAsset['totalBuyNum'] + 1;
-
- $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance;
- $upAssetData = [];
- $upAssetData['balance'] = $remainBalance;
- $upAssetData['integral'] = $userIntegral;
- $upAssetData['point'] = $userPoint;
- $upAssetData['totalExpend'] = $userTotalExpend;
- $upAssetData['totalBuyNum'] = $userTotalBuyNum;
- xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
-
- $integralData = [
- 'userId' => $userId,
- 'userName' => $userName,
- 'merchantId' => $merchantId,
- 'relateId' => (string)$orderId,
- 'integral' => $userIntegral,
- 'num' => $addIntegral,
- 'io' => 1,
- 'payWay' => $payWay,
- 'alipayId' => $alipayId,
- 'event' => $uIntegralEvent,
- 'operatorId' => $userId,
- 'createTime' => $date,
- 'capitalType' => $capitalType,
- 'addTime' => $now,
- ];
- xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
-
- $userCapitalData = [
- 'relateId' => (string)$orderId,
- 'balance' => $remainBalance,
- 'totalIncome' => $userAsset['totalIncome'],
- 'totalExpend' => $userTotalExpend,
- 'amount' => $totalFee,
- 'io' => 0,
- 'payWay' => $payWay,
- 'event' => $uCapitalEvent,
- 'merchantId' => $merchantId,
- 'userId' => $userId,
- 'alipayId' => $alipayId,
- 'userName' => $userName,
- 'operateId' => 0,
- 'createTime' => $date,
- 'addTime' => $now,
- 'capitalType' => $capitalType,
- ];
- xhUserCapitalService::add($userCapitalData);//用户资金流水增加
-
- $transaction->commit();
-
- //订单有选择分类和用途,则直接进行老带新的发放和资金分类登记 shish 2019.9.3
- if (isset($order['categoryId']) && !empty($order['categoryId']) && isset($order['usageId']) && !empty($order['usageId'])) {
- $setOrderData = ['id' => $orderId, 'categoryId' => $order['categoryId'], 'usageId' => $order['usageId']];
- OrderService::setOrderStyle($setOrderData,true);
- }
- return ['code' => 'A0001', 'msg' => '支付成功', 'data' => ['orderId' => $orderId]];
- } catch (Exception $e) {
- $transaction->rollBack();
- return ['code' => 'A0002', 'msg' => '支付没有成功'];
- }
- }
-
- }
|