Skip to content
Open
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
45 changes: 31 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"chart.js": "^4.3.0",
"concurrently": "^8.0.1",
"copy-to-clipboard": "^3.3.3",
"razorpay": "^2.9.6",
"react": "^18.2.0",
"react-chartjs-2": "^5.2.0",
"react-countup": "^6.5.3",
Expand Down
6 changes: 6 additions & 0 deletions server/config/razorpay.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const Razorpay = require("razorpay");

if (!process.env.RAZORPAY_KEY || !process.env.RAZORPAY_SECRET) {
throw new Error("Razorpay credentials are not properly configured");
}

exports.instance = new Razorpay({
key_id: process.env.RAZORPAY_KEY,
key_secret: process.env.RAZORPAY_SECRET,
headers: {
'Content-Type': 'application/json'
}
});

88 changes: 50 additions & 38 deletions server/controllers/Payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,68 @@ const CourseProgress = require("../models/CourseProgress");

//initiate the razorpay order
exports.capturePayment = async(req, res) => {
try {
const {courses} = req.body;
const userId = req.user.id;

const {courses} = req.body;
const userId = req.user.id;
if(!courses || courses.length === 0) {
return res.status(400).json({success:false, message:"Please provide Course Id"});
}

if(courses.length === 0) {
return res.json({success:false, message:"Please provide Course Id"});
}
let totalAmount = 0;

let totalAmount = 0;
for(const course_id of courses) {
let course;
try {
course = await Course.findById(course_id);
if(!course) {
return res.status(404).json({success:false, message:"Could not find the course"});
}

for(const course_id of courses) {
let course;
try{

course = await Course.findById(course_id);
if(!course) {
return res.status(200).json({success:false, message:"Could not find the course"});
}
const uid = new mongoose.Types.ObjectId(userId);
if(course.studentsEnrolled.includes(uid)) {
return res.status(400).json({success:false, message:"Student is already Enrolled"});
}

const uid = new mongoose.Types.ObjectId(userId);
if(course.studentsEnrolled.includes(uid)) {
return res.status(200).json({success:false, message:"Student is already Enrolled"});
totalAmount += course.price;
}
catch(error) {
console.error("Error finding course:", error);
return res.status(500).json({success:false, message:"Error processing course details"});
}

totalAmount += course.price;
}
catch(error) {
console.log(error);
return res.status(500).json({success:false, message:error.message});
}
}
const currency = "INR";
const options = {
amount: totalAmount * 100,
currency,
receipt: Math.random(Date.now()).toString(),
}

try{
const currency = "INR";
const options = {
amount: Math.round(totalAmount * 100), // Ensure amount is a whole number
currency,
receipt: `receipt_${Date.now()}`,
notes: {
userId: userId,
courses: courses
}
};

const paymentResponse = await instance.orders.create(options);
res.json({
success:true,
message:paymentResponse,
})

// Return the response in the expected format
return res.status(200).json({
success: true,
data: {
orderId: paymentResponse.id,
amount: paymentResponse.amount,
currency: paymentResponse.currency,
key: process.env.RAZORPAY_KEY
}
});
}
catch(error) {
console.log(error);
return res.status(500).json({success:false, mesage:"Could not Initiate Order"});
console.error("Payment initiation error:", error);
return res.status(500).json({
success: false,
message: error.message || "Could not initiate payment"
});
}

}


Expand Down
10 changes: 7 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ database.connect();
//middlewares
app.use(express.json());
app.use(cookieParser());
app.use(cors());
app.use(
cors({
origin: process.env.CORS_ORIGIN || "http://localhost:3000",
credentials: true,
})
);

app.use(
fileUpload({
Expand Down Expand Up @@ -50,5 +55,4 @@ app.get("/", (req, res) => {

app.listen(PORT, () => {
console.log(`App is running at ${PORT}`)
})

})
3 changes: 1 addition & 2 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ body {
overflow-x: hidden;
}

/* ===== Scrollbar CSS ===== */
/* Firefox */

* {
scrollbar-width: auto;
scrollbar-color: #afb2bf;
Expand Down
10 changes: 5 additions & 5 deletions src/services/operations/studentFeaturesAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export async function buyCourse(token, courses, userDetails, navigate, dispatch)
console.log("PRINTING orderResponse", orderResponse);
//options
const options = {
key: process.env.RAZORPAY_KEY,
currency: orderResponse.data.message.currency,
amount: `${orderResponse.data.message.amount}`,
order_id:orderResponse.data.message.id,
key: orderResponse.data.data.key,
currency: orderResponse.data.data.currency,
amount: orderResponse.data.data.amount,
order_id: orderResponse.data.data.orderId,
name:"StudyNotion",
description: "Thank You for Purchasing the Course",
image:rzpLogo,
Expand All @@ -61,7 +61,7 @@ export async function buyCourse(token, courses, userDetails, navigate, dispatch)
},
handler: function(response) {
//send successful wala mail
sendPaymentSuccessEmail(response, orderResponse.data.message.amount,token );
sendPaymentSuccessEmail(response, orderResponse.data.data.amount, token);
//verifyPayment
verifyPayment({...response, courses}, token, navigate, dispatch);
}
Expand Down