-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_payment_endpoint.py
82 lines (69 loc) · 2.62 KB
/
fake_payment_endpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask import Flask, request, jsonify, make_response
import argparse
import json
app = Flask(__name__)
@app.route('/api/payments', methods=['POST','GET','DELETE', 'PUT'])
def fake_payment():
default = {
"amount": 100.00,
"transaction_id": "1234567890",
"currency": "USD",
"status": "200",
"message": "Payment processed successfully"
}
data = request.json
print(f"Received payment request: {data}")
if args.data:
enforced_data = json.loads(args.data)
default.update(enforced_data)
else:
enforced_data = {}
if 'amount' in enforced_data:
amount = enforced_data['amount']
else:
amount = data.get('amount', default["amount"])
if 'transaction_id' in enforced_data:
transaction_id = enforced_data['transaction_id']
else:
transaction_id = data.get('transaction_id', default["transaction_id"])
if 'currency' in enforced_data:
currency = enforced_data['currency']
else:
currency = data.get('currency', default["currency"])
status = default["status"]
message = default["message"]
if args.code:
status = args.code
if args.message:
message = args.message
response = {
'status': status,
'message': message,
'transaction_id': transaction_id,
'amount': amount,
'currency': currency
}
additional = args.additional.split(',') if args.additional else []
for x in additional:
if x in enforced_data:
response[x] = enforced_data[x]
else:
response[x] = data.get(x, default.get(x, None))
response_body = jsonify(response)
response_status = int(status)
return make_response(response_body, response_status)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Fake Payment Server')
parser.add_argument('-p', '--port', type=int, default=5000, help='Port to run the server on')
parser.add_argument("-a", "--additional", help="Additional values to read from request", required=False)
parser.add_argument("-m", "--message", help="Fake message to be returned", required=False)
parser.add_argument("-c", "--code", help="Fake code to be returned", required=False)
parser.add_argument('-d', '--data', help='JSON string containing default values')
args = parser.parse_args()
if args.data:
try:
data = json.loads(args.data)
except json.JSONDecodeError:
print("Error: Invalid JSON data string provided.")
exit(1)
app.run(host='0.0.0.0', port=args.port)