Bladeren bron

下单流程优化

shish 5 jaren geleden
bovenliggende
commit
3f7503adf2
3 gewijzigde bestanden met toevoegingen van 429 en 430 verwijderingen
  1. 3 34
      app-ghs/controllers/OrderController.php
  2. 50 25
      biz-ghs/order/classes/OrderClass.php
  3. 376 371
      common/services/xhPayToolService.php

+ 3 - 34
app-ghs/controllers/OrderController.php

@@ -95,6 +95,7 @@ class OrderController extends BaseController
         $post['prePrice'] = 0.01;
         $post['actPrice'] = 0.01;
         $post['expressId'] = $post['expressId'] ?? 0;
+        $post['clearType'] = $post['clearType'] ?? OrderClass::CLEAR_DEBT;
 
         $productJson = $post['product'] ?? '';
         if (empty($productJson)) {
@@ -104,32 +105,20 @@ class OrderController extends BaseController
         if (empty($productList)) {
             util::fail('请选择花材');
         }
-        $orderSn = '';
-        $id = 0;
         $connection = Yii::$app->db;//事务处理
         $transaction = $connection->beginTransaction();
         try {
             //判断花材有效性
             ProductClass::valid($productList, $this->shopId);
             $post['product'] = $productList;
-            $order = OrderClass::addOrder($post);
-            $orderSn = $order['orderSn'] ?? '';
-            $id = $order['id'] ?? 0;
+            OrderClass::addOrder($post, $this->sj, $this->shop);
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
             Yii::info("下单报错:" . $e->getMessage());
             util::fail('下单失败');
         }
-
-        util::success([
-            'orderId' => $id,
-            'orderSn' => $orderSn,
-            'totalPrice' => 0.01,
-            'sendType' => '快递送',
-            'sendTime' => '15:00',
-            'qrCode' => imgUtil::getPrefix() . '/hhb_small.png',
-        ]);
+        util::complete();
     }
 
     //延期支付 shish 2021.1.19
@@ -143,26 +132,6 @@ class OrderController extends BaseController
         util::complete();
     }
 
-    //已收款 shish 2021.3.15
-    public function actionHasPay()
-    {
-        $get = Yii::$app->request->get();
-        $id = $get['id'] ?? 0;
-        $info = OrderClass::getOrderInfo($id);
-        OrderClass::valid($info, $this->shopId);
-        $connection = Yii::$app->db;//事务处理
-        $transaction = $connection->beginTransaction();
-        try {
-            OrderClass::payOk($info, 2);
-            $transaction->commit();
-        } catch (\Exception $e) {
-            $transaction->rollBack();
-            Yii::info("已收款操作报错:" . $e->getMessage());
-            util::fail('操作失败');
-        }
-        util::complete();
-    }
-
     //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
     public function actionWxPay()
     {

+ 50 - 25
biz-ghs/order/classes/OrderClass.php

@@ -21,6 +21,7 @@ class OrderClass extends BaseClass
 
     public static $baseFile = '\bizGhs\order\models\Order';
 
+
     //已退款
     const ORDER_STATUS_REFUND = 6;
     //已取消
@@ -34,6 +35,7 @@ class OrderClass extends BaseClass
     //待付款,待确认
     const ORDER_STATUS_UN_PAY = 1;
 
+
     //未确认
     const SEND_TYPE_UNKNOWN = 1;
     //自取
@@ -43,11 +45,13 @@ class OrderClass extends BaseClass
     //快递送
     const SEND_TYPE_THIRD = 4;
 
+
     //待付款,待确认
     const PAY_STATUS_UN_PAY = 1;
     //已付款
     const PAY_STATUS_HAS_PAY = 2;
 
+
     //待配送
     const SEND_STATUS_UN_SEND = 1;
     //配送中
@@ -55,11 +59,16 @@ class OrderClass extends BaseClass
     //配送完成
     const SEND_STATUS_COMPLETE = 3;
 
+
     //订单总流程数
     const TOTAL_FLOW = 3;
 
+    //清算方式
+    const CLEAR_DEBT = 1;
+    const CLEAR_HAS_GATHERING = 2;
+
     //添加订单 shish 2019.12.5
-    public static function addOrder($data)
+    public static function addOrder($data, $sj, $shop)
     {
         $sjId = $data['merchantId'] ?? 0;
         $month = date("Ym");
@@ -83,17 +92,28 @@ class OrderClass extends BaseClass
         $data['status'] = self::ORDER_STATUS_UN_PAY;
         $return = self::add($data);
 
-
-        //全部订单+1,待付款订单+1
-        $merchantAsset = MerchantAssetClass::getByMerchantId($sjId, true);
-        $merchantAsset->unPayOrder += 1;
-        $merchantAsset->totalOrder += 1;
-        $merchantAsset->save();
+        //总订单和待付款订单增加
+        $shop['totalOrder']++;
+        $shop['unPayOrder']++;
+        $shopId = $shop['id'];
+        ShopClass::updateById($shopId, ['totalOrder' => $shop['totalOrder'], 'unPayOrder' => $shop['unPayOrder']]);
+
+        $clearType = $data['clearType'];
+        if ($clearType == OrderClass::CLEAR_DEBT) {
+            //欠款
+            self::payOk($return, 1, $shop);
+        } elseif ($clearType == OrderClass::CLEAR_HAS_GATHERING) {
+            //已收款
+            self::payOk($return, 2, $shop);
+        } else {
+            util::fail('请选择清算方式');
+        }
 
         //扣库存 2021.2.23 linqh
         foreach ($product as $key => $val) {
             ProductClass::decreaseStock($val['productId'], $val['bigNum'], $val['smallNum']);
         }
+
         return $return;
     }
 
@@ -408,22 +428,34 @@ class OrderClass extends BaseClass
     }
 
     //支付成功,包括微信支付,支付宝支付,余额支付,欠款支付的后续操作 shish 2021.3.15
