Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 17, 2024
1 parent 1275cf8 commit 9d877cc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 29 deletions.
24 changes: 24 additions & 0 deletions src/Events/StripeWebhookInvoked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Cone\Bazar\Stripe\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Stripe\Event;

class StripeWebhookInvoked
{
use Dispatchable;

/**
* The event instance.
*/
public Event $event;

/**
* Create a new event instance.
*/
public function __construct(Event $event)
{
$this->event = $event;
}
}
34 changes: 34 additions & 0 deletions src/Listeners/HandlePaymentIntentSuccededEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

Check warning on line 1 in src/Listeners/HandlePaymentIntentSuccededEvent.php

View workflow job for this annotation

GitHub Actions / 文A Typos check

"Succeded" should be "Succeeded".

namespace Cone\Bazar\Stripe\Listeners;

use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Transaction;
use Cone\Bazar\Stripe\Events\StripeWebhookInvoked;
use Cone\Bazar\Support\Facades\Gateway;
use Throwable;

class HandlePaymentIntentSuccededEvent

Check warning on line 11 in src/Listeners/HandlePaymentIntentSuccededEvent.php

View workflow job for this annotation

GitHub Actions / 文A Typos check

"Succeded" should be "Succeeded".
{
/**
* Handle the event.
*/
public function handle(StripeWebhookInvoked $event): void
{
$stripeEvent = $event->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();
}
}
31 changes: 2 additions & 29 deletions src/StripeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
}
4 changes: 4 additions & 0 deletions src/StripeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ public function boot(): void
'bazar-stripe-config'
);
}

$this->app['events']->listen(
Events\StripeWebhookInvoked::class, Listeners\HandlePaymentIntentSuccededEvent::class
);
}
}

0 comments on commit 9d877cc

Please sign in to comment.