From b03757508b905a1825bdab645b18cf9b6a08d0d7 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 12 Nov 2025 17:27:43 +0300 Subject: [PATCH 1/4] Features: 1) Update callback URL to exclude gateway parameter; 2) Process callback using associated transaction's gateway integration class; Fixes: 1) Remove unused import for `UnknownGatewayError`; Extra: 1) Cleanup unreachable code and obsolete comments in `CallbackAPIView`. --- engine/payments/urls.py | 2 +- engine/payments/views.py | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/engine/payments/urls.py b/engine/payments/urls.py index 02abddf3..b3837941 100644 --- a/engine/payments/urls.py +++ b/engine/payments/urls.py @@ -12,5 +12,5 @@ payment_router.register(prefix=r"transactions", viewset=TransactionViewSet, base urlpatterns = [ path(r"", include(payment_router.urls)), path(r"deposit/", DepositView.as_view()), - path(r"/callback//", CallbackAPIView.as_view()), + path(r"/callback/", CallbackAPIView.as_view()), ] diff --git a/engine/payments/views.py b/engine/payments/views.py index dde5012c..989c48c4 100644 --- a/engine/payments/views.py +++ b/engine/payments/views.py @@ -9,7 +9,6 @@ from rest_framework.response import Response from rest_framework.views import APIView from engine.payments.docs.drf.views import DEPOSIT_SCHEMA -from engine.payments.gateways import UnknownGatewayError from engine.payments.models import Transaction from engine.payments.serializers import DepositSerializer, TransactionProcessSerializer @@ -72,14 +71,10 @@ class CallbackAPIView(APIView): def post(self, request: Request, *args: list[Any], **kwargs: dict[Any, Any]) -> Response: try: - gateway = kwargs.get("gateway", "") - # noinspection PyUnreachableCode - match gateway: - case "gateway": - # Gateway.process_callback(request.data) - return Response(status=status.HTTP_200_OK) - case _: - raise UnknownGatewayError(f"Couldn't match '{gateway}' any gateway") + transaction = Transaction.objects.get(uuid=kwargs.get("uuid")) + gateway = transaction.gateway.get_integration_class_object() + gateway.process_callback(request.data) + return Response(status=status.HTTP_202_ACCEPTED) except Exception as e: return Response( status=status.HTTP_500_INTERNAL_SERVER_ERROR, data={"error": f"{e}; {traceback.format_exc()}"} From af49dacb095c3390c25f0ef6e0f91938fb90e395 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 12 Nov 2025 17:29:21 +0300 Subject: [PATCH 2/4] Features: 1) Handle `UnknownGatewayError` when processing transactions. Fixes: 1) Ensure `uuid` defaults to an empty string in `Transaction` retrieval. Extra: 1) Add missing import for `UnknownGatewayError`. --- engine/payments/views.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/engine/payments/views.py b/engine/payments/views.py index 989c48c4..1c385582 100644 --- a/engine/payments/views.py +++ b/engine/payments/views.py @@ -9,6 +9,7 @@ from rest_framework.response import Response from rest_framework.views import APIView from engine.payments.docs.drf.views import DEPOSIT_SCHEMA +from engine.payments.gateways import UnknownGatewayError from engine.payments.models import Transaction from engine.payments.serializers import DepositSerializer, TransactionProcessSerializer @@ -71,7 +72,9 @@ class CallbackAPIView(APIView): def post(self, request: Request, *args: list[Any], **kwargs: dict[Any, Any]) -> Response: try: - transaction = Transaction.objects.get(uuid=kwargs.get("uuid")) + transaction = Transaction.objects.get(uuid=kwargs.get("uuid", "")) + if not transaction.gateway: + raise UnknownGatewayError() gateway = transaction.gateway.get_integration_class_object() gateway.process_callback(request.data) return Response(status=status.HTTP_202_ACCEPTED) From f3a1bb7110e746d58dbeb3f6cf1efbcd8e5ee5aa Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 12 Nov 2025 22:05:15 +0300 Subject: [PATCH 3/4] Features: 1) Add new `DEBUG_DATABASE` setting for database logging control; 2) Enable conditional logging based on `DEBUG_DATABASE` for `django.db` logger; Fixes: 1) Correct type casting of `uuid` in payment transaction retrieval; Extra: 1) Minor cleanup in logging configuration imports; --- engine/payments/views.py | 2 +- evibes/settings/base.py | 1 + evibes/settings/logconfig.py | 7 ++++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/engine/payments/views.py b/engine/payments/views.py index 1c385582..8c6a8771 100644 --- a/engine/payments/views.py +++ b/engine/payments/views.py @@ -72,7 +72,7 @@ class CallbackAPIView(APIView): def post(self, request: Request, *args: list[Any], **kwargs: dict[Any, Any]) -> Response: try: - transaction = Transaction.objects.get(uuid=kwargs.get("uuid", "")) + transaction = Transaction.objects.get(uuid=str(kwargs.get("uuid"))) if not transaction.gateway: raise UnknownGatewayError() gateway = transaction.gateway.get_integration_class_object() diff --git a/evibes/settings/base.py b/evibes/settings/base.py index 6a4c4819..3e01e454 100644 --- a/evibes/settings/base.py +++ b/evibes/settings/base.py @@ -14,6 +14,7 @@ INITIALIZED = (BASE_DIR / ".initialized").exists() SECRET_KEY = getenv("SECRET_KEY", "SUPER_SECRET_KEY") DEBUG = bool(int(getenv("DEBUG", "1"))) +DEBUG_DATABASE = bool(int(getenv("DEBUG_DATABASE", "1"))) BASE_DOMAIN: str = getenv("EVIBES_BASE_DOMAIN", "localhost") STOREFRONT_DOMAIN: str = getenv("EVIBES_STOREFRONT_DOMAIN", "localhost") diff --git a/evibes/settings/logconfig.py b/evibes/settings/logconfig.py index bb6b5d97..c6a837ff 100644 --- a/evibes/settings/logconfig.py +++ b/evibes/settings/logconfig.py @@ -1,6 +1,6 @@ import logging -from evibes.settings.base import DEBUG +from evibes.settings.base import DEBUG, DEBUG_DATABASE class SkipVariableDoesNotExistFilter(logging.Filter): @@ -63,6 +63,11 @@ LOGGING = { "level": "DEBUG" if DEBUG else "INFO", "propagate": True, }, + "django.db": { + "handlers": ["console"], + "level": "DEBUG" if DEBUG_DATABASE else "WARNING", + "propagate": True, + }, "django.template": { "handlers": ["console"], "level": "DEBUG" if DEBUG else "ERROR", From 425464114c2f6dc0801cd832f1f549dc90744089 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 12 Nov 2025 22:06:44 +0300 Subject: [PATCH 4/4] Fixes: 1) Pass `raise_exc=True` to `get_integration_class_object` to handle unknown gateways properly; Extra: 1) No additional changes. --- engine/payments/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/payments/views.py b/engine/payments/views.py index 8c6a8771..8041115c 100644 --- a/engine/payments/views.py +++ b/engine/payments/views.py @@ -75,7 +75,7 @@ class CallbackAPIView(APIView): transaction = Transaction.objects.get(uuid=str(kwargs.get("uuid"))) if not transaction.gateway: raise UnknownGatewayError() - gateway = transaction.gateway.get_integration_class_object() + gateway = transaction.gateway.get_integration_class_object(raise_exc=True) gateway.process_callback(request.data) return Response(status=status.HTTP_202_ACCEPTED) except Exception as e: