Reputation: 3668
I am using Laravel Cashier / Stripe Checkout (subscriptions).
I have multiple plans and I want to have only one page (/subscribe
) where all the plans will be listed and for each plan there will be a link (button) that will send user to Stripe Checkout page (to subscribe).
When user visits /subscribe
page, I initiate Stripe Checkout sessions for each plan:
Route::get('/subscribe', function () {
$checkout1 = Auth::user()->newSubscription('Supporter', 'price_1')->checkout([
'success_url' => route('dashboard'),
'cancel_url' => route('dashboard'),
'client_reference_id' => auth()->id(),
]);
$checkout2 = Auth::user()->newSubscription('Supporter', 'price_2')->checkout([
'success_url' => route('dashboard'),
'cancel_url' => route('dashboard'),
'client_reference_id' => auth()->id(),
]);
$checkout3 = Auth::user()->newSubscription('Supporter', 'price_3')->checkout([
'success_url' => route('dashboard'),
'cancel_url' => route('dashboard'),
'client_reference_id' => auth()->id(),
]);
$checkout4 = Auth::user()->newSubscription('Supporter', 'price_4')->checkout([
'success_url' => route('dashboard'),
'cancel_url' => route('dashboard'),
'client_reference_id' => auth()->id(),
]);
// ... AND MANY MORE ...
$checkout10 = Auth::user()->newSubscription('Supporter', 'price_10')->checkout([
'success_url' => route('dashboard'),
'cancel_url' => route('dashboard'),
'client_reference_id' => auth()->id(),
]);
return view('subscribe', compact('checkout1', 'checkout2', 'checkout3', 'checkout4' ... '$checkout10'));
});
Is this wrong and bad? I ask because initiating all of these sessions seems to slow down the loading of the /subscribe
page, and I guess it's because whenever we call checkout()
method (that initiates session) - a new API call will be made + Cashier will hit the DB.
If initiating multiple sessions at the same time is wrong, then what would be the correct way? I know that I can add extra page (view) for each plan and then I would have only one session initiation per page, but I want to avoid that if possible (I just want one single page with plans listed, where each plan has own "Subscribe" button and when user clicks on that button - he goes directly to Stripe).
Upvotes: 0
Views: 1060
Reputation: 5857
Yes, this is wrong and bad. This will make your page very slow as you have many requests to the Stripe API creating many Checkout Sessions, most if not all of which will be unused most of the time. Since this runs asynchronously it will do a DB write and Stripe API call sequentially, slowing down your page load significantly.
Instead you should only create the Checkout Session when your user actually clicks the "Subscribe" button. The button click would trigger a request to your backend to create the Session and do the redirect. That way only one is created per user.
Upvotes: 1