Skip to content

Commit

Permalink
1199072_Add_Payment_Status_and_move_mapping_from_PG_to_HOPE_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marekbiczysko committed Jul 11, 2024
1 parent 38e7938 commit fdf0bdc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
6 changes: 5 additions & 1 deletion backend/hct_mis_api/apps/payment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down
20 changes: 14 additions & 6 deletions backend/hct_mis_api/apps/payment/services/payment_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
)
from hct_mis_api.apps.payment.services.payment_gateway import (
AddRecordsResponseData,
PaymentGatewayAPI,
PaymentGatewayService,
PaymentInstructionStatus,
PaymentRecordData,
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fdf0bdc

Please sign in to comment.