0% found this document useful (0 votes)
116 views

studentFeaturesapi

The document outlines a JavaScript function for processing course payments using the Razorpay payment gateway. It includes functionalities for loading the Razorpay script, initiating payment, sending a success email, and verifying the payment. The code also handles errors and updates the application state using Redux for managing payment loading and cart reset actions.

Uploaded by

damafa7880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

studentFeaturesapi

The document outlines a JavaScript function for processing course payments using the Razorpay payment gateway. It includes functionalities for loading the Razorpay script, initiating payment, sending a success email, and verifying the payment. The code also handles errors and updates the application state using Redux for managing payment loading and cart reset actions.

Uploaded by

damafa7880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import { apiConnector } from "..

/apiconnector";
import { studentEndpoints } from "../apis";
import toast from "react-hot-toast";
import rzplogo from "../../assets/Logo/rzp_logo.png"
import { setPaymentLoading } from "../../slices/courseSlice";
import { resetCart } from "../../slices/cartSlice";
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router-dom";
// import { toast } from "react-hot-toast";
// import { studentEndpoints } from "../apis";
// import { apiConnector } from "../apiconnector";

const {COURSE_PAYMENT_API, COURSE_VERIFY_API, SEND_PAYMENT_SUCCESS_EMAIL_API } =


studentEndpoints;

function loadScript(src) {
return new Promise ((resolve) => {
const script = document.createElement("Script");
script.src = src;

script.onload = () => {
resolve(true);
}
script.onerror = () => {
resolve(false);
}
document.body.appendChild(script);

})

export async function buyCourse(token, courses, userDetails,navigate, dispatch) {


const toastId = toast.loading("Loading...")
try{
// load script

const res = await


loadScript("https://checkout.razorpay.com/v1/checkout.js");

if(!res) {
toast.error("RazorPay SDK failed to load");
return;
}
// initiate the order
const orderResponse = await apiConnector("POST", COURSE_PAYMENT_API,
{courses},
{
Authorization:`Bearer ${token}`
})
if(!orderResponse.data.success) {
throw new Error(orderResponse.data.message);

}
const options = {
key:process.env.RAZORPAY_KEY,
currency:orderResponse.data.message.currency,
amount:`${orderResponse.data.message.amount}`,
order_id:orderResponse.data.message.id,
name:"StudyNotion",
description:"Thank you for payment",
image:rzplogo,
prefill:{
name:`${userDetails.firstName}`,
email:userDetails.email,
},
handler:function(response) {
sendPaymentSuccessEmail(response,
orderResponse.data.message.amount, token);
// verify Payment
verifyPayment({...response, courses}, token, navigate, dispatch);
}
}
const paymentObject = new window.Razorpay(options);
paymentObject.open();
paymentObject.on("payment.failed", function(response){
toast.error("Payment failed");
console.log(response.error)
})

} catch(error) {
console.log("PAYMENT ERROR ....", error);
toast.error("could not make Payment")
}
toast.dismiss(toastId);
}

async function sendPaymentSuccessEmail(response, amount, token) {


try{
await apiConnector("POST", SEND_PAYMENT_SUCCESS_EMAIL_API, {
orderId: response.razorpay_order_id,
paymentId: response.razorpay_payment_id,
amount,
}, {
Authorization:`Bearer ${token}`
})

} catch(error) {
console.log("Payment Success email Error", error);
}
}

async function verifyPayment(bodyData, token, navigate, dispatch) {


const toastId = toast.loading("Verifying Payment...");
dispatch(setPaymentLoading(true));
try{
const response = await apiConnector("POST", COURSE_VERIFY_API,bodyData, {
Authorization:`Bearer ${token}`,
})
if(!response.data.success) {
throw new Error(response.data.message);
}
toast.success("Payment Successful, You're added to the course");
navigate("/dashboard/enrolled-courses");
dispatch(resetCart());
}
catch(error) {
console.log("PAYMENT VERIFY ERROR....",error);
toast.error("Could not verify payment");

}
toast.dismiss(toastId);
dispatch(setPaymentLoading(false))

You might also like