// Node.js example using Express
const express = require("express");
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const cors = require("cors");
const app = express();
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
app.use(express.json());
app.use(cors({ origin: process.env.CORS_ORIGIN }));
app.post("/create-checkout-session", async (req, res) => {
const { products } = req.body;
console.log("products",products);
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: products,
mode: "payment",
success_url: `${process.env.FRONTEND_URL}/checkout/success`,
cancel_url: `${process.env.FRONTEND_URL}/cart`,
payment_intent_data: {
statement_descriptor: process.env.STATEMENT_DESCRIPTOR,
},
});
console.log(session.id);
res.json({ id: session.id });
} catch (error) {
res.status(500).send(error.toString());
}
});
app.listen(3000, () => console.log("Running on port 3000"));