Skip to content

Instantly share code, notes, and snippets.

@bsears90
Created October 10, 2022 17:56
Show Gist options
  • Save bsears90/c3f36bfe379dfd13cae749824c5b45ae to your computer and use it in GitHub Desktop.
Save bsears90/c3f36bfe379dfd13cae749824c5b45ae to your computer and use it in GitHub Desktop.
set default payment method on all customers
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY)
async function main(){
for await (const customer of stripe.customers.list({limit: 100})) {
const paymentMethods = (await stripe.customers.listPaymentMethods(
customer.id,
{type: 'card'}
)).data;
// if customer has PM and no default, set it
if(paymentMethods.length > 0 !customer.invoice_settings.default_payment_method){
console.log(`Setting ${paymentMethods[0].id} as default for ${customer.id}`)
await stripe.customers.update(customer.id, {
invoice_settings: {
default_payment_method: paymentMethods[0].id
}
})
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment