From 10b3800171be4802a781cb18e99a20042f891e0e Mon Sep 17 00:00:00 2001 From: i701 Date: Fri, 11 Apr 2025 00:41:44 +0500 Subject: [PATCH] Refactor VerifyPaymentView to improve wallet payment handling and response messages. Remove redundant insufficient funds check and enhance failure message clarity. --- apibase/settings.py | 3 --- billing/views.py | 35 ++++++++++++++++++----------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/apibase/settings.py b/apibase/settings.py index e850751..9413ca7 100644 --- a/apibase/settings.py +++ b/apibase/settings.py @@ -149,9 +149,6 @@ else: "PASSWORD": env("POSTGRES_PASSWORD"), "HOST": env("POSTGRES_HOST"), "PORT": env("POSTGRES_PORT"), - "OPTIONS": { - "pool": True, - }, }, } diff --git a/billing/views.py b/billing/views.py index 08b2619..b2c4e95 100644 --- a/billing/views.py +++ b/billing/views.py @@ -123,10 +123,16 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): devices = payment.devices.all() payment_status = False if method == "WALLET": - payment_status = self.process_wallet_payment( - user, - payment, - ) + if user.wallet_balance < payment.amount: + return Response( + {"message": "Insufficient funds in wallet."}, + status=status.HTTP_400_BAD_REQUEST, + ) + else: + payment_status = self.process_wallet_payment( + user, + payment, + ) if method == "TRANSFER": data = { "benefName": f"{user.first_name} {user.last_name}", @@ -148,17 +154,14 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): return Response( {"message": f"Payment verified successfully using [{method}]."} ) - - return Response({"message": "Payment verified successfully using [{method}]."}) + else: + return Response( + {"message": "Payment verification FAILED using [{method}]."} + ) def process_wallet_payment(self, user, payment): print("processing wallet payment...") print(user, payment.amount) - if user.wallet_balance < payment.amount: - return Response( - {"message": "Insufficient funds in wallet."}, - status=status.HTTP_400_BAD_REQUEST, - ) payment.paid = True payment.paid_at = timezone.now() @@ -177,13 +180,11 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): headers={"Content-Type": "application/json"}, ) response.raise_for_status() - print(response.json()) + mib_resp = response.json() if not response.json().get("success"): - return Response( - {"message": "MIB Payment verification failed."}, - status=status.HTTP_400_BAD_REQUEST, - ) - return True + return mib_resp["success"] + else: + return True class DeletePaymentView(StaffEditorPermissionMixin, generics.DestroyAPIView):