schon/payments/signals.py
Egor fureunoir Gorbunov ea236743ae Features: 1) Switch transaction signal from pre_save to post_save for processing changes; 2) Add created parameter handling for improved transaction processing logic;
Fixes: 1) Remove unused `pre_save` signal import;

Extra: 1) Update conditional logic for `process_transaction_changes` to improve clarity and error handling; 2) Minor formatting adjustments.
2025-07-06 23:30:06 +03:00

35 lines
1.3 KiB
Python

from django.db.models.signals import post_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(post_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)