// app.post("/checkout-session", handlePayment); import bodyParser from "body-parser"; const { STRIPE_SECRET_KEY, WEBHOOK_SECRET_KEY } = process.env; import crypto from "crypto"; import Stripe from "stripe"; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY); // const stripe = require("stripe")("Your Secret Key"); import { Order } from "./orderModel.js"; import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js"; import sendEmail from "../../Utils/sendEmail.js"; // const endpointSecret = STRIPE_SECRET_KEY; //generate unique order id export const generateUniqueOrderId = async () => { const currentYear = new Date().getFullYear(); // Find the latest order to get the last serial number const latestOrder = await Order.findOne({}, {}, { sort: { orderID: -1 } }); let serialNumber = 1; if (latestOrder) { const lastYear = parseInt(latestOrder.orderID.substring(0, 4), 10); if (lastYear === currentYear) { // If the last order was in the current year, increment the serial number serialNumber = parseInt(latestOrder.orderID.substring(4), 10) + 1; } } // Pad the serial number with zeros and concatenate with the current year const paddedSerialNumber = serialNumber.toString().padStart(7, "0"); const orderId = `${currentYear}${paddedSerialNumber}`; return orderId; }; export const handlePayment = async (req, res) => { try { const { email } = req.user; if (!email) return res.status(400).send({ message: "Please enter the email" }); const { address, cart, subtotal } = req.body; if (cart.length < 1) return res.status(400).json({ message: "cart is empty!" }); switch (true) { //validation case !address: { return res.status(404).json({ msg: "please provide shipping address" }); } case !subtotal: { return res.status(404).json({ msg: "please provide product subtotal" }); } } let addss = await ShippingAddress.findById(address); let shipping = { first_Name: addss.first_Name, last_Name: addss.last_Name, phone_Number: addss.phone_Number, street: addss.street, city: addss.city, state: addss.state, postalCode: addss?.postalCode, country: addss.country, addressId: address, }; const orderItems = await cart.map((item) => ({ product: item.product._id, name: item.product.name, price: item.product.total_amount, image: item.product.image, quantity: item.quantity, product_Subtotal: item.subtotal, })); // console.log("line", lineItems[0]); const Id = await generateUniqueOrderId(); const order = await Order.create({ orderID: Id, total_amount: subtotal, orderItems, shippingInfo: shipping, user: req.user._id, }); // console.log("fffffffff", order, "llllllllll"); const lineItems = await cart.map((item) => ({ price_data: { currency: "inr", product_data: { name: item.product.name, images: [item.product.image[0]?.url], }, unit_amount: Number(item.product.total_amount) * 100, }, quantity: Number(item.quantity), })); if (order) { const session = await stripe.checkout.sessions.create({ payment_method_types: ["card"], line_items: lineItems, mode: "payment", customer_email: `${email}`, metadata: { orderId: order._id.toString(), // Add any other key-value pairs as needed }, shipping_address_collection: { allowed_countries: ["IN"], // Allow only India for INR transactions }, billing_address_collection: "required", success_url: `${process.env.FRONTEND_URL}/order-complete`, // Provide your success URL here cancel_url: `${process.env.FRONTEND_URL}/cart`, }); // res.json({ sessionId: session.id }); res .status(200) .send({ message: "order created", url: session.url, id: session.id }); } } catch (err) { console.log(err); res.status(500).send({ message: "Something went wrong", err }); } }; export const webhook = async (req, res) => { const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET; const signature = req.headers["stripe-signature"]; let event; if (webhookSecret) { try { event = stripe.webhooks.constructEvent( req.body, signature, webhookSecret ); } catch (err) { console.log(`❌ Error message: ${err.message}`); res.status(400).send(`Webhook Error: ${err.message}`); return; } } if (event.type === "checkout.session.completed") { const findOrder = await Order.findById(event.data.object.metadata?.orderId); findOrder.paypal_payer_id = event.data.object.id; findOrder.paidAt = new Date(event.data.object.created * 1000); findOrder.isPaid = true; if (event.data.object?.payment_status === "paid") { findOrder.payment_status = "success"; } else { findOrder.payment_status = "failed"; } findOrder.orderStatus = "new"; await findOrder.save(); // Construct the HTML for the email const itemRows = findOrder?.orderItems .map( (item) => `
Great news! Your order #${findOrder?.orderID} has been confirmed. Here are the details:
Item | Quantity | Price |
---|
Shipping Address: ${findOrder?.shippingInfo.first_Name} ${findOrder?.shippingInfo.last_Name},${findOrder?.shippingInfo.postalCode}, ${findOrder?.shippingInfo.street}, ${findOrder?.shippingInfo.city}, ${findOrder?.shippingInfo.state}, ${findOrder?.shippingInfo.country} Phone number:${findOrder?.shippingInfo.phone_Number}
Total: ₹${findOrder.total_amount}