Added and updated autopay logic
This commit is contained in:
@@ -13,7 +13,7 @@ frappe.ready(() => {
|
||||
doctype,
|
||||
after_insert,
|
||||
function (doc) {
|
||||
// Defaults
|
||||
// Safe defaults
|
||||
doc.customer_group = "Commercial";
|
||||
doc.territory = "United States";
|
||||
|
||||
@@ -25,50 +25,60 @@ frappe.ready(() => {
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
frappe.ui.form.on("Customer", {
|
||||
onload(frm) {
|
||||
if (!frm.is_quick_entry) return;
|
||||
|
||||
console.log("NS App: Enhancing Customer Quick Entry");
|
||||
|
||||
// Hide defaults you don't want
|
||||
// Hide fields you don't want exposed
|
||||
frm.toggle_display("customer_group", false);
|
||||
|
||||
// Required fields
|
||||
// Make required
|
||||
frm.set_df_property("mobile_no", "reqd", 1);
|
||||
|
||||
// Defaults
|
||||
// Default territory
|
||||
frm.set_value("territory", "United States");
|
||||
|
||||
// Add Company fields to Quick Entry
|
||||
frm.add_custom_field({
|
||||
fieldname: "company_name",
|
||||
label: "Company Name",
|
||||
fieldtype: "Data",
|
||||
insert_after: "customer_name",
|
||||
reqd: 1
|
||||
});
|
||||
/**
|
||||
* IMPORTANT:
|
||||
* "company_name" MUST already exist as a Custom Field in erpnext
|
||||
* (Customer → Custom Fields)
|
||||
*/
|
||||
frm.toggle_display("company_name", true);
|
||||
|
||||
// Company name is OPTIONAL by default
|
||||
frm.set_df_property("company_name", "reqd", 0);
|
||||
},
|
||||
|
||||
// ZIP auto-fill
|
||||
/**
|
||||
* ZIP auto-fill
|
||||
* NOTE: This only works if pincode/city/state/country
|
||||
* are available in Quick Entry (customized Address block)
|
||||
*/
|
||||
pincode(frm) {
|
||||
if (!frm.is_quick_entry) return;
|
||||
|
||||
const zip = frm.doc.pincode;
|
||||
if (!zip || zip.length < 5) return;
|
||||
|
||||
// Only US ZIPs
|
||||
if (frm.doc.country && frm.doc.country !== "United States") return;
|
||||
|
||||
fetch(`https://api.zippopotam.us/us/${zip}`)
|
||||
.then(res => res.ok ? res.json() : null)
|
||||
.then(data => {
|
||||
if (!data?.places?.length) return;
|
||||
if (!data || !data.places || !data.places.length) return;
|
||||
|
||||
const place = data.places[0];
|
||||
|
||||
frm.set_value("city", place["place name"]);
|
||||
frm.set_value("state", place["state"]);
|
||||
frm.set_value("country", data.country);
|
||||
})
|
||||
.catch(() => {});
|
||||
.catch(() => {
|
||||
// Fail silently (never block entry)
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
frappe.ui.form.on("Sales Invoice", {
|
||||
refresh(frm) {
|
||||
if (frm.doc.docstatus !== 1) return; // submitted only
|
||||
frm.clear_custom_buttons();
|
||||
|
||||
frm.add_custom_button("Run Payment", () => {
|
||||
run_payment_flow(frm);
|
||||
});
|
||||
// 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",
|
||||
@@ -18,24 +37,23 @@ function run_payment_flow(frm) {
|
||||
if (!r.message) return;
|
||||
|
||||
if (r.message.autopay_enabled) {
|
||||
run_autopay(frm, r.message.autopay_id);
|
||||
run_autopay(frm);
|
||||
} else {
|
||||
open_manual_payment_form(frm);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function run_autopay(frm, autopay_id) {
|
||||
function run_autopay(frm) {
|
||||
frappe.confirm(
|
||||
`Run AutoPay for $${frm.doc.grand_total}?`,
|
||||
`Run AutoPay for $${frm.doc.outstanding_amount}?`,
|
||||
() => {
|
||||
frappe.call({
|
||||
method: "ns_app.api.payments.run_autopay_payment",
|
||||
args: {
|
||||
invoice: frm.doc.name,
|
||||
autopay_id: autopay_id,
|
||||
amount: frm.doc.grand_total
|
||||
invoice: frm.doc.name
|
||||
},
|
||||
callback(r) {
|
||||
frappe.msgprint(r.message || "Payment processed");
|
||||
@@ -46,23 +64,39 @@ function run_autopay(frm, autopay_id) {
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: Source needs updated to a correct url.
|
||||
|
||||
// Hosted checkout
|
||||
function open_manual_payment_form(frm) {
|
||||
const dialog = new frappe.ui.Dialog({
|
||||
title: "Enter Payment",
|
||||
fields: [
|
||||
{
|
||||
fieldtype: "HTML",
|
||||
fieldname: "payment_form",
|
||||
options: `<iframe
|
||||
src="https://payments.provider.com/pay?invoice=${frm.doc.name}&amount=${frm.doc.grand_total}"
|
||||
style="width:100%;height:500px;border:none;"
|
||||
></iframe>`
|
||||
}
|
||||
]
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user