Fixes: 1) Remove unused "prometheus/" URL pattern for cleanup. Extra: Update imports and formats for consistency; refine sitemap functions with explicit docstrings.
82 lines
3.2 KiB
Python
82 lines
3.2 KiB
Python
import logging
|
|
import traceback
|
|
|
|
from drf_spectacular.utils import extend_schema, extend_schema_view
|
|
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from payments.docs.drf.views import DEPOSIT_SCHEMA
|
|
from payments.gateways import UnknownGatewayError
|
|
from payments.models import Transaction
|
|
from payments.serializers import DepositSerializer, TransactionProcessSerializer
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@extend_schema_view(**DEPOSIT_SCHEMA)
|
|
class DepositView(APIView):
|
|
"""Handles deposit operations.
|
|
|
|
This class provides an API endpoint to handle deposit transactions.
|
|
It supports the creation of a deposit transaction after validating the
|
|
provided data. If the user is not authenticated, an appropriate response
|
|
is returned. On successful validation and execution, a response
|
|
with the transaction details is provided.
|
|
|
|
Attributes:
|
|
No attributes are declared at the class-level for this view.
|
|
|
|
Methods:
|
|
post: Processes the deposit request, validates the request data, ensures
|
|
user authentication, and creates a transaction.
|
|
"""
|
|
def post(self, request, *args, **kwargs):
|
|
logger.debug(request.__dict__)
|
|
serializer = DepositSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
if not request.user.is_authenticated:
|
|
return Response(data=serializer.errors, status=status.HTTP_401_UNAUTHORIZED)
|
|
|
|
transaction = Transaction.objects.create(
|
|
balance=request.user.payments_balance, amount=serializer.validated_data["amount"], currency="EUR"
|
|
)
|
|
|
|
return Response(TransactionProcessSerializer(transaction).data, status=status.HTTP_303_SEE_OTHER)
|
|
|
|
|
|
@extend_schema(exclude=True)
|
|
class CallbackAPIView(APIView):
|
|
"""
|
|
Handles incoming callback requests to the API.
|
|
|
|
This class processes and routes incoming HTTP POST requests to the appropriate
|
|
gateway handler based on the provided gateway parameter. It is designed to handle
|
|
callback events coming from external systems and provide an appropriate HTTP response
|
|
indicating success or failure.
|
|
|
|
Attributes:
|
|
No additional attributes are defined for this class beyond what is
|
|
inherited from APIView.
|
|
|
|
Methods:
|
|
post(request, *args, **kwargs): Processes POST requests and routes them
|
|
based on the specified gateway. Handles exceptions gracefully by returning
|
|
a server error response if an unknown gateway or other issues occur.
|
|
"""
|
|
def post(self, request, *args, **kwargs):
|
|
logger.debug(request.__dict__)
|
|
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")
|
|
except Exception as e:
|
|
return Response(
|
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR, data={"error": f"{e}; {traceback.format_exc()}"}
|
|
)
|