Refactor UpdatePaymentSerializer to include only number_of_months field, add DeletePaymentView for payment deletion, and enhance VerifyPaymentView with payment_id lookup. Introduce pending_payment_id field in DeviceSerializer to track unpaid payments.
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 2m18s

This commit is contained in:
2025-04-08 21:38:43 +05:00
parent c3abdd8e34
commit 9595476569
4 changed files with 52 additions and 16 deletions

View File

@ -92,21 +92,22 @@ class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
serializer = self.get_serializer(instance, data=request.data, partial=False)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
devices.update(is_active=True, expiry_date=device_expire_date)
devices.update(
is_active=True, expiry_date=device_expire_date, has_a_pending_payment=False
)
return Response(serializer.data)
class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
serializer_class = PaymentSerializer
queryset = Payment.objects.all()
lookup_field = "pk"
def update(self, request, *args, **kwargs):
data = request.data
user = request.user
print(user)
method = data.get("method")
payment_id = data.get("payment_id")
abs_amount = data.get("abs_amount")
payment_id = kwargs.get("pk")
if not method:
return Response(
{"message": "method is required. 'WALLET' or 'TRANSFER'"},
@ -117,11 +118,6 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
{"message": "payment_id is required."},
status=status.HTTP_400_BAD_REQUEST,
)
if not abs_amount:
return Response(
{"message": "abs_amount is required."},
status=status.HTTP_400_BAD_REQUEST,
)
try:
payment = Payment.objects.get(id=payment_id)
@ -182,3 +178,26 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
print(response.json())
if not response.json().get("success"):
raise Exception("Payment verification failed.")
class DeletePaymentView(StaffEditorPermissionMixin, generics.DestroyAPIView):
queryset = Payment.objects.all()
serializer_class = PaymentSerializer
lookup_field = "pk"
def delete(self, request, *args, **kwargs):
instance = self.get_object()
user = request.user
if instance.user != user and not user.is_superuser:
return Response(
{"message": "You are not authorized to delete this payment."},
status=status.HTTP_403_FORBIDDEN,
)
if instance.paid:
return Response(
{"message": "Paid payments cannot be deleted."},
status=status.HTTP_400_BAD_REQUEST,
)
devices = instance.devices.all()
devices.update(is_active=False, expiry_date=None, has_a_pending_payment=False)
return super().delete(request, *args, **kwargs)