diff --git a/billing/views.py b/billing/views.py index aad9276..4e16139 100644 --- a/billing/views.py +++ b/billing/views.py @@ -152,7 +152,7 @@ class PaymentDetailAPIView(StaffEditorPermissionMixin, generics.RetrieveAPIView) class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView): - queryset = Payment.objects.select_related("user").all() + queryset = Payment.objects.select_related("user").prefetch_related("devices").all() serializer_class = UpdatePaymentSerializer lookup_field = "pk" @@ -172,11 +172,12 @@ class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView): class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): serializer_class = PaymentSerializer - queryset = Payment.objects.select_related("user").all() + queryset = Payment.objects.select_related("user").prefetch_related("devices").all() lookup_field = "pk" def update(self, request, *args, **kwargs): payment = self.get_object() + devices = payment.devices.all() data = request.data user = request.user print("logged in user", user) @@ -198,7 +199,6 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): status=status.HTTP_400_BAD_REQUEST, ) - devices = payment.devices.all() if method == "WALLET": if user.wallet_balance < payment.amount: # type: ignore return Response( @@ -209,6 +209,7 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): self.process_wallet_payment( user, # type: ignore payment, + devices, ) return Response( { @@ -271,14 +272,24 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): status=status.HTTP_400_BAD_REQUEST, ) - def process_wallet_payment(self, user: User, payment: Payment): + def process_wallet_payment(self, user: User, payment: Payment, devices=None): print("processing wallet payment...") print(user, payment.amount) + # Use passed devices or fetch if not provided + if devices is None: + devices = payment.devices.all() payment.paid = True payment.paid_at = timezone.now() payment.method = "WALLET" payment.status = "PAID" + expiry_date = timezone.now() + timedelta(days=30 * payment.number_of_months) + devices.update( + is_active=True, + expiry_date=expiry_date, + has_a_pending_payment=False, + registered=True, + ) payment.save() user.deduct_wallet_funds(