add refund api

timetoconfirm
Raz 7 months ago
parent 444020fbcf
commit ef46c71625
  1. 1
      api/urls.py
  2. 31
      api/views.py
  3. 1
      tournaments/models/team_registration.py
  4. 7
      tournaments/services/payment_service.py
  5. 1
      tournaments/services/tournament_unregistration.py

@ -37,6 +37,7 @@ urlpatterns = [
path('api-token-auth/', obtain_auth_token, name='api_token_auth'), path('api-token-auth/', obtain_auth_token, name='api_token_auth'),
path("user-by-token/", views.user_by_token, name="user_by_token"), path("user-by-token/", views.user_by_token, name="user_by_token"),
path('refund-tournament/<str:team_registration_id>/', views.process_refund, name='process-refund'),
# authentication # authentication
path("change-password/", ChangePasswordView.as_view(), name="change_password"), path("change-password/", ChangePasswordView.as_view(), name="change_password"),

@ -15,6 +15,12 @@ from .utils import check_version_smaller_than_1_1_12
from shared.discord import send_discord_log_message 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']) @api_view(['GET'])
def user_by_token(request): def user_by_token(request):
serializer = UserSerializer(request.user) serializer = UserSerializer(request.user)
@ -284,3 +290,28 @@ class ShortUserViewSet(viewsets.ModelViewSet):
queryset = CustomUser.objects.all() queryset = CustomUser.objects.all()
serializer_class = ShortUserSerializer serializer_class = ShortUserSerializer
permission_classes = [] # Users are public whereas the other requests are only for logged users 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)

@ -367,7 +367,6 @@ class TeamRegistration(SideStoreModel):
payment_statuses = [player.has_paid() for player in player_registrations] payment_statuses = [player.has_paid() for player in player_registrations]
print(f"Payment statuses: {payment_statuses}") print(f"Payment statuses: {payment_statuses}")
print(all(payment_statuses))
# If all players have paid # If all players have paid
if all(payment_statuses): if all(payment_statuses):
return 'PAID' return 'PAID'

@ -318,6 +318,13 @@ class PaymentService:
reverse_transfer=True 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 success with refund object
return True, "Votre inscription a été remboursée automatiquement.", refund return True, "Votre inscription a été remboursée automatiquement.", refund

@ -37,7 +37,6 @@ class TournamentUnregistrationService:
if refund_processed: if refund_processed:
# Refund successful, continue with unregistration process # Refund successful, continue with unregistration process
messages.success(self.request, message) messages.success(self.request, message)
TournamentEmailService.send_refund_confirmation(self.tournament, self.player_registration.team_registration, refund_details)
else: else:
# Refund failed, show error but continue with normal unregistration # Refund failed, show error but continue with normal unregistration
messages.error(self.request, message) messages.error(self.request, message)

Loading…
Cancel
Save