-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
112 lines (86 loc) · 3.95 KB
/
app.py
File metadata and controls
112 lines (86 loc) · 3.95 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import logging
import json
from flask import Flask
from flask import request
from time import strftime
from logging.handlers import RotatingFileHandler
MOCKED_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<callbacks_payment_response xmlns="http://api.forticom.com/1.0/">
true
</callbacks_payment_response>
"""
MOCKED_RESPONSE_JSON = """true"""
ERROR_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<ns2:error_response xmlns:ns2='http://api.forticom.com/1.0/'>
<error_code>1001</error_code>
<error_msg>CALLBACK_INVALID_PAYMENT : Payment is invalid and can not be processed</error_msg>
</ns2:error_response>
"""
ERROR_RESPONSE_JSON = """{
"error_code": 1001,
"error_msg": "CALLBACK_INVALID_PAYMENT : Payment is invalid and can not be processed",
"error_data": null
}
"""
BAD_PRODUCT_CODE = "product_error"
app = Flask(__name__)
@app.route('/callback', methods=["GET"])
def callback_mock_get():
ts = strftime('%Y-%b-%d %H:%M:%S')
logger.debug("[{}] [{}]: [{}]".format(ts, request.method, request.args))
if request.args.get("product_code") == BAD_PRODUCT_CODE:
return ERROR_RESPONSE, {'Content-Type': 'application/xml', 'Invocation-error': 1001}
return MOCKED_RESPONSE, {'Content-Type': 'application/xml'}
@app.route('/callback', methods=["POST"])
def callback_mock_post():
ts = strftime('%Y-%b-%d %H:%M:%S')
logger.debug("[{}] [{}]: [{}]".format(ts, request.method, request.form))
if request.args.get("product_code") == BAD_PRODUCT_CODE:
return ERROR_RESPONSE, {'Content-Type': 'application/xml', 'Invocation-error': 1001}
return MOCKED_RESPONSE, {'Content-Type': 'application/xml'}
@app.route('/callback_json', methods=["GET"])
def callback_mock_json_get():
ts = strftime('%Y-%b-%d %H:%M:%S')
logger.debug("[{}] [{}]: [{}]".format(ts, request.method, request.args))
if request.args.get("product_code") == BAD_PRODUCT_CODE:
return ERROR_RESPONSE_JSON, {'Content-Type': 'application/json', 'Invocation-error': 1001}
return MOCKED_RESPONSE_JSON, {'Content-Type': 'application/json'}
@app.route('/callback_json', methods=["POST"])
def callback_mock_json_post():
ts = strftime('%Y-%b-%d %H:%M:%S')
logger.debug("[{}] [{}]: [{}]".format(ts, request.method, request.form))
if request.args.get("product_code") == BAD_PRODUCT_CODE:
return ERROR_RESPONSE_JSON, {'Content-Type': 'application/json', 'Invocation-error': 1001}
return MOCKED_RESPONSE_JSON, {'Content-Type': 'application/json'}
@app.route('/callback_error', methods=["GET"])
def callback_error_mock_get():
ts = strftime('%Y-%b-%d %H:%M:%S')
logger.debug("[{}] [{}]: [{}]".format(ts, request.method, request.args))
return ERROR_RESPONSE, {'Content-Type': 'application/xml', 'Invocation-error': 1001}
@app.route('/callback_error', methods=["POST"])
def callback_error_mock_post():
ts = strftime('%Y-%b-%d %H:%M:%S')
logger.debug("[{}] [{}]: [{}]".format(ts, request.method, request.form))
return ERROR_RESPONSE, {'Content-Type': 'application/xml', 'Invocation-error': 1001}
@app.route('/log', methods=["GET"])
def view_log():
with open("app.log") as log_file:
str_log = "<br/>".join(line.strip("\n") for line in log_file.readlines())
return str_log
@app.route('/flush_log', methods=["POST"])
def flush_log():
open("app.log", 'w').close()
@app.route('/events', methods=["POST"])
def callback_events():
ts = strftime('%Y-%b-%d %H:%M:%S')
data = json.loads(request.data.decode("utf-8"))
logger.debug("[EVENTS] [{}] [{}]: [{}]".format(ts, request.method, data))
if data["webhookType"] == "CONFIRMATION":
return "84fhwgrd", {'Content-Type': 'application/text'}
return "OK", {'Content-Type': 'application/text'}
if __name__ == '__main__':
handler = RotatingFileHandler('app.log', maxBytes=10000, backupCount=3)
logger = logging.getLogger('__name__')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
app.run(debug=True, host="localhost", port=8000)