diff --git a/billing/urls.py b/billing/urls.py index 94d8bf4..76591cd 100644 --- a/billing/urls.py +++ b/billing/urls.py @@ -9,7 +9,7 @@ from .views import ( ListCreateTopupView, VerifyTopupPaymentAPIView, TopupDetailAPIView, - DeleteTopupView, + CancelTopupView, ) urlpatterns = [ @@ -37,8 +37,8 @@ urlpatterns = [ name="verify-topup-payment", ), path( - "topup//delete/", - DeleteTopupView.as_view(), - name="delete-topup", + "topup//cancel/", + CancelTopupView.as_view(), + name="cancel-topup", ), ] diff --git a/billing/views.py b/billing/views.py index f2922a9..0f307fd 100644 --- a/billing/views.py +++ b/billing/views.py @@ -397,6 +397,8 @@ class VerifyTopupPaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIVi if topup_verification_response.success: user.wallet_balance += topup_instance.amount # type: ignore user.save() + topup_instance.status = "PAID" + topup_instance.save() return Response( { "status": topup_verification_response.success, @@ -418,17 +420,22 @@ class VerifyTopupPaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIVi ) -class DeleteTopupView(StaffEditorPermissionMixin, generics.DestroyAPIView): - queryset = Topup.objects.all() +class CancelTopupView(StaffEditorPermissionMixin, generics.UpdateAPIView): + queryset = Topup.objects.all().select_related("user") serializer_class = TopupSerializer lookup_field = "pk" - def delete(self, request, *args, **kwargs): + def update(self, request, *args, **kwargs): instance = self.get_object() user = request.user + if instance.status == "CANCELLED": + return Response( + {"message": "Topup has already been cancelled."}, + status=status.HTTP_400_BAD_REQUEST, + ) if instance.is_expired: return Response( - {"message": "Expired topups cannot be deleted."}, + {"message": "Expired topups cannot be cancelled."}, status=status.HTTP_400_BAD_REQUEST, ) if ( @@ -445,4 +452,6 @@ class DeleteTopupView(StaffEditorPermissionMixin, generics.DestroyAPIView): {"message": "Paid topups cannot be deleted."}, status=status.HTTP_400_BAD_REQUEST, ) - return super().delete(request, *args, **kwargs) + instance.status = "CANCELLED" + instance.save() + return super().update(request, *args, **kwargs)