How to update customer default paymentMethod in Stripe?
An important thing to note is that on the PaymentMethods API, there is no concept of a default PaymentMethod at the customer level. (0) If making a one-off payment against a saved customer, the ID of the customer and of the chosen PaymentMethod must be provided when making the API call to create the PaymentIntent for that payment. (1)
Setting payment_method
when creating a customer(like in your example Java code) is a convenience function that attaches the PaymentMethod to the customer at the time they're created. There isn't an equivalent for updating a customer — instead you'd simply save the PaymentMethod to them, either as part of a payment(save_payment_method
when using a PaymentIntent), or by using the attach endpoint of /v1/payment_methods: (2)
PaymentMethod pm = PaymentMethod.retrieve("pm_1ErMlwJoUivz182DfXf7OlWQ");
pm.attach(PaymentMethodAttachParams.builder().setCustomer(cus.getId()).build());
(0) - There's an exception for recurring payments using Stripe Billing — here, you would set either the invoice settings of the Customer, or the default_payment_method of a particular subscription.
(1) - https://stripe.com/docs/payments/payment-intents/off-session#create-payment-intent
(2) - https://stripe.com/docs/payments/payment-methods/saving
The PaymentMethod.attach doc page (1) says this:
To use this PaymentMethod as the default for invoice or subscription payments, set invoice_settings.default_payment_method, on the Customer to the PaymentMethod’s ID.
So you can use the Customer.update API route (2) and fill the invoice_settings
attribute.
(1) - https://stripe.com/docs/api/payment_methods/attach
(2) - https://stripe.com/docs/api/customers/update#update_customer-invoice_settings