Added and updated autopay logic
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import requests
|
||||
import frappe
|
||||
import requests
|
||||
from frappe.utils import nowdate
|
||||
|
||||
|
||||
# Checks to see if customer has Autopay on
|
||||
@frappe.whitelist()
|
||||
def check_autopay(customer):
|
||||
cust = frappe.get_doc("Customer", customer)
|
||||
@@ -13,27 +13,32 @@ def check_autopay(customer):
|
||||
}
|
||||
|
||||
|
||||
|
||||
# Creates payload to send through API, checks for success, then makes a call to create a payment entry.
|
||||
@frappe.whitelist()
|
||||
def run_autopay_payment(invoice, autopay_id, amount):
|
||||
def run_autopay_payment(invoice):
|
||||
inv = frappe.get_doc("Sales Invoice", invoice)
|
||||
|
||||
if inv.outstanding_amount <= 0:
|
||||
frappe.throw("Invoice is already fully paid")
|
||||
|
||||
cust = frappe.get_doc("Customer", inv.customer)
|
||||
|
||||
if not cust.auto_pay or not cust.auto_pay_id:
|
||||
frappe.throw("Customer does not have AutoPay enabled")
|
||||
|
||||
payload = {
|
||||
"autopay_id": autopay_id,
|
||||
"amount": amount,
|
||||
"invoice": invoice
|
||||
"autopay_id": cust.auto_pay_id,
|
||||
"amount": float(inv.outstanding_amount),
|
||||
"invoice": inv.name
|
||||
}
|
||||
|
||||
response = call_payment_api(payload)
|
||||
|
||||
# Handle gateway failure clearly
|
||||
if not response.get("success"):
|
||||
frappe.throw(response.get("error", "Payment failed"))
|
||||
|
||||
# Create ERPNext payment record
|
||||
payment_entry = create_payment_entry(
|
||||
invoice=invoice,
|
||||
amount=amount,
|
||||
invoice=inv.name,
|
||||
amount=payload["amount"],
|
||||
transaction_id=response.get("transaction_id")
|
||||
)
|
||||
|
||||
@@ -43,14 +48,9 @@ def run_autopay_payment(invoice, autopay_id, amount):
|
||||
}
|
||||
|
||||
|
||||
|
||||
# Call's Crystal CLear's API and Runs an AutoPay transaction using NMI / Crystal Clear using customer_vault_id (autopay_id)
|
||||
|
||||
def call_payment_api(payload):
|
||||
|
||||
url = "https://crystalclear.transactiongateway.com/api/transact.php"
|
||||
|
||||
# Store these in site_config.json or environment variables
|
||||
api_username = frappe.conf.get("nmi_username")
|
||||
api_password = frappe.conf.get("nmi_password")
|
||||
|
||||
@@ -71,38 +71,54 @@ def call_payment_api(payload):
|
||||
response = requests.post(url, data=data, timeout=30)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
frappe.log_error(frappe.get_traceback(), "NMI Payment API Error")
|
||||
frappe.throw("Payment processor unreachable")
|
||||
|
||||
# NMI success condition
|
||||
if result.get("response") == "1":
|
||||
return {
|
||||
"success": True,
|
||||
"transaction_id": result.get("transactionid")
|
||||
}
|
||||
|
||||
# Failure case
|
||||
return {
|
||||
"success": False,
|
||||
"error": result.get("responsetext", "Payment failed")
|
||||
}
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_collect_checkout_url(invoice):
|
||||
inv = frappe.get_doc("Sales Invoice", invoice)
|
||||
|
||||
return (
|
||||
"https://crystalclear.transactiongateway.com/collect/checkout"
|
||||
f"?amount={inv.outstanding_amount}&reference={inv.name}"
|
||||
)
|
||||
|
||||
|
||||
# Auto creates payment entry in ERP Next
|
||||
def create_payment_entry(invoice, amount, transaction_id=None):
|
||||
inv = frappe.get_doc("Sales Invoice", invoice)
|
||||
|
||||
paid_to = frappe.db.get_value(
|
||||
"Company",
|
||||
inv.company,
|
||||
"default_cash_account"
|
||||
)
|
||||
|
||||
if not paid_to:
|
||||
frappe.throw("Default cash account not set for company")
|
||||
|
||||
pe = frappe.new_doc("Payment Entry")
|
||||
pe.payment_type = "Receive"
|
||||
pe.party_type = "Customer"
|
||||
pe.party = inv.customer
|
||||
pe.posting_date = nowdate()
|
||||
pe.paid_amount = amount
|
||||
pe.received_amount = amount
|
||||
pe.paid_to = paid_to
|
||||
pe.reference_no = transaction_id
|
||||
pe.reference_date = frappe.utils.nowdate()
|
||||
pe.reference_date = nowdate()
|
||||
|
||||
pe.append("references", {
|
||||
"reference_doctype": "Sales Invoice",
|
||||
@@ -110,10 +126,7 @@ def create_payment_entry(invoice, amount, transaction_id=None):
|
||||
"allocated_amount": amount
|
||||
})
|
||||
|
||||
pe.insert()
|
||||
pe.insert(ignore_permissions=True)
|
||||
pe.submit()
|
||||
|
||||
return pe.name
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user