179 lines
6.4 KiB
Python
179 lines
6.4 KiB
Python
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
|
|
|
|
|
|
class SimbrellaClient:
|
|
|
|
BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL
|
|
|
|
@staticmethod
|
|
def disbursement(data):
|
|
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/Disbursement"
|
|
logger.info(f"Calling Disbursement 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"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}")
|
|
|
|
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']
|
|
|
|
disbursement_data ={
|
|
"requestId": data['requestId'],
|
|
"transactionId": data['transactionId'],
|
|
"debtId": loan_data['debtId'],
|
|
"customerId": data['customerId'],
|
|
"accountId": data['accountId'],
|
|
"productId": loan_data['productId'],
|
|
"provideAmount": loan_data['currentLoanAmount'],
|
|
"collectAmountInterest": 5000,
|
|
"collectAmountMgtFee": mgt_fee,
|
|
"collectAmountInsurance": 1000,
|
|
"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()}")
|
|
|
|
except Exception as e:
|
|
logger.info(f"Failed to call Disbursement endpoint: {e}")
|
|
return 0
|
|
|
|
return 1
|
|
|
|
@staticmethod
|
|
def collect_loan(data):
|
|
api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/CollectLoan"
|
|
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"Response from database: {repayment}")
|
|
|
|
# If repayment is not found
|
|
if not repayment:
|
|
logger.info(f"Repayment id: {data['transactionId']}, was not found")
|
|
return 0
|
|
|
|
collect_loan_data = {
|
|
"transactionId": "T002",
|
|
"fbnTransactionId": "FBN20231123",
|
|
"debtId": data['debtId'],
|
|
"customerId": data['customerId'],
|
|
"accountId": "2017821799",
|
|
"productId": data['productId'],
|
|
"collectAmount": 80000,
|
|
"penalCharge": 0,
|
|
"collectionMethod": 1,
|
|
"lienAmount": 80000,
|
|
"countryId": "01",
|
|
"comment": "Testing CollectionLoanRequest"
|
|
}
|
|
|
|
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()}")
|
|
|
|
except Exception as e:
|
|
logger.info(f"Failed to call CollectLoan endpoint: {e}")
|
|
return 0
|
|
|
|
return 1
|
|
|
|
@staticmethod
|
|
def verify_transaction():
|
|
try:
|
|
|
|
data = {
|
|
"status": "00",
|
|
"message": "Transaction verified"
|
|
}
|
|
return ResponseHelper.success(data, "Successful")
|
|
|
|
except Exception as e:
|
|
logger.info(f"Failed to call TransactionVerify endpoint: {e}")
|
|
raise
|
|
|
|
@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 |