103 lines
2.6 KiB
JavaScript
103 lines
2.6 KiB
JavaScript
frappe.ui.form.on("Sales Invoice", {
|
|
refresh(frm) {
|
|
frm.clear_custom_buttons();
|
|
|
|
// Submitted invoices only
|
|
if (frm.doc.docstatus !== 1) return;
|
|
if (!frm.doc.customer) return;
|
|
|
|
// Only show manual payment button if AutoPay is OFF
|
|
frappe.db.get_value(
|
|
"Customer",
|
|
frm.doc.customer,
|
|
"auto_pay",
|
|
(r) => {
|
|
if (!r) return;
|
|
|
|
if (!r.auto_pay) {
|
|
frm.add_custom_button(
|
|
"Run Payment",
|
|
() => run_payment_flow(frm),
|
|
"Actions"
|
|
);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
});
|
|
|
|
|
|
function run_payment_flow(frm) {
|
|
frappe.call({
|
|
method: "ns_app.api.payments.check_autopay",
|
|
args: {
|
|
customer: frm.doc.customer
|
|
},
|
|
callback(r) {
|
|
if (!r.message) return;
|
|
|
|
if (r.message.autopay_enabled) {
|
|
run_autopay(frm);
|
|
} else {
|
|
open_manual_payment_form(frm);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
function run_autopay(frm) {
|
|
frappe.confirm(
|
|
`Run AutoPay for $${frm.doc.outstanding_amount}?`,
|
|
() => {
|
|
frappe.call({
|
|
method: "ns_app.api.payments.run_autopay_payment",
|
|
args: {
|
|
invoice: frm.doc.name
|
|
},
|
|
callback(r) {
|
|
frappe.msgprint(r.message || "Payment processed");
|
|
frm.reload_doc();
|
|
}
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
// Hosted checkout
|
|
function open_manual_payment_form(frm) {
|
|
frappe.call({
|
|
method: "ns_app.api.payments.get_collect_checkout_url",
|
|
args: {
|
|
invoice: frm.doc.name
|
|
},
|
|
callback(r) {
|
|
if (!r.message) {
|
|
frappe.msgprint("Unable to start payment");
|
|
return;
|
|
}
|
|
|
|
const dialog = new frappe.ui.Dialog({
|
|
title: "Secure Payment",
|
|
size: "large",
|
|
fields: [
|
|
{
|
|
fieldtype: "HTML",
|
|
fieldname: "payment_form",
|
|
options: `
|
|
<iframe
|
|
src="${r.message}"
|
|
style="width:100%; height:520px; border:none;"
|
|
sandbox="allow-forms allow-scripts allow-same-origin"
|
|
></iframe>
|
|
`
|
|
}
|
|
]
|
|
});
|
|
|
|
dialog.show();
|
|
}
|
|
});
|
|
}
|