Просмотр исходного кода

fixbug:支付异常,释放优惠券

gorden 2 месяцев назад
Родитель
Сommit
ac3e967422
2 измененных файлов с 103 добавлено и 17 удалено
  1. 101 15
      app/admin/controller/order/WholeController.php
  2. 2 2
      app/admin/service/order/OrderService.php

+ 101 - 15
app/admin/controller/order/WholeController.php

@@ -836,6 +836,10 @@ class WholeController extends Curd
         try {
             // 使用优惠券
             $couponUseJson = [];
+            if (!empty($order->order_discount_json)) {
+                // 释放下单时选的优惠券
+                $order->order_discount_json = $this->releaseCoupon(json_decode($order->order_discount_json, true));
+            }
             if (!empty($params['join_order_member_id']) && !empty($params['preferential'])) {
                 $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
                 if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
@@ -844,12 +848,12 @@ class WholeController extends Curd
                 // 组装优惠券使用数据,存主表优惠里
                 if (!empty($couponResult['use_coupon_json'])) {
                     $couponUseJson = $couponResult['use_coupon_json'];
-                    if (!empty($order->order_discount_json)) {
-                        $orderDiscountJson = json_decode($order->order_discount_json, true);
-                        if (!empty($orderDiscountJson)) {
-                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
-                        }
-                    }
+//                    if (!empty($order->order_discount_json)) {
+//                        $orderDiscountJson = json_decode($order->order_discount_json, true);
+//                        if (!empty($orderDiscountJson)) {
+//                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
+//                        }
+//                    }
                 }
             }
             // 存储优惠信息
@@ -1308,6 +1312,9 @@ class WholeController extends Curd
 
             if ($order->order_status_payment != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
+
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "订单支付成功");
@@ -1441,6 +1448,10 @@ class WholeController extends Curd
         Db::beginTransaction();
         try {// 使用优惠券
             $couponUseJson = [];
+            if (!empty($order->order_discount_json)) {
+                // 释放下单时选的优惠券
+                $order->order_discount_json = $this->releaseCoupon(json_decode($order->order_discount_json, true));
+            }
             if (!empty($params['join_order_member_id']) && !empty($params['preferential'])) {
                 $couponResult = OrderService::payUseCoupon('pay', 'Y', $params['join_order_member_id'], $params['goodsContentList'], $params['preferential'], $params['order_amount_total']);
                 if (!empty($couponResult['pay_amount']) && $couponResult['pay_amount'] != $params['order_amount_pay']) {
@@ -1449,12 +1460,12 @@ class WholeController extends Curd
                 // 组装优惠券使用数据,存主表优惠里
                 if (!empty($couponResult['use_coupon_json'])) {
                     $couponUseJson = $couponResult['use_coupon_json'];
-                    if (!empty($order->order_discount_json)) {
-                        $orderDiscountJson = json_decode($order->order_discount_json, true);
-                        if (!empty($orderDiscountJson)) {
-                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
-                        }
-                    }
+//                    if (!empty($order->order_discount_json)) {
+//                        $orderDiscountJson = json_decode($order->order_discount_json, true);
+//                        if (!empty($orderDiscountJson)) {
+//                            $couponUseJson = json_encode(array_merge($orderDiscountJson, json_decode($couponUseJson, true)));
+//                        }
+//                    }
                 }
             }
             // 存储优惠信息
@@ -1751,6 +1762,8 @@ class WholeController extends Curd
             }
             if ($order->order_status_payment != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "订单支付成功");
@@ -2489,6 +2502,9 @@ class WholeController extends Curd
 
             if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
+
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "创建订单成功");
@@ -2508,6 +2524,66 @@ class WholeController extends Curd
         }
     }
 
