diff --git a/.env b/.env index e5b250d..c10b19c 100644 --- a/.env +++ b/.env @@ -12,12 +12,12 @@ CLOUDINARY_API_KEY = "877544192441588" CLOUDINARY_API_SECRET = "9paejuSC-fY5b0WoaUuSFURSnvM" WEBHOOK_SECRET_KEY="whsec_m9u7CFBCY1qWarhxq65CkII6egOBf20K" -STRIPE_SECRET_KEY="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql" -STRIPE_WEBHOOK_SECRET="whsec_dc9b9084fc764c806c8c5c06dd91de1ee809e9c8deab6d56e8e3ef2fc9c30c67" -<<<<<<< HEAD +# STRIPE_SECRET_KEY="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql" +# STRIPE_WEBHOOK_SECRET="whsec_dc9b9084fc764c806c8c5c06dd91de1ee809e9c8deab6d56e8e3ef2fc9c30c67" + FRONTEND_URL="http://127.0.0.1:5173" -RAZERPAY_KEY_ID="rzp_test_smzQmWoS64S2W9" -RAZERPAY_SECRET_KEY="cSn6MgA4xSEaZBpPp4zpDA3C" +RAZERPAY_KEY_ID="rzp_test_2rg1Bq3Ki8xw9e" +RAZERPAY_SECRET_KEY="WFhHbXL7AlLIuull9kKjYiNA" FRONTEND_URL="https://smellika.com" diff --git a/resources/Orders/RazerPayCheckoutController.js b/resources/Orders/RazerPayCheckoutController.js index fe4f436..b0b0afc 100644 --- a/resources/Orders/RazerPayCheckoutController.js +++ b/resources/Orders/RazerPayCheckoutController.js @@ -39,20 +39,7 @@ export const getRzpkey = async (req, res) => { }); }; export const checkout = async (req, res) => { - // console.log(req.body.subtotal); - const options = { - amount: Number(req.body.subtotal * 100), - currency: "INR", - }; - const order = await instance.orders.create(options); - // id: "order_Ns423uPG0r36Dk"; - - //save order in database - - if (order?.id) { - const { email } = req.user; - if (!email) - return res.status(400).send({ message: "Please enter the email" }); + try { const { address, cart, subtotal } = req.body; if (cart.length < 1) return res.status(400).json({ message: "cart is empty!" }); @@ -64,63 +51,90 @@ export const checkout = async (req, res) => { return res .status(404) .json({ message: "please provide product subtotal!" }); - // switch (true) { - // //validation - // case !address: { - // return res.status(404).json({ msg: "please select 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 options = { + amount: Number(req.body.subtotal * 100), + currency: "INR", }; - // console.log("cart", cart[0]?.product?.gst); - const orderItems = await cart.map((item) => ({ - product: item.product._id, - name: item.product.name, - price: item.product.price, - total_Amount: item.product.total_amount, + const order = await instance.orders.create(options); + //save order in database + if (order?.id) { + const { email } = req.user; + if (!email) + return res.status(400).send({ message: "Please enter the email" }); + 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, + }; + // console.log("cart", cart[0]?.product?.gst); + const orderItems = await cart.map((item) => ({ + product: item.product._id, + name: item.product.name, + variant_Name: item.variant.variant_Name, + price: Number(item.variant.price), + total_price: item.quantity * Number(item.variant.price), - image: item.product.image, - quantity: item.quantity, - gst_amount: item.product.gst_amount, - gst_rate: item.product.gst?.tax, - tax_Name: item.product.gst?.name, - product_Subtotal: item.subtotal, - })); + image: item.product.image, + quantity: item.quantity, + gst_amount: Number( + (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100 + )?.toFixed(3), + total_gst_amount: Number( + Number(item.quantity) * + Number( + (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100 + ) + )?.toFixed(3), + gst_rate: item.variant.gst_Id?.tax, + tax_Name: item.variant?.gst_Id?.name, + product_Subtotal: Number( + Number(item.quantity * Number(item.variant.price)) + + Number( + Number(item.quantity) * + Number( + (Number(item.variant.price) * item.variant.gst_Id?.tax) / 100 + ) + ) + ).toFixed(3), + })); - // console.log("line", lineItems[0]); - const Id = await generateUniqueOrderId(); - const orders = await Order.create({ - orderID: Id, - total_amount: subtotal, - orderItems, - shippingInfo: shipping, - user: req.user._id, - razorpay_order_id: order?.id, + // console.log("line", lineItems[0]); + const Id = await generateUniqueOrderId(); + const orders = await Order.create({ + orderID: Id, + total_amount: subtotal, + orderItems, + shippingInfo: shipping, + user: req.user._id, + razorpay_order_id: order?.id, + }); + } else { + return res.status(400).json({ + success: false, + message: "Failled to order Create", + }); + } + + return res.status(200).json({ + success: true, + order, }); - } else { - res.status(400).json({ + } catch (error) { + console.log("error", error); + return res.status(400).json({ success: false, - message: "Failled to order Create", + message: error?.description + ? "Razorpay" + error?.description + : "Something went wrong!", }); } - - res.status(200).json({ - success: true, - order, - }); }; export const paymentVerification = async (req, res) => { @@ -188,6 +202,8 @@ export const paymentVerification = async (req, res) => {