studentFeaturesapi
studentFeaturesapi
/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";
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);
})
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);
}
} catch(error) {
console.log("Payment Success email Error", error);
}
}
}
toast.dismiss(toastId);
dispatch(setPaymentLoading(false))