schon/payments/signals.py
Egor fureunoir Gorbunov 5b06f83cfc Features: 1) None;
Fixes: 1) Remove unused import of `AbstractGateway` from `payments/signals.py`; 2) Adjust type hints in `AbstractGateway` methods to use `Any` instead of `Transaction`;

Extra: 1) Replace unused import of `Transaction` in `payments/gateways/__init__.py` with `Any`.
2025-10-21 12:36:53 +03:00

39 lines
1.5 KiB
Python

import logging
import traceback
from typing import Any
from django.db.models.signals import post_save
from django.dispatch import receiver
from payments.models import Balance, Transaction
from payments.utils.emailing import balance_deposit_email
from vibes_auth.models import User
logger = logging.getLogger("django")
# noinspection PyUnusedLocal
@receiver(post_save, sender=User)
def create_balance_on_user_creation_signal(instance: User, created: bool, **kwargs: dict[Any, Any]) -> None:
if created:
Balance.objects.create(user=instance)
# noinspection PyUnusedLocal
@receiver(post_save, sender=Transaction)
def process_transaction_changes(instance: Transaction, created: bool, **kwargs: dict[Any, Any]) -> None:
if created:
if not instance.gateway:
raise ValueError("gateway is required to process a transaction")
try:
gateway = instance.gateway.get_integration_class_object()
gateway.process_transaction(instance)
except Exception as e:
instance.process = {"status": "ERRORED", "error": str(e)}
logger.error(f"Error processing transaction {instance.uuid}: {e}\n{traceback.format_exc()}")
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) # type: ignore [attr-defined]