shish 4 жил өмнө
parent
commit
cf41462127

+ 68 - 8
app-ghs/controllers/OrderController.php

@@ -2,10 +2,7 @@
 
 namespace ghs\controllers;
 
-use biz\stat\classes\StatOrderCountClass;
 use bizGhs\express\services\DadaExpressServices;
-use bizGhs\order\classes\OrderItemClass;
-use bizGhs\order\classes\StockWastageOrderClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\purchase\services\PurchaseService;
 use bizHd\wx\classes\WxOpenClass;
@@ -14,14 +11,12 @@ use bizGhs\order\classes\OrderClass;
 use bizGhs\order\services\OrderService;
 use bizHd\saas\services\RegionService;
 use bizGhs\product\classes\ProductClass;
-use common\components\dada\dada;
 use common\components\dict;
 use common\components\dateUtil;
 use common\components\freight;
 use common\components\httpUtil;
 use Yii;
 use common\components\util;
-use bizGhs\order\classes\RefundOrderClass;
 use biz\shop\classes\ShopAdminClass;
 
 class OrderController extends BaseController
@@ -29,6 +24,67 @@ class OrderController extends BaseController
 
     public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
 
+    //微信和支付宝付款码支付复查 shish 20211231
+    public function actionCodePayCheck()
+    {
+        ini_set('date.timezone', 'Asia/Shanghai');
+        header("Content-type: text/html; charset=utf-8");
+        $get = Yii::$app->request->get();
+        $id = isset($get['id']) ? $get['id'] : 0;
+        $order = OrderClass::getById($id, true);
+        if (empty($order)) {
+            util::fail('没有找到订单');
+        }
+        if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
+            if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
+                util::fail('订单已付款');
+            }
+            util::fail('订单不是待付款状态');
+        }
+        OrderClass::valid($order, $this->shopId);
+        $cgId = $order->purchaseId ?? 0;
+        $cg = PurchaseClass::getById($cgId, true);
+        $orderSn = $cg->orderSn ?? '';
+        if (empty($orderSn)) {
+            util::fail('没有找到采购单');
+        }
+        $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
+        if ($payWay == dict::getDict('payWay', 'wxPay')) {
+            //微信付款
+            $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
+            require_once($wx . '/lib/WxPay.Api.php');
+            require_once($wx . '/example/WxPay.MicroPay.php');
+
+            //获取微信商户号等信息
+            $sjExtend = WxOpenClass::getGhsWxInfo();
+
+            $microPay = new \MicroPay();
+            $payReturn = $microPay->query($orderSn, $sjExtend);
+            if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
+                if ($payReturn["trade_state"] == "SUCCESS") {
+                    //支付成功
+                    $transactionId = $payReturn['transaction_id'] ?? '';
+                    $openId = $payReturn['openid'] ?? '';
+                    PurchaseService::codePay($payWay, $orderSn, $transactionId, $openId);
+                    util::success(['returnStatus' => 'SUCCESS']);
+                } else if ($payReturn["trade_state"] == "USERPAYING") {
+                    //用户支付中
+                    util::fail('正在付款,请稍候再试');
+                }
+            }
+            if ($payReturn["err_code"] == "ORDERNOTEXIST") {
+                //交易订单号不存在
+                util::fail('订单没有发起过支付');
+            } else {
+                util::fail('系统错误,请稍候再试');
+            }
+        } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
+            //支付宝付款
+        } else {
+            util::fail('未知付款方式');
+        }
+    }
+
     //微信和支付宝付款码支付 shish 2021.4.11
     public function actionCodePay()
     {
@@ -73,9 +129,6 @@ class OrderController extends BaseController
 
         if ($isWx) {
 
-            //获取微信商户号等信息
-            $sjExtend = WxOpenClass::getGhsWxInfo();
-
             try {
 
                 $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
@@ -88,9 +141,16 @@ class OrderController extends BaseController
                 $input->SetTotal_fee($totalFee * 100);
                 $input->SetOut_trade_no($orderSn);
 
+                //获取微信商户号等信息
+                $sjExtend = WxOpenClass::getGhsWxInfo();
+
                 $microPay = new \MicroPay();
                 $res = $microPay->pay($input, $sjExtend);
                 if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS") {
+                    $transactionId = $res['transaction_id'] ?? '';
+                    $openId = $res['openid'] ?? '';
+                    $payWay = dict::getDict('payWay', 'wxPay');
+                    PurchaseService::codePay($payWay, $orderSn, $transactionId, $openId);
                     util::success(['returnStatus' => 'SUCCESS']);
                 }
             } catch (\Exception $e) {

+ 51 - 1
biz-hd/purchase/services/PurchaseService.php

@@ -20,6 +20,7 @@ use bizHd\base\services\BaseService;
 use bizHd\purchase\classes\PurchaseClass;
 use common\components\dict;
 use common\components\imgUtil;
+use common\components\noticeUtil;
 use common\components\util;
 use Yii;
 
@@ -215,6 +216,56 @@ class PurchaseService extends BaseService
         OrderService::payAfter($order, $payWay, true);
     }
 
+    //付款码支付处理流程 shish 20211231
+    public static function codePay($payWay, $orderSn, $transactionId = '', $openId = '')
+    {
+        //4秒内同个订单不允许重复请求
+        $cacheKey = 'code_pay_f2f_' . $orderSn;
+        $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+        if (!empty($has)) {
+            Yii::info("付款码支付重复请求:orderSn:{$orderSn}");
+            noticeUtil::push("付款码支付重复请求:orderSn:{$orderSn}", '15280215347');
+            return false;
+        }
+        Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
+
+        $info = self::getByCondition(['orderSn' => $orderSn], true);
+        if (empty($info)) {
+            $msg = "没有找到订单 orderSn:{$orderSn}";
+            Yii::info($msg);
+            util::fail($msg);
+        }
+
+        //临时解决微信一秒内通知二次问题
+        $id = $info->id;
+        $info = PurchaseClass::getLockById($id);
+
+        $info->onlinePay = dict::getDict('onlinePay', 'yes');
+        $info->codePay = 1;
+        $info->thirdNo = $transactionId;
+        $info->payOpenId = $openId;
+        $info->save();
+        self::payAfter($info, $payWay);
+
+        $saleId = $info->saleId ?? 0;
+        $order = OrderClass::getById($saleId, true);
+        $order->onlinePay = dict::getDict('onlinePay', 'yes');
+        $order->codePay = 1;
+        $order->save();
+
+        //不需要打印小票
+        $order->needPrint = dict::getDict('needPrint', 'noNeed');
+        $order->save();
+
+        OrderService::payAfter($order, $payWay, true);
+
+        //直接完成,不走配送流程
+        $order = OrderClass::getLockById($saleId);
+        OrderClass::selfSend($order);
+        $order = OrderClass::getLockById($saleId);
+        OrderService::reach($order);
+    }
+
     //采购订单支付成功后回调处理流程
     public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId = '')
     {
@@ -263,7 +314,6 @@ class PurchaseService extends BaseService
             $order = OrderClass::getLockById($saleId);
             OrderService::reach($order);
         }
