schon/payments/signals.py
Egor fureunoir Gorbunov 4ed76b85de 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.
2025-07-06 19:21:15 +03:00

35 lines
1.3 KiB
Python

from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from payments.gateways import AbstractGateway
from payments.models import Balance, Transaction
from payments.utils.emailing import balance_deposit_email
from vibes_auth.models import User
@receiver(post_save, sender=User)
def create_balance_on_user_creation_signal(instance, created, **_kwargs):
if created:
Balance.objects.create(user=instance)
@receiver(pre_save, sender=Transaction)
def process_transaction_changes(instance, created, **_kwargs):
if created:
try:
match instance.process.get("gateway", "default"):
case "gateway":
gateway = AbstractGateway
case "default":
gateway = AbstractGateway
case _:
gateway = AbstractGateway()
gateway.process_transaction(instance)
except Exception as e:
instance.process = {"status": "ERRORED", "error": str(e)}
if not created:
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)):
balance_deposit_email.delay(instance.uuid)