#andy_api
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1384638618118914162
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
import 'dotenv/config.js';
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
const PAYMENT_METHOD_ID = 'pm_1RRmqfHUXQT2RuDUoIodio4B';
const OLD_CUSTOMER_ID = 'cus_SUdqSppsyQ5dC4';
const NEW_CUSTOMER_ID = 'cus_SUfePB3rCGxpul';
(async () => {
try {
const paymentMethod = await stripe.paymentMethods.retrieve(PAYMENT_METHOD_ID);
if (!paymentMethod) throw new Error('โ Payment method not found');
console.log(`๐ฆ Payment method found: ${paymentMethod.id} (${paymentMethod.card.brand})`);
if (paymentMethod.usage !== 'off_session') {
throw new Error(`โ Not reusable (usage: ${paymentMethod.usage})`);
}
const paymentIntents = await stripe.paymentIntents.list({
customer: OLD_CUSTOMER_ID,
limit: 10,
});
const usedInPI = paymentIntents.data.find(pi => pi.payment_method === PAYMENT_METHOD_ID);
if (!usedInPI) {
console.warn('โ ๏ธ No PaymentIntent found using this method. Proceder con cuidado.');
} else {
console.log(`๐งพ Found in PaymentIntent: ${usedInPI.id}`);
if (usedInPI.setup_future_usage !== 'off_session') {
throw new Error(`โ PaymentIntent did not have setup_future_usage='off_session' (was: ${usedInPI.setup_future_usage || 'null'})`);
}
}
const setupIntent = await stripe.setupIntents.create({
customer: NEW_CUSTOMER_ID,
payment_method: PAYMENT_METHOD_ID,
confirm: true,
usage: 'off_session',
});
console.log(`โ
SetupIntent confirmed and method attached: ${setupIntent.id}`);
await stripe.customers.update(NEW_CUSTOMER_ID, {
invoice_settings: {
default_payment_method: PAYMENT_METHOD_ID,
},
});
console.log(`โ๏ธ Set as default payment method for new customer`);
} catch (err) {
console.error('โ Error:', err.message);
}
})();