#inderjit-janjua_api

1 messages ¡ Page 1 of 1 (latest)

half abyssBOT
#

👋 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/1287716271055568897

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

brave verge
#

As a test I did this

#

It works

#
import { NextResponse, NextRequest } from 'next/server';
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;

if (!stripeSecretKey) {
    throw new Error('Stripe secret key is not defined in environment variables.');
}

const stripe = require('stripe')(stripeSecretKey);

export async function POST(req: NextRequest) {
    try {
        const { paymentIntentId, accountId, clientSecret, return_url } = await req.json();

        if (!paymentIntentId) {
            return NextResponse.json({ error: 'Missing paymentIntentId' }, { status: 400 });
        }

        // Confirm the payment intent with optional Stripe-Account header for Connect accounts
        const paymentIntent = await stripe.paymentIntents.confirm(paymentIntentId, {
            clientSecret,
            return_url
        }, {
            stripeAccount: accountId || undefined, // Only include if accountId exists
        });

        return NextResponse.json({ paymentIntent });
    } catch (error) {
        console.error('Payment intent confirmation failed:', error);
        return NextResponse.json({ error: 'Failed to confirm payment intent' }, { status: 500 });
    }
}
#

But i'm sure my code uses a different approach

winged hare
#

What should confirm payment intent look like when request body holds Client Secret
Could you provide more details please? I'm not sure I understand you here.

#

Ah when chekcing your request, I see

brave verge
#

To start from scratch

winged hare
#

You shouldn't pass client_secret when confirming the PaymentIntent

#

You need to call the confirmPayment using Stripe Js

brave verge
#

How do I call the confirm payment intent call when I have the paymentIntentId and the connectedAccountId

winged hare
#

What guide are you following ?

brave verge
#

Okay so initially I was doing it this way

const {error} = await stripe.confirmPayment(
  {
    elements,
    confirmParams: {
      // Return URL where the customer should be redirected after the PaymentIntent is confirmed.
      return_url: 'https://example.com',
    },
  }
);
if (error) {
  // Inform the customer that there was an error.
}
#

But I needed to edit this code to connect it to a connected account instead of the platform account

winged hare
#

You need to pass the Account Id when you initialize the Stripe js client

brave verge
#

What does elements hold

#

as a value

#

I want to make thse work on backend

#

Okay so i'm being a dumbass

#

But correct me if i'm wrong

#

confirmPayment using Stripe Js

winged hare
brave verge
#

Does it have to be called in the frontend?

winged hare
#

If you don't have a PaymentMehtod and you are collecting one from your customer, so you need a frontend call using Stripejs

brave verge
#

Okay understood

winged hare
brave verge
#

How does a payment method get collected with stipe elements

#

Okay

winged hare
#

First you collect the PaymentMethod using your frontend integration

#

and then you can reuse the saved PaymentMethod for future purchases using your backend only

brave verge
#

Thank you

#

I admire how quick you guys reply

#

And how clearly you understand my problem

#

Without me articulating it clearly enough

#

Okay i'm gonna simplify it

#

I'll use the front end

#
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        setLoading(true);

        if (!stripe || !elements || !clientSecret) {
            setLoading(false);
            return;
        }

        const { error: submitError } = await elements.submit();

        if (submitError) {
            setErrorMessage(submitError.message);
            setLoading(false);
            return;
        }

        const { error } = await stripe.confirmPayment({
            elements,
            clientSecret,
            confirmParams: {
                return_url: `${getBaseUrl()}/payment/payment-success?amount=${amount}`,
            },
        });

        if (error) {
            setErrorMessage(error.message);
        }

        setLoading(false);
    };
#

This is what I had initially

#

What guide shows me about adding a connected account id to this frontend call

#

Sorry found it

#

This one here

winged hare
#

Yes exactly that's it!

brave verge
#

Example

import {loadStripe} from '@stripe/stripe-js';

// Make sure to call `loadStripe` outside of a component's render to avoid
// recreating the `Stripe` object on every render.
const stripePromise = loadStripe('pk_test_TYooMQauvdEDq54NiTphI7jx', {
  stripeAccount: {{CONNECTED_ACCOUNT_ID}},
});
```
#

My Code

const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY || '',
  stripeAccount: "account_xxx";
);
winged hare
#

Correct

brave verge
#

The framework doesn't recognise stripeAccount

#

Wait

winged hare
#

It's probably TS warning

brave verge
#

I added an extra ;

winged hare
#

you can simply ignore it using @ts-ignore

brave verge
#

Okay thanks

#

Its still having an issue

#
const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY || '', 
  // @ts-ignore
  stripeAccount: accountId
);
#

Fixed it

#

No it doesn't like it

#

I'll look into this tomorrow

#

Thanks for your help

half abyssBOT