diff --git a/app/Events/CouponUsedEvent.php b/app/Events/CouponUsedEvent.php index 49168b60..ceb0ac37 100644 --- a/app/Events/CouponUsedEvent.php +++ b/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; } } diff --git a/app/Extensions/PaymentGateways/Mollie/MollieExtension.php b/app/Extensions/PaymentGateways/Mollie/MollieExtension.php index 9145b067..6edd3411 100644 --- a/app/Extensions/PaymentGateways/Mollie/MollieExtension.php +++ b/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'); + $couponCode = $request->input('couponCode'); - // 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)); } Redirect::route('home')->with('success', 'Your payment is being processed')->send(); diff --git a/app/Extensions/PaymentGateways/PayPal/PayPalExtension.php b/app/Extensions/PaymentGateways/PayPal/PayPalExtension.php index 07771e48..e62cbe54 100644 --- a/app/Extensions/PaymentGateways/PayPal/PayPalExtension.php +++ b/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)); diff --git a/app/Extensions/PaymentGateways/Stripe/StripeExtension.php b/app/Extensions/PaymentGateways/Stripe/StripeExtension.php index f54157d4..b6665593 100644 --- a/app/Extensions/PaymentGateways/Stripe/StripeExtension.php +++ b/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 diff --git a/app/Listeners/CouponUsed.php b/app/Listeners/CouponUsed.php index 75ced4eb..7ba8c34b 100644 --- a/app/Listeners/CouponUsed.php +++ b/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; + } } diff --git a/app/Models/Coupon.php b/app/Models/Coupon.php index 97a5b973..082e423f 100644 --- a/app/Models/Coupon.php +++ b/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 */