我正在使用 Laravel Cashier / Stripe Checkout(订阅)。
我有多个计划,我只想在一页 (/subscribe
) 中列出所有计划,并且每个计划都有一个链接(按钮)将用户发送到 Stripe结帐页面(订阅)。
当用户访问/subscribe
页面时,我会为每个计划启动 Stripe Checkout 会话:
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'));
});
这是错误的吗?我问是因为启动所有这些会话似乎会减慢 /subscribe
页面的加载速度,我想这是因为每当我们调用 checkout()
方法(启动会话)时,都会进行新的 API 调用+ 收银员将击中数据库。
如果同时启动多个会话是错误的,那么正确的方法是什么?我知道我可以为每个计划添加额外的页面(视图),然后每页只有一个会话启动,但我想尽可能避免(我只想要一个包含计划的页面列出,其中每个计划都有自己的“订阅”按钮,当用户点击该按钮时 - 他会直接转到 Stripe)。
答案 0 :(得分:1)
是的,这是错误且糟糕的。这将使您的页面非常缓慢,因为您对 Stripe API 的许多请求创建了许多结帐会话,其中大部分(如果不是全部)大部分时间都不会被使用。由于它是异步运行的,它将按顺序执行数据库写入和 Stripe API 调用,从而显着降低页面加载速度。
相反,您应该只在用户实际单击“订阅”按钮时创建结帐会话。单击按钮会触发对后端的请求以创建会话并执行重定向。这样每个用户只创建一个。