Added repay date #21
@@ -1,285 +0,0 @@
|
||||
import requests
|
||||
from app.config import settings
|
||||
from app.helpers.response_helper import ResponseHelper
|
||||
from app.services.loan import LoanService
|
||||
from app.utils.auth import get_headers
|
||||
from app.utils.extras import preprocess_loan_charges_data
|
||||
from app.utils.logger import logger
|
||||
from flask import jsonify, current_app
|
||||
from app.services.transactions import TransactionService
|
||||
from app.services.repayment import RepaymentService
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class SimbrellaClient:
|
||||
|
||||
BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL
|
||||
BANK_CALL_SMS_BASE_URL = settings.BANK_CALL_SMS_BASE_URL
|
||||
BANK_CALL_DISBURSE_LOAN_ENDPOINT = settings.BANK_CALL_DISBURSE_LOAN_ENDPOINT
|
||||
BANK_CALL_COLLECT_LOAN_ENDPOINT = settings.BANK_CALL_COLLECT_LOAN_ENDPOINT
|
||||
BANK_CALL_TRANSACTION_VERIFY = settings.BANK_CALL_TRANSACTION_VERIFY
|
||||
|
||||
@staticmethod
|
||||
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}")
|
||||
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=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:
|
||||
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])
|
||||
logger.info(f"Here are your loan_charges: {loan_charges}")
|
||||
|
||||
mgt_fee = loan_charges.get("MGTFEE")['amount']
|
||||
vat_fee = loan_charges.get("VAT")['amount']
|
||||
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": debtId,
|
||||
"customerId": loan_data.get('customerId'),
|
||||
"accountId": loan_data.get('accountId'),
|
||||
"productId": str(loan_data.get('productId', "")),
|
||||
"provideAmount": loan_data.get('currentLoanAmount'),
|
||||
"collectAmountInterest": interest_fee,
|
||||
"collectAmountMgtFee": mgt_fee,
|
||||
"collectAmountInsurance": insurance_fee,
|
||||
"collectAmountVAT": vat_fee,
|
||||
"countryId": "01",
|
||||
"comment": "Loan Disbursement",
|
||||
}
|
||||
|
||||
try:
|
||||
logger.info(f"Here is your Disbursement Request data ****** : {disbursement_data}")
|
||||
response = requests.post(api_url, json=disbursement_data, timeout=10, headers=get_headers())
|
||||
logger.info(f"Disbursement response: {response.json()}")
|
||||
result = response.json()
|
||||
LoanService.set_disbursement_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
||||
return ResponseHelper.success(response.json(), "Successful")
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call Disbursement endpoint: {e}")
|
||||
return 0
|
||||
|
||||
return 1
|
||||
|
||||
@staticmethod
|
||||
def verify_transaction(data):
|
||||
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}{SimbrellaClient.BANK_CALL_TRANSACTION_VERIFY}"
|
||||
sms_url = f"{SimbrellaClient.BANK_CALL_SMS_BASE_URL}/singleSMS"
|
||||
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 :
|
||||
LoanService.set_disburse_verify_date(loan_data['debtId'],loan_data['customerId'])
|
||||
loan_data = loan.to_dict()
|
||||
logger.info(f"Here is your loan data after setting verify date: {loan_data}")
|
||||
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 = {
|
||||
"customerId": loan_data.get('customerId'),
|
||||
"accountId": loan_data.get('accountId'),
|
||||
"transactionId": loan_data.get('transactionId'),
|
||||
"transactionType": "provide",
|
||||
"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())
|
||||
result = response.json()
|
||||
LoanService.set_disburse_verify_result(loan_data['debtId'],result.get('responseCode', ''), result.get('responseMessage', ''))
|
||||
sms_data = {
|
||||
"dest": "2347038224367",
|
||||
"text": "test",
|
||||
"unicode": True
|
||||
}
|
||||
try:
|
||||
sms_response = requests.post(sms_url, json=sms_data, timeout=10, headers=get_headers())
|
||||
sms_response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
|
||||
result = sms_response.json()
|
||||
logger.info(f"SMS Response JSON: {result}")
|
||||
if result.get('isSuccess'):
|
||||
logger.info(f"sms sent successfully")
|
||||
return ResponseHelper.success(response.json(), "Successful")
|
||||
logger.info(f"sms failed!")
|
||||
return 1
|
||||
except requests.RequestException as e:
|
||||
# Handle the exception
|
||||
logger.error(f"Failed to send SMS: {e}")
|
||||
return 0
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call TransactionVerify endpoint: {e}")
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def collect_loan(data):
|
||||
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}{SimbrellaClient.BANK_CALL_COLLECT_LOAN_ENDPOINT}"
|
||||
logger.info(f"Calling CollectLoan api_url==> : {api_url}")
|
||||
logger.info(f"Calling CollectLoan endpoint with data: {data}")
|
||||
|
||||
# Check if the repayment exists
|
||||
logger.info(f"Checking if repayment exists")
|
||||
repayment = RepaymentService.get_repayment_by_transaction_id(transaction_id=data['transactionId'])
|
||||
logger.info(f"Repayment Response From Database ** : {repayment}")
|
||||
|
||||
if not repayment:
|
||||
logger.info(f"Repayment with transactionId: {data['transactionId']}, was not found")
|
||||
return ResponseHelper.error("Repayment not found")
|
||||
|
||||
repayment_data = repayment.to_dict()
|
||||
loan = LoanService.get_loan_by_transaction_id(transaction_id=repayment_data['transactionId'])
|
||||
|
||||
# If loan is not found
|
||||
if not loan:
|
||||
logger.info(f"Loan with debtId: {repayment_data.loan_id}, was not found")
|
||||
return ResponseHelper.error("Loan not found")
|
||||
|
||||
loan_data = loan.to_dict()
|
||||
|
||||
if repayment_data['repayDate'] is not None:
|
||||
logger.info(
|
||||
f"Please call verify collection : {data['transactionId']} repayment send for processing at {repayment_data['repayDate']}")
|
||||
return ResponseHelper.error("Repayment already processed")
|
||||
|
||||
# let us set repay date
|
||||
RepaymentService.set_repay_date(repayment_data['Id'], repayment_data['customerId'])
|
||||
repayment = RepaymentService.get_repayment_by_transaction_id(transaction_id=data['transactionId'])
|
||||
repayment_data = repayment.to_dict()
|
||||
logger.info(f"Here is your repayment data after setting repay date: {repayment_data}")
|
||||
debtId = str(loan_data.get('debtId', "")).strip().zfill(6)
|
||||
collect_loan_data = {
|
||||
"transactionId": repayment_data['transactionId'],
|
||||
"fbnTransactionId": loan_data['transactionId'],
|
||||
"debtId": debtId,
|
||||
"customerId": repayment_data['customerId'],
|
||||
"accountId": loan_data['accountId'],
|
||||
"productId": repayment_data['productId'],
|
||||
"collectAmount": loan_data['repaymentAmount'],
|
||||
"penalCharge": 5,
|
||||
"channel": "USSD",
|
||||
"collectionMethod": "1",
|
||||
"lienAmount": 0,
|
||||
"countryId": "01",
|
||||
"comment": "COLLECT LOAN"
|
||||
}
|
||||
|
||||
try:
|
||||
logger.info(f"Here is your CollectLoan Request data ***** : {collect_loan_data}")
|
||||
response = requests.post(api_url, json=collect_loan_data, headers=get_headers())
|
||||
|
||||
logger.info(f"CollectLoan response: {response.json()}")
|
||||
RepaymentService.set_repay_result(repayment_data['Id'], response.json().get('responseCode', ''), response.json().get('responseMessage', ''))
|
||||
return ResponseHelper.success(response.json(), "Successful")
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call CollectLoan endpoint: {e}")
|
||||
return ResponseHelper.error("Failed to call CollectLoan endpoint")
|
||||
|
||||
|
||||
@staticmethod
|
||||
def refresh_disbursement(data):
|
||||
|
||||
try:
|
||||
logger.info(f"Here is your Disbursement Request data ***** : {data}")
|
||||
|
||||
return ResponseHelper.success(data, "Successful")
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call Disbursement endpoint: {e}")
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def payment_callback(data):
|
||||
|
||||
try:
|
||||
logger.info(f"Here is your Payment Callback Request data ***** : {data}")
|
||||
|
||||
return ResponseHelper.success(data, "Successful")
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call Payment Callback endpoint: {e}")
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def penal_charge(data):
|
||||
|
||||
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/PenalCharge"
|
||||
logger.info(f"Calling Penal Charge endpoint with data: {data}")
|
||||
|
||||
try:
|
||||
logger.info(f"Here is your Penal Charge Request data ***** : {data}")
|
||||
|
||||
try:
|
||||
logger.info(f"Here is your Penal Charge Request data ****** : {data}")
|
||||
response = requests.post(api_url, json=data, timeout=10, headers=get_headers())
|
||||
logger.info(f"Penal Charge response: {response.json()}")
|
||||
return ResponseHelper.success(response.json(), "Successful")
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call Penal Charge endpoint: {e}")
|
||||
return ResponseHelper.error("An error occurred", 500)
|
||||
|
||||
except Exception as e:
|
||||
logger.info(f"Failed to call Penal Charge endpoint: {e}")
|
||||
raise
|
||||
Reference in New Issue
Block a user