Next.js Discord

Discord Forum

Next.js 14 Stripe Entegration

Answered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
I created a vehicle sales site with Next.js 14 and I want to integrate it into the ribbon so users can purchase vehicles. How can I do that? or is there an example?

45 Replies

Answer
West African LionOP
from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. what is this error?
West African LionOP
ı want to redirect stripe checkout
West African LionOP
import { NextResponse } from "next/server";
import { stripe } from "@/lib/stripe";

export async function POST() {
try {
const line_items = [
{
quantity: 1,
price_data: {
currency: "USD",
product_data: {
name: "example,
description: "example",
},
unit_amount: Math.round(200 * 100),
},
},
];

const session = await stripe.checkout.sessions.create({
line_items,
mode: "payment",
mode: "payment",
success_url: http://localhost:3000,
cancel_url: http://localhost:3000,
});

return NextResponse.redirect(new URL(session.url));
} catch (error) {
console.log("[CHECKOUT_ERROR]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
is true??
West African LionOP
I think because I'm redirecting to the stripe here.
const handlePayment = async () => {
const { data } = await axios.post("/api/payment", {
name: ${detailinfo.brand} ${detailinfo.model},
description: detailinfo.description,
});
console.log(data);
}; or here????
West African LionOP
who?
what do you mean who?
West African LionOP
sorry
ı will say how ?
import { useRouter } from 'next/navigation';
...

const router = userRouter()

const handlePayment = async () => {
    const { data } = await axios.post("/api/payment", {
      name: ${detailinfo.brand}  ${detailinfo.model},
      description: detailinfo.description,
    }); 
    router.redirect(data.url)
  };  
West African LionOP
okay ı understand
change
return NextResponse.redirect(new URL(session.url));

to
return NextResponse.json({ url: session.url });
West African LionOP
js code  console.log("okay" 
three `
West African LionOP
js code console.log(".") 
lol
West African LionOP
console.log("router.push(data.url)")
West African LionOP
How do I access the data I sent via post?
in my api route
what data?
export async function POST(req) {
  const data = await req.json()
}
West African LionOP
i am using translater
I forgot to use await thanks
West African LionOP
   const session = await stripe.checkout.sessions.create({
      line_items,
      mode: "payment",
      success_url: `http://localhost:3000`,
      cancel_url: `http://localhost:3000`,
    }); 
How can I access on the url in real website?
const session = await stripe.checkout.sessions.create({
      line_items,
      mode: "payment",
      success_url: `${req.header.get('origin')}/success-url`,
      cancel_url: `${req.header.get('origin')}`,
    }); 
West African LionOP
you are so better
West African LionOP
export async function GET(req) {
  try {
    const session = await stripe.checkout.sessions.retrieve(
      req.query.session_id
    );
how to access session_id in api route
@West African Lion js export async function GET(req) { try { const session = await stripe.checkout.sessions.retrieve( req.query.session_id ); how to access session_id in api route
export async function GET(req) {
  try {
    const sessionId = req.nextUrl.searchParams.get("session_id")
    if (!sessionId) return NextResponse.json({ "error": "missing session id" })
    const session = await stripe.checkout.sessions.retrieve(sessionId);
West African LionOP
thank you
@West African Lion js code console.log("okay"
Hoopoe
```ts
code
```
select this text and paste it
@Ray yes this one from vercel https://github.com/vercel/next.js/tree/canary/examples/with-stripe-typescript
Dogue Brasileiro
any reason why its exporting as a const instead of default?
@Dogue Brasileiro Click to see attachment
personal preference I think
Dogue Brasileiro
Ok