laravel / cashier-stripe

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.
https://laravel.com/docs/billing
MIT License
2.37k stars 671 forks source link

Checkout overwrites default `subscription_data` with `$sessionOptions` #1578

Closed edalzell closed 11 months ago

edalzell commented 11 months ago

Cashier Stripe Version

14.12.7

Laravel Version

10.25.2

PHP Version

8.2.11

Database Driver & Version

No response

Description

I was trying to create a Checkout Session with a trial period and not collect payment information.

According to the Stripe docs, you need to set the payload to something like this:

                'payment_method_collection' => 'if_required',
                'subscription_data' => [
                    'trial_period_days' => 14,
                    'trial_settings' => [
                        'end_behavior' => ['missing_payment_method' => 'cancel'],
                    ],
                ],

However, if you pass that in to the checkout method, the array_merge gets rid of the "default" payload, including:

                    'metadata' => ['name' => 'default'],

I believe what's needed is array_merge_recursive instead of array_merge here

Steps To Reproduce

Try to create a checkout session using:

            ->checkout([
                'payment_method_collection' => 'if_required',
                'subscription_data' => [
                    'trial_period_days' => 14,
                    'trial_settings' => [
                        'end_behavior' => ['missing_payment_method' => 'cancel'],
                    ],
                ],
            ]);

And you'll see the metadata is missing from the Stripe request.

edalzell commented 11 months ago

The work-around is to add the default fields:

            ->checkout([
                'payment_method_collection' => 'if_required',
                'subscription_data' => [
                    'metadata' => ['name' => 'default'],
                    'trial_period_days' => 14,
                    'trial_settings' => [
                        'end_behavior' => ['missing_payment_method' => 'cancel'],
                    ],
                ],
            ])->redirect();
driesvints commented 11 months ago

Hey @edalzell. Thanks for this. I believe you're right. I've sent in a PR for this: https://github.com/laravel/cashier-stripe/pull/1579