failed to submit the form
Answered
Boby posted this in #help-forum
BobyOP
I am not able to submit the form, can anyone please have a look at it?
TIA
https://github.com/Boby900/thumbnail-creator
TIA
https://github.com/Boby900/thumbnail-creator
Answered by Ray
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
export async function POST(req) {
const origin = new URL(req.url).origin;
try {
// Create Checkout Sessions from body params.
const session = await stripe.checkout.sessions.create({
line_items: [
{
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
price: "{{PRICE_ID}}",
quantity: 1,
},
],
mode: "payment",
success_url: `${origin}/?success=true`,
cancel_url: `${origin}/?canceled=true`,
});
return Response.redirect(session.url, { status: 303 });
} catch (err) {
console.log(err)
return new Response(err.message, { status: 500 });
}
}add a console.log here
72 Replies
@Boby I am not able to submit the form, can anyone please have a look at it?
TIA
https://github.com/Boby900/thumbnail-creator
try to make the request to
/api since you create the route handler in src/app/api/route.js@Ray try to make the request to `/api` since you create the route handler in `src/app/api/route.js`
BobyOP
Sorry, I am not getting it, what are you trying to say?
@Boby Sorry, I am not getting it, what are you trying to say?
const res = await fetch('./api/route.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
});your url is wrong
it should be
/api@Boby Okay, I have to remove that extra `.`?
fetch('/api')you need to provide the url to fetch not the path of the file
@Ray `fetch('/api')`
BobyOP
Okay, so I don't have to add that
route.js?I am new to Next.js
Sorry if it's very basic
@Boby Okay, so I don't have to add that `route.js`?
yeah just
/apiBobyOP
Okay, thank you for your help
I will let you know after trying this
I will let you know after trying this
@Ray
@Boby Click to see attachment
the code in app/api/route.js is incorrect, try this code
export async function POST(req: Request) {
const origin = new URL(req.url).origin;
try {
// Create Checkout Sessions from body params.
const session = await stripe.checkout.sessions.create({
line_items: [
{
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
price: "{{PRICE_ID}}",
quantity: 1,
},
],
mode: "payment",
success_url: `${origin}/?success=true`,
cancel_url: `${origin}/?canceled=true`,
});
return NextResponse.redirect(session.url, { status: 303 });
} catch (err) {
return new NextResponse(err.message, { status: 500 });
}
}@Boby Click to see attachment
import { NextResponse } from 'next/server'
BobyOP
I have imported that and added in my route.js file but still getting the same error
@Ray
@Boby I have imported that and added in my route.js file but still getting the same error
could you show the error on terminal again
@Boby Click to see attachment
could you show the code on api/route.js again
@Boby Click to see attachment
try to restart the dev server
@Ray try to restart the dev server
BobyOP
still getting the error
@Boby still getting the error
ok replace
NextResponse with Response@Ray ok replace `NextResponse` with `Response`
BobyOP
still persists
could you push the code to github
BobyOP
I've pushed it
@Ray
ok let me see
@Boby I've pushed it
you need to replace all NextResponse to Response
you just change the import one
@Ray you need to replace all NextResponse to Response
BobyOP
oh sry, I've changed all of them but still persists
I've pushed it to GitHub as well
@Boby oh sry, I've changed all of them but still persists
remove
import { Response } from "next/server";BobyOP
still persists
@Ray
show the error
BobyOP
@Ray
@Boby Click to see attachment
try to submit again
@Ray try to submit again
BobyOP
still getting the error
@Boby still getting the error
show the error again
@Ray https://github.com/Boby900/thumbnail-creator/blob/212f6cdcd2fd2dcc9ac56adefab16569719993b7/src/app/stripe/page.tsx#L5
remove the import here
BobyOP
Okay, I will get back to you in a few minutes
There is a power cut at my place
I am on PC
@Boby Click to see attachment
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
export async function POST(req) {
const origin = new URL(req.url).origin;
try {
// Create Checkout Sessions from body params.
const session = await stripe.checkout.sessions.create({
line_items: [
{
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
price: "{{PRICE_ID}}",
quantity: 1,
},
],
mode: "payment",
success_url: `${origin}/?success=true`,
cancel_url: `${origin}/?canceled=true`,
});
return Response.redirect(session.url, { status: 303 });
} catch (err) {
console.log(err)
return new Response(err.message, { status: 500 });
}
}add a console.log here
Answer
most likely you didn't set the
price@Ray ts
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
export async function POST(req) {
const origin = new URL(req.url).origin;
try {
// Create Checkout Sessions from body params.
const session = await stripe.checkout.sessions.create({
line_items: [
{
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
price: "{{PRICE_ID}}",
quantity: 1,
},
],
mode: "payment",
success_url: `${origin}/?success=true`,
cancel_url: `${origin}/?canceled=true`,
});
return Response.redirect(session.url, { status: 303 });
} catch (err) {
console.log(err)
return new Response(err.message, { status: 500 });
}
}
add a console.log here
BobyOP
yes you need to set the price_id
@Ray yes you need to set the price_id
BobyOP
okay, I had not set that I will get back to you if something unexpected will come
thanks a lot, for your help @Ray
BobyOP
I am finally able to submit the form, thank you @Ray