Skip to content

Commit

Permalink
Fix tests by overriding the default route registration for the Stripe…
Browse files Browse the repository at this point in the history
… webhook
  • Loading branch information
lorinczdev committed Nov 22, 2023
1 parent 1db27b0 commit d4a4de2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
16 changes: 16 additions & 0 deletions routes/webhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Dystcz\LunarApi\Domain\Payments\Http\Controllers\HandlePaymentWebhookController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;

Route::post(
'/stripe/webhook',
fn (Request $request) => App::make(HandlePaymentWebhookController::class)(
Config::get('lunar-api.stripe.driver'),
$request
)
)
->name('payments.webhook');
2 changes: 2 additions & 0 deletions src/LunarApiStripeAdapterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
15 changes: 6 additions & 9 deletions src/StripePaymentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -143,7 +144,8 @@ public function handleWebhook(Request $request): JsonResponse
}

return new JsonResponse([
'message' => 'success',
'webhook_successful' => true,
'message' => 'Webook handled successfully',
]);
}

Expand All @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/HandleStripeWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand All @@ -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();
Expand Down

0 comments on commit d4a4de2

Please sign in to comment.