#rhy_code
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/1218170282368303205
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hi there, sorry there wasn't enough space to write the code
What's the issue?
I'm trying to link my page to the stripe hosted checkout page
I previously did it, then dabbled with elements, and now am deciding to revert back to the hosted checkout page
I still have the code for it which I will paste below
but for some erason I can't link it to the page
What do you mean by 'link it to page'?
link the buy button to the checkout page
I am trying via a form that redirects the user to
/api/payment
where the stripe checkout page is located
something like this
<form action="/api/payment" method="POST">
OK, any more context?
Here's the /api/payment page, it's too long so I split it into 2 parts
import Stripe from 'stripe';
import {NextResponse, NextRequest} from 'next/server';
export async function POST (request) {
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
let data = await request.json();
let priceId = data.priceId;
const session = await stripe.checkout.sessions.create({
mode: 'payment',
shipping_address_collection: {
allowed_countries: [
'AC', 'AD', 'AE', 'AF'
],
},
shipping_options: [
{
shipping_rate_data: {
type: 'fixed_amount',
fixed_amount: {
amount: 3000,
currency: 'usd',
},
display_name: 'Delivered ASAP',
delivery_estimate: {
minimum: {
unit: 'business_day',
value: 1,
},
maximum: {
unit: 'business_day',
value: 1,
},
},
},
},
],
line_items: [
{
price: 'price_1OIc0bB5oiepJ50uHShfyCok',
quantity: 1,
},
],
success_url: 'http://youtube.com',
cancel_url: 'http://youtube.com',
});
return NextResponse.json(session.url)
}
so basically I'm linking the form to this /api/payment page but it's saying the page isn't working
Well to be clear this is a Next.js implementation Q, nothing specific to Stripe
But seems like you're mixing Server Action concepts with Route Handlers?
so which should I use?
Up to you! But there is a Server Action example here: https://github.com/vercel/next.js/blob/canary/examples/with-stripe-typescript/app/components/CheckoutForm.tsx
ah is this an example for a stripe hosted checkout?
A Server Action is just a regular JS function that you invoke. You're passing an endpoint for a Route Handler, which won't work. You'd need to call that fetch or something: https://nextjs.org/docs/app/building-your-application/routing/route-handlers
can you expand where I might be using server action?
You're conflating the two concepts, so it won't work
since we're supposed to be using only route handlers
The <form action=""> in your code โ the action attr there expects a Server Action
Our Next.js quickstart is more akin to what you want: https://docs.stripe.com/payments/quickstart?client=next
Yeah but how that works is depedent on which flavour of Next.js you're using - App Router or Pages
I think im using app router
Our quickstart uses pages still, which is now the old non-default Next.js concept
and the example is too tho
Which example?
this one
No it isn't โ you can see the pages/ directory in the paths
You need to use the example I linked earlier if you're in the App Router
this one?
If you want to use a Route Handler in App Router then you'd need an onClick/onSubmit handler and then call the endpoint via fetch
gimme a sec
Will give that a try in a bit, I assume this thread will close soon?
Can I refer to this later when I reopen it
Sure