feat(views): implement topup cancellation and update status to CANCELLED

This commit is contained in:
2025-07-05 17:37:35 +05:00
parent 2da9dcf141
commit 7003e4bcba
2 changed files with 18 additions and 9 deletions

View File

@ -9,7 +9,7 @@ from .views import (
ListCreateTopupView,
VerifyTopupPaymentAPIView,
TopupDetailAPIView,
DeleteTopupView,
CancelTopupView,
)
urlpatterns = [
@ -37,8 +37,8 @@ urlpatterns = [
name="verify-topup-payment",
),
path(
"topup/<str:pk>/delete/",
DeleteTopupView.as_view(),
name="delete-topup",
"topup/<str:pk>/cancel/",
CancelTopupView.as_view(),
name="cancel-topup",
),
]

View File

@ -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)