gorden 6 months ago
parent
commit
46f81c9319

+ 99 - 12
app/admin/controller/order/WholeController.php

@@ -246,7 +246,7 @@ class WholeController extends Curd
                 } else if (!empty($payWay[0]) && $payWay[0] == 'ALIPAY') {
                     $item['payWay'] = '支付宝';
                 }
-            }else{
+            } else {
                 $item['payWay'] = null;
             }
         }
@@ -1857,24 +1857,35 @@ class WholeController extends Curd
                     $result = json_encode($result);
                     $params['pay_json_response'] = $result;
                     $result = json_decode($result, true);
+                    dump("resulr", $result);
 
                     $prefix = substr($params['qrcode_nbr'], 0, 2);
                     if (in_array($prefix, [10, 11, 12, 13, 14, 15])) {
                         $params['pay_category'] = 'WXPAY';
-                        if ($result['return_code'] != 'SUCCESS' || $result['result_code'] != 'SUCCESS') {
-                            Db::rollBack();
-                            return json_fail('支付失败');
+                        if ((!isset($result['return_code']) || $result['return_code'] != 'SUCCESS') || (!isset($result['result_code']) || $result['result_code'] != 'SUCCESS') || (empty($result['trade_state']) || $result['trade_state'] != 'SUCCESS')) {
+                            $params['order_status_system'] = 'PAYING';
+                            $params['order_status_payment'] = 'PENDING';
+//                            Db::rollBack();
+//                            return json_fail('支付失败');
+                        } else {
+                            $params['order_status_system'] = $systemStatus;
+                            $params['order_status_payment'] = 'SUCCESS';
                         }
                     } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
                         $params['pay_category'] = 'ALIPAY';
-                        if ($result['code'] != '10000' || $result['msg'] != 'Success') {
-                            Db::rollBack();
-                            return json_fail('支付失败');
+                        if ((!isset($result['code']) || $result['code'] != '10000') || (empty($result['trade_status']) || $result['trade_status'] != 'TRADE_SUCCESS')) {
+                            $params['order_status_system'] = 'PAYING';
+                            $params['order_status_payment'] = 'PENDING';
+//                            Db::rollBack();
+//                            return json_fail('支付失败');
+                        } else {
+                            $params['order_status_system'] = $systemStatus;
+                            $params['order_status_payment'] = 'SUCCESS';
                         }
                     }
 
                     // 组合支付,追加加一条支付记录 pay_detail
-                    if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) {
+                    if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0 && $params['order_status_payment'] == 'SUCCESS') {
                         $insertPayDetailData = [
                             'join_pay_member_id' => $params['join_order_member_id'],
                             'join_pay_order_id' => $params['orderGroupId'],
@@ -1894,8 +1905,6 @@ class WholeController extends Curd
                         $params['pay_category'] = $submitPayCategory;
                     }
 
-                    $params['order_status_system'] = $systemStatus;
-                    $params['order_status_payment'] = 'SUCCESS';
                     // 账户支付的金额
                     $params['order_amount_pay'] = $accountAmount;
                 }
@@ -2100,7 +2109,11 @@ class WholeController extends Curd
                 }
             }
 
+            dump($params['settlement_now'], $params['order_status_payment']);
             _syslog("订单", "创建订单成功");
