But I think I am using this correctly, I am sharing the whole file under here, so you understand how the req and res are being setup.
import type { NextApiRequest, NextApiResponse } from 'next';
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY ?? '', {
apiVersion: '2022-11-15',
});
console.log('outside the const handler!');
export async function POST(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') {
return res.status(405).end();
}
try {
const { price, quantity, items } = req.body;
const lineItems = items
? items.map((item: any) => ({
price: item.id,
quantity: item.quantity,
}))
: [
{
price,
quantity,
},
];
console.log('logging the req.headers.origin:');
console.log(`${req.headers.origin}`);
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price: 'price_1N2UwFHjh6rwfnNSgLo5XwJs',
quantity: 1,
},
],
// line_items: lineItems,
mode: 'payment',
success_url: `http://localhost:3000/result?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `http://localhost:3000/cancel`,
automatic_tax: { enabled: true },
});
if (items) {
res.status(200).json({
url: session.url,
});
} else {
res.redirect(301, session.url ?? '');
}
} catch (e: any) {
console.log(e);
res.status(e.statusCode || 500).json({
message: e.message,
});
}
}
@sacred cape