retry loans
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import requests
|
import requests
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
from app.helpers.response_helper import ResponseHelper
|
from app.helpers.response_helper import ResponseHelper
|
||||||
|
#from app.routes.autocall import verify_transaction
|
||||||
from app.services.loan import LoanService
|
from app.services.loan import LoanService
|
||||||
from app.services.loan_repayment_schedule import LoanRepaymentScheduleService
|
from app.services.loan_repayment_schedule import LoanRepaymentScheduleService
|
||||||
from app.utils.auth import get_headers
|
from app.utils.auth import get_headers
|
||||||
@@ -63,8 +64,15 @@ class SimbrellaClient:
|
|||||||
logger.info(f"Here is your loan data: {loan_data}")
|
logger.info(f"Here is your loan data: {loan_data}")
|
||||||
|
|
||||||
if loan_data['disburseDate'] is not None:
|
if loan_data['disburseDate'] is not None:
|
||||||
|
logger.info("*************************SEEM LIKE A RETRY CALL -WE WILL VERIFY Result to Continue ")
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Please call verify loan : {data['transactionId']} loan send for processing at {loan_data['disburseDate']}")
|
f"Please call verify loan : {data['transactionId']} loan sent for processing at {loan_data['disburseDate']}")
|
||||||
|
# return 0 -- we need the disburseResult = '00' to be sure all is good
|
||||||
|
|
||||||
|
if loan_data['disburseDate'] is not None and loan_data['disburseResult'] == '00':
|
||||||
|
logger.info("*************************Duplicate call to completed loan ")
|
||||||
|
logger.info(
|
||||||
|
f"Duplicate call detected for loan : {data['transactionId']} loan sent for processing at {loan_data['disburseDate']}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
# let us set disbursement date
|
# let us set disbursement date
|
||||||
@@ -83,7 +91,7 @@ class SimbrellaClient:
|
|||||||
|
|
||||||
disbursement_data = {
|
disbursement_data = {
|
||||||
"transactionId": loan_data.get('transactionId'),
|
"transactionId": loan_data.get('transactionId'),
|
||||||
"FbnTransactionId": loan_data.get('transactionId'),
|
"fbnTransactionId": loan_data.get('transactionId'),
|
||||||
"debtId": debtId,
|
"debtId": debtId,
|
||||||
"customerId": loan_data.get('customerId'),
|
"customerId": loan_data.get('customerId'),
|
||||||
"accountId": loan_data.get('accountId'),
|
"accountId": loan_data.get('accountId'),
|
||||||
@@ -96,24 +104,60 @@ class SimbrellaClient:
|
|||||||
"countryId": "01",
|
"countryId": "01",
|
||||||
"comment": "Loan Disbursement",
|
"comment": "Loan Disbursement",
|
||||||
}
|
}
|
||||||
|
# '''
|
||||||
|
# { Veryfing with the bank
|
||||||
|
# "transactionId": "string",
|
||||||
|
# "fbnTransactionId": "string",
|
||||||
|
# "debtId": "string",
|
||||||
|
# "customerId": "string",
|
||||||
|
# "accountId": "string",
|
||||||
|
# "productId": "string",
|
||||||
|
# "provideAmount": 0,
|
||||||
|
# "collectAmountInterest": 0,
|
||||||
|
# "collectAmountMgtFee": 0,
|
||||||
|
# "collectAmountInsurance": 0,
|
||||||
|
# "collectAmountVAT": 0,
|
||||||
|
# "countryId": "string",
|
||||||
|
# "comment": "string"
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
try:
|
try:
|
||||||
logger.info(f"Here is your Disbursement Request data ****** : {disbursement_data}")
|
logger.info(f"Here is your Disbursement Request data ****** : {disbursement_data}")
|
||||||
response = requests.post(api_url, json=disbursement_data, timeout=10, headers=get_headers())
|
response = requests.post(api_url, json=disbursement_data, timeout=10, headers=get_headers())
|
||||||
|
logger.info(f"Call to bank end point returned with Here is your Disbursement Request data ****** : {disbursement_data}")
|
||||||
if response.status_code == 404:
|
if response.status_code == 404:
|
||||||
logger.error("Received 404 from external service")
|
logger.error("")
|
||||||
|
LoanService.set_disbursement_loan_description(loan_data['debtId'],"Disbursement Service url not found (404)" )
|
||||||
return ResponseHelper.error("Disbursement Service url not found (404)", status_code=404)
|
return ResponseHelper.error("Disbursement Service url not found (404)", status_code=404)
|
||||||
|
|
||||||
logger.info(f"Disbursement response: {response.json()}")
|
logger.info(f"Disbursement response: {response.json()}")
|
||||||
|
|
||||||
result = response.json()
|
if response.status_code == 200:
|
||||||
LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
result = response.json()
|
||||||
return ResponseHelper.success(response.json(), "Successful")
|
LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
||||||
|
reload_loan = LoanService.get_loan_by_transaction_id(transaction_id=data['transactionId'])
|
||||||
|
reload_loan_data = reload_loan.to_dict()
|
||||||
|
verify_disbursement_transaction(reload_loan_data)
|
||||||
|
return ResponseHelper.success(response.json(), "Successful")
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.error("")
|
||||||
|
errorMessage = "Unable to complete Disbursement Service with HTTP status code: " + str(response.status_code)
|
||||||
|
LoanService.set_disbursement_loan_description(loan_data['debtId'],errorMessage )
|
||||||
|
return ResponseHelper.error(errorMessage, status_code=response.status_code)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info(f"Failed to call Disbursement endpoint: {e}")
|
logger.info(f"Failed to call Disbursement endpoint: {e}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def verify_disbursement_transaction(loan_data):
|
||||||
|
if loan_data['disburseResult'] and loan_data['disburseResult'] == '00':
|
||||||
|
verify_transaction(loan_data)
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def verify_transaction(data):
|
def verify_transaction(data):
|
||||||
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}{SimbrellaClient.BANK_CALL_TRANSACTION_VERIFY}"
|
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}{SimbrellaClient.BANK_CALL_TRANSACTION_VERIFY}"
|
||||||
@@ -164,7 +208,17 @@ class SimbrellaClient:
|
|||||||
"countryId": "NG",
|
"countryId": "NG",
|
||||||
"requestId": loan_data.get('transactionId')
|
"requestId": loan_data.get('transactionId')
|
||||||
}
|
}
|
||||||
|
# '''
|
||||||
|
# { Verify with bank
|
||||||
|
# "accountId": "string",
|
||||||
|
# "customerId": "string",
|
||||||
|
# "transactionId": "string",
|
||||||
|
# "fbnTransactionId": "string",
|
||||||
|
# "transactionType": "string",
|
||||||
|
# "countryId": "string",
|
||||||
|
# "requestId": "string"
|
||||||
|
# }
|
||||||
|
# '''
|
||||||
try:
|
try:
|
||||||
logger.info(f"Here is your TransactionVerify Request data ****** : {verify_data}")
|
logger.info(f"Here is your TransactionVerify Request data ****** : {verify_data}")
|
||||||
response = requests.post(api_url, json=verify_data, timeout=10, headers=get_headers())
|
response = requests.post(api_url, json=verify_data, timeout=10, headers=get_headers())
|
||||||
|
|||||||
@@ -158,6 +158,29 @@ class Loan(db.Model):
|
|||||||
logger.error(f"Failed to update disburse verify date: {e}")
|
logger.error(f"Failed to update disburse verify date: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def set_disbursement_message(cls, loan_id, description):
|
||||||
|
"""
|
||||||
|
Update the disburse result and description of the loan with the given loan_id.
|
||||||
|
"""
|
||||||
|
# Retrieve loan
|
||||||
|
loan = cls.query.get(loan_id)
|
||||||
|
|
||||||
|
if not loan:
|
||||||
|
raise ValueError(f"Loan with ID {loan_id} does not exist.")
|
||||||
|
|
||||||
|
# Update disburse description only
|
||||||
|
loan.disburse_description = description
|
||||||
|
|
||||||
|
# Commit changes to database
|
||||||
|
try:
|
||||||
|
logger.info(f"Updating disburse result for loan ID {loan_id} with description {description}")
|
||||||
|
db.session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
logger.error(f"Failed to update disbursement result: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_disbursement_result(cls, loan_id, result, description):
|
def set_disbursement_result(cls, loan_id, result, description):
|
||||||
@@ -182,6 +205,8 @@ class Loan(db.Model):
|
|||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
logger.error(f"Failed to update disbursement result: {e}")
|
logger.error(f"Failed to update disbursement result: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_disburse_verify_result(cls, loan_id, result, description):
|
def set_disburse_verify_result(cls, loan_id, result, description):
|
||||||
"""
|
"""
|
||||||
|
|||||||
+27
-1
@@ -31,7 +31,7 @@ def verify_transaction():
|
|||||||
|
|
||||||
data = {
|
data = {
|
||||||
"transactionId": loan_data.get('transactionId'),
|
"transactionId": loan_data.get('transactionId'),
|
||||||
"FbnTransactionId": loan_data.get('transactionId'),
|
"fbnTransactionId": loan_data.get('transactionId'),
|
||||||
"debtId": str(loan_data.get('debtId')),
|
"debtId": str(loan_data.get('debtId')),
|
||||||
"customerId": loan_data.get('customerId'),
|
"customerId": loan_data.get('customerId'),
|
||||||
"accountId": loan_data.get('accountId'),
|
"accountId": loan_data.get('accountId'),
|
||||||
@@ -64,6 +64,32 @@ def disbursement():
|
|||||||
response = SimbrellaClient.disburse_loan(data)
|
response = SimbrellaClient.disburse_loan(data)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@autocall_bp.route("/retry-disbursement", methods=["POST"])
|
||||||
|
def retry_disbursement():
|
||||||
|
data = request.get_json()
|
||||||
|
logger.info(f"Data received: {data}")
|
||||||
|
|
||||||
|
transactionId = data["transactionId"]
|
||||||
|
|
||||||
|
logger.info(f"Calling Disbursement Components")
|
||||||
|
loan = LoanService.get_loan_by_transaction_id(transactionId)
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
@autocall_bp.route("/direct/loan", methods=["POST"])
|
@autocall_bp.route("/direct/loan", methods=["POST"])
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ class LoanService:
|
|||||||
Update the disbursement result of the loan with the given loan_id.
|
Update the disbursement result of the loan with the given loan_id.
|
||||||
"""
|
"""
|
||||||
return Loan.set_disbursement_result(loan_id, result, description)
|
return Loan.set_disbursement_result(loan_id, result, description)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def set_disbursement_loan_description(cls,loan_id,description):
|
||||||
|
|
||||||
|
return Loan.set_disbursement_message(loan_id, result, description)
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_disburse_verify_result(cls, loan_id, result, description):
|
def set_disburse_verify_result(cls, loan_id, result, description):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user