`app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', ['http://localhost:4242', 'https://ummyahyasbakery.com']);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.use(cors({
origin: ['http://localhost:3000', 'https://ummyahyasbakery.com'],
credentials: true
}));
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "usd",
automatic_payment_methods: {
enabled: true,
},
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
app.listen(process.env.PORT || 4242, () => {
console.log("Node server listening on port 4242!");
});`
``````This is the relevant part of the client side:
`useEffect(() => {
// Create PaymentIntent as soon as the page loads
fetch("/create-payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items: selectedProduct }),
})
.then((res) => {
console.log(res)
if (res.ok) return res.json();
throw new Error("Status code error: " + res.status);
})
.then((data) => setClientSecret(data.clientSecret))
.catch((error) => console.error("Error:", error));
}, [selectedProduct]);`
```
@EliteSamurai You cannot, as Netlify doesn’t provide runtime node.js hosting, see:
Your options are:
- Adjust your code to work with Serverless Functions
- Host your back-end elsewhere and use a Proxy
- Host your entire application elsewhere