#andy_api

1 messages ยท Page 1 of 1 (latest)

austere oracleBOT
#

๐Ÿ‘‹ 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.

neon roost
#

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);
}
})();

quartz plank
#

Hey there

#

So looks like you are hitting the:

    if (paymentMethod.usage !== 'off_session') {
      throw new Error(โŒ Not reusable (usage: ${paymentMethod.usage}));
    }
#

Can you share that PaymentMethod ID in text?

#

Hmm also usage is not a property on the PaymentMethod object