-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
282 lines (236 loc) · 9.58 KB
/
app.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import json
import os
from flask import Flask, render_template, request, abort, send_from_directory, redirect, url_for
from flask_caching import Cache
import config as config
from makeCall import makeCall
import uuid
from config import read_config
from payByLink import adyen_pay_by_link
app = Flask(__name__)
cache = Cache(app)
authonly = False
def page_not_found(error):
return render_template('error.html'), 404
# Register 404 handler
app.register_error_handler(404, page_not_found)
# read in values from config.ini file and load them into project
read_config()
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template('home.html')
@app.route('/cart/<integration>')
def cart(integration):
return render_template('cart.html', method=integration)
@app.route('/checkout/<integration>')
def checkout(integration):
if integration in config.supported_integrations:
body = {
"merchantAccount": config.merchant_account,
"amount": {
"currency": "EUR",
"value": 5808
},
"shopperReference": "prueba"
#"splitCardFundingSources": "true"
#"shopperReference": "shopperNoExistente"
}
# "shopperReference": "xee6f62b4-9a22-4860-b6c9-e69de062ba61"
resp = makeCall('paymentMethods', json.dumps(body), config.checkout_apikey)
return render_template('component.html', method=integration, client_key=config.client_key, payments=resp.text)
elif integration == 'paymentLink':
payUrl = adyen_pay_by_link(config.merchant_account, config.checkout_apikey)
return redirect(payUrl, code=302)
else:
abort(404)
# @cache.cached(timeout=300)
@app.route('/makePayment', methods=['GET', 'POST'])
def makePayment():
data = request.json
if ('storedPaymentMethodId' in data['paymentMethod']):
data['shopperInteraction'] = 'ContAuth'
data['recurringProcessingModel'] = 'CardOnFile'
data['shopperReference'] = "xee6f62b4-9a22-4860-b6c9-e69de062ba61"
#if(data['paymentMethod']['brand']=='maestro'):
# data['shopperInteraction'] = 'Ecommerce'
if ('storePaymentMethod' in data) and (data['storePaymentMethod'] == True):
data['shopperInteraction'] = 'Ecommerce'
data['recurringProcessingModel'] = 'CardOnFile'
data['shopperReference'] = "xee6f62b4-9a22-4860-b6c9-e69de062ba61"
if (authonly):
data['threeDSAuthenticationOnly'] = 'true'
reference = str(uuid.uuid4())
returnUrl = data['origin'] + '/handleShopperRedirect?orderRef=' + reference
body_string = """{
"enablePayOut" : false,
"merchantAccount": \"""" + config.merchant_account + """\",
"amount": {
"currency": "EUR",
"value": 6000
},""" + json.dumps(data).replace('\'', '\"')[1: -1] + """,
"reference": \"""" + reference + """\",
"shopperLocale": "es_ES",
"countryCode": "ES",
"shopperIP":"192.0.2.1",
"channel":"web",
"telephoneNumber": "+346763507s90",
"additionalData": {
"allow3DS2": true
},
"shopperEmail":"youremail@email.com",
"shopperName":{
"firstName":"Testperson-es",
"gender":"UNKNOWN",
"lastName":"Approved"
},
"shopperStatement":"prueba de Shopper Statement",
"shopperReference": "prueba",
"billingAddress": {
"country": "ES",
"city": "Madrid",
"street": "Atocha",
"houseNumberOrName": "1",
"stateOrProvince": "N/A",
"postalCode": "28002"
},
"lineItems":[
{
"quantity":"1",
"amountExcludingTax":"5000",
"taxPercentage":"0",
"description":"Test item 1",
"id":"item1",
"taxAmount":"0",
"amountIncludingTax":"5000"
},
{
"quantity":"1",
"amountExcludingTax":"10000",
"taxPercentage":"0",
"description":"Test item 2",
"id":"item2",
"taxAmount":"0",
"amountIncludingTax":"10000"
}
],
"returnUrl": \"""" + returnUrl + """\"
}"""
# "threeDSAuthenticationOnly":true, //Flujo autenticacion y autorizacion por separado.
# "additionalData": {
# "allow3DS2": true
# },
# "shopperReference": "xee6f62b4-9a22-4860-b6c9-e69de062ba61",
#"customRoutingFlag": "mcDebit"
#"riskData":{
#"riskProfileReference":"8016358411114661"
#},
body = json.loads(body_string)
resp = makeCall('payments', json.dumps(body), config.checkout_apikey)
'''if('action' in resp.json()):
if(resp.json()['action']['type']== "redirect"):
global paymentData
paymentData=resp.json()['action']['paymentData']'''
print(resp)
return resp.text
@app.route('/makeDetailsCall', methods=['GET', 'POST'])
def makeDetailsCall():
print(request.json)
data = request.json
if (authonly):
data['threeDSAuthenticationOnly'] = 'true' # solo para flujo Auth Only
resp = makeCall('payments/details', json.dumps(data), config.checkout_apikey)
if resp.json()["resultCode"] == 'AuthenticationFinished':
reference = str(uuid.uuid4())
body = {
"amount": {
"currency": "EUR",
"value": 50000
},
"reference": reference,
"paymentMethod": {
"type": "scheme",
"encryptedCardNumber": "test_5201285093823592",
"encryptedExpiryMonth": "test_03",
"encryptedExpiryYear": "test_2030",
"encryptedSecurityCode": "test_737"
},
"mpiData": {
"cavv": resp.json()['threeDS2Result']["threeDSServerTransID"],
"eci": resp.json()['threeDS2Result']["eci"],
"dsTransID": resp.json()['threeDS2Result']["threeDSServerTransID"],
"authenticationResponse": resp.json()['threeDS2Result']['transStatus'],
"threeDSVersion": resp.json()['threeDS2Result']["messageVersion"]
},
"channel": "web",
"merchantAccount": "MerchantTestNatalia"
}
resp_authorise = makeCall('payments', json.dumps(body), config.checkout_apikey)
print(resp_authorise.json()["resultCode"])
resp = resp_authorise
return resp.text
@app.route('/handleShopperRedirect', methods=['GET', 'POST'])
def handleShopperRedirect():
details = {}
if request.method == "GET":
redirectResult = ''
if ('redirectResult' in request.args.keys()):
redirectResult = request.args.get('redirectResult')
details = {"redirectResult": redirectResult}
if request.method == "POST":
md = request.form['MD']
pares = request.form['PaRes']
details = {"MD": md,
"PaRes": pares}
'''global paymentData
print(paymentData)
body={ "details" : details, "paymentData": paymentData }'''
body = {"details": details}
resp = makeCall('payments/details', json.dumps(body), config.checkout_apikey)
print(resp)
if resp.json()["resultCode"] == 'Authorised':
return redirect(url_for('checkout_success'))
elif resp.json()["resultCode"] == 'Received' or resp.json()["resultCode"] == 'Pending':
return redirect(url_for('checkout_pending'))
else:
return redirect(url_for('checkout_failure'))
@app.route('/process_payment', methods=['GET', 'POST'])
def process_payment():
print(request.args.keys())
if request.method == "GET":
if ('amazonCheckoutSessionId' in request.args.keys()):
amazonSessionId = request.args.get('amazonCheckoutSessionId')
print(amazonSessionId)
body = {
"merchantAccount": config.merchant_account,
"amount": {
"currency": "EUR",
"value": 5808
},
"shopperReference": "prueba"
# "splitCardFundingSources": "true"
# "shopperReference": "shopperNoExistente"
}
# "shopperReference": "xee6f62b4-9a22-4860-b6c9-e69de062ba61"
resp = makeCall('paymentMethods', json.dumps(body), config.checkout_apikey)
return render_template('amazon.html', amazonSessionId=amazonSessionId, client_key=config.client_key, payments=resp.text)
else:
return redirect(url_for('checkout_failure'))
@app.route('/result/success', methods=['GET'])
def checkout_success():
return render_template('checkout-success.html')
@app.route('/result/failed', methods=['GET'])
def checkout_failure():
return render_template('checkout-failed.html')
@app.route('/result/pending', methods=['GET'])
def checkout_pending():
return render_template('checkout-success.html')
@app.route('/result/error', methods=['GET'])
def checkout_error():
return render_template('checkout-failed.html')
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'img/favicon.ico')
if __name__ == '__main__':
#ssl_context='adhoc',
app.run(debug=True, host='0.0.0.0',port=8000)