change product and razorpay

This commit is contained in:
pawan-dot 2024-04-18 17:50:07 +05:30
parent d0cf999ebe
commit d1481c0efc
4 changed files with 119 additions and 80 deletions

10
.env
View File

@ -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"

View File

@ -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,15 +51,16 @@ 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" });
// }
// }
const options = {
amount: Number(req.body.subtotal * 100),
currency: "INR",
};
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,
@ -89,15 +77,32 @@ export const checkout = async (req, res) => {
const orderItems = await cart.map((item) => ({
product: item.product._id,
name: item.product.name,
price: item.product.price,
total_Amount: item.product.total_amount,
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,
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]);
@ -111,16 +116,25 @@ export const checkout = async (req, res) => {
razorpay_order_id: order?.id,
});
} else {
res.status(400).json({
return res.status(400).json({
success: false,
message: "Failled to order Create",
});
}
res.status(200).json({
return res.status(200).json({
success: true,
order,
});
} catch (error) {
console.log("error", error);
return res.status(400).json({
success: false,
message: error?.description
? "Razorpay" + error?.description
: "Something went wrong!",
});
}
};
export const paymentVerification = async (req, res) => {
@ -188,6 +202,8 @@ export const paymentVerification = async (req, res) => {
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Variant</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
@ -209,6 +225,9 @@ export const paymentVerification = async (req, res) => {
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product.name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product?.variant_Name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;"><img src="${
product?.image[0]?.url
@ -226,7 +245,7 @@ export const paymentVerification = async (req, res) => {
product?.gst_amount
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product.product_Subtotal
product?.product_Subtotal
}</td>
</tr>
@ -234,7 +253,7 @@ export const paymentVerification = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
findSameOrder?.total_amount
}</td>

View File

@ -176,14 +176,12 @@ export const updateOrderStatusById = async (req, res) => {
<thead>
<tr>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Variant</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Price</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">GST Amount</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">SubTotal</th>
</tr>
@ -199,6 +197,9 @@ export const updateOrderStatusById = async (req, res) => {
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product.name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product?.variant_Name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;"><img src="${
product?.image[0]?.url
@ -224,7 +225,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
order?.total_amount
}</td>
@ -267,6 +268,7 @@ export const updateOrderStatusById = async (req, res) => {
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Variant</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
@ -288,6 +290,9 @@ export const updateOrderStatusById = async (req, res) => {
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product.name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product?.variant_Name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;"><img src="${
product?.image[0]?.url
@ -313,7 +318,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
order?.total_amount
}</td>
@ -383,6 +388,7 @@ export const updateOrderStatusById = async (req, res) => {
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Variant</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
@ -404,6 +410,9 @@ export const updateOrderStatusById = async (req, res) => {
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product.name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product?.variant_Name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;"><img src="${
product?.image[0]?.url
@ -429,7 +438,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
order?.total_amount
}</td>
@ -470,8 +479,8 @@ export const updateOrderStatusById = async (req, res) => {
<th style="border: 1px solid #555; padding: 2px; text-align: center;">S No.</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Product Name</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Variant</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Image</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Quantity</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">Price</th>
<th style="border: 1px solid #555; padding: 2px; text-align: center;">GST Amount</th>
@ -491,6 +500,9 @@ export const updateOrderStatusById = async (req, res) => {
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product.name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
product?.variant_Name
}</td>
<td style="border: 1px solid #555; padding: 2px; text-align: center;"><img src="${
product?.image[0]?.url
@ -516,7 +528,7 @@ export const updateOrderStatusById = async (req, res) => {
)
.join("")}
<tr>
<th colspan="6" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<th colspan="7" style="border: 1px solid #555; padding: 2px; text-align: right;">Total Amount :</th>
<td style="border: 1px solid #555; padding: 2px; text-align: center;">${
order?.total_amount
}</td>

View File

@ -62,11 +62,15 @@ const orderSchema = new mongoose.Schema(
type: String,
default: "",
},
variant_Name: {
type: String,
default: "",
},
price: {
type: Number,
default: "",
},
total_Amount: {
total_price: {
type: Number,
default: "",
},
@ -85,6 +89,10 @@ const orderSchema = new mongoose.Schema(
type: Number,
default: "",
},
total_gst_amount: {
type: Number,
default: "",
},
gst_rate: {
type: Number,
default: "",