【问题标题】:Initiating multiple (many) Stripe Checkout sessions at the same time?同时启动多个(许多)Stripe Checkout 会话?
【发布时间】:2021-09-18 10:40:30
【问题描述】:

我正在使用Laravel Cashier / Stripe Checkout(订阅)。

我有多个计划,我只想有一个页面 (/subscribe),其中将列出所有计划,并且每个计划都会有一个链接(按钮)将用户发送到 Stripe Checkout 页面(订阅)。

当用户访问/subscribepage 时,我会为每个计划启动 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'));
});

这是错误的和坏的吗?我问是因为启动所有这些会话似乎会减慢 /subscribepage 的加载速度,我猜这是因为每当我们调用 checkout() 方法(启动会话)时 - 将进行新的 API 调用 + Cashier 会命中数据库。

如果同时启动多个会话是错误的,那么正确的方法是什么?我知道我可以为每个计划添加额外的页面(视图),然后每页只有一个会话启动,但我想尽可能避免(我只想要一个带有计划的页面列出,每个计划都有自己的“订阅”按钮,当用户点击该按钮时 - 他直接进入 Stripe)。

【问题讨论】:

    标签: laravel-cashier stripe-payments


    【解决方案1】:

    是的,这是错误和糟糕的。这将使您的页面变得非常慢,因为您对 Stripe API 的许多请求会创建许多 Checkout Sessions,即使不是全部,大多数情况下大部分时间都不会使用。由于这是异步运行的,它会按顺序执行 DB 写入和 Stripe API 调用,从而显着降低页面加载速度。

    相反,您应该只在用户实际单击“订阅”按钮时创建结帐会话。单击按钮将触发对您的后端的请求,以创建会话并进行重定向。这样每个用户只会创建一个。

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2021-09-23
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-28
      相关资源
      最近更新 更多