-    // $type 1 商家点延期收款 2商家点已付款 3客户微信支付
-    public static function payOk($order, $type)
+    // $type 1 商家点款 2商家点已付款 3客户微信支付
+    public static function payOk($order, $type, $shop)
     {
         //不是待付款和待确认订单
         if ($order['status'] != self::ORDER_STATUS_UN_PAY) {
             util::fail('已操作过了');
         }
 
+        $amount = $order['actPrice'] ?? 0.00;
         //更新订单基础状态和支付状态
         $unSend = self::ORDER_STATUS_UN_SEND;
         $upData = ['status' => $unSend];
         if ($type == 3) {
+            //客户微信支付
             $upData['payStatus'] = 2;
-        }
-        if ($type == 2) {
+        } elseif ($type == 2) {
+            //商家点已付款
             $upData['payStatus'] = 2;
+        } elseif ($type == 1) {
+            //商家点欠款
+            $customId = $order['customId'] ?? 0;
+            if (!empty($customId)) {
+                $customInfo = CustomClass::getCustom($customId);
+                $debtAmount = bcadd($customInfo['debtAmount'], $amount, 2);
+                CustomClass::updateById($customId, ['debtAmount' => $debtAmount, 'isDebt' => 1]);
+            }
+        } else {
+            util::fail('未知的付款成功类型');
         }
         $orderId = $order['id'];
         $orderSn = $order['orderSn'];
@@ -432,24 +464,17 @@ class OrderClass extends BaseClass
         //增加业绩
         StatYjClass::addYj($order);
 
+        $shop['unSendOrder']++;
+        $shop['unPayOrder']--;
+        $shop['payOrder']++;
+        $shop['totalIncome'] = bcadd($shop['totalIncome'], $amount, 2);
+        $shopId = $shop['id'];
+        ShopClass::updateById($shopId, ['unSendOrder' => $shop['unSendOrder'], 'unPayOrder' => $shop['unPayOrder'], 'totalIncome' => $shop['totalIncome'], 'payOrder' => $shop['payOrder']]);
+
         //增加已下单流程
         $time = date("Y-m-d H:i:s");
         $placeData = ['payTime' => $time, 'orderSn' => $orderSn];
         OrderSendClass::placeOrder($placeData);
     }
 
-    //商家操作延期收款的订单 shish 2021.3.15
-    public static function debt($order, $shop)
-    {
-        $customId = $order['customId'] ?? 0;
-        if (empty($customId)) {
-            util::fail('散客订单不能延期收款');
-        }
-        OrderClass::payOk($order, 1);
-        $amount = $order['actPrice'] ?? 0.00;
-        $customInfo = CustomClass::getCustom($customId);
-        $debtAmount = bcadd($customInfo['debtAmount'], $amount, 2);
-        CustomClass::updateById($customId, ['debtAmount' => $debtAmount, 'isDebt' => 1]);
-    }
-
 }

