浏览代码

Just by improving the code.

Ferks-FK 2 年之前
父节点
当前提交
6d50834f9c

+ 3 - 1
app/Events/CouponUsedEvent.php

@@ -12,14 +12,16 @@ class CouponUsedEvent
     use Dispatchable, InteractsWithSockets, SerializesModels;
 
     public Coupon $coupon;
+    public string $couponCode;
 
     /**
      * Create a new event instance.
      *
      * @return void
      */
-    public function __construct(Coupon $coupon)
+    public function __construct(Coupon $coupon, string $couponCode)
     {
         $this->coupon = $coupon;
+        $this->couponCode = $couponCode;
     }
 }

+ 3 - 7
app/Extensions/PaymentGateways/Mollie/MollieExtension.php

@@ -119,14 +119,10 @@ class MollieExtension extends AbstractExtension
     {
         $payment = Payment::findOrFail($request->input('payment'));
         $payment->status = 'pending';
-        $coupon_code = $request->input('couponCode');
-
-        // increase the use of the coupon when the payment is confirmed.
-        if ($coupon_code) {
-            $coupon = new Coupon;
-            $coupon->incrementUses($coupon_code);
+        $couponCode = $request->input('couponCode');
 
-            event(new CouponUsedEvent($coupon));
+        if ($couponCode) {
+            event(new CouponUsedEvent(new Coupon, $couponCode));
         }
 
         Redirect::route('home')->with('success', 'Your payment is being processed')->send();

+ 3 - 7
app/Extensions/PaymentGateways/PayPal/PayPalExtension.php

@@ -141,7 +141,7 @@ class PayPalExtension extends AbstractExtension
 
         $payment = Payment::findOrFail($laravelRequest->payment);
         $shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
-		$coupon_code = $laravelRequest->input('couponCode');
+		$couponCode = $laravelRequest->input('couponCode');
 
         $request = new OrdersCaptureRequest($laravelRequest->input('token'));
         $request->prefer('return=representation');
@@ -156,12 +156,8 @@ class PayPalExtension extends AbstractExtension
                     'payment_id' => $response->result->id,
                 ]);
 
-                // increase the use of the coupon when the payment is confirmed.
-                if ($coupon_code) {
-                    $coupon = new Coupon;
-                    $coupon->incrementUses($coupon_code);
-
-                    event(new CouponUsedEvent($coupon));
+                if ($couponCode) {
+                    event(new CouponUsedEvent(new Coupon, $couponCode));
                 }
 
                 event(new UserUpdateCreditsEvent($user));

+ 6 - 9
app/Extensions/PaymentGateways/Stripe/StripeExtension.php

@@ -44,9 +44,6 @@ class StripeExtension extends AbstractExtension
     {
         $user = Auth::user();
         $shopProduct = ShopProduct::findOrFail($request->shopProduct);
-        $discount = PartnerDiscount::getDiscount();
-        $couponCode = $request->input('couponCode');
-        $isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
         $price = $shopProduct->price;
 
         // check if the price is valid for stripe
@@ -55,6 +52,10 @@ class StripeExtension extends AbstractExtension
             return;
         }
 
+        $discount = PartnerDiscount::getDiscount();
+        $couponCode = $request->input('couponCode');
+        $isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
+
         // Coupon Discount.
         if ($isValidCoupon->getStatusCode() == 200) {
             $price = $this->calcDiscount($price, $isValidCoupon->getData());
@@ -152,12 +153,8 @@ class StripeExtension extends AbstractExtension
                     'status' => 'paid',
                 ]);
 
-                 // increase the use of the coupon when the payment is confirmed.
-                 if ($couponCode) {
-                    $coupon = new Coupon;
-                    $coupon->incrementUses($couponCode);
-
-                    event(new CouponUsedEvent($coupon));
+                if ($couponCode) {
+                    event(new CouponUsedEvent(new Coupon, $couponCode));
                 }
 
                 //payment notification

+ 30 - 0
app/Listeners/CouponUsed.php

@@ -30,6 +30,14 @@ class CouponUsed
      */
     public function handle(CouponUsedEvent $event)
     {
+        // Always check the authenticity of the coupon.
+        if (!$this->isValidCoupon($event)) {
+            return;
+        }
+
+        // Automatically increments the coupon usage.
+        $this->incrementUses($event);
+
         if ($this->delete_coupon_on_expires) {
             if (!is_null($event->coupon->expired_at)) {
                 if ($event->coupon->expires_at <= Carbon::now()->timestamp) {
@@ -44,4 +52,26 @@ class CouponUsed
             }
         }
     }
+
+    /**
+     * Increments the use of a coupon.
+     *
+     * @param \App\Events\CouponUsedEvent  $event
+     */
+    private function incrementUses(CouponUsedEvent $event)
+    {
+        $event->coupon->where('code', $event->coupon->code)->increment('uses');
+    }
+
+    /**
+     * It checks that the coupon received from the request really exists.
+     *
+     * @param \App\Events\CouponUsedEvent  $event
+     *
+     * @return bool
+     */
+    private function isValidCoupon(CouponUsedEvent $event): bool
+    {
+        return $event->coupon->code === $event->couponCode ? true : false;
+    }
 }

+ 0 - 15
app/Models/Coupon.php

@@ -107,21 +107,6 @@ class Coupon extends Model
         return $coupons;
     }
 
-    /**
-     * Increments the use of a coupon.
-     *
-     * @param string $code Coupon Code.
-     * @param int $amount Amount to increment.
-     *
-     * @return bool
-     */
-    public function incrementUses(string $code, int $amount = 1): bool
-    {
-        $this->where('code', $code)->increment('uses', $amount);
-
-        return true;
-    }
-
     /**
      * @return BelongsToMany
      */