2021-04-10 00:47:40 -07:00
|
|
|
const razorpay = require('razorpay') ;
|
|
|
|
const Order = require('../models/Order') ;
|
|
|
|
const User = require('../models/User') ;
|
|
|
|
const CourseType = require('../models/CourseType') ;
|
|
|
|
const Course = require('../models/Course') ;
|
|
|
|
const Student = require('../models/Student');
|
|
|
|
|
|
|
|
//test credentials of razorpay
|
|
|
|
const instance = new razorpay({
|
2021-04-12 04:42:57 -07:00
|
|
|
key_id : 'rzp_test_tLx9c6GiWjKcsl' ,
|
|
|
|
key_secret : '4Cf52d6C3amkptdRLCTdC2sT'
|
2021-04-10 00:47:40 -07:00
|
|
|
}) ;
|
|
|
|
|
|
|
|
module.exports.postVerify =async (req , res , next) => {
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//Secret we put in razorpay
|
|
|
|
const SECRET = 'CantileverLabs' ;
|
|
|
|
|
|
|
|
const crypto = require('crypto')
|
|
|
|
|
|
|
|
const shasum = crypto.createHmac('sha256', SECRET) ;
|
|
|
|
shasum.update(JSON.stringify(req.body))
|
|
|
|
const digest = shasum.digest('hex')
|
|
|
|
|
|
|
|
//if secret is matched
|
|
|
|
if (digest === req.headers['x-razorpay-signature']) {
|
|
|
|
console.log('request is legit')
|
|
|
|
// process it
|
|
|
|
const orderId = req.body.payload.payment.entity.order_id ;
|
|
|
|
//fetching the order from the orderId
|
|
|
|
let order = await Order.findOne({orderId:orderId}) ;
|
|
|
|
let courseId = order.course ;
|
|
|
|
//adding the payment detail
|
|
|
|
order.paymentDetail = req.body.payload.payment ;
|
|
|
|
//making the paymentSuccess as true
|
|
|
|
order.paymentSuccess = true ;
|
|
|
|
|
|
|
|
//now getting the details of user which has bought this course
|
|
|
|
const userId = order.user ;
|
|
|
|
let user = await User.findById(userId) ;
|
|
|
|
let student =await Student.findById(user.student) ;
|
|
|
|
|
|
|
|
let ind = -1 ;
|
|
|
|
//checking whether user has already bought this course or not
|
2021-04-12 04:42:57 -07:00
|
|
|
if(student.courses)
|
2021-04-10 00:47:40 -07:00
|
|
|
{
|
2021-04-12 04:42:57 -07:00
|
|
|
ind = student.courses.findIndex(course => String(course.basicInfo) == String(courseId)) ;
|
2021-04-10 00:47:40 -07:00
|
|
|
}
|
|
|
|
console.log(ind);
|
|
|
|
if(ind == -1)
|
|
|
|
{
|
|
|
|
//adding a new object in the courses array of student
|
|
|
|
let newObj = {
|
|
|
|
basicInfo : courseId ,
|
|
|
|
details : {
|
|
|
|
completed : []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let courses = [...student.courses] ;
|
|
|
|
courses.push(newObj) ;
|
|
|
|
student.courses = courses ;
|
|
|
|
await student.save() ;
|
|
|
|
await order.save() ;
|
|
|
|
res.status(200).json({
|
|
|
|
status : 'ok'
|
|
|
|
}) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//user has already bought this course
|
2021-04-12 04:42:57 -07:00
|
|
|
res.status(200).json({
|
2021-04-10 00:47:40 -07:00
|
|
|
error:'already paid'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//now updating that course in user also
|
|
|
|
//console.log(req.body.payload.payment) ;
|
|
|
|
//require('fs').writeFileSync('payment1.json', JSON.stringify(req.body, null, 4))
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// pass it
|
|
|
|
console.log("HELLO IN ERROR");
|
2021-04-12 04:42:57 -07:00
|
|
|
res.status(200).json({
|
2021-04-10 00:47:40 -07:00
|
|
|
error:'error'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(err)
|
|
|
|
{
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} ;
|
|
|
|
|
|
|
|
module.exports.postRazorpay = async (req , res , next) => {
|
|
|
|
try
|
|
|
|
{
|
2021-04-12 04:42:57 -07:00
|
|
|
|
2021-04-10 00:47:40 -07:00
|
|
|
//this route will be called after clicking the payment
|
|
|
|
|
|
|
|
//here this is the id of courseType
|
2021-04-12 04:42:57 -07:00
|
|
|
const courseId = req.body.courseId || "6074201a4f355c3e4830dfe0" ;
|
2021-04-10 00:47:40 -07:00
|
|
|
const userId = req.user._id ;
|
|
|
|
let user = await User.findById(userId) ;
|
2021-04-12 04:42:57 -07:00
|
|
|
let student =await Student.findById(user.student) ;
|
2021-04-10 00:47:40 -07:00
|
|
|
let course =await CourseType.findById(courseId) ;
|
2021-04-12 04:42:57 -07:00
|
|
|
|
|
|
|
console.log(student.courses);
|
2021-04-10 00:47:40 -07:00
|
|
|
//we are creating a new order associated to it
|
|
|
|
//we are setting the paymentSuccess to be false
|
|
|
|
|
|
|
|
|
2021-04-12 04:42:57 -07:00
|
|
|
let ind = -1 ;
|
|
|
|
//checking whether user has already bought this course or not
|
|
|
|
if(student.courses)
|
|
|
|
{
|
|
|
|
ind = student.courses.findIndex(course => String(course.basicInfo) == String(courseId)) ;
|
|
|
|
}
|
|
|
|
console.log(ind);
|
|
|
|
if(ind == -1)
|
|
|
|
{
|
|
|
|
let order = new Order({
|
|
|
|
course : courseId ,
|
|
|
|
user : userId ,
|
|
|
|
paymentSuccess : false
|
|
|
|
}) ;
|
2021-04-10 00:47:40 -07:00
|
|
|
|
2021-04-12 04:42:57 -07:00
|
|
|
order = await order.save() ;
|
2021-04-10 00:47:40 -07:00
|
|
|
|
2021-04-12 04:42:57 -07:00
|
|
|
const payment_capture = 1 ;
|
|
|
|
|
|
|
|
//setting the amount according to the course
|
|
|
|
const amount = Number(course.amount) ;
|
|
|
|
|
|
|
|
const options = {amount : (amount*100).toString()
|
|
|
|
, currency : "INR"
|
|
|
|
, receipt : order._id.toString()
|
|
|
|
, payment_capture
|
|
|
|
} ;
|
2021-04-10 00:47:40 -07:00
|
|
|
|
2021-04-12 04:42:57 -07:00
|
|
|
//this is a razorpay feature
|
|
|
|
const response = await instance.orders.create(options) ;
|
|
|
|
|
|
|
|
order.orderId = response.id ;
|
|
|
|
await order.save() ;
|
|
|
|
res.status(200).json({
|
|
|
|
id:response.id ,
|
|
|
|
currency : response.currency ,
|
|
|
|
amount : response.amount ,
|
|
|
|
name:(user.firstName + " " + user.lastName) ,
|
|
|
|
email:user.email ,
|
|
|
|
receipt : response.receipt
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//user has already bought this course
|
|
|
|
res.status(500).json({
|
|
|
|
error:'already paid'
|
|
|
|
})
|
|
|
|
}
|
2021-04-10 00:47:40 -07:00
|
|
|
}
|
|
|
|
catch(err)
|
|
|
|
{
|
|
|
|
console.log(err);
|
2021-04-12 04:42:57 -07:00
|
|
|
res.status(500).json({
|
2021-04-10 00:47:40 -07:00
|
|
|
error : "error"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|