+ 376 - 371
common/services/xhPayToolService.php

@@ -24,375 +24,380 @@ use common\components\wxUtil;
  */
 class xhPayToolService
 {
-	
-	//余额支付
-	public static function balancePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
-	{
-		$payWay = dict::getConfig('payWay', 'balancePay');
-		return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
-	}
-	
-	/**
-	 * 微信支付
-	 */
-	public static function wxPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
-	{
-		$payWay = dict::getConfig('payWay', 'wxPay');
-		return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
-	}
-	
-	/**
-	 * 支付宝支付
-	 */
-	public static function alipay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $alipayParams)
-	{
-		$payWay = dict::getConfig('payWay', 'alipay');
-		return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
-	}
-	
-	/**
-	 * 现金支付,转店主微信,转店主支付宝
-	 */
-	public static function cashPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
-	{
-		$payWay = dict::getConfig('payWay', 'cashPay');
-		return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
-	}
-	
-	//供货商订单支付成功 shish 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();
-		}
-		$orderId = $order['id'];
-		
-		\bizGhs\order\classes\OrderClass::payOk($order, 3);
-		
-		$transaction->commit();
-		
-	}
-	
-	//充值回调处理 shish 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];
-		}
-		
-		//处理充值成功的流程
-		RechargeClass::complete($recharge);
-		
-		$transaction->commit();
-		return ['amount' => $totalFee];
-	}
-	
-	//零售采购回调 shish 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::getConfig('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'];
-			$totalFee = $order['actPrice'];
-			$merchantId = $order['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);
-			$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::getConfig('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::getConfig('payWay', 'alipay');
-			$wxWay = dict::getConfig('payWay', 'wxPay');
-			//确认h5还是小程序支付
-			$wxPayType = isset($callbackParams['wxPayType']) ? $callbackParams['wxPayType'] : 0;
-			if (empty($userId)) {
-				if ($payWay == $alipayWay) {
-					$source = UserClass::$userSourceId['alipay']['name'];
-					$info = ['alipayId' => $alipayId, 'merchantId' => $merchantId];
-					$user = UserService::replaceUser($info, $source, $merchantId);
-					$userId = $user['id'];
-					$userAsset = UserAssetService::getByUserId($userId);
-					$updateData['userId'] = $userId;
-					
-					//更新访问时间
-					UserService::updateVisitTime($merchantId, $userId);
-				}
-			} else {
-				$user = xhUserService::getById($userId);
-				$userAsset = xhUserAssetService::getByUserId($userId);
-			}
-			if (empty($user)) {
-				util::fail('没有客户信息');
-			}
-			
-			$balance = $userAsset['balance'];
-			if ($payWay == $balancePay && $balance < $totalFee) {
-				Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
-				util::fail('余额不足');
-			}
-			
-			$mAsset = xhMerchantAssetService::getByMerchantId($merchantId);
-			$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
-			$mPayOrder = $mAsset['payOrder'] + 1;//付款订单+1
-			$mUpdateData = [
-				'totalIncome' => $mIncome,
-				'usedRecharge' => $mUseRecharge,
-				'totalDeal' => $mTotalDeal,
-				'payOrder' => $mPayOrder,
-				'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,
-				'merchantId' => $merchantId,
-				'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'] = 1;//支付成功待配送
-			$updateData['couponId'] = $couponId;
-			$updateData['payWay'] = $payWay;
-			$updateData['payTime'] = $now;
-			//确认h5还是小程序支付
-			$updateData['wxPayType'] = $wxPayType;
-			switch ($capitalType) {
-				case $capitalTypeList['xhOrder']['id']:
-					OrderClass::payAfter($order, $updateData);
-					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,
-				'merchantId' => $merchantId,
-				'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'],
-				'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();
-			
-			//如果是小程序支付可以获取unionId并进行更新关联
-			if ($payWay == $wxWay && $wxPayType == 1) {
-				OrderClass::payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user);
-			}
-			
-			return ['balance' => $remainBalance];
-			
-		} catch (Exception $e) {
-			$transaction->rollBack();
-			util::fail('支付没有成功');
-		}
-	}
-	
+
+    //余额支付
+    public static function balancePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
+    {
+        $payWay = dict::getConfig('payWay', 'balancePay');
+        return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
+    }
+
+    /**
+     * 微信支付
+     */
+    public static function wxPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
+    {
+        $payWay = dict::getConfig('payWay', 'wxPay');
+        return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
+    }
+
+    /**
+     * 支付宝支付
+     */
+    public static function alipay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $alipayParams)
+    {
+        $payWay = dict::getConfig('payWay', 'alipay');
+        return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay, $alipayParams);
+    }
+
+    /**
+     * 现金支付,转店主微信,转店主支付宝
+     */
+    public static function cashPay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId)
+    {
+        $payWay = dict::getConfig('payWay', 'cashPay');
+        return self::basePay($callbackParams, $orderSn, $totalFee, $capitalType, $couponId, $payWay);
+    }
+
+    //供货商订单支付成功 shish 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 = \biz\shop\classes\ShopClass::getShopInfo($shopId);
+        if (empty($shop)) {
+            Yii::warning('支付第三方回调通知,capitalType:' . $capitalType . ' orderSn:' . $orderSn . " 没有找到门店");
+            util::end();
+        }
+
+        \bizGhs\order\classes\OrderClass::payOk($order, 3, $shop);
+
+        $transaction->commit();
+
+    }
+
+    //充值回调处理 shish 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];
+        }
+
+        //处理充值成功的流程
+        RechargeClass::complete($recharge);
+
+        $transaction->commit();
+        return ['amount' => $totalFee];
+    }
+
+    //零售采购回调 shish 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::getConfig('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'];
+            $totalFee = $order['actPrice'];
+            $merchantId = $order['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);
+            $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::getConfig('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::getConfig('payWay', 'alipay');
+            $wxWay = dict::getConfig('payWay', 'wxPay');
+            //确认h5还是小程序支付
+            $wxPayType = isset($callbackParams['wxPayType']) ? $callbackParams['wxPayType'] : 0;
+            if (empty($userId)) {
+                if ($payWay == $alipayWay) {
+                    $source = UserClass::$userSourceId['alipay']['name'];
+                    $info = ['alipayId' => $alipayId, 'merchantId' => $merchantId];
+                    $user = UserService::replaceUser($info, $source, $merchantId);
+                    $userId = $user['id'];
+                    $userAsset = UserAssetService::getByUserId($userId);
+                    $updateData['userId'] = $userId;
+
+                    //更新访问时间
+                    UserService::updateVisitTime($merchantId, $userId);
+                }
+            } else {
+                $user = xhUserService::getById($userId);
+                $userAsset = xhUserAssetService::getByUserId($userId);
+            }
+            if (empty($user)) {
+                util::fail('没有客户信息');
+            }
+
+            $balance = $userAsset['balance'];
+            if ($payWay == $balancePay && $balance < $totalFee) {
+                Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn);
+                util::fail('余额不足');
+            }
+
+            $mAsset = xhMerchantAssetService::getByMerchantId($merchantId);
+            $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
+            $mPayOrder = $mAsset['payOrder'] + 1;//付款订单+1
+            $mUpdateData = [
+                'totalIncome' => $mIncome,
+                'usedRecharge' => $mUseRecharge,
+                'totalDeal' => $mTotalDeal,
+                'payOrder' => $mPayOrder,
+                '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,
+                'merchantId' => $merchantId,
+                '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'] = 1;//支付成功待配送
+            $updateData['couponId'] = $couponId;
+            $updateData['payWay'] = $payWay;
+            $updateData['payTime'] = $now;
+            //确认h5还是小程序支付
+            $updateData['wxPayType'] = $wxPayType;
+            switch ($capitalType) {
+                case $capitalTypeList['xhOrder']['id']:
+                    OrderClass::payAfter($order, $updateData);
+                    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,
+                'merchantId' => $merchantId,
+                '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'],
+                '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();
+
+            //如果是小程序支付可以获取unionId并进行更新关联
+            if ($payWay == $wxWay && $wxPayType == 1) {
+                OrderClass::payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user);
+            }
+
+            return ['balance' => $remainBalance];
+
+        } catch (Exception $e) {
+            $transaction->rollBack();
+            util::fail('支付没有成功');
+        }
+    }
+
 }