How to update customer default paymentMethod in Stripe?

Viewed 16326

Let's assume that we have created a payment method - pm_xxx. When we create a customer we can attache this method as the default payment method to the customer via java code:

CustomerCreateParams.Builder customerCreateParamsBuilder = CustomerCreateParams.builder()
                    .setEmail(email)
                    .setPaymentMethod('pm_xxx');       
Customer.create(customerCreateParamsBuilder.build());

Unfortunately corresponding api for update customer is not available. So the question is what is preferred way to update customer default payment method in Stripe?

EDIT: Based on @karllekko answer, because My use case is recurring payments so I combine 2 actions: attach customer to payment method:

paymentMethod.attach(PaymentMethodAttachParams.builder().setCustomer(customer.getId()).build());            

and make payment method default for customer invoice:

customer.update(CustomerUpdateParams.builder().setInvoiceSettings(CustomerUpdateParams.InvoiceSettings.builder().setDefaultPaymentMethod(token).build()).build());
2 Answers

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

Related