#jasfo_api

1 messages ¡ Page 1 of 1 (latest)

mellow bobcatBOT
#

👋 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.

warm lantern
#

Hello

umbral pawn
#

Hello !

warm lantern
#

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

umbral pawn
#

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;
}
warm lantern
#

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

umbral pawn
#

Oh, okay, I understand better ^^"