#inderjit-janjua_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/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.
- inderjit-janjua_api, 7 minutes ago, 43 messages
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
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
To start from scratch
You shouldn't pass client_secret when confirming the PaymentIntent
You need to call the confirmPayment using Stripe Js
How do I call the confirm payment intent call when I have the paymentIntentId and the connectedAccountId
How are you collecting the PaymentMethod ?
What guide are you following ?
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
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
if you want to do these in your backend, you'll need to have a collected PaymentMethod previously
Does it have to be called in the frontend?
If you don't have a PaymentMehtod and you are collecting one from your customer, so you need a frontend call using Stripejs
Okay understood
I invite you to follow this guide first:
https://docs.stripe.com/payments/save-during-payment
First you collect the PaymentMethod using your frontend integration
and then you can reuse the saved PaymentMethod for future purchases using your backend only
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
Yes exactly that's it!
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";
);
Correct
It's probably TS warning
I added an extra ;
you can simply ignore it using @ts-ignore
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