From 3be4d1b87c0fed8f47157f2733c32d0874126530 Mon Sep 17 00:00:00 2001 From: Alan Woodman Date: Thu, 16 Oct 2025 10:56:51 +0800 Subject: [PATCH] update --- query_mysql.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/query_mysql.py b/query_mysql.py index 96e225e..0062f4d 100644 --- a/query_mysql.py +++ b/query_mysql.py @@ -180,6 +180,20 @@ def find_set_pending_splynx_invoices(splynx_id: int) -> List[Dict[str, Any]]: updated_invoices.append(res) return updated_invoices +def find_set_pending_splynx_invoices_to_unpaid(splynx_id: int) -> List[Dict[str, Any]]: + result = splynx.get(url=f"/api/2.0/admin/finance/invoices?main_attributes[customer_id]={splynx_id}&main_attributes[status]=pending") + + invoice_pay = { + "status": "not_paid" + } + + updated_invoices = [] + for pay in result: + res = splynx.put(url=f"/api/2.0/admin/finance/invoices/{pay['id']}", params=invoice_pay) + if res: + updated_invoices.append(res) + return updated_invoices + def delete_splynx_invoices(splynx_id: int, payintent: str) -> Dict[str, Any]: """Delete Splynx payment records for a given customer and payment intent.""" try: @@ -884,6 +898,14 @@ def handle_failed_payment_notification(payment_record, error_details: str, payme 'payment_intent': payment_record.Payment_Intent } + # Revert pending invoices back to "not_paid" (only in live mode) + if PROCESS_LIVE: + updated_invoices = find_set_pending_splynx_invoices_to_unpaid(splynx_id=payment_record.Splynx_ID) + if updated_invoices: + logger.info(f"✅ Payment failure pending invoices reverted back to not_paid Splynx ID: {payment_record.Splynx_ID} - PayID: {payment_record.id}") + else: + logger.error(f"❌ Failed to send payment failure email for payment {payment_record.id}") + # Send email notification (only in live mode) if PROCESS_LIVE: email_sent = notification_service.send_payment_failure_notification(payment_data)