This commit is contained in:
CHIEFSOFT\ameye
2025-06-05 07:13:35 -04:00
parent 67363ff6e5
commit 50769f7faf
5 changed files with 137 additions and 65 deletions
+3 -3
View File
@@ -37,8 +37,8 @@ class Config:
SQLALCHEMY_TRACK_MODIFICATIONS = False
BANK_CALL_BASE_URL = os.getenv("BANK_CALL_BASE_URL", "https://bank-emulator.dev.simbrellang.net")
BANK_CALL_DISBURSE_LOAN_ENDPOINT = os.getenv("BANK_CALL_DISBURSE_LOAN_ENDPOINT","DisburseLoan")
BANK_CALL_COLLECT_LOAN_ENDPOINT = os.getenv("BANK_CALL_COLLECT_LOAN_ENDPOINT","CollectLoan")
BANK_CALL_TRANSACTION_VERIFY = os.getenv("BANK_CALL_TRANSACTION_VERIFY", "TransactionVerify")
BANK_CALL_DISBURSE_LOAN_ENDPOINT = os.getenv("BANK_CALL_DISBURSE_LOAN_ENDPOINT","api/DisburseLoan")
BANK_CALL_COLLECT_LOAN_ENDPOINT = os.getenv("BANK_CALL_COLLECT_LOAN_ENDPOINT","api/CollectLoan")
BANK_CALL_TRANSACTION_VERIFY = os.getenv("BANK_CALL_TRANSACTION_VERIFY", "api/TransactionVerify")
settings = Config()
+82 -44
View File
@@ -19,34 +19,41 @@ class SimbrellaClient:
BANK_CALL_TRANSACTION_VERIFY = settings.BANK_CALL_TRANSACTION_VERIFY
@staticmethod
def disburse_loan():
def disburse_loan(data):
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/{SimbrellaClient.BANK_CALL_DISBURSE_LOAN_ENDPOINT}"
logger.info(f"Calling DisburseLoan api_url==> : {api_url}")
loan = LoanService.get_latest_loan_without_disburse_date()
if not loan:
logger.info(f"No loan found without disbursement date")
return 0
logger.info(f"Calling DisburseLoan endpoint with data: {loan}")
loan_data = loan.to_dict()
logger.info(f"Here is your loan data: {loan_data}")
logger.info(f"Calling DisburseLoan endpoint with data: {data}")
# Check if the transaction exists
logger.info(f"Checking if transaction exists")
transaction = TransactionService.get_transaction_by_transaction_id(transaction_id=loan_data['transactionId'])
transaction = TransactionService.get_transaction_by_transaction_id(transaction_id=data['transactionId'])
logger.info(f"Loan Response From Database ** : {transaction}")
# If transaction is not found
if not transaction:
logger.info(f"Transaction id: {loan_data['transactionId']}, was not found")
logger.info(f"Transaction id: {data['transactionId']}, was not found")
return 0
# Fetch the loan based on the transaction_id
logger.info(f"Fetching the loan with transaction ID: {data['transactionId']}")
loan = LoanService.get_loan_by_transaction_id(transaction_id=data['transactionId'])
logger.info(f"Response from database: {loan}")
# If loan is not found
if not loan:
logger.info(f"Could not find loan with transaction id: {data['transactionId']}")
return 0
loan_data = loan.to_dict()
logger.info(f"Here is your loan data: {loan_data}")
if loan_data['disburseDate'] is not None:
logger.info(f"Please call verify loan : {loan_data['transactionId']} loan send for processing at {loan_data['disburseDate']}")
else:
logger.info(f"Here are your cal 111 : *********************************************************")
# let us set disbursement date
LoanService.set_disbursement_date(loan_data['debtId'], loan_data['customerId']) # toda this must return something
logger.info(f"Here are your cal 000 : *********************************************************")
logger.info(
f"Please call verify loan : {data['transactionId']} loan send for processing at {loan_data['disburseDate']}")
return 0
# let us set disbursement date
LoanService.set_disbursement_date(loan_data['debtId'],loan_data['customerId']) # toda this must return something
logger.info(f"Here is your loan data after setting disbursement date: {loan_data}")
loan_charges = preprocess_loan_charges_data([loan_charge.to_dict() for loan_charge in loan.loan_charges])
@@ -57,10 +64,12 @@ class SimbrellaClient:
interest_fee = loan_charges.get("INTEREST")['amount']
insurance_fee = loan_charges.get("INSURANCE")['amount']
debtId = str(loan_data.get('debtId', "")).strip().zfill(6)
disbursement_data = {
"transactionId": loan_data.get('transactionId'),
"FbnTransactionId": loan_data.get('transactionId'),
"debtId": str(loan_data.get('debtId', "")).strip().zfill(6),
"debtId": debtId,
"customerId": loan_data.get('customerId'),
"accountId": loan_data.get('accountId'),
"productId": str(loan_data.get('productId', "")),
@@ -79,14 +88,68 @@ class SimbrellaClient:
logger.info(f"Disbursement response: {response.json()}")
result = response.json()
LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
except Exception as e:
logger.info(f"Failed to call Disbursement endpoint: {e}")
return 0
return 1
#Calling CollectLoan endpoint with data: {'transactionId': 'TRX1747960765928545', 'customerId': 'CID0000055362', 'productId': '101', 'debtId': '9590'}
@staticmethod
def verify_transaction(data):
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/{SimbrellaClient.BANK_CALL_DISBURSE_LOAN_ENDPOINT}"
logger.info(f"Calling TransactionVerify api_url==> : {api_url}")
# Check if the transaction exists
logger.info(f"Checking if transaction exists")
transaction = TransactionService.get_transaction_by_transaction_id(transaction_id=data['transactionId'])
logger.info(f"Loan Response From Database ** : {transaction}")
# If transaction is not found
if not transaction:
logger.info(f"Transaction id: {data['transactionId']}, was not found")
return 0
# Fetch the loan based on the transaction_id
logger.info(f"Fetching the loan with transaction ID: {data['transactionId']}")
loan = LoanService.get_loan_by_transaction_id(transaction_id=data['transactionId'])
logger.info(f"Response from database: {loan}")
# If loan is not found
if not loan:
logger.info(f"Could not find loan with transaction id: {data['transactionId']}")
return 0
loan_data = loan.to_dict()
logger.info(f"Here is your loan data: {loan_data}")
if loan_data['disburseDate'] is not None and loan_data['disburseVerify'] is None :
logger.info(f"Good to Verify transaction id: {data['transactionId']}")
else:
logger.info(
f"Please call disburse loan : {data['transactionId']} loan send for processing first")
return 0
verify_data = {
"channel": "USSD",
"customerId": loan_data.get('customerId'),
"accountId": loan_data.get('accountId'),
"transactionId": loan_data.get('transactionId'),
"transactionType": "LOAN",
"countryId": "NG",
"requestId": loan_data.get('transactionId')
}
try:
logger.info(f"Here is your TransactionVerify Request data ****** : {verify_data}")
response = requests.post(api_url, json=verify_data, timeout=10, headers=get_headers())
logger.info(f"TransactionVerify Response: {response.json()}")
return ResponseHelper.success(response, "Successful")
except Exception as e:
logger.info(f"Failed to call TransactionVerify endpoint: {e}")
return 0
@staticmethod
def collect_loan(data):
@@ -136,31 +199,6 @@ class SimbrellaClient:
return 1
@staticmethod
def verify_transaction():
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/{SimbrellaClient.BANK_CALL_DISBURSE_LOAN_ENDPOINT}"
logger.info(f"Calling TransactionVerify api_url==> : {api_url}")
verify_data = {
"channel": "string",
"accountId": "string",
"customerId": "string",
"transactionId": "string",
"transactionType": "string",
"countryId": "string",
"requestId": "string"
}
try:
logger.info(f"Here is your TransactionVerify Request data ****** : {verify_data}")
response = requests.post(api_url, json=verify_data, timeout=10, headers=get_headers())
logger.info(f"TransactionVerify Response: {response.json()}")
return ResponseHelper.success(response, "Successful")
except Exception as e:
logger.info(f"Failed to call TransactionVerify endpoint: {e}")
return 0
@staticmethod
def refresh_disbursement(data):
+9
View File
@@ -144,6 +144,15 @@ class Loan(db.Model):
@classmethod
def get_latest_loan_without_disburse_date(cls):
"""
Get the latest loan without a disbursement date.
"""
return cls.query.filter(
cls.disburse_date.is_(None)
).order_by(cls.created_at.desc()).first()
@classmethod
def get_latest_loan_with_disburse_date(cls):
"""
Get the latest loan without a disbursement date.
"""
+35 -17
View File
@@ -5,6 +5,7 @@ from app.helpers.response_helper import ResponseHelper
from app.utils.auth import get_headers
from app.utils.logger import logger
from app.integrations.simbrella import SimbrellaClient
from app.services.loan import LoanService
autocall_bp = Blueprint("autocall", __name__)
@@ -12,29 +13,46 @@ autocall_bp = Blueprint("autocall", __name__)
def verify_transaction():
logger.info(f"Calling VerifyTransaction Components")
response = SimbrellaClient.verify_transaction()
loan = LoanService.get_latest_loan_with_disburse_date()
if not loan:
logger.info(f"No loan found without disbursement date")
return 0
logger.info(f"Calling VerifyTransaction endpoint with data: {loan}")
loan_data = loan.to_dict()
data = {
"transactionId": loan_data.get('transactionId'),
"FbnTransactionId": loan_data.get('transactionId'),
"debtId": str(loan_data.get('debtId')),
"customerId": loan_data.get('customerId'),
"accountId": loan_data.get('accountId'),
"productId": str(loan_data.get('productId', "")),
"provideAmount": loan_data.get('currentLoanAmount'),
}
response = SimbrellaClient.verify_transaction(data)
return response
@autocall_bp.route("/refresh-disbursement", methods=["GET"])
def disbursement():
# data = request.json()
logger.info(f"Calling Disbursement Components")
""" data = {
"transactionId": "TRX1749033507722975",
"FbnTransactionId":"TRX1748643654575327",
"debtId": "94696900",
"customerId": "CID00000559140T",
"accountId": "ACC20267810160T",
"productId": "3MPC",
"provideAmount": 10000.0,
"collectAmountInterest": 19.0,
"collectAmountMgtFee": 66.0,
"collectAmountInsurance": 66.0,
"collectAmountVAT": 75.00,
"countryId": "01",
"comment": "Loan Disbursement"
}"""
response = SimbrellaClient.disburse_loan()
loan = LoanService.get_latest_loan_without_disburse_date()
if not loan:
logger.info(f"No loan found without disbursement date")
return 0
logger.info(f"Calling DisburseLoan endpoint with data: {loan}")
loan_data = loan.to_dict()
data = {
"transactionId": loan_data.get('transactionId'),
"FbnTransactionId": loan_data.get('transactionId'),
"debtId": str(loan_data.get('debtId')),
"customerId": loan_data.get('customerId'),
"accountId": loan_data.get('accountId'),
"productId": str(loan_data.get('productId', "")),
"provideAmount": loan_data.get('currentLoanAmount'),
}
response = SimbrellaClient.disburse_loan(data)
return response
+8 -1
View File
@@ -43,4 +43,11 @@ class LoanService:
"""
Get the latest loan without a disbursement date.
"""
return Loan.get_latest_loan_without_disburse_date()
return Loan.get_latest_loan_without_disburse_date()
@classmethod
def get_latest_loan_with_disburse_date(cls):
"""
Get the latest loan without a disbursement date.
"""
return Loan.get_latest_loan_with_disburse_date()