diff --git a/controllers/payment.js b/controllers/payment.js index eab06f1..7c0210d 100644 --- a/controllers/payment.js +++ b/controllers/payment.js @@ -7,8 +7,8 @@ const Student = require('../models/Student'); //test credentials of razorpay const instance = new razorpay({ - key_id : 'rzp_test_8YMWcm8JJ4fhzp' , - key_secret : 'jP1P03cD4ShN8Fp7Q3T6BXQo' + key_id : 'rzp_test_tLx9c6GiWjKcsl' , + key_secret : '4Cf52d6C3amkptdRLCTdC2sT' }) ; module.exports.postVerify =async (req , res , next) => { @@ -44,9 +44,9 @@ module.exports.postVerify =async (req , res , next) => { let ind = -1 ; //checking whether user has already bought this course or not - if(student.courses.basicInfo) + if(student.courses) { - ind = student.courses.basicInfo.findIndex(course => String(course) == String(courseId)) ; + ind = student.courses.findIndex(course => String(course.basicInfo) == String(courseId)) ; } console.log(ind); if(ind == -1) @@ -70,7 +70,7 @@ module.exports.postVerify =async (req , res , next) => { else { //user has already bought this course - res.status(500).json({ + res.status(200).json({ error:'already paid' }) } @@ -83,7 +83,7 @@ module.exports.postVerify =async (req , res , next) => { else { // pass it console.log("HELLO IN ERROR"); - res.status(500).json({ + res.status(200).json({ error:'error' }) } @@ -99,53 +99,75 @@ module.exports.postVerify =async (req , res , next) => { 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 courseId = req.body.courseId || "6074201a4f355c3e4830dfe0" ; const userId = req.user._id ; let user = await User.findById(userId) ; + let student =await Student.findById(user.student) ; let course =await CourseType.findById(courseId) ; - + + console.log(student.courses); //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) ; + 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 + }) ; - const options = {amount : (amount*100).toString() - , currency : "INR" - , receipt : order._id.toString() - , payment_capture - } ; + order = await order.save() ; - //this is a razorpay feature - const response = await instance.orders.create(options) ; + const payment_capture = 1 ; + + //setting the amount according to the course + const amount = Number(course.amount) ; - 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 - }); + 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.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' + }) + } } catch(err) { console.log(err); - res.json({ + res.status(500).json({ error : "error" }) }