Created
October 10, 2022 17:56
-
-
Save bsears90/c3f36bfe379dfd13cae749824c5b45ae to your computer and use it in GitHub Desktop.
set default payment method on all customers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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