From ef46c71625553d118f24fcfe97b78cc9bae99aa4 Mon Sep 17 00:00:00 2001 From: Raz Date: Fri, 11 Apr 2025 21:22:42 +0200 Subject: [PATCH] add refund api --- api/urls.py | 1 + api/views.py | 31 +++++++++++++++++++ tournaments/models/team_registration.py | 1 - tournaments/services/payment_service.py | 7 +++++ .../services/tournament_unregistration.py | 1 - 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/api/urls.py b/api/urls.py index ae8fb58..1d7ec8c 100644 --- a/api/urls.py +++ b/api/urls.py @@ -37,6 +37,7 @@ urlpatterns = [ path('api-token-auth/', obtain_auth_token, name='api_token_auth'), path("user-by-token/", views.user_by_token, name="user_by_token"), + path('refund-tournament//', views.process_refund, name='process-refund'), # authentication path("change-password/", ChangePasswordView.as_view(), name="change_password"), diff --git a/api/views.py b/api/views.py index bfbc0c3..1534039 100644 --- a/api/views.py +++ b/api/views.py @@ -15,6 +15,12 @@ from .utils import check_version_smaller_than_1_1_12 from shared.discord import send_discord_log_message +from rest_framework.decorators import permission_classes +from rest_framework.permissions import IsAuthenticated +from django.shortcuts import get_object_or_404 + +from ..tournaments.services.payment_service import PaymentService + @api_view(['GET']) def user_by_token(request): serializer = UserSerializer(request.user) @@ -284,3 +290,28 @@ class ShortUserViewSet(viewsets.ModelViewSet): queryset = CustomUser.objects.all() serializer_class = ShortUserSerializer permission_classes = [] # Users are public whereas the other requests are only for logged users + +@api_view(['POST']) +@permission_classes([IsAuthenticated]) +def process_refund(request, team_registration_id): + try: + # Verify the user is the tournament umpire + team_registration = get_object_or_404(TeamRegistration, id=team_registration_id) + if request.user != team_registration.tournament.event.creator: + return Response({ + 'success': False, + 'message': "Vous n'êtes pas autorisé à effectuer ce remboursement" + }, status=403) + + payment_service = PaymentService(request) + success, message, refund = payment_service.process_refund(team_registration_id) + return Response({ + 'success': success, + 'message': message, + 'players': team_registration.players.all() + }) + except Exception as e: + return Response({ + 'success': False, + 'message': str(e) + }, status=400) diff --git a/tournaments/models/team_registration.py b/tournaments/models/team_registration.py index f1958da..d8dc972 100644 --- a/tournaments/models/team_registration.py +++ b/tournaments/models/team_registration.py @@ -367,7 +367,6 @@ class TeamRegistration(SideStoreModel): payment_statuses = [player.has_paid() for player in player_registrations] print(f"Payment statuses: {payment_statuses}") - print(all(payment_statuses)) # If all players have paid if all(payment_statuses): return 'PAID' diff --git a/tournaments/services/payment_service.py b/tournaments/services/payment_service.py index 255e87b..e043f66 100644 --- a/tournaments/services/payment_service.py +++ b/tournaments/services/payment_service.py @@ -318,6 +318,13 @@ class PaymentService: reverse_transfer=True ) + for player_reg in player_registrations: + player_reg.payment_type = None + player_reg.payment_id = None + player_reg.save() + + TournamentEmailService.send_refund_confirmation(tournament, team_registration, refund) + # Return success with refund object return True, "Votre inscription a été remboursée automatiquement.", refund diff --git a/tournaments/services/tournament_unregistration.py b/tournaments/services/tournament_unregistration.py index e921833..f531a49 100644 --- a/tournaments/services/tournament_unregistration.py +++ b/tournaments/services/tournament_unregistration.py @@ -37,7 +37,6 @@ class TournamentUnregistrationService: if refund_processed: # Refund successful, continue with unregistration process messages.success(self.request, message) - TournamentEmailService.send_refund_confirmation(self.tournament, self.player_registration.team_registration, refund_details) else: # Refund failed, show error but continue with normal unregistration messages.error(self.request, message)