Laravel Cashier (Stripe) 订阅增加可选产品数量

问题描述 投票:0回答:1

我正在为包含多个可选产品的订阅创建 Stripe 结账会话。其中一些可选产品要求用户指定数量,但我需要帮助弄清楚如何动态增加 Stripe 中的数量。

这就是我正在尝试的:

// The $order I'm passing contains a collection with the selected extras
public function createStripeCheckoutSession(Order $order)
{
    // I used this to get the "stripe_price" of the main subscription plan
    $subscription_plan = $order->plans->where('plan_type', 2)->first();
    $checkout = $request->user()->company()->newSubscription('default', $subscription_plan->stripe_price);
                
    // Here I'm getting the remaining selected products without the $subscription_plan
    $extras = $order->plans->filter(function($value) {
        return $value->id > 2;
    });
        
    // Here I tried to loop through every "extra" and call the quantity() method of Cashier
    foreach($extras as $extra){
        $checkout->quantity($extra->pivot->quantity, $extra->stripe_price);
    }
        
    $checkout->skipTrial()
    ->allowPromotionCodes()
    ->checkout(
    [
        'success_url' => route('dashboard.subscribed', ['user' => $request->user()]),
        'cancel_url' => route('dashboard')
    ]);

    return $checkout;
}

但是它没有返回 Stripe 的结帐,而是给了我一个错误。

Symfony\Component\HttpFoundation\Response::setContent():参数 #1 ($content) 必须是 ?string 类型, Laravel\Cashier\SubscriptionBuilder 给出,调用 /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Response.php 73号线

如果我使用这个,它会返回 Stripe 结帐,但我不知道如何指定每个额外的数量:

return $checkout = $request->user()->company()
        ->newSubscription('default', $subscription_plan->stripe_price)
        ->skipTrial()
        ->allowPromotionCodes()
        ->checkout([
            'success_url' => route('dashboard.subscribed', ['user' => $request->user()]),
            'cancel_url' => route('dashboard'),
        ]);
laravel stripe-payments laravel-cashier
1个回答
0
投票

好的,我找到了。你不能

return $checkout;

你必须:

return $checkout->checkout([
        'success_url' => route('dashboard.subscribed', ['user' => $request->user()]),
        'cancel_url' => route('dashboard'),
    ]);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.