attempt to use Name first, if name fails, then use Account number
This commit is contained in:
parent
6e0fc175d9
commit
16a226393f
73
api.py
73
api.py
@ -21,39 +21,49 @@ def fetch_account_name(account_no):
|
|||||||
app.logger.error(f"Error fetching account name: {str(e)}")
|
app.logger.error(f"Error fetching account name: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def verify_transaction(benef_name, abs_amount, request_time, tx_data_list):
|
||||||
|
for tx_data in tx_data_list:
|
||||||
|
required_keys = ['trxDate', 'benefName', 'absAmount']
|
||||||
|
if not all(key in tx_data for key in required_keys):
|
||||||
|
continue # Skip this transaction if it's missing required keys
|
||||||
|
|
||||||
|
try:
|
||||||
|
tx_time = datetime.strptime(tx_data['trxDate'], "%Y-%m-%d %H:%M:%S")
|
||||||
|
time_diff = abs(tx_time - request_time)
|
||||||
|
|
||||||
|
tx_benef_name = tx_data['benefName'].strip().lower()
|
||||||
|
|
||||||
|
if (tx_benef_name == benef_name.strip().lower() and
|
||||||
|
str(tx_data['absAmount']) == str(abs_amount) and
|
||||||
|
time_diff <= timedelta(minutes=30)):
|
||||||
|
return True
|
||||||
|
except ValueError as e:
|
||||||
|
app.logger.error(f"Error processing transaction: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
@app.route('/verify-payment', methods=['POST'])
|
@app.route('/verify-payment', methods=['POST'])
|
||||||
def verify_payment():
|
def verify_payment():
|
||||||
# Get data from request
|
|
||||||
data = request.json
|
data = request.json
|
||||||
benef_name = data.get('benefName', '').strip().lower()
|
benef_name = data.get('benefName', '').strip()
|
||||||
|
account_no = data.get('accountNo')
|
||||||
abs_amount = data.get('absAmount')
|
abs_amount = data.get('absAmount')
|
||||||
time_str = data.get('time')
|
time_str = data.get('time')
|
||||||
account_no = data.get('accountNo')
|
|
||||||
|
|
||||||
# Fetch account name if account number is provided
|
if not all([abs_amount, time_str]):
|
||||||
if account_no:
|
|
||||||
fetched_name = fetch_account_name(account_no)
|
|
||||||
if fetched_name:
|
|
||||||
benef_name = fetched_name.strip().lower()
|
|
||||||
else:
|
|
||||||
return jsonify({"success": False, "message": "Failed to fetch account name"}), 400
|
|
||||||
|
|
||||||
# Validate input
|
|
||||||
if not all([benef_name, abs_amount, time_str]):
|
|
||||||
return jsonify({"success": False, "message": "Missing required parameters"}), 400
|
return jsonify({"success": False, "message": "Missing required parameters"}), 400
|
||||||
|
|
||||||
|
if not benef_name and not account_no:
|
||||||
|
return jsonify({"success": False, "message": "Either benefName or accountNo must be provided"}), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Parse the input time
|
|
||||||
request_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M")
|
request_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return jsonify({"success": False, "message": "Invalid time format"}), 400
|
return jsonify({"success": False, "message": "Invalid time format"}), 400
|
||||||
|
|
||||||
# Execute tx.sh and get the output
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(['./tx.sh'], capture_output=True, text=True, check=True)
|
result = subprocess.run(['./tx.sh'], capture_output=True, text=True, check=True)
|
||||||
tx_response = json.loads(result.stdout)
|
tx_response = json.loads(result.stdout)
|
||||||
|
|
||||||
# Debug: Print the entire tx_response
|
|
||||||
app.logger.debug(f"tx_response: {json.dumps(tx_response, indent=2)}")
|
app.logger.debug(f"tx_response: {json.dumps(tx_response, indent=2)}")
|
||||||
|
|
||||||
if not tx_response.get('success'):
|
if not tx_response.get('success'):
|
||||||
@ -68,30 +78,17 @@ def verify_payment():
|
|||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
return jsonify({"success": False, "message": f"Error parsing tx.sh output: {str(e)}", "output": result.stdout}), 500
|
return jsonify({"success": False, "message": f"Error parsing tx.sh output: {str(e)}", "output": result.stdout}), 500
|
||||||
|
|
||||||
# Check transactions
|
# First, try to verify with benefName if provided
|
||||||
for tx_data in tx_data_list:
|
if benef_name and verify_transaction(benef_name, abs_amount, request_time, tx_data_list):
|
||||||
# Check if the required keys exist in tx_data
|
return jsonify({"success": True, "message": "Payment verified using beneficiary name"})
|
||||||
required_keys = ['trxDate', 'benefName', 'absAmount']
|
|
||||||
missing_keys = [key for key in required_keys if key not in tx_data]
|
|
||||||
if missing_keys:
|
|
||||||
continue # Skip this transaction if it's missing required keys
|
|
||||||
|
|
||||||
try:
|
# If benefName verification failed or wasn't provided, try with accountNo
|
||||||
tx_time = datetime.strptime(tx_data['trxDate'], "%Y-%m-%d %H:%M:%S")
|
if account_no:
|
||||||
time_diff = abs(tx_time - request_time)
|
fetched_name = fetch_account_name(account_no)
|
||||||
|
if fetched_name and verify_transaction(fetched_name, abs_amount, request_time, tx_data_list):
|
||||||
# Normalize transaction beneficiary name for comparison
|
return jsonify({"success": True, "message": "Payment verified using account number"})
|
||||||
tx_benef_name = tx_data['benefName'].strip().lower()
|
|
||||||
|
|
||||||
if (tx_benef_name == benef_name and
|
|
||||||
str(tx_data['absAmount']) == str(abs_amount) and
|
|
||||||
time_diff <= timedelta(minutes=30)):
|
|
||||||
return jsonify({"success": True, "message": "Payment verified"})
|
|
||||||
except ValueError as e:
|
|
||||||
app.logger.error(f"Error processing transaction: {str(e)}")
|
|
||||||
continue # Skip this transaction if there's an error processing it
|
|
||||||
|
|
||||||
# If we've checked all transactions and found no match
|
# If both verifications fail
|
||||||
return jsonify({"success": False, "message": "Transaction not found, contact support"})
|
return jsonify({"success": False, "message": "Transaction not found, contact support"})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user