Just by improving the code.

This commit is contained in:
Ferks-FK 2023-05-20 19:55:19 +00:00 committed by IceToast
parent 4320fa9ef6
commit 6d50834f9c
6 changed files with 45 additions and 39 deletions

View file

@ -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;
}
}

View file

@ -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();

View file

@ -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));

View file

@ -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

View file

@ -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;
}
}

View file

@ -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
*/