File tree Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Expand file tree Collapse file tree 2 files changed +40
-4
lines changed Original file line number Diff line number Diff line change 88use Livewire \Attributes \Layout ;
99use Livewire \Attributes \Title ;
1010use 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 ;
Original file line number Diff line number Diff line change 1212use PHPUnit \Framework \Attributes \Test ;
1313use Stripe \Checkout \Session as CheckoutSession ;
1414use Stripe \Collection ;
15+ use Stripe \Exception \InvalidRequestException ;
1516use Stripe \LineItem ;
1617use Stripe \StripeClient ;
1718use 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 ([
You can’t perform that action at this time.
0 commit comments