diff --git a/billing/views.py b/billing/views.py index 7f04b5e..5f904b0 100644 --- a/billing/views.py +++ b/billing/views.py @@ -327,25 +327,36 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): def verify_transfer_payment(self, data, payment) -> PaymentVerificationResponse: if not PAYMENT_BASE_URL: - raise ValueError( - "PAYMENT_BASE_URL is not set. Please set it in your environment variables." - ) - response = requests.post( - f"{PAYMENT_BASE_URL}/verify-payment", - json=data, - headers={"Content-Type": "application/json"}, - ) - logger.info("MIB Verification Response -> ", response) - try: - response.raise_for_status() - except requests.exceptions.HTTPError as e: - logger.error(f"HTTPError: {e}") + logger.error("PAYMENT_BASE_URL is not set.") return PaymentVerificationResponse( - message="Payment verification failed.", success=False, transaction=None + message="Payment gateway is not configured. Please contact support.", + success=False, + transaction=None, ) - mib_resp = response.json() - logger.info("MIB Verification Response ->", mib_resp) - if not response.json().get("success"): + try: + response = requests.post( + f"{PAYMENT_BASE_URL}/verify-payment", + json=data, + headers={"Content-Type": "application/json"}, + ) + response.raise_for_status() + mib_resp = response.json() + except requests.exceptions.RequestException as e: + logger.error(f"MIB request failed: {e}") + return PaymentVerificationResponse( + message="Unable to reach the payment gateway. Please try again or contact support.", + success=False, + transaction=None, + ) + except ValueError as e: + logger.error(f"MIB returned an invalid response: {e}") + return PaymentVerificationResponse( + message="Received an invalid response from the payment gateway. Please contact support.", + success=False, + transaction=None, + ) + logger.info("MIB Verification Response -> %s", mib_resp) + if not mib_resp.get("success"): return PaymentVerificationResponse( message=mib_resp["message"], success=mib_resp["success"], @@ -471,25 +482,37 @@ class VerifyTopupPaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIVi def verify_transfer_topup(self, data, topup) -> PaymentVerificationResponse: if not PAYMENT_BASE_URL: - raise ValueError( - "PAYMENT_BASE_URL is not set. Please set it in your environment variables." + logger.error("PAYMENT_BASE_URL is not set.") + return PaymentVerificationResponse( + message="Payment gateway is not configured. Please contact support.", + success=False, + transaction=None, ) logger.info(data) - response = requests.post( - f"{PAYMENT_BASE_URL}/verify-payment", - json=data, - headers={"Content-Type": "application/json"}, - ) try: - response.raise_for_status() - except requests.exceptions.HTTPError as e: - logger.error(f"HTTPError: {e}") - return PaymentVerificationResponse( - message="Payment verification failed.", success=False, transaction=None + response = requests.post( + f"{PAYMENT_BASE_URL}/verify-payment", + json=data, + headers={"Content-Type": "application/json"}, ) - mib_resp = response.json() - print(mib_resp) - if not response.json().get("success"): + response.raise_for_status() + mib_resp = response.json() + except requests.exceptions.RequestException as e: + logger.error(f"MIB request failed: {e}") + return PaymentVerificationResponse( + message="Unable to reach the payment gateway. Please try again or contact support.", + success=False, + transaction=None, + ) + except ValueError as e: + logger.error(f"MIB returned an invalid response: {e}") + return PaymentVerificationResponse( + message="Received an invalid response from the payment gateway. Please contact support.", + success=False, + transaction=None, + ) + logger.info("MIB Verification Response -> %s", mib_resp) + if not mib_resp.get("success"): return PaymentVerificationResponse( message=mib_resp["message"], success=mib_resp["success"],