From d4a4de2a82030855dcde2a0f7589366cb41b8d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20L=C5=91rincz?= Date: Wed, 22 Nov 2023 18:45:16 +0100 Subject: [PATCH] Fix tests by overriding the default route registration for the Stripe webhook --- routes/webhooks.php | 16 ++++++++++++++++ src/LunarApiStripeAdapterServiceProvider.php | 2 ++ src/StripePaymentAdapter.php | 15 ++++++--------- tests/HandleStripeWebhookTest.php | 8 ++++---- 4 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 routes/webhooks.php diff --git a/routes/webhooks.php b/routes/webhooks.php new file mode 100644 index 0000000..a71f7c5 --- /dev/null +++ b/routes/webhooks.php @@ -0,0 +1,16 @@ + App::make(HandlePaymentWebhookController::class)( + Config::get('lunar-api.stripe.driver'), + $request + ) +) + ->name('payments.webhook'); diff --git a/src/LunarApiStripeAdapterServiceProvider.php b/src/LunarApiStripeAdapterServiceProvider.php index 8d8b415..684d9b5 100644 --- a/src/LunarApiStripeAdapterServiceProvider.php +++ b/src/LunarApiStripeAdapterServiceProvider.php @@ -27,6 +27,8 @@ public function boot(): void fn (Application $app) => $app->make(StripeManager::class), ); + $this->loadRoutesFrom(__DIR__.'/../routes/webhooks.php'); + StripePaymentAdapter::register(); if ($this->app->runningInConsole()) { diff --git a/src/StripePaymentAdapter.php b/src/StripePaymentAdapter.php index 82b21ba..b62f926 100644 --- a/src/StripePaymentAdapter.php +++ b/src/StripePaymentAdapter.php @@ -124,6 +124,7 @@ public function handleWebhook(Request $request): JsonResponse $order = App::make(FindOrderByIntent::class)($paymentIntent); } catch (Throwable $e) { return new JsonResponse([ + 'webhook_successful' => false, 'message' => "Order not found for payment intent {$paymentIntent->id}", ], 404); } @@ -143,7 +144,8 @@ public function handleWebhook(Request $request): JsonResponse } return new JsonResponse([ - 'message' => 'success', + 'webhook_successful' => true, + 'message' => 'Webook handled successfully', ]); } @@ -158,17 +160,12 @@ protected function constructEvent(Request $request): JsonResponse|Event $request->header('Stripe-Signature'), Config::get('services.stripe.webhooks.payment_intent') ); - } catch (UnexpectedValueException $e) { + } catch (UnexpectedValueException|SignatureVerificationException $e) { report($e); return new JsonResponse([ - 'error' => 'Invalid payload', - ], 400); - } catch (SignatureVerificationException $e) { - report($e); - - return new JsonResponse([ - 'error' => 'Invalid signature', + 'webhook_successful' => false, + 'message' => $e->getMessage(), ], 400); } } diff --git a/tests/HandleStripeWebhookTest.php b/tests/HandleStripeWebhookTest.php index b89df9f..252ce95 100644 --- a/tests/HandleStripeWebhookTest.php +++ b/tests/HandleStripeWebhookTest.php @@ -33,7 +33,7 @@ $this->app->bind(\Lunar\Stripe\Concerns\ConstructsWebhookEvent::class, function ($app) { return new class implements \Lunar\Stripe\Concerns\ConstructsWebhookEvent { - public function constructEvent(string $jsonPayload, string $signature, string $secret) + public function constructEvent(string $jsonPayload, string $signature, string $secret): \Stripe\Event { return \Stripe\Event::constructFrom([]); } @@ -72,7 +72,7 @@ public function constructEvent(string $jsonPayload, string $signature, string $s $response->assertSuccessful(); Event::assertDispatched(OrderPaid::class); -})->todo(); +}); it('can handle canceled event', function () { /** @var TestCase $this */ @@ -93,7 +93,7 @@ public function constructEvent(string $jsonPayload, string $signature, string $s $response->assertSuccessful(); Event::assertDispatched(OrderPaymentCanceled::class); -})->todo(); +}); it('can handle payment_failed event', function () { /** @var TestCase $this */ @@ -113,7 +113,7 @@ public function constructEvent(string $jsonPayload, string $signature, string $s $response->assertSuccessful(); Event::assertDispatched(OrderPaymentFailed::class); -})->todo(); +}); it('can handle any other event', function () { $events = Event::fake();