+    /**
+     * @Desc 释放下单时选的优惠券
+     * @Author Gorden
+     * @Date 2024/9/19 9:56
+     *
+     * @param $coupon
+     * @return false|string
+     */
+    private function releaseCoupon($coupon)
+    {
+        if (!empty($coupon) && is_array($coupon)) {
+            $updateData = [
+                'coupon_detail_status'=>'ACTIVED',
+                'coupon_detail_used_datetime' => ''
+            ];
+            foreach ($coupon as $key => $item) {
+                if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
+                    foreach ($item['coupon_detail_id'] as $detailId) {
+                        CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
+                            ->where('coupon_detail_id', $detailId)
+                            ->where('coupon_detail_status','<>','USED')
+                            ->update($updateData);
+                    }
+                    unset($coupon[$key]);
+                }
+            }
+        }
+
+        return json_encode($coupon);
+    }
+
+    /**
+     * @Desc 修改优惠券状态
+     * @Author Gorden
+     * @Date 2024/9/19 9:03
+     *
+     * @param $coupon
+     * @param $status
+     * @return void
+     */
+    private function changeOrderCouponStatus($coupon, $status)
+    {
+        if (!empty($coupon) && is_array($coupon)) {
+            $updateData['coupon_detail_status'] = $status;
+            if ($status == 'ACTIVED') {
+                $updateData['coupon_detail_used_datetime'] = '';
+            } elseif ($status == 'USED') {
+                $updateData['coupon_detail_used_datetime'] = date('Y-m-d H:i:s');
+            }
+            foreach ($coupon as $item) {
+                if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
+                    foreach ($item['coupon_detail_id'] as $detailId) {
+                        CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
+                            ->where('coupon_detail_id', $detailId)->update($updateData);
+                    }
+                }
+            }
+        }
+    }
+
     /**
      * 组合支付下单
      */
@@ -2973,6 +3049,9 @@ class WholeController extends Curd
 
             if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
+                // 恢复优惠券
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
+
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "创建订单成功");
@@ -4232,7 +4311,6 @@ class WholeController extends Curd
             } else {
                 $orderIds = $goodsOrderIds;
             }
-            dump($orderIds);
         }
         if (!empty($orderId) || !empty($goodsName)) {
             $where['order_id'] = ['in', implode(',', $orderIds)];
@@ -4507,6 +4585,11 @@ class WholeController extends Curd
             return json_fail('订单不存在');
         }
 
+        $discountJson = [];
+        if (!empty($order->order_discount_json)) {
+            $discountJson = json_decode($order->order_discount_json, true);
+        }
+
         if ($order->order_status_payment == 'SUCCESS') {
             return json_success('success');
         }
@@ -4518,10 +4601,10 @@ class WholeController extends Curd
                 $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') {
+                    $order->order_status_payment = 'SUCCESS';
                     if (in_array($order->order_category, ['SERVICE', 'CHNMED', 'CHNNCD', 'DISHES', 'MEALS', 'VIP'])) {
                         $order->order_is_complete = 'Y';
                         $order->order_status_system = 'DONE';
-                        $order->order_status_payment = 'SUCCESS';
                     } else {
                         $order->order_status_system = 'SENDING';
                     }
@@ -4530,6 +4613,8 @@ class WholeController extends Curd
                     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']);
+                    // 优惠券标记使用
+                    $this->changeOrderCouponStatus($discountJson, 'USED');
 
                     Db::commit();
                     return json_success('success');
@@ -4550,7 +4635,8 @@ class WholeController extends Curd
                     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', 'ALIPAY')->update(['pay_status' => 'SUCCESS']);
-
+                    // 优惠券标记使用
+                    $this->changeOrderCouponStatus($discountJson, 'USED');
                     Db::commit();
                     return json_success('success');
                 }

+ 2 - 2
app/admin/service/order/OrderService.php

@@ -548,7 +548,7 @@ class OrderService
                     ->where('join_goods_coupon_id', $couponId)
                     ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
                 if ($settlementNow == 'Y' && $type == 'pay') {
-                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
+                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
                 } else {
                     $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
                 }
@@ -731,7 +731,7 @@ class OrderService
                     ->where('join_goods_coupon_id', $couponId)
                     ->where('coupon_detail.join_coupon_detail_member_id', $memberId);
                 if ($settlementNow == 'Y') {
-                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
+                    $couponDetail = $couponDetail->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED']);
                 } else {
                     $couponDetail = $couponDetail->where('coupon_detail.coupon_detail_status', 'ACTIVED');
                 }