Just by improving the code.
This commit is contained in:
parent
4320fa9ef6
commit
6d50834f9c
|
@ -12,14 +12,16 @@ class CouponUsedEvent
|
||||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||||
|
|
||||||
public Coupon $coupon;
|
public Coupon $coupon;
|
||||||
|
public string $couponCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new event instance.
|
* Create a new event instance.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(Coupon $coupon)
|
public function __construct(Coupon $coupon, string $couponCode)
|
||||||
{
|
{
|
||||||
$this->coupon = $coupon;
|
$this->coupon = $coupon;
|
||||||
|
$this->couponCode = $couponCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,14 +119,10 @@ class MollieExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
$payment = Payment::findOrFail($request->input('payment'));
|
$payment = Payment::findOrFail($request->input('payment'));
|
||||||
$payment->status = 'pending';
|
$payment->status = 'pending';
|
||||||
$coupon_code = $request->input('couponCode');
|
$couponCode = $request->input('couponCode');
|
||||||
|
|
||||||
// increase the use of the coupon when the payment is confirmed.
|
if ($couponCode) {
|
||||||
if ($coupon_code) {
|
event(new CouponUsedEvent(new Coupon, $couponCode));
|
||||||
$coupon = new Coupon;
|
|
||||||
$coupon->incrementUses($coupon_code);
|
|
||||||
|
|
||||||
event(new CouponUsedEvent($coupon));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Redirect::route('home')->with('success', 'Your payment is being processed')->send();
|
Redirect::route('home')->with('success', 'Your payment is being processed')->send();
|
||||||
|
|
|
@ -141,7 +141,7 @@ class PayPalExtension extends AbstractExtension
|
||||||
|
|
||||||
$payment = Payment::findOrFail($laravelRequest->payment);
|
$payment = Payment::findOrFail($laravelRequest->payment);
|
||||||
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
||||||
$coupon_code = $laravelRequest->input('couponCode');
|
$couponCode = $laravelRequest->input('couponCode');
|
||||||
|
|
||||||
$request = new OrdersCaptureRequest($laravelRequest->input('token'));
|
$request = new OrdersCaptureRequest($laravelRequest->input('token'));
|
||||||
$request->prefer('return=representation');
|
$request->prefer('return=representation');
|
||||||
|
@ -156,12 +156,8 @@ class PayPalExtension extends AbstractExtension
|
||||||
'payment_id' => $response->result->id,
|
'payment_id' => $response->result->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// increase the use of the coupon when the payment is confirmed.
|
if ($couponCode) {
|
||||||
if ($coupon_code) {
|
event(new CouponUsedEvent(new Coupon, $couponCode));
|
||||||
$coupon = new Coupon;
|
|
||||||
$coupon->incrementUses($coupon_code);
|
|
||||||
|
|
||||||
event(new CouponUsedEvent($coupon));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
event(new UserUpdateCreditsEvent($user));
|
event(new UserUpdateCreditsEvent($user));
|
||||||
|
|
|
@ -44,9 +44,6 @@ class StripeExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
|
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
|
||||||
$discount = PartnerDiscount::getDiscount();
|
|
||||||
$couponCode = $request->input('couponCode');
|
|
||||||
$isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
|
|
||||||
$price = $shopProduct->price;
|
$price = $shopProduct->price;
|
||||||
|
|
||||||
// check if the price is valid for stripe
|
// check if the price is valid for stripe
|
||||||
|
@ -55,6 +52,10 @@ class StripeExtension extends AbstractExtension
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$discount = PartnerDiscount::getDiscount();
|
||||||
|
$couponCode = $request->input('couponCode');
|
||||||
|
$isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
|
||||||
|
|
||||||
// Coupon Discount.
|
// Coupon Discount.
|
||||||
if ($isValidCoupon->getStatusCode() == 200) {
|
if ($isValidCoupon->getStatusCode() == 200) {
|
||||||
$price = $this->calcDiscount($price, $isValidCoupon->getData());
|
$price = $this->calcDiscount($price, $isValidCoupon->getData());
|
||||||
|
@ -152,12 +153,8 @@ class StripeExtension extends AbstractExtension
|
||||||
'status' => 'paid',
|
'status' => 'paid',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// increase the use of the coupon when the payment is confirmed.
|
|
||||||
if ($couponCode) {
|
if ($couponCode) {
|
||||||
$coupon = new Coupon;
|
event(new CouponUsedEvent(new Coupon, $couponCode));
|
||||||
$coupon->incrementUses($couponCode);
|
|
||||||
|
|
||||||
event(new CouponUsedEvent($coupon));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//payment notification
|
//payment notification
|
||||||
|
|
|
@ -30,6 +30,14 @@ class CouponUsed
|
||||||
*/
|
*/
|
||||||
public function handle(CouponUsedEvent $event)
|
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 ($this->delete_coupon_on_expires) {
|
||||||
if (!is_null($event->coupon->expired_at)) {
|
if (!is_null($event->coupon->expired_at)) {
|
||||||
if ($event->coupon->expires_at <= Carbon::now()->timestamp) {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,21 +107,6 @@ class Coupon extends Model
|
||||||
return $coupons;
|
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
|
* @return BelongsToMany
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue