I can’t figure out what is causing 405 error when trying to fetch an endpoint in next.js
Here is my fetch function:
React.useEffect(() => {
// Create PaymentIntent as soon as the page loads
fetch("/api/create-payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items: [{ id: "xl-tshirt" }] }),
})
.then((res) => res.json())
.then((data) => setClientSecret(data.clientSecret));
}, []);
And here is the endpoint:
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const calculateOrderAmount = (items) => {
return 1400;
};
export default async function handler(req, res) {
const { items } = req.body;
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "eur",
automatic_payment_methods: {
enabled: true,
},
});
res.send({
clientSecret: paymentIntent.client_secret,
});
};