add getaccount route
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 2m55s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 2m55s
This commit is contained in:
106
api.py
106
api.py
@@ -6,13 +6,61 @@ from dotenv import load_dotenv
|
|||||||
from flask import Flask, request, jsonify
|
from flask import Flask, request, jsonify
|
||||||
import subprocess
|
import subprocess
|
||||||
import json
|
import json
|
||||||
|
import requests
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
load_dotenv() # This will load environment variables from a .env file if it exists
|
load_dotenv() # This will load environment variables from a .env file if it exists
|
||||||
TIME_DIFF_LIMIT = int(os.getenv('TIME_DIFF_LIMIT', 2)) # Default to 2mins if not set
|
TIME_DIFF_LIMIT = int(os.getenv('TIME_DIFF_LIMIT', 2)) # Default to 2mins if not set
|
||||||
|
COOKIE_SERVER = os.getenv('COOKIE_SERVER', 'http://localhost:5000')
|
||||||
|
|
||||||
|
# Bank BIC to name mapping
|
||||||
|
BANK_BIC_NAMES = {
|
||||||
|
"MADVMVMV": "Maldives Islamic Bank PLC",
|
||||||
|
"MALBMVMV": "Bank of Maldives",
|
||||||
|
}
|
||||||
|
|
||||||
|
BANK_BIC_SHORT = {
|
||||||
|
"MADVMVMV": "MIB",
|
||||||
|
"MALBMVMV": "BML",
|
||||||
|
}
|
||||||
|
|
||||||
|
# Shared headers (same as getcookie.py)
|
||||||
|
BASE_HEADERS = {
|
||||||
|
"User-Agent": "Mozilla/5.0 (ismath-said-will) give-real-api/so-i-wont-have-to-do-this AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36",
|
||||||
|
}
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
def get_cookies():
|
||||||
|
"""Fetch authentication cookies from the cookie server."""
|
||||||
|
try:
|
||||||
|
response = requests.get(f"{COOKIE_SERVER}/getcookie", timeout=30)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.json()
|
||||||
|
except requests.RequestException as e:
|
||||||
|
app.logger.error(f"Error fetching cookies: {str(e)}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_ips_account(account_number):
|
||||||
|
"""Look up account details via MIB IPS API."""
|
||||||
|
cookies = get_cookies()
|
||||||
|
if not cookies:
|
||||||
|
return None, "Failed to get authentication cookies"
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.post(
|
||||||
|
'https://faisanet.mib.com.mv/AjaxAlias/getIPSAccount',
|
||||||
|
headers=BASE_HEADERS,
|
||||||
|
cookies=cookies,
|
||||||
|
data={'benefAccount': account_number},
|
||||||
|
timeout=30
|
||||||
|
)
|
||||||
|
return response.json(), None
|
||||||
|
except requests.RequestException as e:
|
||||||
|
return None, f"Request failed: {str(e)}"
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
return None, f"Invalid JSON response: {str(e)}"
|
||||||
|
|
||||||
def fetch_account_name(account_no):
|
def fetch_account_name(account_no):
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(['./fetchname.sh', account_no], capture_output=True, text=True, check=True)
|
result = subprocess.run(['./fetchname.sh', account_no], capture_output=True, text=True, check=True)
|
||||||
@@ -150,6 +198,64 @@ def verify_payment():
|
|||||||
app.logger.error(f"Unexpected error: {str(e)}")
|
app.logger.error(f"Unexpected error: {str(e)}")
|
||||||
return jsonify({"success": False, "message": "Internal server error"}), 500
|
return jsonify({"success": False, "message": "Internal server error"}), 500
|
||||||
|
|
||||||
|
@app.route('/getaccount/<accountnumber>', methods=['GET'])
|
||||||
|
def get_account(accountnumber):
|
||||||
|
try:
|
||||||
|
result, error = get_ips_account(accountnumber)
|
||||||
|
|
||||||
|
if error:
|
||||||
|
return jsonify({
|
||||||
|
"success": False,
|
||||||
|
"message": error,
|
||||||
|
"accountName": None,
|
||||||
|
"bankSwift": None,
|
||||||
|
"currency": None,
|
||||||
|
"bankName": None,
|
||||||
|
"bankNameShort": None
|
||||||
|
}), 500
|
||||||
|
|
||||||
|
if not result.get('success'):
|
||||||
|
reason_text = result.get('reasonText', '')
|
||||||
|
if reason_text == "Transaction forbidden on this type of account.":
|
||||||
|
message = "Invalid currency"
|
||||||
|
elif reason_text == "Account number is invalid or missing.":
|
||||||
|
message = "Invalid account"
|
||||||
|
else:
|
||||||
|
message = reason_text or "Account not found"
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
"success": False,
|
||||||
|
"message": message,
|
||||||
|
"accountName": None,
|
||||||
|
"bankSwift": None,
|
||||||
|
"currency": None,
|
||||||
|
"bankName": None,
|
||||||
|
"bankNameShort": None
|
||||||
|
}), 404
|
||||||
|
|
||||||
|
bank_bic = result.get('bankBic', '')
|
||||||
|
return jsonify({
|
||||||
|
"success": True,
|
||||||
|
"message": "Success",
|
||||||
|
"accountName": result.get('accountName'),
|
||||||
|
"bankSwift": bank_bic,
|
||||||
|
"currency": "MVR",
|
||||||
|
"bankName": BANK_BIC_NAMES.get(bank_bic, "<unknown>"),
|
||||||
|
"bankNameShort": BANK_BIC_SHORT.get(bank_bic, "<unknown>")
|
||||||
|
})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
app.logger.error(f"Error in get_account: {str(e)}")
|
||||||
|
return jsonify({
|
||||||
|
"success": False,
|
||||||
|
"message": "Internal server error",
|
||||||
|
"accountName": None,
|
||||||
|
"bankSwift": None,
|
||||||
|
"currency": None,
|
||||||
|
"bankName": None,
|
||||||
|
"bankNameShort": None
|
||||||
|
}), 500
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
debug_mode = os.getenv('APP_DEBUG', 'False').lower() in ('true', '1', 't')
|
debug_mode = os.getenv('APP_DEBUG', 'False').lower() in ('true', '1', 't')
|
||||||
port = int(os.getenv('PORT', 5000))
|
port = int(os.getenv('PORT', 5000))
|
||||||
|
|||||||
Reference in New Issue
Block a user