File tree 2 files changed +40
-4
lines changed
2 files changed +40
-4
lines changed Original file line number Diff line number Diff line change 8
8
use Livewire \Attributes \Layout ;
9
9
use Livewire \Attributes \Title ;
10
10
use Livewire \Component ;
11
+ use Stripe \Exception \InvalidRequestException ;
11
12
12
13
#[Layout('components.layout ' )]
13
14
#[Title('Thank You for Your Purchase ' )]
@@ -41,8 +42,11 @@ private function loadEmail(): ?string
41
42
return $ email ;
42
43
}
43
44
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
+ }
46
50
47
51
if (! ($ email = $ checkoutSession ?->customer_details?->email)) {
48
52
return null ;
@@ -86,8 +90,11 @@ private function loadSubscription(): ?Subscription
86
90
return Subscription::tryFrom ($ subscription );
87
91
}
88
92
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
+ }
91
98
92
99
if (! $ priceId ) {
93
100
return null ;
Original file line number Diff line number Diff line change 12
12
use PHPUnit \Framework \Attributes \Test ;
13
13
use Stripe \Checkout \Session as CheckoutSession ;
14
14
use Stripe \Collection ;
15
+ use Stripe \Exception \InvalidRequestException ;
15
16
use Stripe \LineItem ;
16
17
use Stripe \StripeClient ;
17
18
use Tests \TestCase ;
@@ -111,6 +112,34 @@ public function it_polls_for_updates_from_database()
111
112
->assertDontSee ('License registration in progress ' );
112
113
}
113
114
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
+
114
143
private function mockStripeClient (): void
115
144
{
116
145
$ mockCheckoutSession = CheckoutSession::constructFrom ([
You can’t perform that action at this time.
0 commit comments