diff --git a/lms/lms/doctype/lms_course/lms_course.py b/lms/lms/doctype/lms_course/lms_course.py index 057ab74c..d40a9adc 100644 --- a/lms/lms/doctype/lms_course/lms_course.py +++ b/lms/lms/doctype/lms_course/lms_course.py @@ -9,6 +9,8 @@ from frappe.utils import cint from frappe.utils.telemetry import capture from lms.lms.utils import get_chapters, can_create_courses from ...utils import generate_slug, validate_image +from frappe import _ +import razorpay class LMSCourse(Document): @@ -354,3 +356,44 @@ def reorder_chapter(chapter_array): "idx": chapter_array.index(chap) + 1, }, ) + + +@frappe.whitelist() +def buy_course(course): + razorpay_key = frappe.get_single_value("LMS Settings", "razorpay_key") + razorpay_secret = frappe.get_single_value("LMS Settings", "razorpay_secret") + + if not razorpay_key and not razorpay_secret: + frappe.throw( + _( + "There is a problem with the payment gateway. Please contact the Administrator to proceed." + ) + ) + + create_payment_link(razorpay_key, razorpay_secret, course) + + +def create_payment_link(razorpy_key, razorpay_secret, course): + client = razorpay.Client(auth=(razorpy_key, razorpay_secret)) + + course_details = frappe.db.get_value( + "LMS Course", course, ["title", "price_course", "currency"], as_dict=True + ) + user_details = frappe.db.get_value( + "User", frappe.session.user, ["full_name", "email"], as_dict=True + ) + + client.payment_link.create( + { + "amount": course_details.price_course, + "currency": course_details.currency, + "description": "Complete your course purchase", + "customer": { + "name": user_details.full_name, + "email": user_details.email, + }, + "notify": {"sms": True, "email": True}, + "callback_url": "/api/method/lms.lms.doctype.lms_course.lms_course.verify_payment", + "callback_method": "get", + } + ) diff --git a/lms/lms/doctype/lms_settings/lms_settings.json b/lms/lms/doctype/lms_settings/lms_settings.json index 132cfb5d..4c8b4d35 100644 --- a/lms/lms/doctype/lms_settings/lms_settings.json +++ b/lms/lms/doctype/lms_settings/lms_settings.json @@ -19,8 +19,10 @@ "column_break_2", "allow_student_progress", "payment_section", + "razorpay_key", "default_currency", "column_break_cfcv", + "razorpay_secret", "signup_settings_tab", "signup_settings_section", "terms_of_use", @@ -211,12 +213,22 @@ { "fieldname": "column_break_iqxy", "fieldtype": "Column Break" + }, + { + "fieldname": "razorpay_key", + "fieldtype": "Data", + "label": "Razorpay Key" + }, + { + "fieldname": "razorpay_secret", + "fieldtype": "Password", + "label": "Razorpay Secret" } ], "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2023-08-02 12:30:50.897156", + "modified": "2023-08-02 18:59:01.267732", "modified_by": "Administrator", "module": "LMS", "name": "LMS Settings", diff --git a/lms/www/courses/course.html b/lms/www/courses/course.html index c5ee4d8d..6035ea88 100644 --- a/lms/www/courses/course.html +++ b/lms/www/courses/course.html @@ -222,9 +222,9 @@ {% elif course.paid_course %} - +
{{ _("Buy This Course") }} - +
{% elif show_start_learing_cta(course, membership) %}
diff --git a/lms/www/courses/course.js b/lms/www/courses/course.js index 9196bd44..1f78b87b 100644 --- a/lms/www/courses/course.js +++ b/lms/www/courses/course.js @@ -21,24 +21,17 @@ frappe.ready(() => { submit_for_review(e); }); - $("#apply-certificate").click((e) => { - apply_cetificate(e); - }); - - $("#slot-date").on("change", (e) => { - display_slots(e); - }); - - $("#submit-slot").click((e) => { - submit_slot(e); - }); - - $(".close-slot-modal").click((e) => { - close_slot_modal(e); - }); - - $(document).on("click", ".slot", (e) => { - select_slot(e); + $("#buy-course").click((e) => { + e.preventDefault(); + frappe.call({ + method: "lms.lms.doctype.lms_course.lms_course.buy_course", + args: { + course: decodeURIComponent( + $(e.currentTarget).attr("data-course") + ), + }, + callback: (data) => {}, + }); }); }); @@ -162,99 +155,3 @@ const submit_for_review = (e) => { }, }); }; - -const apply_cetificate = (e) => { - $("#slot-modal").modal("show"); -}; - -const submit_slot = (e) => { - e.preventDefault(); - const slot = window.selected_slot; - frappe.call({ - method: "lms.lms.doctype.lms_certificate_request.lms_certificate_request.create_certificate_request", - args: { - course: slot.data("course"), - date: $("#slot-date").val(), - day: slot.data("day"), - start_time: slot.data("start"), - end_time: slot.data("end"), - }, - callback: (data) => { - $("#slot-modal").modal("hide"); - frappe.show_alert( - { - message: __( - "Your slot has been booked. Prepare well for the evaluations." - ), - indicator: "green", - }, - 3 - ); - setTimeout(() => { - window.location.reload(); - }, 3000); - }, - }); -}; - -const display_slots = (e) => { - frappe.call({ - method: "lms.lms.doctype.course_evaluator.course_evaluator.get_schedule", - args: { - course: $(e.currentTarget).data("course"), - date: $(e.currentTarget).val(), - }, - callback: (data) => { - let options = ""; - data.message.forEach((obj) => { - options += ``; - }); - e.preventDefault(); - $("#slot-modal .slots").html(options); - const weekday = [ - "Sunday", - "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - "Saturday", - ]; - const day = weekday[new Date($(e.currentTarget).val()).getDay()]; - - $(".slot").addClass("hide"); - $(".slot-label").addClass("hide"); - - if ($(`[data-day='${day}']`).length) { - $(".slot-label").removeClass("hide"); - $(`[data-day='${day}']`).removeClass("hide"); - $("#no-slots-message").addClass("hide"); - } else { - $("#no-slots-message").removeClass("hide"); - } - }, - }); -}; - -const select_slot = (e) => { - $(".slot").removeClass("btn-outline-primary"); - $(e.currentTarget).addClass("btn-outline-primary"); - window.selected_slot = $(e.currentTarget); -}; - -const format_time = (time) => { - let date = moment(new Date()).format("ddd MMM DD YYYY"); - return moment(`${date} ${time}`).format("HH:mm a"); -}; - -const close_slot_modal = (e) => { - $("#slot-date").val(""); - $(".slot-label").addClass("hide"); -};