Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed wallet feature #94

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 150 additions & 84 deletions controllers/payment_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,109 +4,174 @@ require("dotenv").config();
var crypto = require("crypto");
const { ErrorHandler } = require("../middlewares/error");
const { User, Course } = require("../models");
const { courseIdSchema } = require("../utils/validator");
const { amountSchema } = require("../utils/validator");

const paymentCtrl = {
createOrderCart: async (req, res, next) => {
buycourse: async (req, res, next) => {
try {
const user = req.user;
if (user.cart.length == 0) {
return next(new ErrorHandler(404, "No course in cart"));
const courseId = req.params.courseId;
const course = await Course.findById(courseId);
if (!course) {
return next(new ErrorHandler(400, "No course found"));
}
const amount = req.params.amount;
const razorpayInstance = new Razorpay({
key_id: process.env.KEY_ID,
key_secret: process.env.KEY_SECRET,
const courseIdIndex = user.ownedCourse.findIndex((course) =>
course.courseId.equals(courseId)
);
if (course.createdBy.equals(user._id)) {
return next(new ErrorHandler(400, "You cannot buy your own course"));
}
if (courseIdIndex != -1) {
return next(
new ErrorHandler(402, "You have already enrolled in this course")
);
}
const amount = parseInt(course.price);
if (user.wallet < amount) {
return next(new ErrorHandler(402, "Insufficient balance"));
}
user.wallet -= amount;
const creater = course.createdBy;
const createrUser = await User.findById(creater);
console.log(course.price)
createrUser.wallet += amount * 0.9;
await createrUser.save();
user.ownedCourse.push({ courseId: courseId });
await user.save();

await Course.findByIdAndUpdate(courseId, {
$inc: { totalStudents: 1 },
});
const options = {
amount: amount * 100,
currency: "INR",
partial_payment: false,
payment_capture: 1,
};
const order = await razorpayInstance.orders.create(options);
return res.json({
success: true,
message: "Order Created",

order_id: order.id,
key_id: process.env.KEY_ID,
createdAt: Date.now(),
order: order,
message: "Payment successful",
});
} catch (e) {
next(e);
}
},
checkPaymentCart: async (req, res, next) => {
buyCourseCart: async (req, res, next) => {
try {
body = req.body.order_id + "|" + req.body.payment_id;
var expectedSignature = crypto
.createHmac("sha256", process.env.KEY_SECRET)
.update(body.toString())
.digest("hex");

console.log("sig" + req.body.signature);
console.log("sig" + expectedSignature);
const user = req.user;
if (expectedSignature === req.body.signature) {
for (let i = 0; i < user.cart.length; i++) {
const courseId = user.cart[i];
await Course.findByIdAndUpdate(courseId, {
$inc: { totalStudents: 1 },
});
await User.findByIdAndUpdate(req.user._id, {
$push: { ownedCourse: { courseId: courseId } },
});
}
await User.findByIdAndUpdate(req.user._id, {
$set: { cart: [] },
});
return res.json({
success: true,
message: "Payment successful",
const amount = parseInt(req.params.amount);
const result = amountSchema.validateAsync(amount)
const price = result.amount
if (user.cart.length == 0) {
return next(new ErrorHandler(404, "No course in cart"));
}
if (user.wallet < price) {
return next(new ErrorHandler(402, "Insufficient balance"));
}
for (let i = 0; i < user.cart.length; i++) {
const courseId = user.cart[i];
// const course = await Course.findById(courseId);
const course = await Course.findById(courseId);
const createdBy = course.createdBy;
const createrUser = await User.findById(createdBy);
createrUser.wallet += parseInt(course.price) * 0.9;
await createrUser.save();
user.ownedCourse.push({ courseId: courseId });
await Course.findByIdAndUpdate(courseId, {
$inc: { totalStudents: 1 },
});
} else {
return next(new ErrorHandler(402, "Payment failed"));
}
user.wallet -= price;
user.cart = [];
await user.save();
res.json({
success: true,
message: "Payment successful",
});
} catch (e) {
next(e);
}
},
// createOrderCart: async (req, res, next) => {
// try {
// const user = req.user;
// if (user.cart.length == 0) {
// return next(new ErrorHandler(404, "No course in cart"));
// }
// const amount = req.params.amount;
// const razorpayInstance = new Razorpay({
// key_id: process.env.KEY_ID,
// key_secret: process.env.KEY_SECRET,
// });
// const options = {
// amount: amount * 100,
// currency: "INR",
// partial_payment: false,
// payment_capture: 1,
// };
// const order = await razorpayInstance.orders.create(options);
// return res.json({
// success: true,
// message: "Order Created",

// order_id: order.id,
// key_id: process.env.KEY_ID,
// createdAt: Date.now(),
// order: order,
// });
// } catch (e) {
// next(e);
// }
// },
// checkPaymentCart: async (req, res, next) => {
// try {
// body = req.body.order_id + "|" + req.body.payment_id;
// var expectedSignature = crypto
// .createHmac("sha256", process.env.KEY_SECRET)
// .update(body.toString())
// .digest("hex");

// console.log("sig" + req.body.signature);
// console.log("sig" + expectedSignature);
// const user = req.user;
// if (expectedSignature === req.body.signature) {
// for (let i = 0; i < user.cart.length; i++) {
// const courseId = user.cart[i];
// await Course.findByIdAndUpdate(courseId, {
// $inc: { totalStudents: 1 },
// });
// await User.findByIdAndUpdate(req.user._id, {
// $push: { ownedCourse: { courseId: courseId } },
// });
// }
// await User.findByIdAndUpdate(req.user._id, {
// $set: { cart: [] },
// });
// return res.json({
// success: true,
// message: "Payment successful",
// });
// } else {
// return next(new ErrorHandler(402, "Payment failed"));
// }
// } catch (e) {
// next(e);
// }
// },
createOrder: async (req, res, next) => {
try {
const courseid = req.params.courseId;
const result = await courseIdSchema.validateAsync({ params: courseid });
const courseId = result.params;
const course = await Course.findById(courseId);
if (!course) {
return next(new ErrorHandler(400, "No course found"));
}
const user = req.user;
const courseIdIndex = user.ownedCourse.findIndex((course) =>
course.courseId.equals(courseId)
);
if (courseIdIndex != -1) {
return next(
new ErrorHandler(402, "You have already enrolled in this course")
);
}
const amount = course.price;

// const user = req.user;
const amount = parseInt(req.params.amount);
const result = amountSchema.validateAsync(amount)
const price = result.amount
const razorpayInstance = new Razorpay({
key_id: process.env.KEY_ID,
key_secret: process.env.KEY_SECRET,
});

const options = {
amount: amount * 100,
amount: price * 100,
currency: "INR",
partial_payment: false,
payment_capture: 1,
};

const order = await razorpayInstance.orders.create(options);
// console.log(order);

return res.json({
success: true,
message: "Order Created",
Expand All @@ -117,16 +182,16 @@ const paymentCtrl = {
order: order,
});
} catch (error) {
// console.log(error);
next(error);
}
},

checkPayment: async (req, res, next) => {
try {
const courseid = req.params.courseId;
const result = await courseIdSchema.validateAsync({ params: courseid });
const courseId = result.params;
const amount = parseInt(req.params.amount);
const result =amountSchema.validateAsync(amount)
const price = result.amount

const user = req.user;
body = req.body.order_id + "|" + req.body.payment_id;
var expectedSignature = crypto
Expand All @@ -138,17 +203,18 @@ const paymentCtrl = {
console.log("sig" + expectedSignature);

if (expectedSignature === req.body.signature) {
const cartCourseIndex = user.cart.indexOf(courseId);
if (cartCourseIndex != -1) {
user.cart.splice(cartCourseIndex, 1);
await user.save();
}
await Course.findByIdAndUpdate(courseId, {
$inc: { totalStudents: 1 },
});
await User.findByIdAndUpdate(req.user._id, {
$push: { ownedCourse: { courseId: courseId } },
});
user.wallet+=price;
// const cartCourseIndex = user.cart.indexOf(courseId);
// if (cartCourseIndex != -1) {
// user.cart.splice(cartCourseIndex, 1);
// await user.save();
// }
// await Course.findByIdAndUpdate(courseId, {
// $inc: { totalStudents: 1 },
// });
// await User.findByIdAndUpdate(req.user._id, {
// $push: { ownedCourse: { courseId: courseId } },
// });
return res.json({
success: true,
message: "Payment successful",
Expand All @@ -162,4 +228,4 @@ const paymentCtrl = {
}
},
};
module.exports = paymentCtrl;
module.exports = paymentCtrl;
Loading