Features: 1) Add pre_save signal handling for Transaction model; 2) Enhance error status handling within process_transaction_changes.

Fixes: 1) Correct type conversion for process status to a string for consistency.

Extra: 1) Adjust signal usage from post_save to pre_save for improved transaction handling.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-06 19:21:15 +03:00
parent 8ed5bc4c17
commit 4ed76b85de

View file

@ -1,4 +1,4 @@
from django.db.models.signals import post_save
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from payments.gateways import AbstractGateway
@ -13,7 +13,7 @@ def create_balance_on_user_creation_signal(instance, created, **_kwargs):
Balance.objects.create(user=instance)
@receiver(post_save, sender=Transaction)
@receiver(pre_save, sender=Transaction)
def process_transaction_changes(instance, created, **_kwargs):
if created:
try:
@ -26,9 +26,9 @@ def process_transaction_changes(instance, created, **_kwargs):
gateway = AbstractGateway()
gateway.process_transaction(instance)
except Exception as e:
instance.process = {"status": "NOGATEWAY", "error": str(e)}
instance.process = {"status": "ERRORED", "error": str(e)}
if not created:
status = instance.process.get("status", "").lower()
status = str(instance.process.get("status", "")).lower()
success = instance.process.get("success", False)
if ("success" in status or success) and (instance.process.get("notify", False)):