This commit is contained in:
Sibunnayak 2024-04-01 16:11:45 +05:30
commit da8687a109
10 changed files with 5886 additions and 75 deletions

4
.env
View File

@ -14,6 +14,10 @@ WEBHOOK_SECRET_KEY="whsec_m9u7CFBCY1qWarhxq65CkII6egOBf20K"
STRIPE_SECRET_KEY="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql"
STRIPE_WEBHOOK_SECRET="whsec_dc9b9084fc764c806c8c5c06dd91de1ee809e9c8deab6d56e8e3ef2fc9c30c67"
<<<<<<< HEAD
FRONTEND_URL="http://127.0.0.1:5173"
RAZERPAY_KEY_ID="rzp_test_smzQmWoS64S2W9"
RAZERPAY_SECRET_KEY="cSn6MgA4xSEaZBpPp4zpDA3C"
FRONTEND_URL="https://smellika.com"

4
app.js
View File

@ -9,8 +9,8 @@ import cors from "cors";
import cookieParser from "cookie-parser";
// Design Router
import designRoute from "./resources/Design/designRouter.js";
// app.use(express.json({ limit: "50mb" }));
// app.use(express.urlencoded({ extended: true, limit: "50mb" }));
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ extended: true, limit: "50mb" }));
//webhook call route according strip (webhook want raw data not json data)
app.use((req, res, next) => {

5350
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,8 @@
"multer-storage-cloudinary": "^4.0.0",
"nodemailer": "^6.9.4",
"nodemon": "^3.0.1",
"pm2": "^5.3.1",
"razorpay": "^2.9.2",
"secure-random-password": "^0.2.3",
"stripe": "^14.16.0",
"uuid": "^9.0.1",

View File

@ -0,0 +1,360 @@
import bodyParser from "body-parser";
import crypto from "crypto";
import Razorpay from "razorpay";
import { Order } from "./orderModel.js";
import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
import sendEmail from "../../Utils/sendEmail.js";
const instance = new Razorpay({
key_id: process.env.RAZERPAY_KEY_ID,
key_secret: process.env.RAZERPAY_SECRET_KEY,
});
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 getRzpkey = async (req, res) => {
const { name, email } = req.user;
res.status(200).json({
success: true,
key: process.env.RAZERPAY_KEY_ID,
name,
email,
});
};
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" });
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 orders = await Order.create({
orderID: Id,
total_amount: subtotal,
orderItems,
shippingInfo: shipping,
user: req.user._id,
razorpay_order_id: order?.id,
});
} else {
res.status(400).json({
success: false,
message: "Failled to order Create",
});
}
res.status(200).json({
success: true,
order,
});
};
export const paymentVerification = async (req, res) => {
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } =
req.body;
const body = razorpay_order_id + "|" + razorpay_payment_id;
const expectedSignature = crypto
.createHmac("sha256", process.env.RAZERPAY_SECRET_KEY)
.update(body.toString())
.digest("hex");
const isAuthentic = expectedSignature === razorpay_signature;
if (isAuthentic) {
// Database comes here
let findSameOrder = await Order.findOne({
razorpay_order_id: razorpay_order_id,
}).populate({
path: "user",
select: "name email -_id",
});
console.log("findSameOrder", findSameOrder);
if (findSameOrder) {
(findSameOrder.razorpay_payment_id = razorpay_payment_id), // await Payment.create({
(findSameOrder.isPaid = true),
(findSameOrder.paidAt = Date.now()),
(findSameOrder.razorpay_signature = razorpay_signature);
// await Payment.create({
findSameOrder.payment_status = "success";
findSameOrder.orderStatus = "new";
await findSameOrder.save();
}
//send email to customer
await sendEmail({
to: `${findSameOrder?.user?.email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `Your Order #${findSameOrder?.orderID} Confirmation`,
html: ` <h1 style="color: #333; text-align: center; font-family: Arial, sans-serif;">Welcome to Smellika - Let the Shopping Begin!</h1>
<strong style="color: #1b03a3; font-size: 16px"> Hi ${findSameOrder?.shippingInfo?.first_Name},</strong>
<p style="color: #555; font-size: 15px;">Great news! Your order #${findSameOrder?.orderID} has been confirmed. Here are the details</p>
<br/>
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
<span style="color: #555; font-size: 13px;">Team Smellika</span>`,
});
// console.log("findSameOrder", findSameOrder);
// // findSameOrder.razorpay_payment_id=razorpay_payment_id,// await Payment.create({
// findOrder.paidAt = new Date(event.data.object.created * 1000);
// findOrder.isPaid = true;
// razorpay_signature: { type: String },
// razorpay_order_id,
// razorpay_payment_id,
// razorpay_signature,
// });
res.redirect(`http://localhost:5173/account`);
// res.redirect(
// `http://localhost:5173/cart/paymentsuccess?reference=${razorpay_payment_id}`
// );
} else {
res.status(400).json({
success: false,
});
}
};
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);
// console.log(addss?.postalCode);
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
},
success_url: `${process.env.FRONTEND_URL}/cart`,
cancel_url: `${process.env.FRONTEND_URL}/error`,
});
// res.json({ sessionId: session.id });
res.status(200).send({ message: "order created", url: session.url });
}
} 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") {
// console.log("dddddddddddd", event.data);
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();
await sendEmail({
to: `${event.data.object.customer_email}`, // Change to your recipient
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
subject: `Your Order #${findOrder?.orderID} Confirmation`,
html: ` <h1 style="color: #333; text-align: center; font-family: Arial, sans-serif;">Welcome to Smellika - Let the Shopping Begin!</h1>
<strong style="color: #1b03a3; font-size: 16px"> Hi ${findOrder?.shippingInfo?.first_Name},</strong>
<p style="color: #555; font-size: 15px;">Great news! Your order #${findOrder?.orderID} has been confirmed. Here are the details</p>
<br/>
<span style="color: #555; font-size: 13px;">Best regards,</span><br/>
<span style="color: #555; font-size: 13px;">Team Smellika</span>`,
});
// Items: [List of Purchased Items]
// Total Amount: [Total Amount]
// Shipping Address: [Shipping Address]
// We'll keep you updated on the shipping progress. Thanks for choosing Smellika!
// Best regards
// Team Smellika
console.log(
"event.data.object",
event.data.object,
"---------------------"
);
console.log(`💰 Payment status: ${event.data.object?.payment_status}`);
// Saving the payment details in the database
// const payment = await Payment.create({
// customer_email: event.data.object.customer_email,
// amount: event.data.object.amount_total / 100,
// paymentId: event.data.object.id,
// paymentStatus: event.data.object.payment_status,
// createdAt: event.data.object.created,
// });
}
// if (event.type === "checkout.session.completed") {
// console.log("dddddddddddd", event.data);
// console.log("event.data.object", event.data.object);
// console.log(`💰 Payment status: ${event.data.object?.payment_status}`);
// payment_intent.payment_failed;
// // Saving the payment details in the database
// // const payment = await Payment.create({
// // customer_email: event.data.object.customer_email,
// // amount: event.data.object.amount_total / 100,
// // paymentId: event.data.object.id,
// // paymentStatus: event.data.object.payment_status,
// // createdAt: event.data.object.created,
// // });
// }
// Return a 200 res to acknowledge receipt of the event
res.status(200).end();
// res.send().end();
};

