Skip to content

Commit

Permalink
Merge pull request #7 from Abhishek-Wagh/develop
Browse files Browse the repository at this point in the history
added a new config variable for translating fa
  • Loading branch information
shibu-narayanan authored Nov 23, 2023
2 parents 24f902a + 05da15e commit dd024ad
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
16 changes: 15 additions & 1 deletion gpb-mojaloop-sdk-payment-backend/payment_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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())
Expand Down Expand Up @@ -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": {
Expand All @@ -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,
Expand Down
17 changes: 16 additions & 1 deletion gpb-simple-mpesa-payment-backend/payment_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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())

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions gpb-translate-id-fa/src/gpb_translate_id_fa/app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit dd024ad

Please sign in to comment.