From 7789d135840bf4b595b74c3563d67674ecfe5a62 Mon Sep 17 00:00:00 2001 From: Ty Reynolds Date: Wed, 27 May 2026 13:19:12 -0400 Subject: [PATCH] Added some fail safes for inconsequential errors. --- ns_app/api/payments.py | 193 ++++++++++++++++++++++++++++------------- 1 file changed, 133 insertions(+), 60 deletions(-) diff --git a/ns_app/api/payments.py b/ns_app/api/payments.py index dfccc2b..734706f 100644 --- a/ns_app/api/payments.py +++ b/ns_app/api/payments.py @@ -157,6 +157,17 @@ Response: if success == "1": + frappe.log_error( + f""" +PAYMENT SUCCESS FAILSAFE + +Invoice: {invoice} +Transaction ID: {transaction_id} +Amount: {payload['amount']} +""", + "PAYMENT SUCCESS FAILSAFE" + ) + if payment_type == "check": mode_of_payment = "ACH" else: @@ -175,12 +186,23 @@ Response: "duplicate": True } - create_payment_entry( - invoice=invoice, - amount=payload["amount"], - transaction_id=transaction_id, - mode_of_payment=mode_of_payment - ) + try: + + create_payment_entry( + invoice=invoice, + amount=payload["amount"], + transaction_id=transaction_id, + mode_of_payment=mode_of_payment + ) + + frappe.db.commit() + + except Exception: + + frappe.log_error( + frappe.get_traceback(), + "PAYMENT ENTRY FAILURE AFTER SUCCESSFUL CHARGE" + ) return { "success": True, @@ -379,6 +401,17 @@ Response: )[0] } + frappe.log_error( + f""" +PAYMENT SUCCESS FAILSAFE + +Invoice: {invoice} +Transaction ID: {transaction_id} +Amount: {inv.outstanding_amount} +""", + "PAYMENT SUCCESS FAILSAFE" + ) + existing_pe = frappe.db.exists( "Payment Entry", {"reference_no": transaction_id} @@ -386,54 +419,74 @@ Response: if not existing_pe: - create_payment_entry( - invoice=invoice, - amount=inv.outstanding_amount, - transaction_id=transaction_id, - mode_of_payment="Credit Card" - ) + try: + + create_payment_entry( + invoice=invoice, + amount=inv.outstanding_amount, + transaction_id=transaction_id, + mode_of_payment="Credit Card" + ) + + frappe.db.commit() + + except Exception: + + frappe.log_error( + frappe.get_traceback(), + "PAYMENT ENTRY FAILURE AFTER SUCCESSFUL CHARGE" + ) # Save AutoPay info locally if save_autopay and vault_id: - customer.custom_auto_pay_id = ( - vault_id - ) + try: - customer.custom_auto_pay_status = 1 + customer.custom_auto_pay_id = ( + vault_id + ) - customer.custom_auto_pay_first_name = ( - first_name - ) + customer.custom_auto_pay_status = 1 - customer.custom_auto_pay_last_name = ( - last_name - ) + customer.custom_auto_pay_first_name = ( + first_name + ) - customer.custom_auto_pay_company = ( - company - ) + customer.custom_auto_pay_last_name = ( + last_name + ) - customer.custom_auto_pay_zip = ( - billing_zip - ) + customer.custom_auto_pay_company = ( + company + ) - customer.save( - ignore_permissions=True - ) + customer.custom_auto_pay_zip = ( + billing_zip + ) - frappe.db.commit() + customer.save( + ignore_permissions=True + ) - frappe.log_error( - f""" + frappe.db.commit() + + frappe.log_error( + f""" Vault save complete customer={customer.name} vault_id={vault_id} """, - "AUTOPAY DEBUG - SAVE COMPLETE" - ) + "AUTOPAY DEBUG - SAVE COMPLETE" + ) + + except Exception: + + frappe.log_error( + frappe.get_traceback(), + "AUTOPAY CUSTOMER SAVE FAILED" + ) return { "success": True, @@ -551,31 +604,40 @@ def save_to_autopay( or "duplicate" in message.lower() ): - cust.custom_auto_pay_id = ( - returned_vault_id - ) + try: - cust.custom_auto_pay_status = 1 + cust.custom_auto_pay_id = ( + returned_vault_id + ) - cust.custom_auto_pay_first_name = ( - first_name - ) + cust.custom_auto_pay_status = 1 - cust.custom_auto_pay_last_name = ( - last_name - ) + cust.custom_auto_pay_first_name = ( + first_name + ) - cust.custom_auto_pay_company = ( - company - ) + cust.custom_auto_pay_last_name = ( + last_name + ) - cust.custom_auto_pay_zip = ( - billing_zip - ) + cust.custom_auto_pay_company = ( + company + ) - cust.save(ignore_permissions=True) + cust.custom_auto_pay_zip = ( + billing_zip + ) - frappe.db.commit() + cust.save(ignore_permissions=True) + + frappe.db.commit() + + except Exception: + + frappe.log_error( + frappe.get_traceback(), + "AUTOPAY CUSTOMER SAVE FAILED" + ) return { "success": True, @@ -606,12 +668,23 @@ def crystalclear_webhook(): else: mode_of_payment = "Credit Card" - create_payment_entry( - invoice=invoice, - amount=amount, - transaction_id=transaction_id, - mode_of_payment=mode_of_payment - ) + try: + + create_payment_entry( + invoice=invoice, + amount=amount, + transaction_id=transaction_id, + mode_of_payment=mode_of_payment + ) + + frappe.db.commit() + + except Exception: + + frappe.log_error( + frappe.get_traceback(), + "WEBHOOK PAYMENT ENTRY FAILURE" + ) return "ok"