From b352430b63ad2a77280e67f12cc5d82c03887ea6 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Wed, 24 Sep 2025 11:53:31 -0400 Subject: [PATCH] Delete invoice when crypto payment is cancelled When a crypto payment is cancelled, delete the associated invoice and line items since no actual purchase occurred. Keep the crypto_payment record for transaction history and audit trails. This prevents cancelled quotes from cluttering user purchase history. --- make_post_sell/views/crypto.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/make_post_sell/views/crypto.py b/make_post_sell/views/crypto.py index bd83ae2..c1196e0 100644 --- a/make_post_sell/views/crypto.py +++ b/make_post_sell/views/crypto.py @@ -742,6 +742,22 @@ def crypto_cancel(request): # Cancel the payment crypto_payment.status = CryptoPayment.STATUS_CANCELLED request.dbsession.add(crypto_payment) + + # Delete the invoice since this was never a real purchase + # Keep the crypto_payment for transaction history + invoice = crypto_payment.invoice + if invoice: + # Delete invoice line items first + for line_item in invoice.line_items: + request.dbsession.delete(line_item) + + # Clear the invoice reference from crypto payment + crypto_payment.invoice_id = None + crypto_payment.invoice = None + + # Delete the invoice + request.dbsession.delete(invoice) + request.dbsession.flush() # Get the cart URL for redirect