-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
242 lines (178 loc) · 7.87 KB
/
index.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
from bottle import Bottle, run, request, template, response, HTTPResponse, static_file
import requests
import json
import qrcode
from PIL import Image
from io import BytesIO
import pprint
app = Bottle()
@app.route('/', method='GET')
def homepage():
return template('index.html', access_token='', targetAccount='', sourceAccounts='')
@app.route('/webshop', method='GET')
def create():
code = request.GET.get('code')
return template('webshop.html')
@app.route('/create-button', method='GET')
def create():
code = request.GET.get('code')
return template('create-button.html')
def get_accounts(access_token, profileid):
headers = {
'accept': "application/json",
'authorization': "Bearer " + access_token
}
respAccounts = requests.get("https://test-restgw.transferwise.com/v1/accounts?profile=" + str(profileid), headers=headers)
accounts = []
for a in json.loads(respAccounts.text):
accounts.append({'currency': a.get('currency'),
'accountNumber': a.get('details').get('accountNumber'),
'account_id': a.get('id')})
return accounts
def get_profileid(access_token):
headers = {
'accept': "application/json",
'authorization': "Bearer " + access_token
}
respProfiles = requests.get("https://test-restgw.transferwise.com/v1/profiles", headers=headers)
for p in json.loads(respProfiles.text):
if p['type'] == 'personal':
return p['id']
print ('No profile')
return None
def get_profile(access_token):
headers = {
'accept': "application/json",
'authorization': "Bearer " + access_token
}
respProfiles = requests.get("https://test-restgw.transferwise.com/v1/profiles", headers=headers)
for p in json.loads(respProfiles.text):
if p['type'] == 'personal':
return p
print ('No profile')
return None
@app.route('/donate', method='GET')
def get_donate():
code = request.GET.get('code')
target = request.GET.get('target')
targetAccountId = target.split('_')[0]
targetCountry = target.split('_')[0]
targetCurrency = target.split('_')[2]
targetAccountNumber = target.split('_')[3]
targetSortCode = target.split('_')[4]
targetFirstName = target.split('_')[5]
targetLastName = target.split('_')[6]
print(code)
data = {
'grant_type': 'authorization_code',
'client_id': 'f272f4a3-ecc1-44fe-b3f4-9a20e9433f4e',
'code': code,
'redirect_uri': 'http://localhost:8000/donate?target=' + targetAccountId + '_' + targetCurrency
}
resp = requests.post('https://test-restgw.transferwise.com/oauth/token',
data=data, auth=('f272f4a3-ecc1-44fe-b3f4-9a20e9433f4e', '534cda42-719c-4b26-86c2-c96b7cb03437'))
access_token = json.loads(resp.text).get('access_token')
profileid = get_profileid(access_token)
sourceAccounts = get_accounts(access_token, profileid)
print(sourceAccounts)
# selected from form
sourceAccount = sourceAccounts[3].get('account_id')
sourceCurrency = sourceAccounts[3].get('currency')
print('sourceAccount', sourceAccount)
print('sourceCurrency', sourceCurrency)
print('targetCurrency', targetCurrency)
sourceAmount = 100
message = 'Test sjssj'
def create_quote(access_token, profileid, sourceCurrency, sourceAmount, targetCurrency):
payload = "{\"profile\":" + str(profileid) + ",\"rateType\":\"FIXED\", \
\"source\":\"" + sourceCurrency + "\",\"sourceAmount\":" + str(sourceAmount) + ",\"target\":\"" + targetCurrency + "\"}"
headers = {
'accept': "application/json",
'authorization': "Bearer " + access_token,
'content-type': "application/json"
}
resp = requests.post("https://test-restgw.transferwise.com/v1/quotes", data=payload, headers=headers)
return json.loads(resp.text).get('id')
quote_id = create_quote(access_token, profileid, sourceCurrency, sourceAmount, targetCurrency)
print('quote_id', quote_id)
def create_transfer(access_token, source_account, target_account, quote_id, message):
payload = "{\"sourceAccount\":" + str(source_account) + ",\"targetAccount\":" + target_account + ",\"quote\":" + str(quote_id) + ",\
\"reference\":\"Early\",\"payInMethod\":\"transfer\"}"
print('payload', payload)
headers = {
'accept': "application/json",
'authorization': "Bearer " + access_token,
'content-type': "application/json"
}
resp = requests.post("https://test-restgw.transferwise.com/v1/transfers", data=payload, headers=headers)
create_transfer(access_token, sourceAccount, targetAccountId, quote_id, message)
return template('index.html', access_token=access_token, targetAccount=targetAccountId, sourceAccounts=sourceAccounts)
@app.route('/css/<filename:re:.*\.css>')
def send_css(filename):
return static_file(filename, root='static/css', mimetype='text/css')
@app.route('/img/<filename:re:.*\.jpg>')
def send_jpg(filename):
return static_file(filename, root='static/img', mimetype='img/jpg')
@app.route('/img/<filename:re:.*\.png>')
def send_png(filename):
return static_file(filename, root='static/img', mimetype='img/png')
@app.route('/fonts/<filename:re:.*\.woff>')
def send_woff(filename):
return static_file(filename, root='static/fonts')
@app.route('/fonts/<filename:re:.*\.woff2>')
def send_woff2(filename):
return static_file(filename, root='static/fonts')
@app.route('/fonts/<filename:re:.*\.ttf>')
def send_ttf(filename):
return static_file(filename, root='static/fonts')
@app.route('/fonts/<filename:re:.*\.eot>')
def send_eot(filename):
return static_file(filename, root='static/fonts')
@app.route('/getButton', method='GET')
def buttonize():
return "It works!"
@app.route('/video/<filename:re:.*\.mp4>')
def send_png(filename):
return static_file(filename, root='static/video', mimetype='video/mp4')
@app.route('/qr', method='GET')
def get_image():
img = qrcode.make(request.query['url'])
with BytesIO() as output:
img.save(output, 'PNG')
data = output.getvalue()
response.set_header('Content-type', 'image/png')
return data
@app.route('/register', method='GET')
def create():
code = request.GET.get('code')
#if not code:
# resp = requests.get('https://test-restgw.transferwise.com/oauth/authorize?response_type=code&client_id=f272f4a3-ecc1-44fe-b3f4-9a20e9433f4e&redirect_uri=http://localhost:8000/register')
#code = request.GET.get('code')
data = {
'grant_type': 'authorization_code',
'client_id': 'f272f4a3-ecc1-44fe-b3f4-9a20e9433f4e',
'code': code,
'redirect_uri': 'http://localhost:8000/register'
}
resp = requests.post('https://test-restgw.transferwise.com/oauth/token',
data=data, auth=('f272f4a3-ecc1-44fe-b3f4-9a20e9433f4e', '534cda42-719c-4b26-86c2-c96b7cb03437'))
#import ipdb; ipdb.set_trace()
access_token = json.loads(resp.text).get('access_token')
print(access_token)
profileid = get_profileid(access_token)
profile = get_profile(access_token)
sourceAccounts = get_accounts(access_token, profileid)
#profileid = profile.get('details').get('firstName')
firstName = profile.get('details').get('firstName')
lastName = profile.get('details').get('lastName')
avatar = profile.get('details').get('avatar')
#sourceAccount = sourceAccounts[0].get('account_id')
currency = sourceAccounts[0].get('currency')
currency='GB'
country = sourceAccounts[0].get('country')
country='GBP'
number = sourceAccounts[0].get('id')
link = 'https://test-restgw.transferwise.com/oauth/authorize?response_type=code&client_id=f272f4a3-ecc1-44fe-b3f4-9a20e9433f4e&redirect_uri=http://localhost:8000/donate?target=3995210_'+country+'_'+currency+'_38285680_309648_'+firstName+'_'+lastName
#code = request.GET.get('code')
return template('registerButton.html', name=firstName+' '+lastName, link=link, avatar=avatar)
run(app, host='127.0.0.1', port=8000)