feat: ✨ Added Enum for payment states
This commit is contained in:
parent
48e5cae9a1
commit
2161464ee3
5 changed files with 28 additions and 27 deletions
12
app/Enums/PaymentStatus.php
Normal file
12
app/Enums/PaymentStatus.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
// Payment status are open, processing, paid and canceled
|
||||
enum PaymentStatus: String
|
||||
{
|
||||
case OPEN = "open";
|
||||
case PROCESSING = "processing";
|
||||
case PAID = "paid";
|
||||
case CANCELED = "canceled";
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Extensions\PaymentGateways\Mollie;
|
||||
|
||||
use App\Classes\AbstractExtension;
|
||||
use App\Enums\PaymentStatus;
|
||||
use App\Events\PaymentEvent;
|
||||
use App\Events\UserUpdateCreditsEvent;
|
||||
use App\Models\PartnerDiscount;
|
||||
|
@ -74,12 +75,8 @@ class MollieExtension extends AbstractExtension
|
|||
static function success(Request $request): void
|
||||
{
|
||||
$payment = Payment::findOrFail($request->input('payment'));
|
||||
$payment->status = 'pending';
|
||||
$couponCode = $request->input('couponCode');
|
||||
|
||||
if ($couponCode) {
|
||||
event(new CouponUsedEvent($couponCode));
|
||||
}
|
||||
$payment->status = PaymentStatus::PROCESSING;
|
||||
$payment->save();
|
||||
|
||||
Redirect::route('home')->with('success', 'Your payment is being processed')->send();
|
||||
return;
|
||||
|
@ -100,16 +97,15 @@ class MollieExtension extends AbstractExtension
|
|||
return response()->json(['success' => false]);
|
||||
}
|
||||
|
||||
$payment = Payment::findOrFail($response->json()['metadata']['payment_id']);
|
||||
$payment->status->update([
|
||||
'status' => $response->json()['status'],
|
||||
]);
|
||||
|
||||
$payment = Payment::findOrFail($response->json()['metadata']['payment_id']);
|
||||
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
||||
event(new PaymentEvent($payment, $payment, $shopProduct));
|
||||
|
||||
if ($response->json()['status'] == 'paid') {
|
||||
$user = User::findOrFail($payment->user_id);
|
||||
$payment->status = PaymentStatus::PAID;
|
||||
$payment->save();
|
||||
event(new UserUpdateCreditsEvent($user));
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
|
|
|
@ -7,6 +7,7 @@ use App\Events\PaymentEvent;
|
|||
use App\Events\UserUpdateCreditsEvent;
|
||||
use App\Extensions\PaymentGateways\PayPal\PayPalSettings;
|
||||
use App\Classes\PaymentExtension;
|
||||
use App\Enums\PaymentStatus;
|
||||
use App\Models\PartnerDiscount;
|
||||
use App\Models\Payment;
|
||||
use App\Models\ShopProduct;
|
||||
|
@ -104,7 +105,6 @@ class PayPalExtension extends PaymentExtension
|
|||
|
||||
$payment = Payment::findOrFail($laravelRequest->payment);
|
||||
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
||||
$couponCode = $laravelRequest->input('couponCode');
|
||||
|
||||
$request = new OrdersCaptureRequest($laravelRequest->input('token'));
|
||||
$request->prefer('return=representation');
|
||||
|
@ -115,13 +115,10 @@ class PayPalExtension extends PaymentExtension
|
|||
if ($response->statusCode == 201 || $response->statusCode == 200) {
|
||||
//update payment
|
||||
$payment->update([
|
||||
'status' => 'paid',
|
||||
'status' => PaymentStatus::PAID,
|
||||
'payment_id' => $response->result->id,
|
||||
]);
|
||||
|
||||
if ($couponCode) {
|
||||
event(new CouponUsedEvent($couponCode));
|
||||
}
|
||||
|
||||
event(new UserUpdateCreditsEvent($user));
|
||||
event(new PaymentEvent($user, $payment, $shopProduct));
|
||||
|
@ -134,7 +131,7 @@ class PayPalExtension extends PaymentExtension
|
|||
dd($response);
|
||||
} else {
|
||||
$payment->update([
|
||||
'status' => 'cancelled',
|
||||
'status' => PaymentStatus::CANCELED,
|
||||
'payment_id' => $response->result->id,
|
||||
]);
|
||||
abort(500);
|
||||
|
@ -146,7 +143,7 @@ class PayPalExtension extends PaymentExtension
|
|||
dd($ex->getMessage());
|
||||
} else {
|
||||
$payment->update([
|
||||
'status' => 'cancelled',
|
||||
'status' => PaymentStatus::CANCELED,
|
||||
'payment_id' => $response->result->id,
|
||||
]);
|
||||
abort(422);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Extensions\PaymentGateways\Stripe;
|
||||
|
||||
use App\Classes\AbstractExtension;
|
||||
use App\Enums\PaymentStatus;
|
||||
use App\Events\PaymentEvent;
|
||||
use App\Events\CouponUsedEvent;
|
||||
use App\Events\UserUpdateCreditsEvent;
|
||||
|
@ -93,7 +94,6 @@ class StripeExtension extends AbstractExtension
|
|||
$user = User::findOrFail($user->id);
|
||||
$payment = Payment::findOrFail($request->input('payment'));
|
||||
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
||||
$couponCode = $request->input('couponCode');
|
||||
|
||||
Redirect::route('home')->with('success', 'Please wait for success')->send();
|
||||
|
||||
|
@ -112,16 +112,11 @@ class StripeExtension extends AbstractExtension
|
|||
//update payment
|
||||
$payment->update([
|
||||
'payment_id' => $paymentSession->payment_intent,
|
||||
'status' => 'paid',
|
||||
'status' => PaymentStatus::PAID,
|
||||
]);
|
||||
|
||||
if ($couponCode) {
|
||||
event(new CouponUsedEvent(new Coupon, $couponCode));
|
||||
}
|
||||
|
||||
//payment notification
|
||||
$user->notify(new ConfirmPaymentNotification($payment));
|
||||
|
||||
event(new UserUpdateCreditsEvent($user));
|
||||
event(new PaymentEvent($user, $payment, $shopProduct));
|
||||
|
||||
|
@ -133,7 +128,7 @@ class StripeExtension extends AbstractExtension
|
|||
//update payment
|
||||
$payment->update([
|
||||
'payment_id' => $paymentSession->payment_intent,
|
||||
'status' => 'processing',
|
||||
'status' => PaymentStatus::PROCESSING,
|
||||
]);
|
||||
|
||||
event(new PaymentEvent($user, $payment, $shopProduct));
|
||||
|
@ -174,7 +169,7 @@ class StripeExtension extends AbstractExtension
|
|||
//update payment db entry status
|
||||
$payment->update([
|
||||
'payment_id' => $payment->payment_id ?? $paymentIntent->id,
|
||||
'status' => 'paid'
|
||||
'status' => PaymentStatus::PAID,
|
||||
]);
|
||||
|
||||
//payment notification
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Enums\PaymentStatus;
|
||||
use App\Events\PaymentEvent;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
@ -48,7 +49,7 @@ class UserPayment
|
|||
$shopProduct = $event->shopProduct;
|
||||
|
||||
// only update user if payment is paid
|
||||
if ($event->payment->status != "paid") {
|
||||
if ($event->payment->status != PaymentStatus::PAID) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue