#jasfo_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/1285255445975535696
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hello
Hello !
If you want to re-use a PaymentIntent then you would track that across your user's session using something like cookies to do that
I'm trying to retrieve the payment intent using email. Here is what I did :
const createPaymentIntent = async (req, res) => {
const { promoCode, email, first_name, last_name, civility, creation_date, street, complement, postal_code, city, country } = req.body;
let amount = 12600;
try {
if (promoCode) {
try {
const coupon = await stripe.coupons.retrieve(promoCode);
if (coupon.amount_off) {
amount -= coupon.amount_off;
} else if (coupon.percent_off) {
amount -= (amount * (coupon.percent_off / 100));
}
} catch (error) {
console.log("Invalid promo code");
}
}
let paymentIntent = await getExistingPaymentIntent(email);
if (!paymentIntent) {
paymentIntent = await stripe.paymentIntents.create({
amount,
currency: 'eur',
payment_method_types: ['card'],
metadata: {
integration_check: 'accept_a_payment',
email,
first_name,
last_name,
civility,
creation_date,
street,
complement,
postal_code,
city,
country
},
});
} else {
await stripe.paymentIntents.update(paymentIntent.id, {
amount,
metadata: {
integration_check: 'accept_a_payment',
email,
first_name,
last_name,
civility,
creation_date,
street,
complement,
postal_code,
city,
country
},
});
}
res.send({
clientSecret: paymentIntent.client_secret,
});
} catch (error) {
const paymentIntents = await stripe.paymentIntents.list({
limit: 1,
customer: email,
status: 'requires_payment_method',
});
res.status(500).send({ error: error.message });
}
};
async function getExistingPaymentIntent(email) {
const paymentIntents = await stripe.paymentIntents.search({
query: `status:'requires_source' AND metadata['email']:'${email}'`,
limit: 1,
});
return paymentIntents.data.length > 0 ? paymentIntents.data[0] : null;
}
Just sharing a block of code doesn't help. However, you should not use the Search API for something like this -- it isn't designed for read-after-write flows as we explain at https://docs.stripe.com/api/payment_intents/search
So really you shouldn't need to be retrieving the PaymentIntent ID at all here
You should store it via the customer's session
Then you can reuse it
Otherwise you create a new one
Oh, okay, I understand better ^^"