Skip to content

Commit 390e387

Browse files
committed
redirect to /mobile upon invalid stripe checkout session in url
1 parent 2def1cd commit 390e387

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

app/Livewire/OrderSuccess.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Livewire\Attributes\Layout;
99
use Livewire\Attributes\Title;
1010
use Livewire\Component;
11+
use Stripe\Exception\InvalidRequestException;
1112

1213
#[Layout('components.layout')]
1314
#[Title('Thank You for Your Purchase')]
@@ -41,8 +42,11 @@ private function loadEmail(): ?string
4142
return $email;
4243
}
4344

44-
$stripe = Cashier::stripe();
45-
$checkoutSession = $stripe->checkout->sessions->retrieve($this->checkoutSessionId);
45+
try {
46+
$checkoutSession = Cashier::stripe()->checkout->sessions->retrieve($this->checkoutSessionId);
47+
} catch (InvalidRequestException $e) {
48+
return $this->redirect('/mobile');
49+
}
4650

4751
if (! ($email = $checkoutSession?->customer_details?->email)) {
4852
return null;
@@ -86,8 +90,11 @@ private function loadSubscription(): ?Subscription
8690
return Subscription::tryFrom($subscription);
8791
}
8892

89-
$stripe = Cashier::stripe();
90-
$priceId = $stripe->checkout->sessions->allLineItems($this->checkoutSessionId)->first()?->price->id;
93+
try {
94+
$priceId = Cashier::stripe()->checkout->sessions->allLineItems($this->checkoutSessionId)->first()?->price->id;
95+
} catch (InvalidRequestException $e) {
96+
return $this->redirect('/mobile');
97+
}
9198

9299
if (! $priceId) {
93100
return null;

tests/Feature/Livewire/OrderSuccessTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPUnit\Framework\Attributes\Test;
1313
use Stripe\Checkout\Session as CheckoutSession;
1414
use Stripe\Collection;
15+
use Stripe\Exception\InvalidRequestException;
1516
use Stripe\LineItem;
1617
use Stripe\StripeClient;
1718
use Tests\TestCase;
@@ -111,6 +112,34 @@ public function it_polls_for_updates_from_database()
111112
->assertDontSee('License registration in progress');
112113
}
113114

115+
#[Test]
116+
public function it_redirects_to_mobile_route_when_checkout_session_is_not_found()
117+
{
118+
$mockStripeClient = $this->createMock(StripeClient::class);
119+
120+
$mockStripeClient->checkout = new class {};
121+
122+
$mockStripeClient->checkout->sessions = new class
123+
{
124+
public function retrieve()
125+
{
126+
throw new InvalidRequestException('No such checkout.session');
127+
}
128+
129+
public function allLineItems()
130+
{
131+
throw new InvalidRequestException('No such checkout.session');
132+
}
133+
};
134+
135+
$this->app->bind(StripeClient::class, function ($app, $parameters) use ($mockStripeClient) {
136+
return $mockStripeClient;
137+
});
138+
139+
Livewire::test(OrderSuccess::class, ['checkoutSessionId' => 'not_a_real_checkout_session'])
140+
->assertRedirect('/mobile');
141+
}
142+
114143
private function mockStripeClient(): void
115144
{
116145
$mockCheckoutSession = CheckoutSession::constructFrom([

0 commit comments

Comments
 (0)