diff --git a/README.md b/README.md index a1e6b54..ca77a76 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,8 @@ class StripeWebhookHandler { public function handle(WebhookInvoked $event): void { + // https://stripe.com/docs/api/events/types $callback = match ($event->event->type) { - // https://stripe.com/docs/api/events/types 'payment_intent.payment_failed' => function (Event $event): void { // mark transaction as failed }, @@ -72,7 +72,7 @@ class StripeWebhookHandler }, }; - call_user_func_array($callback, [$evet->event]); + call_user_func_array($callback, [$event->event]); } } ``` diff --git a/composer.json b/composer.json index 2728a2d..05299cf 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^8.1 || ^8.2", + "php": "^8.2 || ^8.3", "conedevelopment/bazar": "@dev", "stripe/stripe-php": "^13.1" }, diff --git a/src/Listeners/HandleWebook.php b/src/Listeners/HandleWebook.php new file mode 100644 index 0000000..72d16d5 --- /dev/null +++ b/src/Listeners/HandleWebook.php @@ -0,0 +1,29 @@ +event->type) { + 'payment_intent.succeeded' => function (Event $stripeEvent): void { + $transaction = Transaction::query()->where('key', $stripeEvent->data['object']['id'])->firstOrFail(); + + $transaction->markAsCompleted(); + }, + default => function (): void { + // + }, + }; + + call_user_func_array($callback, [$event->event]); + } +} diff --git a/src/StripeDriver.php b/src/StripeDriver.php index 15138f8..89dd05c 100644 --- a/src/StripeDriver.php +++ b/src/StripeDriver.php @@ -16,6 +16,11 @@ class StripeDriver extends Driver { + /** + * The driver name. + */ + protected string $name = 'stripe'; + /** * The Stripe client instance. */ @@ -47,7 +52,7 @@ public static function redirectUrlAfterPayment(Closure $callback): void /** * Resolve the redirect URL after payment. */ - public function resolveRedirectUrlAfterPayment(Order $order, string $status, Transaction $transaction = null): string + public function resolveRedirectUrlAfterPayment(Order $order, string $staus, ?Transaction $transaction = null): string { if (! is_null(static::$redirectUrlAfterPayment)) { return call_user_func_array(static::$redirectUrlAfterPayment, [$order, $status, $transaction]); @@ -59,22 +64,6 @@ public function resolveRedirectUrlAfterPayment(Order $order, string $status, Tra }; } - /** - * {@inheritdoc} - */ - public function pay(Order $order, float $amount = null, array $attributes = []): Transaction - { - return $order->pay($amount, 'stripe', $attributes); - } - - /** - * {@inheritdoc} - */ - public function refund(Order $order, float $amount = null, array $attributes = []): Transaction - { - return $order->refund($amount, 'stripe', $attributes); - } - /** * {@inheritdoc} */ @@ -101,7 +90,7 @@ protected function createSession(Order $order): Session 'quantity' => $item->getQuantity(), ]; })->toArray(), - 'billing_address_collection' => 'required', + // 'billing_address_collection' => 'required', 'mode' => 'payment', 'success_url' => $this->redirectUrl('success'), 'cancel_url' => $this->redirectUrl('cancelled'), diff --git a/src/StripeServiceProvider.php b/src/StripeServiceProvider.php index 2bd2f1d..cfc096c 100644 --- a/src/StripeServiceProvider.php +++ b/src/StripeServiceProvider.php @@ -2,8 +2,10 @@ namespace Cone\Bazar\Stripe; +use Cone\Bazar\Stripe\Events\WebhookInvoked; use Cone\Bazar\Stripe\Http\Controllers\PaymentController; use Cone\Bazar\Stripe\Http\Controllers\WebhookController; +use Cone\Bazar\Stripe\Listeners\HandleWebhook; use Cone\Bazar\Support\Facades\Gateway; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\ServiceProvider; @@ -36,6 +38,15 @@ public function boot(): void ); } + $this->registerRoutes(); + $this->registerEvents(); + } + + /** + * Register the package routes. + */ + protected function registerRoutes(): void + { if (! $this->app->routesAreCached()) { $this->app['router'] ->get('/bazar/stripe/payment', PaymentController::class) @@ -46,4 +57,12 @@ public function boot(): void ->name('bazar.stripe.webhook'); } } + + /** + * Register events. + */ + protected function registerEvents(): void + { + $this->app['events']->listen(WebhookInvoked::class, HandleWebhook::class); + } }