razorpay bugs corrected

This commit is contained in:
hardcodder 2021-04-12 17:12:57 +05:30
parent f940193d17
commit 870a222650
1 changed files with 58 additions and 36 deletions

View File

@ -7,8 +7,8 @@ const Student = require('../models/Student');
//test credentials of razorpay //test credentials of razorpay
const instance = new razorpay({ const instance = new razorpay({
key_id : 'rzp_test_8YMWcm8JJ4fhzp' , key_id : 'rzp_test_tLx9c6GiWjKcsl' ,
key_secret : 'jP1P03cD4ShN8Fp7Q3T6BXQo' key_secret : '4Cf52d6C3amkptdRLCTdC2sT'
}) ; }) ;
module.exports.postVerify =async (req , res , next) => { module.exports.postVerify =async (req , res , next) => {
@ -44,9 +44,9 @@ module.exports.postVerify =async (req , res , next) => {
let ind = -1 ; let ind = -1 ;
//checking whether user has already bought this course or not //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); console.log(ind);
if(ind == -1) if(ind == -1)
@ -70,7 +70,7 @@ module.exports.postVerify =async (req , res , next) => {
else else
{ {
//user has already bought this course //user has already bought this course
res.status(500).json({ res.status(200).json({
error:'already paid' error:'already paid'
}) })
} }
@ -83,7 +83,7 @@ module.exports.postVerify =async (req , res , next) => {
else { else {
// pass it // pass it
console.log("HELLO IN ERROR"); console.log("HELLO IN ERROR");
res.status(500).json({ res.status(200).json({
error:'error' error:'error'
}) })
} }
@ -99,53 +99,75 @@ module.exports.postVerify =async (req , res , next) => {
module.exports.postRazorpay = async (req , res , next) => { module.exports.postRazorpay = async (req , res , next) => {
try try
{ {
//this route will be called after clicking the payment //this route will be called after clicking the payment
//here this is the id of courseType //here this is the id of courseType
const courseId = req.body.courseId || "607142ec6de6df1a31bf509b" ; const courseId = req.body.courseId || "6074201a4f355c3e4830dfe0" ;
const userId = req.user._id ; const userId = req.user._id ;
let user = await User.findById(userId) ; let user = await User.findById(userId) ;
let student =await Student.findById(user.student) ;
let course =await CourseType.findById(courseId) ; let course =await CourseType.findById(courseId) ;
console.log(student.courses);
//we are creating a new order associated to it //we are creating a new order associated to it
//we are setting the paymentSuccess to be false //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 ; 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
}) ;
//setting the amount according to the course order = await order.save() ;
const amount = Number(course.amount) ;
const options = {amount : (amount*100).toString() const payment_capture = 1 ;
, currency : "INR"
, receipt : order._id.toString()
, payment_capture
} ;
//this is a razorpay feature //setting the amount according to the course
const response = await instance.orders.create(options) ; const amount = Number(course.amount) ;
order.orderId = response.id ; const options = {amount : (amount*100).toString()
await order.save() ; , currency : "INR"
res.json({ , receipt : order._id.toString()
id:response.id , , payment_capture
currency : response.currency , } ;
amount : response.amount ,
name:(user.firstName + " " + user.lastName) , //this is a razorpay feature
email:user.email , const response = await instance.orders.create(options) ;
receipt : response.receipt
}); 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) catch(err)
{ {
console.log(err); console.log(err);
res.json({ res.status(500).json({
error : "error" error : "error"
}) })
} }