View File

@ -121,7 +121,10 @@ const orderSchema = new mongoose.Schema(
},
// paypal_payer_id: { type: String },
stripe_payment_id: { type: String },
// stripe_payment_id: { type: String },
razorpay_payment_id: { type: String },
razorpay_order_id: { type: String },
razorpay_signature: { type: String },
// paypal_signature: { type: String },
// order_used: { type: Boolean, default: false },
isDelivered: { type: Boolean, required: true, default: false },

View File

@ -19,6 +19,11 @@ const app = express();
// Configure bodyParser to parse the raw request body as a buffer
app.use(bodyParser.raw({ type: "application/json" }));
import { handlePayment, webhook } from "./StripeCheckOutController.js";
import {
checkout,
getRzpkey,
paymentVerification,
} from "./RazerPayCheckoutController.js";
const router = express.Router();
//checkout Routes-------------------------//
router.route("/checkout/").post(isAuthenticatedUser, createOrderCheckout);
@ -48,6 +53,11 @@ router
.route("/delete/:id")
.delete(isAuthenticatedUser, authorizeRoles("admin"), deleteOneOrder);
//RAZERPAY checkout
router.route("/getRzpKey/").get(isAuthenticatedUser, getRzpkey);
router.route("/Rzpcheckout/").post(isAuthenticatedUser, checkout);
router.route("/paymentverification").post(paymentVerification);
// router.route("/product/getAll/").get(getAllProduct)
export default router;

View File