-
     }
 
     //第三方支付、余额支付和欠款支付后调用的流程 shish 2021.4.27

+ 11 - 26
vendor/wxPayApi_v3.0.10/example/WxPay.MicroPay.php

@@ -57,40 +57,25 @@ class MicroPay
 	 * @param int $succCode         查询订单结果
 	 * @return 0 订单不成功,1表示订单成功,2表示继续等待
 	 */
-	public function query($out_trade_no, &$succCode)
+	public function query($out_trade_no, $sjExtend)
 	{
 		$queryOrderInput = new WxPayOrderQuery();
 		$queryOrderInput->SetOut_trade_no($out_trade_no);
+
 		$config = new WxPayConfig();
+        $mid = $sjExtend['wxPayMerchantId'] ?? '';
+        $appId = $sjExtend['miniAppId'] ?? '';
+        $key = $sjExtend['wxPayKey'] ?? '';
+        $config->SetAppId($appId);
+        $config->SetMerchantId($mid);
+        $config->SetKey($key);
+
 		try{
 			$result = WxPayApi::orderQuery($config, $queryOrderInput);
+			return $result;
 		} catch(Exception $e) {
-			Log::ERROR(json_encode($e));
+			//Log::ERROR(json_encode($e));
 		}
-		if($result["return_code"] == "SUCCESS" 
-			&& $result["result_code"] == "SUCCESS")
-		{
-			//支付成功
-			if($result["trade_state"] == "SUCCESS"){
-				$succCode = 1;
-			   	return $result;
-			}
-			//用户支付中
-			else if($result["trade_state"] == "USERPAYING"){
-				$succCode = 2;
-				return false;
-			}
-		}
-		
-		//如果返回错误码为“此交易订单号不存在”则直接认定失败
-		if($result["err_code"] == "ORDERNOTEXIST")
-		{
-			$succCode = 0;
-		} else{
-			//如果是系统错误,则后续继续
-			$succCode = 2;
-		}
-		return false;
 	}
 	
 	/**