| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- <?php
- namespace common\services;
- use biz\shop\classes\ShopAdminClass;
- use biz\shop\models\Shop;
- use bizHd\coupon\classes\CouponClass;
- use bizHd\merchant\classes\ShopClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\user\classes\UserClass;
- use bizHd\user\services\UserAssetService;
- use bizHd\user\services\UserService;
- use biz\recharge\classes\RechargeClass;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- use common\components\dict;
- /**
- * 支付工具
- */
- class xhPayToolService
- {
- //余额支付
- public static function balancePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
- {
- $payWay = dict::getDict('payWay', 'balancePay');
- return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
- }
- /**
- * 微信支付
- */
- public static function wxPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
- {
- $payWay = dict::getDict('payWay', 'wxPay');
- return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
- }
- /**
- * 支付宝支付
- */
- public static function alipay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $alipayParams)
- {
- $payWay = dict::getDict('payWay', 'alipay');
- return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
- }
- /**
- * 现金支付,转店主微信,转店主支付宝
- */
- public static function cashPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
- {
- $payWay = dict::getDict('payWay', 'cashPay');
- return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
- }
- //供货商订单支付成功 ssh 2021.3.15
- public static function ghsOrderPay($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
- {
- $order = \bizGhs\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn]);
- if (empty($order)) {
- Yii::warning("支付回调通知,没有找到订单 编号{$orderSn} ");
- util::end();
- }
- if ($order['payStatus'] == 2) {
- Yii::warning("支付回调通知,订单已付款 编号{$orderSn} ");
- util::end();
- }
- if ($totalFee != $order['actPrice']) {
- Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::end();
- }
- $shopId = $order['shopId'] ?? 0;
- $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
- if (empty($shop)) {
- Yii::warning('支付第三方回调通知,capitalType:' . $capitalType . ' orderSn:' . $orderSn . " 没有找到门店");
- util::end();
- }
- \bizGhs\order\classes\OrderClass::payOk($order, 3, $shop);
- $transaction->commit();
- }
- //充值回调处理 ssh 2021.2.21
- public static function recharge($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
- {
- $recharge = RechargeClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($recharge)) {
- noticeUtil::push('充值付款成功,收到回调,但没有找到充值记录,orderSn:' . $orderSn, '15280215347');
- return ['amount' => $totalFee];
- }
- if ($totalFee != $recharge->amount) {
- noticeUtil::push("充值付款成功,收到回调,但金额不一致 {$totalFee} {$recharge->amount},orderSn:" . $orderSn, '15280215347');
- return ['amount' => $totalFee];
- }
- if ($recharge->payStatus == RechargeClass::PAY_STATUS_HAS_PAY) {
- noticeUtil::push("充值付款成功,收到回调,但订单是已经支付过了,orderSn:" . $orderSn, '15280215347');
- return ['amount' => $totalFee];
- }
- $rechargeRenew = $callbackParams['rechargeRenew'] ?? 0;
- //处理充值成功的流程
- RechargeClass::complete($recharge, $rechargeRenew);
- $transaction->commit();
- return ['amount' => $totalFee];
- }
- //零售采购回调 ssh 2021.2.28
- public static function retailPurchase($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams)
- {
- $purchase = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($purchase)) {
- return [];
- }
- PurchaseClass::complete($purchase);
- $transaction->commit();
- }
- /**
- * 基础支付
- * $capitalType 需要支付订单来源:xhOrder xhActiveOrder xhApplyOrder
- * $payWay 支付方式:wxPay alipay balancePay cashPay
- */
- public static function basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams = [])
- {
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- $capitalTypeList = dict::getDict('capitalType');//流水类型列表
- $order = [];
- switch ($capitalType) {
- case $capitalTypeList['xhRecharge']['id']:
- return self::recharge($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
- Yii::$app->end();
- break;
- case $capitalTypeList['xhOrder']['id']:
- //购买商品
- $order = xhOrderService::getByOrderSn($orderSn);
- break;
- case $capitalTypeList['xhActiveOrder']['id']:
- //活动报名
- $order = xhActiveOrderService::getById($orderSn);
- break;
- case $capitalTypeList['xhApplyOrder']['id']:
- $order = xhApplyOrderService::getById($orderSn);
- break;
- case $capitalTypeList['xhGhsOrder']['id']:
- //供货商订单
- return self::ghsOrderPay($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
- Yii::$app->end();
- break;
- case $capitalTypeList['xhGhsOrder']['id']:
- //零售店采购
- return self::retailPurchase($transaction, $callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
- Yii::$app->end();
- break;
- default:
- Yii::warning('获取订单信息失败,回调传过来的 capitalType 不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::fail('订单流水类型不明确');
- }
- if (empty($order)) {
- Yii::warning('order info empty,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::fail('订单信息为空');
- }
- if ($order['payStatus'] == 1) {
- Yii::warning('already pay,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::fail('已经付款过了');
- }
- if ($totalFee != $order['actPrice']) {
- Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::fail('订单出错了(第三方回调金额与订单金额不一致)');
- }
- $orderId = $order['id'];
- $orderSn = $order['orderSn'];
- $userId = $order['userId'] ?? 0;
- $totalFee = $order['actPrice'];
- $sjId = $order['sjId'];
- $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($sjId);
- $merchantExtend = xhMerchantExtendService::getBySjId($sjId);
- $shopId = isset($order['shopId']) ? $order['shopId'] : 0;
- if (empty($shopId)) {
- $shopId = ShopClass::getDefaultShopId($merchant);
- }
- $now = time();
- $date = date("Y-m-d H:i:s", $now);
- //余额支付
- $balancePay = dict::getDict('payWay', 'balancePay');//余额支付
- if ($payWay == $balancePay && !empty($order['deadline']) && $now > $order['deadline']) {
- util::fail('订单已经过期');
- }
- if (empty($userId) && $payWay == $balancePay) {
- $transaction->rollBack();
- Yii::warning('未登陆不能使用余额支付,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::fail('没有登陆不能使用余额支付');
- }
- //使用优惠券
- CouponClass::useCoupon($couponId, $orderId);
- $updateData = [];
- $updateData['alipayId'] = $alipayId;
- //支付宝支付
- $alipayWay = dict::getDict('payWay', 'alipay');
- $wxWay = dict::getDict('payWay', 'wxPay');
- //确认h5还是小程序支付
- $wxPayType = isset($callbackParams['wxPayType']) ? $callbackParams['wxPayType'] : 0;
- if (empty($userId)) {
- if ($payWay == $alipayWay) {
- $source = UserClass::$userSourceId['alipay']['name'];
- $info = ['alipayId' => $alipayId, 'sjId' => $sjId];
- $user = UserService::replaceUser($info, $source, $sjId);
- $userId = $user['id'];
- $userAsset = UserAssetService::getByUserId($userId);
- $updateData['userId'] = $userId;
- //更新访问时间
- UserService::updateVisitTime($sjId, $userId);
- }
- } else {
- $user = xhUserService::getById($userId);
- $userAsset = xhUserAssetService::getByUserId($userId);
- }
- if (empty($user)) {
- //util::fail('没有客户信息');
- }
- $balance = $userAsset['balance'] ?? 0;
- if ($payWay == $balancePay && $balance < $totalFee) {
- Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::fail('余额不足');
- }
- $mAsset = xhMerchantAssetService::getBySjId($sjId);
- $mBalance = $mAsset['balance'];
- $mExpend = $mAsset['totalExpend'];
- $mIncome = stringUtil::calcAdd($mAsset['totalIncome'], $totalFee);
- $mUseRecharge = stringUtil::calcAdd($mAsset['usedRecharge'], $totalFee);
- $mTotalDeal = $mAsset['totalDeal'] + 1;//付款成功成交量+1
- $mUnSendOrder = $mAsset['unSendOrder'] + 1;//待配送订单+1
- $mUnPayOrder = $mAsset['unPayOrder'] - 1;//待付款订单-1
- $mUpdateData = [
- 'totalIncome' => $mIncome,
- 'usedRecharge' => $mUseRecharge,
- 'totalDeal' => $mTotalDeal,
- 'unPayOrder' => $mUnPayOrder,
- 'unSendOrder' => $mUnSendOrder,
- ];
- //支付方式要转到商家资产变更里去
- $order['payWay'] = $payWay;
- $order['payTime'] = $now;
- //xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $shopId, $mUpdateData, $order, $capitalType);//商家资产改变
- $userName = isset($user['userName']) ? $user['userName'] : '游客';
- $mCapitalEvent = '';
- switch ($capitalType) {
- case $capitalTypeList['xhOrder']['id']://购买商品
- $mCapitalEvent = '购买商品';
- $uCapitalEvent = '购买商品';
- $uIntegralEvent = '购买商品';
- break;
- case $capitalTypeList['xhActiveOrder']['id']://活动报名
- $mCapitalEvent = '活动报名';
- $uCapitalEvent = '活动报名';
- $uIntegralEvent = '活动报名';
- break;
- case $capitalTypeList['xhApplyOrder']['id']://申请服务
- $mCapitalEvent = '申请服务';
- $uCapitalEvent = '申请服务';
- $uIntegralEvent = '申请服务';
- break;
- default:
- }
- $fromType = isset($order['fromType']) ? $order['fromType'] : 0;
- $capitalData = [
- 'relateId' => $orderId,
- 'balance' => $mBalance,
- 'totalIncome' => $mIncome,
- 'totalExpend' => $mExpend,
- 'amount' => $totalFee,
- 'io' => 1,
- 'shopId' => $order['shopId'],
- 'payWay' => $payWay,
- 'fromType' => $fromType,
- 'event' => $mCapitalEvent,
- 'sjId' => $sjId,
- 'userId' => $userId,
- 'userName' => $userName,
- 'alipayId' => $alipayId,
- 'createTime' => $date,
- 'addTime' => $now,
- 'capitalType' => $capitalType,
- ];
- // $merchantCapital = xhSjCapitalService::add($capitalData);//商家资金流水记录增加
- // $merchantCapitalId = $merchantCapital['id'];
- // $updateData['capitalId'] = $merchantCapitalId;
- $updateData['payStatus'] = 1;//支付成功
- $updateData['status'] = OrderClass::ORDER_STATUS_UN_SEND;
- $updateData['couponId'] = $couponId;
- $updateData['payWay'] = $payWay;
- $updateData['payTime'] = $now;
- //确认h5还是小程序支付
- $updateData['wxPayType'] = $wxPayType;
- switch ($capitalType) {
- case $capitalTypeList['xhOrder']['id']:
- break;
- case $capitalTypeList['xhActiveOrder']['id']://活动报名
- xhActiveOrderService::updateById($orderId, $updateData);
- break;
- case $capitalTypeList['xhApplyOrder']['id']://申请服务
- $updateData['inviteCodeStatus'] = 1;
- xhApplyOrderService::updateById($orderId, $updateData);
- break;
- default:
- Yii::warning('更新订单失败,回调的capitalType不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
- util::fail('订单流水类型不明确');
- }
- //累计消费
- //$userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee);
- //成长值增加
- // $userGrowth = $userAsset['growth'] + floor($totalFee);
- // $userTotalBuyNum = $userAsset['totalBuyNum'] + 1;
- //
- // $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance;
- // $upAssetData = [];
- // $upAssetData['balance'] = $remainBalance;
- // $upAssetData['growth'] = $userGrowth;
- // $upAssetData['totalExpend'] = $userTotalExpend;
- // $upAssetData['totalBuyNum'] = $userTotalBuyNum;
- //xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变
- $growData = [
- 'userId' => $userId,
- 'userName' => $userName,
- 'sjId' => $sjId,
- 'relateId' => $orderId,
- //'growth' => $userGrowth,
- 'num' => floor($totalFee),
- 'io' => 1,
- 'payWay' => $payWay,
- 'alipayId' => $alipayId,
- //'event' => $uIntegralEvent,
- 'operatorId' => $userId,
- 'createTime' => $date,
- 'gainType' => 1,
- 'addTime' => $now,
- ];
- //xhUserGrowthService::add($growData);//用户兑换型积分流水增加
- $userCapitalData = [
- 'relateId' => $orderId,
- //'balance' => $remainBalance,
- 'totalIncome' => $userAsset['totalIncome'] ?? 0,
- 'totalExpend' => $userTotalExpend,
- 'amount' => $totalFee,
- 'io' => 0,
- 'payWay' => $payWay,
- //'event' => $uCapitalEvent,
- 'sjId' => $sjId,
- 'userId' => $userId,
- 'alipayId' => $alipayId,
- 'userName' => $userName,
- 'operateId' => 0,
- 'createTime' => $date,
- 'addTime' => $now,
- 'capitalType' => $capitalType,
- ];
- //xhUserCapitalService::add($userCapitalData);//用户资金流水增加
- $transaction->commit();
- $shopId = $order['shopId'] ?? 0;
- $money = $order['actPrice'] ?? 0.00;
- ShopAdminClass::incomeNotice($shopId, $money, 0);
- //如果是小程序支付可以获取unionId并进行更新关联
- if ($payWay == $wxWay && $wxPayType == 1) {
- //OrderClass::payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user);
- }
- return ['balance' => 0];
- //return ['balance' => $remainBalance];
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail('支付没有成功');
- }
- }
- }
|