diff --git a/backend/hct_mis_api/apps/payment/models.py b/backend/hct_mis_api/apps/payment/models.py index 900b000798..dfe67acece 100644 --- a/backend/hct_mis_api/apps/payment/models.py +++ b/backend/hct_mis_api/apps/payment/models.py @@ -923,7 +923,11 @@ def is_reconciled(self) -> bool: return ( self.eligible_payments.exclude( - status__in=[GenericPayment.STATUS_PENDING, GenericPayment.STATUS_SENT_TO_PG] + status__in=[ + GenericPayment.STATUS_PENDING, + GenericPayment.STATUS_SENT_TO_PG, + GenericPayment.STATUS_SENT_TO_FSP, + ] ).count() == self.eligible_payments.count() ) diff --git a/backend/hct_mis_api/apps/payment/services/payment_gateway.py b/backend/hct_mis_api/apps/payment/services/payment_gateway.py index 46d2724ee2..51fe47859b 100644 --- a/backend/hct_mis_api/apps/payment/services/payment_gateway.py +++ b/backend/hct_mis_api/apps/payment/services/payment_gateway.py @@ -163,9 +163,8 @@ def get_transferred_status_based_on_delivery_amount() -> str: self.payout_amount, entitlement_quantity ) except Exception: - raise PaymentGatewayAPI.PaymentGatewayAPIException( - f"Invalid delivered_quantity {self.payout_amount} for Payment {self.remote_id}" - ) + logger.error(f"Invalid delivered_quantity {self.payout_amount} for Payment {self.remote_id}") + _hope_status = Payment.STATUS_ERROR return _hope_status mapping = { @@ -180,7 +179,8 @@ def get_transferred_status_based_on_delivery_amount() -> str: hope_status = mapping.get(self.status) if not hope_status: - raise PaymentGatewayAPI.PaymentGatewayAPIException(f"Invalid Payment status: {self.status}") + logger.error(f"Invalid Payment status: {self.status}") + hope_status = Payment.STATUS_ERROR return hope_status() if callable(hope_status) else hope_status @@ -435,8 +435,16 @@ def update_payment( _payment.fsp_auth_code = matching_pg_payment.auth_code update_fields = ["status", "status_date", "fsp_auth_code"] - if _payment.status not in Payment.ALLOW_CREATE_VERIFICATION and matching_pg_payment.message: - _payment.reason_for_unsuccessful_payment = matching_pg_payment.message + if _payment.status in [ + Payment.STATUS_ERROR, + Payment.STATUS_MANUALLY_CANCELLED, + ]: + if matching_pg_payment.message: + _payment.reason_for_unsuccessful_payment = matching_pg_payment.message + elif matching_pg_payment.payout_amount: + _payment.reason_for_unsuccessful_payment = f"Delivered amount: {matching_pg_payment.payout_amount}" + else: + _payment.reason_for_unsuccessful_payment = "Unknown error" update_fields.append("reason_for_unsuccessful_payment") delivered_quantity = matching_pg_payment.payout_amount diff --git a/backend/hct_mis_api/apps/payment/tests/test_payment_gateway_service.py b/backend/hct_mis_api/apps/payment/tests/test_payment_gateway_service.py index 6f80290eea..9460666c7b 100644 --- a/backend/hct_mis_api/apps/payment/tests/test_payment_gateway_service.py +++ b/backend/hct_mis_api/apps/payment/tests/test_payment_gateway_service.py @@ -33,7 +33,6 @@ ) from hct_mis_api.apps.payment.services.payment_gateway import ( AddRecordsResponseData, - PaymentGatewayAPI, PaymentGatewayService, PaymentInstructionStatus, PaymentRecordData, @@ -259,14 +258,12 @@ def test_get_hope_status(self) -> None: self.assertEqual(p.get_hope_status(self.payments[0].entitlement_quantity), Payment.STATUS_DISTRIBUTION_SUCCESS) self.assertEqual(p.get_hope_status(Decimal(1000000.00)), Payment.STATUS_DISTRIBUTION_PARTIAL) - with self.assertRaisesMessage(PaymentGatewayAPI.PaymentGatewayAPIException, "Invalid delivered_quantity"): - p.payout_amount = None # type: ignore - p.get_hope_status(Decimal(1000000.00)) + p.payout_amount = None # type: ignore + self.assertEqual(p.get_hope_status(Decimal(1000000.00)), Payment.STATUS_ERROR) - with self.assertRaisesMessage(PaymentGatewayAPI.PaymentGatewayAPIException, "Invalid Payment status"): - p.payout_amount = float(self.payments[0].entitlement_quantity) - p.status = "NOT EXISTING STATUS" - p.get_hope_status(Decimal(1000000.00)) + p.payout_amount = float(self.payments[0].entitlement_quantity) + p.status = "NOT EXISTING STATUS" + self.assertEqual(p.get_hope_status(Decimal(1000000.00)), Payment.STATUS_ERROR) @mock.patch( "hct_mis_api.apps.payment.services.payment_gateway.PaymentGatewayAPI.add_records_to_payment_instruction"