From ee5f7f94919fc7644b8f45519ac774b1a7329e4a Mon Sep 17 00:00:00 2001 From: pawan-dot <71133473+pawan-dot@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:52:33 +0530 Subject: [PATCH] razerpay integration --- .../Orders/RazerPayCheckoutController.js | 120 +++++++++++++++--- resources/Orders/orderModel.js | 16 +++ 2 files changed, 120 insertions(+), 16 deletions(-) diff --git a/resources/Orders/RazerPayCheckoutController.js b/resources/Orders/RazerPayCheckoutController.js index c39b04f..fe4f436 100644 --- a/resources/Orders/RazerPayCheckoutController.js +++ b/resources/Orders/RazerPayCheckoutController.js @@ -56,15 +56,23 @@ export const checkout = async (req, res) => { 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" }); - } - } + if (!address) + return res + .status(404) + .json({ message: "please select shipping address!" }); + if (!subtotal) + 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, @@ -77,12 +85,18 @@ export const checkout = async (req, res) => { 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, - price: item.product.total_amount, + price: item.product.price, + total_Amount: item.product.total_amount, + 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, })); @@ -130,7 +144,7 @@ export const paymentVerification = async (req, res) => { path: "user", select: "name email -_id", }); - console.log("findSameOrder", findSameOrder); + // console.log("findSameOrder", findSameOrder); if (findSameOrder) { (findSameOrder.razorpay_payment_id = razorpay_payment_id), // await Payment.create({ (findSameOrder.isPaid = true), @@ -143,7 +157,7 @@ export const paymentVerification = async (req, res) => { await findSameOrder.save(); } //send email to customer - + // console.log("findSameOrder", findSameOrder); await sendEmail({ to: `${findSameOrder?.user?.email}`, // Change to your recipient @@ -151,10 +165,84 @@ export const paymentVerification = async (req, res) => { subject: `Your Order #${findSameOrder?.orderID} Confirmation`, html: `

Welcome to Smellika - Let the Shopping Begin!

- Hi ${findSameOrder?.shippingInfo?.first_Name}, + Hi ${ + findSameOrder?.shippingInfo?.first_Name + }, -

Great news! Your order #${findSameOrder?.orderID} has been confirmed. Here are the details

-
+

Great news! Your order #${ + findSameOrder?.orderID + } has been confirmed. Here are the details

+

Shipping Address : ${ + findSameOrder?.shippingInfo?.first_Name + } ${findSameOrder?.shippingInfo?.last_Name} , ${ + findSameOrder?.shippingInfo?.street + } ${findSameOrder?.shippingInfo?.city} ${ + findSameOrder?.shippingInfo?.state + } ${findSameOrder?.shippingInfo?.country}, PIN-${ + findSameOrder?.shippingInfo?.postalCode + }, Phone Number: ${findSameOrder?.shippingInfo?.phone_Number}

+

Order Items :

+ + + + + + + + + + + + + + + + + + ${findSameOrder?.orderItems + ?.map( + (product, index) => ` + + + + + + + + + + + + + ` + ) + .join("")} + + + + + +
S No.Product NameImageQuantityPriceGST AmountSubTotal
${ + index + 1 + }${ + product.name + }${
+          product.name
+        }${ + product.quantity + }₹${ + product.price + }₹${ + product?.gst_amount + }₹${ + product.product_Subtotal + }
Total Amount :₹${ + findSameOrder?.total_amount + }
+ +
Best regards,
Team Smellika`, @@ -171,7 +259,7 @@ export const paymentVerification = async (req, res) => { // razorpay_signature, // }); - res.redirect(`http://localhost:5173/account`); + res.redirect(`https://smellika.com/shop`); // res.redirect( // `http://localhost:5173/cart/paymentsuccess?reference=${razorpay_payment_id}` // ); diff --git a/resources/Orders/orderModel.js b/resources/Orders/orderModel.js index 3268b67..c360aba 100644 --- a/resources/Orders/orderModel.js +++ b/resources/Orders/orderModel.js @@ -66,6 +66,10 @@ const orderSchema = new mongoose.Schema( type: Number, default: "", }, + total_Amount: { + type: Number, + default: "", + }, quantity: { type: Number, default: "", @@ -77,6 +81,18 @@ const orderSchema = new mongoose.Schema( type: Number, default: "", }, + gst_amount: { + type: Number, + default: "", + }, + gst_rate: { + type: Number, + default: "", + }, + tax_Name: { + type: String, + default: "", + }, product: { type: mongoose.Schema.ObjectId, ref: "Product",