fix: payment gateway fields

This commit is contained in:
Jannat Patel
2024-10-01 15:17:17 +05:30
parent 8a17dca351
commit be870e8145
4 changed files with 68 additions and 111 deletions

View File

@@ -2,6 +2,28 @@
// For license information, please see license.txt // For license information, please see license.txt
frappe.ui.form.on("LMS Settings", { frappe.ui.form.on("LMS Settings", {
// refresh: function(frm) { setup: function (frm) {
// } frappe.call({
method: "lms.lms.doctype.lms_settings.lms_settings.check_payments_app",
callback: (data) => {
if (!data.message) {
frm.set_df_property("payment_section", "hidden", 1);
frm.trigger("set_no_payments_app_html");
} else {
frm.set_df_property("no_payments_app", "hidden", 1);
}
},
});
},
set_no_payments_app_html(frm) {
frm.get_field("payments_app_is_not_installed").html(`
<div class="alert alert-warning">
Please install the
<a target="_blank" style="color: var(--alert-text-warning); background: var(--alert-bg-warning);" href="https://frappecloud.com/marketplace/apps/payments">
Payments app
</a>
to enable payment gateway.
`);
},
}); });

View File

@@ -49,6 +49,8 @@
"apply_gst", "apply_gst",
"show_usd_equivalent", "show_usd_equivalent",
"apply_rounding", "apply_rounding",
"no_payments_app",
"payments_app_is_not_installed",
"email_templates_tab", "email_templates_tab",
"certification_template", "certification_template",
"batch_confirmation_template", "batch_confirmation_template",
@@ -323,15 +325,23 @@
}, },
{ {
"fieldname": "payment_gateway", "fieldname": "payment_gateway",
"fieldtype": "Link", "fieldtype": "Data",
"label": "Payment Gateway", "label": "Payment Gateway"
"options": "Payment Gateway" },
{
"fieldname": "no_payments_app",
"fieldtype": "Section Break"
},
{
"fieldname": "payments_app_is_not_installed",
"fieldtype": "HTML",
"label": "Payments app is not installed"
} }
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"issingle": 1, "issingle": 1,
"links": [], "links": [],
"modified": "2024-09-30 18:30:28.689084", "modified": "2024-10-01 12:15:49.800242",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "LMS", "module": "LMS",
"name": "LMS Settings", "name": "LMS Settings",

View File

@@ -39,3 +39,33 @@ class LMSSettings(Document):
frappe.bold("Course Evaluator"), frappe.bold("Course Evaluator"),
) )
) )
@frappe.whitelist()
def check_payments_app():
installed_apps = frappe.get_installed_apps()
print("payments" not in installed_apps)
if "payments" not in installed_apps:
return False
else:
filters = {
"doctype_or_field": "DocField",
"doc_type": "LMS Settings",
"field_name": "payment_gateway",
}
if frappe.db.exists("Property Setter", filters):
return True
link_property = frappe.new_doc("Property Setter")
link_property.update(filters)
link_property.property = "fieldtype"
link_property.value = "Link"
link_property.save()
options_property = frappe.new_doc("Property Setter")
options_property.update(filters)
options_property.property = "options"
options_property.value = "Payment Gateway"
options_property.save()
return True

View File

@@ -908,39 +908,6 @@ def get_upcoming_evals(student, courses):
return upcoming_evals return upcoming_evals
@frappe.whitelist()
def get_payment_options(doctype, docname, phone, country):
if not frappe.db.exists(doctype, docname):
frappe.throw(_("Invalid document provided."))
validate_phone_number(phone, True)
details = get_details(doctype, docname)
details.amount, details.currency = check_multicurrency(
details.amount, details.currency, country, details.amount_usd
)
if details.currency == "INR":
details.amount, details.gst_applied = apply_gst(details.amount, country)
client = get_client()
order = create_order(client, details.amount, details.currency)
options = {
"key_id": frappe.db.get_single_value("LMS Settings", "razorpay_key"),
"name": frappe.db.get_single_value("Website Settings", "app_name"),
"description": _("Payment for {0} course").format(details["title"]),
"order_id": order["id"],
"amount": cint(order["amount"]) * 100,
"currency": order["currency"],
"prefill": {
"name": frappe.db.get_value("User", frappe.session.user, "full_name"),
"email": frappe.session.user,
"contact": phone,
},
}
return options
def check_multicurrency(amount, currency, country=None, amount_usd=None): def check_multicurrency(amount, currency, country=None, amount_usd=None):
settings = frappe.get_single("LMS Settings") settings = frappe.get_single("LMS Settings")
show_usd_equivalent = settings.show_usd_equivalent show_usd_equivalent = settings.show_usd_equivalent
@@ -998,78 +965,6 @@ def apply_gst(amount, country=None):
return amount, gst_applied return amount, gst_applied
def get_details(doctype, docname):
if doctype == "LMS Course":
details = frappe.db.get_value(
"LMS Course",
docname,
["name", "title", "paid_course", "currency", "course_price as amount", "amount_usd"],
as_dict=True,
)
if not details.paid_course:
frappe.throw(_("This course is free."))
else:
details = frappe.db.get_value(
"LMS Batch",
docname,
["name", "title", "paid_batch", "currency", "amount", "amount_usd"],
as_dict=True,
)
if not details.paid_batch:
frappe.throw(_("To join this batch, please contact the Administrator."))
return details
def get_client():
settings = frappe.get_single("LMS Settings")
razorpay_key = settings.razorpay_key
razorpay_secret = settings.get_password("razorpay_secret", raise_exception=True)
if not razorpay_key and not razorpay_secret:
frappe.throw(
_(
"There is a problem with the payment gateway. Please contact the Administrator to proceed."
)
)
return razorpay.Client(auth=(razorpay_key, razorpay_secret))
def create_order(client, amount, currency):
try:
return client.order.create(
{
"amount": cint(amount) * 100,
"currency": currency,
}
)
except Exception as e:
frappe.throw(
_(
"Error during payment: {0} Please contact the Administrator. Amount {1} Currency {2} Formatted {3}"
).format(e, amount, currency, cint(amount))
)
def get_payment_details(doctype, docname, address):
amount_field = "course_price" if doctype == "LMS Course" else "amount"
amount = frappe.db.get_value(doctype, docname, amount_field)
currency = frappe.db.get_value(doctype, docname, "currency")
amount_usd = frappe.db.get_value(doctype, docname, "amount_usd")
amount_with_gst = 0
amount, currency = check_multicurrency(amount, currency, None, amount_usd)
if currency == "INR" and address.country == "India":
amount_with_gst, gst_applied = apply_gst(amount, address.country)
return {
"amount": amount,
"currency": currency,
"amount_with_gst": amount_with_gst,
}
def create_membership(course, payment): def create_membership(course, payment):
membership = frappe.new_doc("LMS Enrollment") membership = frappe.new_doc("LMS Enrollment")
membership.update( membership.update(