diff --git a/g2p-payments-bridge-core/src/g2p_payments_bridge_core/config.py b/g2p-payments-bridge-core/src/g2p_payments_bridge_core/config.py index e1e8c65..2359e8d 100644 --- a/g2p-payments-bridge-core/src/g2p_payments_bridge_core/config.py +++ b/g2p-payments-bridge-core/src/g2p_payments_bridge_core/config.py @@ -57,8 +57,7 @@ class Settings(Settings): db_dbname: str = "gpbdb" response_sender_id: str = "g2p.payments.bridge.openg2p" - - enable_id_translation: bool = True + get_backend_name_from_translate: bool = True # TODO: Convert this to ORM Model rather than config multiplex_fa_backend_mapping: List[FaBackendMapping] = [] diff --git a/g2p-payments-bridge-core/src/g2p_payments_bridge_core/models/orm/payment_list.py b/g2p-payments-bridge-core/src/g2p_payments_bridge_core/models/orm/payment_list.py index 0e6aa83..5d621a0 100644 --- a/g2p-payments-bridge-core/src/g2p_payments_bridge_core/models/orm/payment_list.py +++ b/g2p-payments-bridge-core/src/g2p_payments_bridge_core/models/orm/payment_list.py @@ -24,10 +24,12 @@ class PaymentListItem(BaseORMModelWithTimes): to_fa: Mapped[str] = mapped_column(String()) amount: Mapped[str] = mapped_column(String()) currency: Mapped[str] = mapped_column(String()) - status: Mapped[MsgStatusEnum] = mapped_column(Enum(MsgStatusEnum)) + status: Mapped[MsgStatusEnum] = mapped_column( + Enum(MsgStatusEnum, native_enum=False) + ) file: Mapped[Optional[str]] = mapped_column(String()) error_code: Mapped[Optional[SingleDisburseStatusEnum]] = mapped_column( - Enum(SingleDisburseStatusEnum) + Enum(SingleDisburseStatusEnum, native_enum=False) ) error_msg: Mapped[Optional[str]] = mapped_column(String()) backend_name: Mapped[Optional[str]] = mapped_column(String()) diff --git a/g2p-payments-bridge-core/src/g2p_payments_bridge_core/services/payment_multiplexer.py b/g2p-payments-bridge-core/src/g2p_payments_bridge_core/services/payment_multiplexer.py index 5d564b5..bc2158d 100644 --- a/g2p-payments-bridge-core/src/g2p_payments_bridge_core/services/payment_multiplexer.py +++ b/g2p-payments-bridge-core/src/g2p_payments_bridge_core/services/payment_multiplexer.py @@ -41,26 +41,29 @@ async def get_payment_backend_from_fa(self, fa: str): return None async def make_disbursements(self, disburse_request: DisburseRequest): - payee_fa_list = [] - try: - payee_fa_list = await self.id_translate_service.translate( - [ - disbursement.payee_fa - for disbursement in disburse_request.disbursements - ] - ) - except Exception: - # TODO: handle the failures - pass - # TODO : we want to make backend name configurable if true then all this or of false then None - for i, disbursement in enumerate(disburse_request.disbursements): + if _config.get_backend_name_from_translate: + payee_fa_list = [] try: - backend_name = await self.get_payment_backend_from_fa( - payee_fa_list[i] or "" + payee_fa_list = await self.id_translate_service.translate( + [ + disbursement.payee_fa + for disbursement in disburse_request.disbursements + ] ) except Exception: - # TODO : handle the failures + # TODO: handle the failures pass + # TODO : we want to make backend name configurable if true then all this or of false then None + for i, disbursement in enumerate(disburse_request.disbursements): + backend_name = None + if _config.get_backend_name_from_translate: + try: + backend_name = await self.get_payment_backend_from_fa( + payee_fa_list[i] or "" + ) + except Exception: + # TODO : handle the failures + pass await PaymentListItem.insert( disburse_request.transaction_id, disbursement, backend_name=backend_name ) diff --git a/gpb-mojaloop-sdk-payment-backend/payment_backend.py b/gpb-mojaloop-sdk-payment-backend/payment_backend.py index 803d027..b1f4dd4 100644 --- a/gpb-mojaloop-sdk-payment-backend/payment_backend.py +++ b/gpb-mojaloop-sdk-payment-backend/payment_backend.py @@ -14,6 +14,7 @@ ) from g2p_payments_bridge_core.models.msg_header import MsgStatusEnum from g2p_payments_bridge_core.models.orm.payment_list import PaymentListItem +from g2p_payments_bridge_core.services.id_translate_service import IdTranslateService from openg2p_fastapi_common.config import Settings as BaseSettings from openg2p_fastapi_common.context import dbengine from openg2p_fastapi_common.service import BaseService @@ -46,6 +47,7 @@ class Settings(BaseSettings): payer_id_value: str = "" payee_id_type: str = "" transfer_note: str = "GPB benefit transfer" + translate_id_to_fa: bool = True _config = Settings.get_config() @@ -61,6 +63,13 @@ class ReferenceIdStatus(BaseModel): class MojaloopSdkPaymentBackendService(BaseService): def __init__(self, name="", **kwargs): super().__init__(name if name else _config.payment_backend_name, **kwargs) + self._id_translate_service = IdTranslateService.get_component() + + @property + def id_translate_service(self): + if not self._id_translate_service: + self._id_translate_service = IdTranslateService.get_component() + return self._id_translate_service def post_init(self): asyncio.create_task(self.disburse_loop()) @@ -116,6 +125,11 @@ async def disburse_loop(self): async def disburse(self, payments: List[PaymentListItem], session: AsyncSession): for payment in payments: + payee_acc_no = "" + if _config.translate_id_to_fa: + payee_acc_no = self.id_translate_service.translate(payment.to_fa) + else: + payee_acc_no = payment.to_fa data = { "homeTransactionId": payment.request_id, "from": { @@ -125,7 +139,7 @@ async def disburse(self, payments: List[PaymentListItem], session: AsyncSession) "to": { "idType": _config.payee_id_type, "idValue": await self.get_payee_id_value_from_payee_fa( - payment.to_fa + payee_acc_no ), }, "currency": payment.currency, diff --git a/gpb-simple-mpesa-payment-backend/payment_backend.py b/gpb-simple-mpesa-payment-backend/payment_backend.py index 1c45661..8636b73 100755 --- a/gpb-simple-mpesa-payment-backend/payment_backend.py +++ b/gpb-simple-mpesa-payment-backend/payment_backend.py @@ -14,6 +14,7 @@ ) from g2p_payments_bridge_core.models.msg_header import MsgStatusEnum from g2p_payments_bridge_core.models.orm.payment_list import PaymentListItem +from g2p_payments_bridge_core.services.id_translate_service import IdTranslateService from openg2p_fastapi_common.config import Settings as BaseSettings from openg2p_fastapi_common.context import dbengine from openg2p_fastapi_common.service import BaseService @@ -48,6 +49,7 @@ class Settings(BaseSettings): dsbmt_loop_interval_secs: int = 10 dsbmt_loop_filter_backend_name: bool = True dsbmt_loop_filter_status: List[str] = ["rcvd", "fail"] + translate_id_to_fa: bool = True _config = Settings.get_config() @@ -64,6 +66,14 @@ class SimpleMpesaPaymentBackendService(BaseService): def __init__(self, name="", **kwargs): super().__init__(name if name else _config.payment_backend_name, **kwargs) + self._id_translate_service = IdTranslateService.get_component() + + @property + def id_translate_service(self): + if not self._id_translate_service: + self._id_translate_service = IdTranslateService.get_component() + return self._id_translate_service + def post_init(self): asyncio.create_task(self.disburse_loop()) @@ -143,12 +153,17 @@ async def disburse(self, payments: List[PaymentListItem], session: AsyncSession) return for payment in payments: + payee_acc_no = "" + if _config.translate_id_to_fa: + payee_acc_no = self.id_translate_service.translate(payment.to_fa) + else: + payee_acc_no = payment.to_fa headers = { "Authorization": f"Bearer {auth_token}", } data = { "amount": int(float(payment.amount)), - "accountNo": await self.get_account_no_from_payee_fa(payment.to_fa), + "accountNo": await self.get_account_no_from_payee_fa(payee_acc_no), "customerType": _config.customer_type, } try: diff --git a/gpb-translate-id-fa/src/gpb_translate_id_fa/app.py b/gpb-translate-id-fa/src/gpb_translate_id_fa/app.py index 8e48b84..5025d4f 100644 --- a/gpb-translate-id-fa/src/gpb_translate_id_fa/app.py +++ b/gpb-translate-id-fa/src/gpb_translate_id_fa/app.py @@ -1,9 +1,3 @@ -# ruff: noqa: E402 - -from .config import Settings - -_config = Settings.get_config() - from openg2p_fastapi_common.app import Initializer from .services import G2PConnectIdTranslateService