Reputation: 147
I'm using Stripe checkout to collect card details and customer address as shown below
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
mode: 'setup',
customer: req.subscription.customerId,
client_reference_id: email,
metadata: {'plan': 'basic'},
billing_address_collection: 'required',
success_url: req.protocol + '://' + req.get('host') + '/payment/middle?'+queryParams,
cancel_url: req.protocol + '://' + req.get('host') + '/payment/failure',
});
How can I make the card as the default payment method during checkout flow?
Upvotes: 2
Views: 2222
Reputation: 2219
The Checkout Session automatically attaches the new payment method to the customer. If you want to then set this payment method as the default one for subscription payments, you need to manually update the invoice_settings.default_payment_method
property of the customer object.
I recommend listening to the checkout.session.completed
webhook and do this:
// The checkout session object sent by the webhook
const session = event.data.object;
// Retrieve the associated setup intent (we need it to get the payment_method just after)
const setupIntent = await stripe.setupIntents.retrieve(
session.setup_intent
);
// Update the default payment method for the customer
const customer = await stripe.customers.update(session.customer, {
invoice_settings: {
default_payment_method: setupIntent.payment_method,
},
});
You can learn more about this by reading this page in the Stripe documentation.
Upvotes: 6