AdrianR
AdrianR

Reputation: 13

Laravel Cashier (Stripe) subscription increase quantity of optional products

I'm creating a Stripe checkout session for a Subscription that includes multiple optional products. Some of these optional products require the user to specify the quantity, but I need help figuring out how to increase the quantity in Stripe dynamically.

Here's what I'm attempting:

// 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;
}

But instead of returning Stripe's checkout, it gives me an error.

Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Laravel\Cashier\SubscriptionBuilder given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Response.php on line 73

If I use this instead, it returns the Stripe checkout, but I don't know how to specify the quantity for each extra:

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'),
        ]);

Upvotes: 0

Views: 198

Answers (1)

AdrianR
AdrianR

Reputation: 13

Ok, I found it. You can't

return $checkout;

you have to:

return $checkout->checkout([
        'success_url' => route('dashboard.subscribed', ['user' => $request->user()]),
        'cancel_url' => route('dashboard'),
    ]);

Upvotes: 0

Related Questions