stop giving false json

This commit is contained in:
Shihaam Abdul Rahman 2024-12-09 08:45:34 +05:00
parent 867b32e45e
commit 5fa2bf780d
Signed by: shihaam
GPG Key ID: 6DA2E87EBC227636

27
api.py
View File

@ -69,10 +69,25 @@ def compare_amounts(amount1, amount2):
"""Compare two amount strings as Decimal objects."""
return Decimal(amount1) == Decimal(amount2)
@app.errorhandler(400)
def bad_request(error):
return jsonify({"success": False, "message": "Invalid request: Malformed JSON"}), 400
@app.errorhandler(500)
def internal_error(error):
return jsonify({"success": False, "message": "Internal server error"}), 500
@app.route('/verify-payment', methods=['POST'])
def verify_payment():
data = request.json
benef_name = data.get('benefName', '').strip()
try:
if not request.is_json:
return jsonify({"success": False, "message": "Request must be JSON"}), 400
data = request.get_json()
if data is None:
return jsonify({"success": False, "message": "Invalid JSON format"}), 400
benef_name = data.get('benefName', '').strip() if data.get('benefName') else ''
account_no = data.get('accountNo')
abs_amount = data.get('absAmount')
time_str = data.get('time')
@ -102,9 +117,9 @@ def verify_payment():
return jsonify({"success": False, "message": "No transaction data found"}), 404
except subprocess.CalledProcessError as e:
return jsonify({"success": False, "message": f"Error executing tx.sh: {str(e)}", "stderr": e.stderr}), 500
return jsonify({"success": False, "message": f"Error executing tx.sh: {str(e)}"}), 500
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)}"}), 500
# First, try to verify with benefName if provided
if benef_name:
@ -131,6 +146,10 @@ def verify_payment():
# If both verifications fail
return jsonify({"success": False, "message": "Transaction not found, contact support"})
except Exception as e:
app.logger.error(f"Unexpected error: {str(e)}")
return jsonify({"success": False, "message": "Internal server error"}), 500
if __name__ == '__main__':
debug_mode = os.getenv('APP_DEBUG', 'False').lower() in ('true', '1', 't')
port = int(os.getenv('PORT', 5000))