Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDefaultPaymentMethod() sometimes throws an error #1729

Closed
prattcmp opened this issue Jan 7, 2025 · 1 comment · Fixed by #1732
Closed

getDefaultPaymentMethod() sometimes throws an error #1729

prattcmp opened this issue Jan 7, 2025 · 1 comment · Fixed by #1732
Labels

Comments

@prattcmp
Copy link

prattcmp commented Jan 7, 2025

Cashier Stripe Version

15.6.0

Laravel Version

11.5.0

PHP Version

8.1.0

Database Driver & Version

No response

Description

When we call the defaultPaymentMethod() method for certain users, we get a Attempt to read property "default_payment_method" on null exception triggered in /vendor/laravel/cashier/src/Concerns/ManagesPaymentMethods.php on this if statement even though the customer has a valid subscription and Stripe ID:

if ($customer->invoice_settings->default_payment_method) {
    return new PaymentMethod($this, $customer->invoice_settings->default_payment_method);
}

We believe this may be happening because the customer has a 100% off coupon and therefore wasn't required to enter a payment method when creating their subscription using Stripe Checkout. We create all new subscriptions with Stripe Checkout using this method:

return $user
            ->newSubscription(config('cashier.subscriptions.monthly'), $validated['interval'] == 'annual' ? $plan['yearly_price_id'] : $plan['monthly_price_id'])
            ->quantity($validated['quantity'])
            ->checkout([
                'success_url' => route('billing.show').'?'.$new_user_query_string.'session_id={CHECKOUT_SESSION_ID}',
                'cancel_url' => route('billing.show'),
                'allow_promotion_codes' => true,
                'tax_id_collection' => [
                    'enabled' => true,
                ],
            ]);

Steps To Reproduce

  1. Create a subscription using Stripe Checkout and use a 100% off coupon code.
  2. Call $user->defaultPaymentMethod().
@driesvints
Copy link
Member

Thanks for your report. This should actually never happen because invoice_settings is always an associative array according to the Stripe API docs: https://docs.stripe.com/api/customers/object#customer_object-invoice_settings. It should never be null.

Regardless, we should protect against it so it doesn't fails over in Cashier.

But if you can, could you submit this as a support question to Stripe so they can look into it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants