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({ key_id : 'rzp_test_8YMWcm8JJ4fhzp' , key_secret : 'jP1P03cD4ShN8Fp7Q3T6BXQo' }) ; 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 if(student.courses.basicInfo) { ind = student.courses.basicInfo.findIndex(course => String(course) == String(courseId)) ; } 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 res.status(500).json({ 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"); res.status(500).json({ error:'error' }) } } catch(err) { console.log(err); } } ; module.exports.postRazorpay = async (req , res , next) => { try { //this route will be called after clicking the payment //here this is the id of courseType const courseId = req.body.courseId || "607142ec6de6df1a31bf509b" ; const userId = req.user._id ; let user = await User.findById(userId) ; let course =await CourseType.findById(courseId) ; //we are creating a new order associated to it //we are setting the paymentSuccess to be false let order = new Order({ course : courseId , user : userId , paymentSuccess : false }) ; order = await order.save() ; 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 } ; //this is a razorpay feature const response = await instance.orders.create(options) ; order.orderId = response.id ; await order.save() ; res.json({ id:response.id , currency : response.currency , amount : response.amount , name:(user.firstName + " " + user.lastName) , email:user.email , receipt : response.receipt }); } catch(err) { console.log(err); res.json({ error : "error" }) } }