+            if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
+                return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
+            }
             return json_success('创建订单成功');
         } catch (BusinessException $e) {
             Db::rollBack();
@@ -2662,7 +2675,7 @@ class WholeController extends Curd
                     'join_sheet_order_id' => $params['orderId'],
                     'join_sheet_goods_id' => $goods['goods_id'],
                     'join_sheet_goods_sku_id' => $goods['sku_id'],
-                    'order_sheet_status' => $params['settlement_now'] == 'Y' ? 'DONE' : 'PAYING',
+                    'order_sheet_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'DONE' : 'PAYING',
                     'order_sheet_category' => (isset($params['submit_goods_classify']) && $params['submit_goods_classify'] == 'MEALS') ? 'DISHES' : 'NORMAL',
                     'order_sheet_num' => $goods['nbr'],
                     'order_sheet_price' => $goods['goods_sales_price'],
@@ -2733,7 +2746,7 @@ class WholeController extends Curd
             $data = [
                 'join_pay_member_id' => $params['join_order_member_id'],
                 'join_pay_order_id' => $params['orderGroupId'],
-                'pay_status' => $params['settlement_now'] == 'Y' ? 'SUCCESS' : 'WAITING',
+                'pay_status' => $params['settlement_now'] == 'Y' && $params['order_status_payment'] == 'SUCCESS' ? 'SUCCESS' : 'WAITING',
                 'pay_category' => $params['goods_classify'],
                 'pay_amount' => $params['order_amount_pay'],
                 'pay_prepayid' => $payPrepayid,
@@ -3913,6 +3926,80 @@ class WholeController extends Curd
         return $data;
     }
 
+    /**
+     * @Desc 查询订单状态
+     * @Author Gorden
+     * @Date 2024/8/19 11:55
+     *
+     * @param Request $request
+     * @return Response|void
+     */
+    public function getOrderPayStatus(Request $request)
+    {
+        $orderId = $request->get('order_id', '');
+        if (!$orderId) {
+            return json_fail('订单不存在');
+        }
+
+        $order = Order::where('order_id', $orderId)->first();
+        if (!$order) {
+            return json_fail('订单不存在');
+        }
+
+        if ($order->order_status_payment == 'SUCCESS') {
+            return json_success('success');
+        }
+
+        $payDetailType = PayDetail::where('join_pay_order_id', $order->order_groupby)->pluck('pay_prepayid')->toArray();
+        try {
+            Db::beginTransaction();
+            if (in_array('WXPAY', $payDetailType)) {
+                $result = Pay::wechat(config('payment.wxpay'))->find($order->order_groupby, 'pos');
+                $result = json_decode(json_encode($result), true);
+                if (!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['result_code']) && $result['result_code'] == 'SUCCESS' && !empty($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
+                    if (in_array($order->order_category, ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'VIP'])) {
+                        $order->order_is_complete = 'Y';
+                        $order->order_status_system = 'DONE';
+                        $order->order_status_payment = 'SUCCESS';
+                    } else {
+                        $order->order_status_system = 'SENDING';
+                    }
+                    $order->save();
+                    // Sheet
+                    OrderSheet::where('join_sheet_order_id', $orderId)->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
+                    // 支付记录
+                    PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_prepayid','WXPAY')->update(['pay_status'=>'SUCCESS']);
+
+                    Db::commit();
+                    return json_success('success');
+                }
+            } else if (in_array('ALIPAY', $payDetailType)) {
+                $result = Pay::alipay(config('payment.alipay'))->find($orderId);
+                $result = json_decode(json_encode($result), true);
+                if (!empty($result['code']) && $result['code'] == '10000' && !empty($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
+                    if (in_array($order->order_category, ['SERVICE', 'CHNMED', 'CHNNCD', 'MEALS', 'VIP'])) {
+                        $order->order_is_complete = 'Y';
+                        $order->order_status_system = 'DONE';
+                        $order->order_status_payment = 'SUCCESS';
+                    } else {
+                        $order->order_status_system = 'SENDING';
+                    }
+                    $order->save();
+                    // Sheet
+                    OrderSheet::where('join_sheet_order_id', $orderId)->where('order_sheet_status', 'PAYING')->update(['order_sheet_status' => 'DONE']);
+                    // 支付记录
+                    PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_prepayid','WXPAY')->update(['pay_status'=>'SUCCESS']);
+
+                    Db::commit();
+                    return json_success('success');
+                }
+            }
+            Db::rollBack();
+        } catch (\Exception $e) {
+            Db::rollBack();
+        }
+    }
+
     public static $goodsClassify = [
         // goodsClassify: {GOODS:"健康产品订单",SERVICE:"服务订单",DEVICE:"设备订单",SPECIAL:"专题业务订单",PACKAGE:"套餐订单",RECHARGE:"充值订单",OTHER:"其他订单"},
 

+ 17 - 13
app/admin/service/order/OrderService.php

@@ -229,9 +229,11 @@ class OrderService
                 $result = self::findWxpay($params['orderGroupId'], 0);
             } catch (\Exception $e) {
                 $log->error("WXPAY", ['msg' => $e->getMessage()]);
-                throw new BusinessException("支付失败");
+                $result = self::findWxpay($params['orderGroupId'], 0);
+//                throw new BusinessException("支付失败");
             }
             try{
+                dump($result);
                 $log->info("WXPAY_RETURN", json_decode(json_encode($result), true));
             }catch (\Exception $e){}
 
@@ -277,13 +279,9 @@ class OrderService
      */
     public static function findWxpay($orderId, $nbr = 0)
     {
-        if ($nbr >= 6) {
-            SupportLog::channel('pay')->error("FIND_WXPAY", ['msg' => '订单查询失败','order_id'=>$orderId]);
-            throw new BusinessException("订单查询失败");
-        }
         try {
-            $result = Pay::wechat(config('payment.wxpay'))->find($orderId, 'pos');
-            $result = json_decode(json_encode($result), true);
+//            $result = Pay::wechat(config('payment.wxpay'))->find($orderId, 'pos');
+//            $result = json_decode(json_encode($result), true);
         } catch (\Exception $e) {
             SupportLog::channel('pay')->error("FIND_WXPAY", ['msg' => $e->getMessage()]);
         }
@@ -291,9 +289,14 @@ class OrderService
         if (!empty($result['return_code']) && $result['return_code'] == 'SUCCESS' && !empty($result['result_code']) && $result['result_code'] == 'SUCCESS' && !empty($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
             return $result;
         } else {
+            if ($nbr > 2) {
+                SupportLog::channel('pay')->error("FIND_WXPAY", ['msg' => '订单查询失败','order_id'=>$orderId]);
+                return ['msg'=>'订单查询失败'];
+            }
+            dump('nbr-'.$nbr);
             sleep(5);
             SupportLog::channel('pay')->error("FIND_WXPAY", ['nbr' => $nbr,'order_id'=>$orderId]);
-            self::findWxpay($orderId, $nbr + 1);
+            return self::findWxpay($orderId, $nbr + 1);
         }
     }
 
@@ -309,10 +312,6 @@ class OrderService
      */
     public static function findAlipay($orderId, $nbr = 0)
     {
-        if ($nbr >= 6) {
-            SupportLog::channel('pay')->error("FIND_ALIPAY", ['msg' => '订单查询失败','order_id'=>$orderId]);
-            throw new BusinessException("订单查询失败");
-        }
         try {
             $result = Pay::alipay(config('payment.alipay'))->find($orderId);
             $result = json_decode(json_encode($result), true);
@@ -323,9 +322,14 @@ class OrderService
         if (!empty($result['code']) && $result['code'] == '10000' && !empty($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
             return $result;
         } else {
+            if ($nbr > 5) {
+                SupportLog::channel('pay')->error("FIND_ALIPAY", ['msg' => '订单查询失败','order_id'=>$orderId]);
+//                throw new BusinessException("订单查询失败");
+                return ['msg'=>'订单查询失败'];
+            }
             sleep(5);
             SupportLog::channel('pay')->error("FIND_ALIPAY", ['nbr' => $nbr,'order_id'=>$orderId]);
-            self::findAlipay($orderId, $nbr + 1);
+            return self::findAlipay($orderId, $nbr + 1);
         }
     }
 

+ 23 - 7
app/functions.php

@@ -3,8 +3,9 @@
  * Here is your custom functions.
  */
 
- if (!function_exists('http_post')) {
-    function http_post($url,$params){
+if (!function_exists('http_post')) {
+    function http_post($url, $params)
+    {
         $ch = curl_init();
         $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
         curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
@@ -25,7 +26,8 @@
 }
 
 if (!function_exists('http_post_json')) {
-    function http_post_json($url,$params){
+    function http_post_json($url, $params)
+    {
         $ch = curl_init();
         $this_header = array('Content-Type: application/json;charset=UTF-8');
         curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
@@ -45,11 +47,12 @@ if (!function_exists('http_post_json')) {
     }
 }
 
-if (!function_exists('month_12')){
-    function month_12(){
+if (!function_exists('month_12')) {
+    function month_12()
+    {
         $months = [];
-        for ($i=0;$i<12;$i++){
-            $months[$i] = date('Y/m',strtotime(date('Y-m-01')."-".$i."month"));
+        for ($i = 0; $i < 12; $i++) {
+            $months[$i] = date('Y/m', strtotime(date('Y-m-01') . "-" . $i . "month"));
         }
 
         return $months;
@@ -84,4 +87,17 @@ if (!function_exists('_syslog')) {
         $model->log_operation_data = $operationData ? json_encode($operationData) : null;
         $model->save();
     }
+}
+
+if (!function_exists('json_throw')) {
+    function json_throw($code, $message, $data = '', $options = JSON_UNESCAPED_UNICODE)
+    {
+        $return = [
+            'code' => $code,
+            'message' => $message,
+            'data' => $data,
+        ];
+
+        return new \support\Response(200, ['Content-Type' => 'application/json'], json_encode($return, $options));
+    }
 }

+ 1 - 0
route/admin.php

@@ -898,6 +898,7 @@ Route::group('/admin', function () {
             Route::post('/cutMeals', [\app\admin\controller\order\WholeController::class, 'cutMeals']);
             Route::post('/customPrinter', [\app\admin\controller\order\WholeController::class, 'customPrinter']);
             Route::post('/cancelOrder', [\app\admin\controller\order\WholeController::class, 'cancelOrder']);
+            Route::get('/getOrderPayStatus', [\app\admin\controller\order\WholeController::class, 'getOrderPayStatus']);
         })->middleware([
             \app\middleware\AdminAuthCheck::class
         ]);