Browse Source

improve logic about coupon

- improve check coupon status & language scripts
- return coupon usage when order closed, instead of payment closed
兔姬桑 4 years ago
parent
commit
46123cf61d

+ 15 - 10
app/Http/Controllers/UserController.php

@@ -400,30 +400,35 @@ class UserController extends Controller
 
         $coupon = Coupon::whereSn($coupon_sn)->whereIn('type', [1, 2])->first();
         if (! $coupon) {
-            return Response::json(['status' => 'fail', 'title' => trans('common.failed'), 'message' => trans('user.unknown').trans('user.coupon.attribute')]);
+            return Response::json(['status' => 'fail', 'title' => trans('common.failed'), 'message' => trans('user.coupon.error.unknown')]);
         }
 
         if ($coupon->status === 1) {
-            return Response::json(['status' => 'fail', 'title' => trans('common.sorry'), 'message' => trans('user.coupon.attribute').trans('user.status.used')]);
+            return Response::json(['status' => 'fail', 'title' => trans('common.sorry'), 'message' => trans('user.coupon.error.used')]);
         }
-
-        if ($coupon->status === 2) {
-            return Response::json(['status' => 'fail', 'title' => trans('common.sorry'), 'message' => trans('user.coupon.attribute').trans('user.status.expired')]);
-        }
-
         if ($coupon->getRawOriginal('end_time') < time()) {
             $coupon->status = 2;
             $coupon->save();
 
-            return Response::json(['status' => 'fail', 'title' => trans('common.sorry'), 'message' => trans('user.coupon.attribute').trans('user.status.expired')]);
+            return Response::json(['status' => 'fail', 'title' => trans('common.sorry'), 'message' => trans('user.coupon.error.expired')]);
+        }
+
+        if ($coupon->status === 2) {
+            if ($coupon->usable_times === 0) {
+                return Response::json(['status' => 'fail', 'title' => trans('common.sorry'), 'message' => trans('user.coupon.error.run_out')]);
+            }
+
+            return Response::json(['status' => 'fail', 'title' => trans('common.sorry'), 'message' => trans('user.coupon.error.expired')]);
         }
 
         if ($coupon->start_time > date('Y-m-d H:i:s')) {
-            return Response::json(['status' => 'fail', 'title' => trans('user.coupon.inactive'), 'message' => trans('user.coupon.wait_active', ['time' => $coupon->start_time])]);
+            return Response::json(['status'  => 'fail', 'title' => trans('user.coupon.error.inactive'),
+                'message' => trans('user.coupon.error.wait', ['time' => $coupon->start_time]),
+            ]);
         }
 
         if ($good_price < $coupon->rule) {
-            return Response::json(['status' => 'fail', 'title' => trans('user.coupon.limit'), 'message' => trans('user.coupon.higher', ['amount' => $coupon->rule])]);
+            return Response::json(['status' => 'fail', 'title' => trans('user.coupon.error.limit'), 'message' => trans('user.coupon.error.higher', ['amount' => $coupon->rule])]);
         }
 
         $data = [

+ 7 - 10
app/Observers/OrderObserver.php

@@ -14,16 +14,13 @@ class OrderObserver
     {
         $changes = $order->getChanges();
         if (Arr::exists($changes, 'status')) {
-            // 本地订单-在线订单 关闭互联
-            if ($changes['status'] === -1) {
-                $payment = $order->payment;
-                if ($payment) {
-                    // 关闭在线订单
-                    $payment->close();
-                    // 退回优惠券
-                    if ($order->coupon_id && $this->returnCoupon($order->coupon)) {
-                        Helpers::addCouponLog('订单超时未支付,自动退回', $order->coupon_id, $order->goods_id, $order->id);
-                    }
+            if ($changes['status'] === -1) { // 本地订单-在线订单 关闭互联
+                if ($order->has('payment')) {
+                    $order->payment->close(); // 关闭在线订单
+                }
+
+                if ($order->has('coupon') && $this->returnCoupon($order->coupon)) { // 退回优惠券
+                    Helpers::addCouponLog('订单超时未支付,自动退回', $order->coupon_id, $order->goods_id, $order->id);
                 }
             }
 

+ 14 - 8
resources/lang/en/user.php

@@ -85,14 +85,20 @@ return [
         'unbind'     => 'Unbind',
     ],
     'coupon'              => [
-        'attribute'   => 'Coupon',
-        'voucher'     => 'Voucher',
-        'recharge'    => 'Gift Card',
-        'inactive'    => 'Coupon Inactive',
-        'wait_active' => 'The Event will begin until :time, Please wait',
-        'limit'       => 'Coupon Not Satisfied',
-        'higher'      => 'The minimum requirement of this coupon is ¥:amount',
-        'discount'    => 'Discount',
+        'attribute' => 'Coupon',
+        'voucher'   => 'Voucher',
+        'recharge'  => 'Gift Card',
+        'discount'  => 'Discount',
+        'error'     => [
+            'unknown'  => 'unKnown Coupon',
+            'used'     => 'This Coupon has been used.',
+            'expired'  => 'Out of Date',
+            'run_out'  => 'Run Out of Usage',
+            'inactive' => 'Coupon Inactive yet',
+            'wait'     => 'The Event will begin until :time, Please wait',
+            'limit'    => 'Order\'s price is reach the minimum requirement of this coupon',
+            'higher'   => 'The minimum requirement of this coupon is ¥:amount',
+        ],
     ],
     'error_response'      => 'Something went wrong, please try again later.',
     'invite'              => [

+ 14 - 8
resources/lang/zh_CN/user.php

@@ -85,14 +85,20 @@ return [
         'unbind'     => '解 绑',
     ],
     'coupon'              => [
-        'attribute'   => '优惠券',
-        'voucher'     => '代金券',
-        'recharge'    => '充值券码',
-        'inactive'    => '优惠券尚未生效',
-        'wait_active' => '活动将于:time生效,请耐心等待!',
-        'limit'       => '使用条件未满足',
-        'higher'      => '本券最低使用金额为 ¥:amount',
-        'discount'    => '优惠',
+        'attribute' => '优惠券',
+        'voucher'   => '代金券',
+        'recharge'  => '充值券码',
+        'discount'  => '优惠',
+        'error'     => [
+            'unknown'  => '无效优惠券',
+            'used'     => '优惠券已被使用',
+            'expired'  => '优惠券已过期',
+            'run_out'  => '优惠券耗尽',
+            'inactive' => '优惠券尚未生效',
+            'wait'     => '活动将于:time生效,请耐心等待!',
+            'limit'    => '使用条件未满足',
+            'higher'   => '本券最低使用金额为 ¥:amount',
+        ],
     ],
     'error_response'      => '出现了错误,请稍后再试。',
     'invite'              => [