diff --git a/src/Events/StripeWebhookInvoked.php b/src/Events/StripeWebhookInvoked.php new file mode 100644 index 0000000..e507d57 --- /dev/null +++ b/src/Events/StripeWebhookInvoked.php @@ -0,0 +1,24 @@ +event = $event; + } +} diff --git a/src/Listeners/HandlePaymentIntentSuccededEvent.php b/src/Listeners/HandlePaymentIntentSuccededEvent.php new file mode 100644 index 0000000..d80734e --- /dev/null +++ b/src/Listeners/HandlePaymentIntentSuccededEvent.php @@ -0,0 +1,34 @@ +event; + + try { + $transaction = Transaction::query()->where('key', $stripeEvent->data['object']['id'])->firstOrFail(); + } catch (Throwable $exception) { + $order = Order::query()->where('uuid', $stripeEvent->data['object']['metadata']['order'])->firstOrFail(); + + $transaction = Gateway::driver('stripe')->pay( + $order, + $stripeEvent->data['object']['amount'] / 100, + ['key' => $stripeEvent->data['object']['id']] + ); + } + + $transaction->markAsCompleted(); + } +} diff --git a/src/StripeDriver.php b/src/StripeDriver.php index 9103995..40380ab 100644 --- a/src/StripeDriver.php +++ b/src/StripeDriver.php @@ -7,13 +7,12 @@ use Cone\Bazar\Interfaces\LineItem; use Cone\Bazar\Models\Order; use Cone\Bazar\Models\Transaction; +use Cone\Bazar\Stripe\Events\StripeWebhookInvoked; use Illuminate\Http\Request; use Illuminate\Support\Facades\URL; use Stripe\Checkout\Session; -use Stripe\Event; use Stripe\StripeClient; use Stripe\Webhook; -use Throwable; class StripeDriver extends Driver { @@ -156,34 +155,8 @@ public function handleNotification(Request $request): Response $this->config['secret'] ); - switch ($event->type) { - case 'payment_intent.succeeded': - $this->handlePaymentIntentSucceeded($request, $event); - break; - default: - break; - } + StripeWebhookInvoked::dispatch($event); return parent::handleNotification($request); } - - /** - * Handle the payment_intent.succeeded event. - */ - protected function handlePaymentIntentSucceeded(Request $request, Event $event): void - { - try { - $transaction = Transaction::query()->where('key', $event->data['object']['id'])->firstOrFail(); - } catch (Throwable $exception) { - $order = Order::query()->where('uuid', $event->data['object']['metadata']['order'])->firstOrFail(); - - $transaction = $this->pay( - $order, - $event->data['object']['amount'] / 100, - ['key' => $event->data['object']['id']] - ); - } - - $transaction->markAsCompleted(); - } } diff --git a/src/StripeServiceProvider.php b/src/StripeServiceProvider.php index f211a0f..a8335ed 100644 --- a/src/StripeServiceProvider.php +++ b/src/StripeServiceProvider.php @@ -33,5 +33,9 @@ public function boot(): void 'bazar-stripe-config' ); } + + $this->app['events']->listen( + Events\StripeWebhookInvoked::class, Listeners\HandlePaymentIntentSuccededEvent::class + ); } }