@ -75,6 +75,48 @@ export const getAllProduct = async (req, res) => {
});
}
};
// get all product with device products first
export const getAllProductsDevicesFirst = async (req, res) => {
try {
// we want products with category name Device to be displayed first, so i have first found the products with category name Devices then made another request to find all products and filtered products with category devices , then merged both arrays so we get devices first then all other categories
const categoryName = 'Devices';
// Find the category object by name first
const category = await CategoryModel.findOne({ categoryName });
if (!category) {
throw new Error("Category not found");
}
// products with device category
const deviceProducts = await Product.find({ category: category._id }).populate('category');
// all products
const allProducts = await Product.find()
.populate({
path: "category gst addedBy",
select: "name categoryName tax",
})
.sort({
createdAt: -1,
});
// filtering out products with device category
const filteredProducts = allProducts.filter((ele) => { return ele.category?.categoryName !== categoryName })
// merging both deviceProcuts and filtered products
const product = deviceProducts.concat(filteredProducts)
if (product) {
return res.status(200).json({
success: true,
product,
});
}
} catch (error) {
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",
});
}
};
//get One Product
export const getOneProduct = async (req, res) => {
try {
@ -98,8 +140,109 @@ export const getOneProduct = async (req, res) => {
};
// 3.update Product
// export const updateProduct = async (req, res) => {
// const {
// name,
// description,
// price,
// category,
// image,
// gst_amount,
// gst,
// total_amount,
// } = req.body;
// console.log(gst_amount, gst, total_amount);
// try {
// // Prepare an array for the images
// const jsonArray = JSON.parse(image);
// const AllImages = jsonArray.map(({ public_id, url }) => ({
// public_id,
// url,
// }));
// if (req.files && req.files.newImages) {
// const newuploadImages = Array.isArray(req.files.newImages)
// ? req.files.newImages
// : [req.files.newImages];
// const imagesLinks = [];
// for (let i = 0; i < newuploadImages.length; i++) {
// const result = await cloudinary.v2.uploader.upload(
// newuploadImages[i].tempFilePath,
// {
// folder: "smellica/product",
// }
// );
// imagesLinks.push({
// public_id: result.public_id,
// url: result.secure_url,
// });
// }
// // Combine the existing images and the newly uploaded images
// const updatedImages = [...AllImages, ...imagesLinks];
// // Perform the product update
// const ModifyProduct = await Product.findOneAndUpdate(
// { _id: req.params.id },
// {
// $set: {
// name,
// description,
// price,
// category,
// image: updatedImages,
// gst,
// gst_amount,
// total_amount,
// },
// },
// { new: true }
// );
// return res.status(200).json({
// success: true,
// ModifyProduct,
// });
// } else {
// const ModifyProduct = await Product.findOneAndUpdate(
// { _id: req.params.id },
// {
// $set: {
// name,
// description,
// price,
// category,
// image: AllImages,
// },
// },
// { new: true }
// );
// return res.status(200).json({
// success: true,
// ModifyProduct,
// });
// }
// } catch (error) {
// res.status(500).json({
// success: false,
// msg: error.message ? error.message : "Something went wrong!",
// });
// }
// };
export const updateProduct = async (req, res) => {
const { name, description, price, category, image } = req.body;
const {
name,
description,
price,
category,
image,
gst_amount,
gst,
total_amount,
} = req.body;
try {
// Prepare an array for the images
@ -109,16 +252,18 @@ export const updateProduct = async (req, res) => {
url,
}));
let updatedImages = AllImages;
if (req.files && req.files.newImages) {
const newuploadImages = Array.isArray(req.files.newImages)
const newUploadImages = Array.isArray(req.files.newImages)
? req.files.newImages
: [req.files.newImages];
const imagesLinks = [];
for (let i = 0; i < newuploadImages.length; i++) {
for (let i = 0; i < newUploadImages.length; i++) {
const result = await cloudinary.v2.uploader.upload(
newuploadImages[i].tempFilePath,
newUploadImages[i].tempFilePath,
{
folder: "smellica/product",
}
@ -131,46 +276,37 @@ export const updateProduct = async (req, res) => {
}
// Combine the existing images and the newly uploaded images
const updatedImages = [...AllImages, ...imagesLinks];
// Perform the product update
const ModifyProduct = await Product.findOneAndUpdate(
{ _id: req.params.id },
{
$set: {
name,
description,
price,
category,
image: updatedImages,
},
},
{ new: true }
);
return res.status(200).json({
success: true,
ModifyProduct,
});
} else {
const ModifyProduct = await Product.findOneAndUpdate(
{ _id: req.params.id },
{
$set: {
name,
description,
price,
category,
image: AllImages,
},
},
{ new: true }
);
return res.status(200).json({
success: true,
ModifyProduct,
});
updatedImages = [...AllImages, ...imagesLinks];
}
// Perform the product update
const updatedProduct = await Product.findOneAndUpdate(
{ _id: req.params.id },
{
$set: {
name,
description,
price,
category,
image: updatedImages,
gst,
gst_amount,
total_amount,
},
},
{ new: true }
);
if (!updatedProduct) {
return res.status(404).json({ success: false, msg: "Product not found" });
}
return res.status(200).json({
success: true,
updatedProduct,
});
} catch (error) {
console.error("Error updating product:", error);
res.status(500).json({
success: false,
msg: error.message ? error.message : "Something went wrong!",

View File

@ -5,7 +5,7 @@ const productSchema = new Schema(
{
name: {
type: String,
maxLength: [25, "name cannot exceed 25 characters"],
maxLength: [35, "name cannot exceed 25 characters"],
required: [true, "Please Enter product Name"],
trim: true,
},
@ -15,7 +15,7 @@ const productSchema = new Schema(
},
description: {
type: String,
maxLength: [100, "description cannot exceed 100 characters"],
maxLength: [400, "description cannot exceed 100 characters"],
required: [true, "Please Enter product Description"],
},
price: {

View File

@ -7,6 +7,7 @@ import {
getOneProduct,
deleteImageFromCloudinary,
getProductsByCategory,
getAllProductsDevicesFirst,
} from "./ProductController.js";
const router = express.Router();
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
@ -14,6 +15,7 @@ router
.route("/product/create/")
.post(isAuthenticatedUser, authorizeRoles("admin"), createProduct);
router.route("/product/getAll/").get(getAllProduct);
router.route("/product/getAllProductsDevicesFrist/").get(getAllProductsDevicesFirst);
router.route("/product/getOne/:id").get(getOneProduct);
router
.route("/product/update/:id")