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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import time
|
4 |
+
import datetime
|
5 |
+
import requests
|
6 |
+
import paramiko
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
from selenium import webdriver
|
9 |
+
import webbrowser
|
10 |
+
import os
|
11 |
+
from datetime import date
|
12 |
+
from selenium.webdriver.common.by import By
|
13 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
14 |
+
from selenium.webdriver.support import expected_conditions as EC
|
15 |
+
from request_json.sbt_request_generator import generate_request
|
16 |
+
|
17 |
+
load_dotenv()
|
18 |
+
REDIRECT_URL = os.environ.get("REDIRECT_URL")
|
19 |
+
_base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
|
20 |
+
CUSTOMER_TOKEN = os.environ.get("CUSTOMER_TOKEN")
|
21 |
+
APP_ID = os.environ.get("APP_ID")
|
22 |
+
CLIENT_ID = os.environ.get("CLIENT_ID")
|
23 |
+
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
def connect():
|
28 |
+
ssh_host = '18.166.103.96'
|
29 |
+
ssh_user = 'ec2-user'
|
30 |
+
ssh_key_path = '.ssh/ipygg-api-test.pem'
|
31 |
+
|
32 |
+
ssh_client = paramiko.SSHClient()
|
33 |
+
ssh_client.load_system_host_keys()
|
34 |
+
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
35 |
+
ssh_key = paramiko.RSAKey.from_private_key_file(ssh_key_path)
|
36 |
+
ssh_client.connect(ssh_host, username=ssh_user, pkey=ssh_key)
|
37 |
+
print("Successfully connected to the test server")
|
38 |
+
|
39 |
+
transport = ssh_client.get_transport()
|
40 |
+
if transport.is_active():
|
41 |
+
print("SSH connection is active")
|
42 |
+
else:
|
43 |
+
print("SSH connection is not active")
|
44 |
+
|
45 |
+
return ssh_client
|
46 |
+
|
47 |
+
|
48 |
+
def command():
|
49 |
+
mysql_host = 'awseb-e-az6qppyyjf-stack-awsebrdsdatabase-g7iebolqxtzk.ccrm7ijo8xsm.ap-east-1.rds.amazonaws.com'
|
50 |
+
mysql_user = 'iPYGG'
|
51 |
+
mysql_password = 'yt1uQ8aiTd!2cY6K2re342!G6g'
|
52 |
+
mysql_command = f"mysql -h {mysql_host} -u {mysql_user} -p{mysql_password} -e "
|
53 |
+
return mysql_command
|
54 |
+
|
55 |
+
|
56 |
+
def initialize_SBTdb(ssh_client):
|
57 |
+
# ssh_client.exec_command(command() + "'DROP DATABASE IF EXISTS test_SBTdb'")
|
58 |
+
try:
|
59 |
+
ssh_client.exec_command(command() + "'CREATE DATABASE IF NOT EXISTS SBTdb'")
|
60 |
+
ssh_client.exec_command(command() + """'USE SBTdb;
|
61 |
+
CREATE TABLE IF NOT EXISTS token_key (
|
62 |
+
user_id VARCHAR(233) NOT NULL PRIMARY KEY,
|
63 |
+
token_id VARCHAR(233),
|
64 |
+
decryption_key VARBINARY(255),
|
65 |
+
ipfs_link1 VARCHAR(255),
|
66 |
+
ipfs_link2 VARCHAR(255),
|
67 |
+
ipfs_link3 VARCHAR(255),
|
68 |
+
membership_status VARCHAR(255)
|
69 |
+
);'""")
|
70 |
+
except Exception as e:
|
71 |
+
print(e)
|
72 |
+
|
73 |
+
|
74 |
+
def close(ssh_client):
|
75 |
+
ssh_client.close()
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
# **************************API calls**************************
|
81 |
+
def store_attribute(db_name, table_name, restriction_name, restriction_value, attribute_name, attribute_value):
|
82 |
+
ssh_client = connect()
|
83 |
+
try:
|
84 |
+
# Check if the row already exists
|
85 |
+
select_query = f"SELECT COUNT(*) AS count FROM {table_name} WHERE {restriction_name}='{restriction_value}'"
|
86 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {select_query}\"")
|
87 |
+
count_str = stdout.read().decode()
|
88 |
+
print("output: ", count_str)
|
89 |
+
|
90 |
+
count_line = count_str.split('\n')[1]
|
91 |
+
count = int(count_line)
|
92 |
+
print("error:", stderr.read().decode())
|
93 |
+
|
94 |
+
if count > 0:
|
95 |
+
# Update the existing row
|
96 |
+
update_query = f"UPDATE {table_name} SET {attribute_name}='{attribute_value}' WHERE {restriction_name}='{restriction_value}'"
|
97 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {update_query}\"")
|
98 |
+
print("error:", stderr.read().decode())
|
99 |
+
else:
|
100 |
+
# Insert a new row
|
101 |
+
insert_query = f"INSERT INTO {table_name} ({restriction_name}, {attribute_name}) VALUES ('{restriction_value}', '{attribute_value}')"
|
102 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {insert_query}\"")
|
103 |
+
print("error:", stderr.read().decode())
|
104 |
+
except Exception as e:
|
105 |
+
print("ha! Exception!")
|
106 |
+
print(e)
|
107 |
+
close(ssh_client)
|
108 |
+
|
109 |
+
def get_attribute(db_name, table_name, restriction, restriction_value, attribute_name):
|
110 |
+
ssh_client = connect()
|
111 |
+
try:
|
112 |
+
query = f"SELECT {attribute_name} FROM {table_name} WHERE {restriction}='{restriction_value}'"
|
113 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {query}\"")
|
114 |
+
print("output: ", stdout)
|
115 |
+
print("error:", stderr.read().decode())
|
116 |
+
except Exception as e:
|
117 |
+
print(e)
|
118 |
+
result = stdout.read().decode().strip()
|
119 |
+
rows = result.splitlines()
|
120 |
+
attribute_value = rows[1]
|
121 |
+
close(ssh_client)
|
122 |
+
return attribute_value
|
123 |
+
|
124 |
+
def get_attribute_2(db_name, table_name, restriction_1, restriction_value_1, restriction_2, restriction_value_2, attribute_name):
|
125 |
+
ssh_client = connect()
|
126 |
+
try:
|
127 |
+
query = f"SELECT {attribute_name} FROM {table_name} WHERE {restriction_1}='{restriction_value_1}' AND {restriction_2}='{restriction_value_2}'"
|
128 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {query}\"")
|
129 |
+
print("output: ", stdout, "end")
|
130 |
+
# print("error:", stderr.read().decode())
|
131 |
+
except Exception as e:
|
132 |
+
print(e)
|
133 |
+
stdout = ""
|
134 |
+
# rows = stdout.read().decode().strip()
|
135 |
+
result = stdout.read().decode().strip()
|
136 |
+
rows = result.splitlines()
|
137 |
+
# print ("result:", rows)
|
138 |
+
attribute_value = rows[1]
|
139 |
+
# print ("result2:", attribute_value)
|
140 |
+
close(ssh_client)
|
141 |
+
return attribute_value
|
142 |
+
|
143 |
+
# called through API\metadata
|
144 |
+
def store_ipfs(user_id, ipfs_link, link_num):
|
145 |
+
ssh_client = connect()
|
146 |
+
try:
|
147 |
+
if(link_num==1):
|
148 |
+
store_query = f"INSERT INTO token_key (user_id, ipfs_link1) VALUES ('{user_id}', '{ipfs_link}') ON DUPLICATE KEY UPDATE ipfs_link1 = '{ipfs_link}'"
|
149 |
+
elif(link_num==2):
|
150 |
+
store_query = f"INSERT INTO token_key (user_id, ipfs_link2) VALUES ('{user_id}', '{ipfs_link}') ON DUPLICATE KEY UPDATE ipfs_link2 = '{ipfs_link}'"
|
151 |
+
elif(link_num==3):
|
152 |
+
store_query = f"INSERT INTO token_key (user_id, ipfs_link3) VALUES ('{user_id}', '{ipfs_link}') ON DUPLICATE KEY UPDATE ipfs_link3 = '{ipfs_link}'"
|
153 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {store_query}\"")
|
154 |
+
print("error:", stderr.read().decode())
|
155 |
+
|
156 |
+
except Exception as e:
|
157 |
+
print(e)
|
158 |
+
close(ssh_client)
|
159 |
+
|
160 |
+
# called through API\minting
|
161 |
+
def store_token(ipfs_link1, ipfs_link2, ipfs_link3, membership_status, user_id, token_id):
|
162 |
+
ssh_client = connect()
|
163 |
+
# check if the record already exists
|
164 |
+
check_query = f"SELECT COUNT(*) FROM token_key WHERE user_id='{user_id}'"
|
165 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} 'USE SBTdb; {check_query}'")
|
166 |
+
|
167 |
+
count_str = stdout.read().decode()
|
168 |
+
count_line = count_str.split('\n')[1]
|
169 |
+
count = int(count_line)
|
170 |
+
print(f"count: {count}")
|
171 |
+
|
172 |
+
|
173 |
+
if count == 0:
|
174 |
+
# if the record doesn't exist, insert a new record
|
175 |
+
try:
|
176 |
+
insert_query = f"INSERT INTO token_key (user_id, token_id, ipfs_link1, ipfs_link2, ipfs_link3, membership_status) VALUES ('{user_id}', '{token_id}', '{ipfs_link1}', '{ipfs_link2}', '{ipfs_link3}', '{membership_status}')"
|
177 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {insert_query}\"")
|
178 |
+
print("error:", stderr.read().decode())
|
179 |
+
except Exception as e:
|
180 |
+
print(e)
|
181 |
+
else:
|
182 |
+
# if the record exists, update the existing record
|
183 |
+
try:
|
184 |
+
update_query = f"UPDATE token_key SET token_id='{token_id}', ipfs_link1='{ipfs_link1}', ipfs_link2='{ipfs_link2}', ipfs_link3='{ipfs_link3}', membership_status='{membership_status}' WHERE user_id='{user_id}'"
|
185 |
+
# sql_query = f"USE SBTdb ;{update_query}"
|
186 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {update_query}\"")
|
187 |
+
print("error:", stderr.read().decode())
|
188 |
+
except Exception as e:
|
189 |
+
print(e)
|
190 |
+
|
191 |
+
close(ssh_client)
|
192 |
+
return token_id
|
193 |
+
|
194 |
+
|
195 |
+
# called through API\general, KYC, marketing
|
196 |
+
def get_userinfo(user_id, token_id, type):
|
197 |
+
ssh_client = connect()
|
198 |
+
|
199 |
+
info = {}
|
200 |
+
try:
|
201 |
+
query = f"SELECT token_id FROM token_key WHERE user_id='{user_id}' "
|
202 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {query}\"")
|
203 |
+
print("error:", stderr.read().decode())
|
204 |
+
except Exception as e:
|
205 |
+
print(e)
|
206 |
+
|
207 |
+
token_id_str = stdout.read().decode()
|
208 |
+
print("token_id_str:", token_id_str)
|
209 |
+
token_id_db = token_id_str.split('\n')[1]
|
210 |
+
|
211 |
+
if token_id_db == token_id:
|
212 |
+
if type == "general":
|
213 |
+
retrieve_query = f"SELECT facebook_id FROM ipygg_users WHERE user_id='{user_id}'"
|
214 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE ipygg; {retrieve_query}\"")
|
215 |
+
print("error:", stderr.read().decode())
|
216 |
+
facebook_id = stdout.read().decode().split('\n')[1]
|
217 |
+
info['facebook_id'] = facebook_id
|
218 |
+
elif type == "KYC":
|
219 |
+
retrieve_query = f"SELECT email, salutation, first_name, last_name, birthday, country, region, location, address, postcode, area_code, phone, hk_id, passport FROM ipygg_users WHERE user_id='{user_id}'"
|
220 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE ipygg; {retrieve_query}\"")
|
221 |
+
print("error:", stderr.read().decode())
|
222 |
+
info_list = stdout.read().decode().split('\n')[1].split('\t')
|
223 |
+
info = {
|
224 |
+
'email': info_list[0],
|
225 |
+
'salutation': info_list[1],
|
226 |
+
'first_name': info_list[2],
|
227 |
+
'last_name': info_list[3],
|
228 |
+
'birthday': info_list[4],
|
229 |
+
'country': info_list[5],
|
230 |
+
'region': info_list[6],
|
231 |
+
'location': info_list[7],
|
232 |
+
'address': info_list[8],
|
233 |
+
'postcode': info_list[9],
|
234 |
+
'area_code': info_list[10],
|
235 |
+
'phone': info_list[11],
|
236 |
+
'hk_id': info_list[12],
|
237 |
+
'passport': info_list[13]
|
238 |
+
}
|
239 |
+
info['dual_passport'] = False #TODO: add dual_passport to ipygg_users
|
240 |
+
elif type == "marketing":
|
241 |
+
retrieve_query = f"SELECT email, facebook_id, wealth_level FROM ipygg_users WHERE user_id='{user_id}'"
|
242 |
+
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE ipygg; {retrieve_query}\"")
|
243 |
+
print("error:", stderr.read().decode())
|
244 |
+
info_list = stdout.read().decode().split('\n')[1].split('\t')
|
245 |
+
info['email'] = info_list[0]
|
246 |
+
info['facebook_id'] = info_list[1]
|
247 |
+
info['wealth_level'] = info_list[2]
|
248 |
+
close(ssh_client)
|
249 |
+
return True, info
|
250 |
+
else:
|
251 |
+
close(ssh_client)
|
252 |
+
return False, info
|
253 |
+
|
254 |
+
# def update_Status(request_id, status, user_id, institution_id, latest_update_date=time.localtime().tm_mday):
|
255 |
+
# conn, cursor = get_conn_and_cursor(request_id)
|
256 |
+
|
257 |
+
# try:
|
258 |
+
# sql = "update request_log set status = \"{}\" where user_id = \"{}\" AND " \
|
259 |
+
# "institution_id = \"{}\" AND request_id = \"{}\";".format(
|
260 |
+
# status, user_id, institution_id, request_id)
|
261 |
+
# # addRequestLog(request_id, sql)
|
262 |
+
# # cursor.execute(sql)
|
263 |
+
# # conn.commit()
|
264 |
+
# except Exception as e:
|
265 |
+
# print(e)
|
266 |
+
# # addRequestLog(request_id, "Could not update user Status")
|
267 |
+
|
268 |
+
def getCustomerToken(request_id, user_id):
|
269 |
+
"""
|
270 |
+
Returns: *customer token* for generating link token
|
271 |
+
Uses: *Client ID*, *Client secret* (Defined in constants.py)
|
272 |
+
Url: "https://api.prod.finverse.net/auth/customer/token"
|
273 |
+
Section 0 in the Flowchart
|
274 |
+
"""
|
275 |
+
url = "https://api.prod.finverse.net/auth/customer/token"
|
276 |
+
payload = json.dumps({
|
277 |
+
"client_id": f"{CLIENT_ID}",
|
278 |
+
"client_secret": f"{CLIENT_SECRET}",
|
279 |
+
"grant_type": "client_credentials"
|
280 |
+
})
|
281 |
+
headers = {
|
282 |
+
'X-Request-Id': f'{user_id}-{int(time.time())}',
|
283 |
+
'Content-Type': 'application/json'
|
284 |
+
}
|
285 |
+
response = requests.request(
|
286 |
+
"POST", url, headers=headers, data=payload).json()
|
287 |
+
# logger(
|
288 |
+
# request_id, f'=============headers X-Request-Id============\n{headers["X-Request-Id"]}')
|
289 |
+
# logger(
|
290 |
+
# request_id, f'============1 post response==========\n{response}')
|
291 |
+
return response["access_token"]
|
292 |
+
|
293 |
+
def getLinkToken(customer_token, request_id, user_id):
|
294 |
+
url = "https://api.prod.finverse.net/link/token"
|
295 |
+
payload = json.dumps({
|
296 |
+
"client_id": f"{CLIENT_ID}",
|
297 |
+
"user_id": user_id,
|
298 |
+
"redirect_uri": f"{REDIRECT_URL}",
|
299 |
+
"state": "ipygg_stateparameter",
|
300 |
+
"grant_type": "client_credentials",
|
301 |
+
"response_mode": "form_post",
|
302 |
+
"response_type": "code",
|
303 |
+
"automatic_data_refresh": "",
|
304 |
+
"countries": [],
|
305 |
+
"institution_id": "",
|
306 |
+
"institution_status": "",
|
307 |
+
"language": "",
|
308 |
+
"link_mode": "test",
|
309 |
+
"products_requested": [],
|
310 |
+
"products_supported": [],
|
311 |
+
"ui_mode": "redirect",
|
312 |
+
"user_type": []
|
313 |
+
})
|
314 |
+
headers = {
|
315 |
+
'X-Request-Id': f'{user_id}-{int(time.time())}',
|
316 |
+
'Authorization': f'Bearer {customer_token}',
|
317 |
+
'Content-Type': 'application/json'
|
318 |
+
}
|
319 |
+
try:
|
320 |
+
print(request_id, "Trying to get login ID from finverse")
|
321 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
322 |
+
j = response.json()
|
323 |
+
link_url = j["link_url"]
|
324 |
+
print(request_id, link_url)
|
325 |
+
|
326 |
+
if 'error' in j.keys():
|
327 |
+
if j['error']['code'] == 40001:
|
328 |
+
print(j)
|
329 |
+
return 404
|
330 |
+
|
331 |
+
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id,
|
332 |
+
# request_id, date)
|
333 |
+
return j['link_url']
|
334 |
+
except Exception as e:
|
335 |
+
print(request_id, "Unable to refresh")
|
336 |
+
print(request_id, e)
|
337 |
+
|
338 |
+
|
339 |
+
def getLoginID(customer_token, request_id, user_id):
|
340 |
+
"""
|
341 |
+
Returns: *link token*
|
342 |
+
Uses: *Customer token*, *login refresh token*
|
343 |
+
Documentation: Data API -> 12-Data Refresh -> POST /auth/token/refresh (1st step in the Refresh flow)
|
344 |
+
URL: https://api.prod.finverse.net/auth/token/refresh
|
345 |
+
Section 9 in the Flowchart
|
346 |
+
"""
|
347 |
+
|
348 |
+
url = "https://api.prod.finverse.net/login_identity"
|
349 |
+
payload = json.dumps({
|
350 |
+
})
|
351 |
+
headers = {
|
352 |
+
'X-Request-Id': f'{user_id}-{int(time.time())}',
|
353 |
+
'Authorization': f'Bearer {customer_token}',
|
354 |
+
'Content-Type': 'application/json'
|
355 |
+
}
|
356 |
+
try:
|
357 |
+
print(request_id, "Trying to get login ID from finverse")
|
358 |
+
response = requests.request("GET", url, headers=headers)
|
359 |
+
j = response.json()
|
360 |
+
login_id = j["login_identity"]["login_identity_id"]
|
361 |
+
print(request_id, login_id)
|
362 |
+
|
363 |
+
if 'error' in j.keys():
|
364 |
+
if j['error']['code'] == 40001:
|
365 |
+
print(j)
|
366 |
+
return 404
|
367 |
+
|
368 |
+
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id,
|
369 |
+
# request_id, date)
|
370 |
+
return j['access_token']
|
371 |
+
except Exception as e:
|
372 |
+
print(request_id, "Unable to refresh")
|
373 |
+
print(request_id, e)
|
374 |
+
|
375 |
+
|
376 |
+
def getLinkCode(user_id, request_id, code, customer_token):
|
377 |
+
url = "https://api.prod.finverse.net/auth/token"
|
378 |
+
payload = {
|
379 |
+
"client_id": f"{CLIENT_ID}",
|
380 |
+
"code": f"{code}",
|
381 |
+
"redirect_uri": f"{REDIRECT_URL}",
|
382 |
+
"grant_type": "authorization_code"
|
383 |
+
}
|
384 |
+
headers = {
|
385 |
+
'X-Request-Id': f'{user_id}-{int(time.time())}',
|
386 |
+
'Authorization': f'Bearer {customer_token}',
|
387 |
+
'Content-Type': 'application/x-www-form-urlencoded'
|
388 |
+
}
|
389 |
+
try:
|
390 |
+
print(request_id, "Trying to get link code from finverse")
|
391 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
392 |
+
j = response.json()
|
393 |
+
# print(j)
|
394 |
+
login_id = j["login_identity_id"]
|
395 |
+
login_id_token = j["access_token"]
|
396 |
+
refresh_token = j["refresh_token"]
|
397 |
+
print(f'{request_id}, login id: {login_id} , refresh token: {refresh_token}')
|
398 |
+
|
399 |
+
if 'error' in j.keys():
|
400 |
+
if j['error']['code'] == 40001:
|
401 |
+
return 404
|
402 |
+
|
403 |
+
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id,
|
404 |
+
# request_id, date)
|
405 |
+
return login_id_token
|
406 |
+
except Exception as e:
|
407 |
+
print(request_id, "Unable to get link code")
|
408 |
+
print(request_id, e)
|
409 |
+
|
410 |
+
|
411 |
+
def getRefreshedToken(customer_token, request_id, user_id, refresh_token, institution_id, date, redirect_uri):
|
412 |
+
"""
|
413 |
+
Returns: *link token*
|
414 |
+
Uses: *Customer token*, *login refresh token*
|
415 |
+
Documentation: Data API -> 12-Data Refresh -> POST /auth/token/refresh (1st step in the Refresh flow)
|
416 |
+
URL: https://api.prod.finverse.net/auth/token/refresh
|
417 |
+
Section 9 in the Flowchart
|
418 |
+
"""
|
419 |
+
|
420 |
+
url = "https://api.prod.finverse.net/auth/token/refresh"
|
421 |
+
payload = json.dumps({
|
422 |
+
"redirect_uri": f"{redirect_uri}",
|
423 |
+
"grant_type": "client_credentials",
|
424 |
+
"code": ""
|
425 |
+
})
|
426 |
+
headers = {
|
427 |
+
'X-Request-Id': f'{user_id}-{int(time.time())}',
|
428 |
+
'Authorization': f'Bearer {customer_token}',
|
429 |
+
'Content-Type': 'application/json'
|
430 |
+
}
|
431 |
+
try:
|
432 |
+
print(request_id, "Trying to get refreshed credentials from finverse")
|
433 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
434 |
+
j = response.json()
|
435 |
+
print(request_id, j)
|
436 |
+
|
437 |
+
if 'error' in j.keys():
|
438 |
+
if j['error']['code'] == 40001:
|
439 |
+
return 404
|
440 |
+
|
441 |
+
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id,
|
442 |
+
# request_id, date)
|
443 |
+
return j['access_token']
|
444 |
+
except Exception as e:
|
445 |
+
print(request_id, "Unable to refresh")
|
446 |
+
print(request_id, e)
|
447 |
+
|
448 |
+
def get_pdf(userId, statement_id, login_identity_token, request_id):
|
449 |
+
try:
|
450 |
+
# logger(request_id, f"\nfetching PDF for {statement_id}")
|
451 |
+
url = f"https://api.prod.finverse.net/statements/{statement_id}?redirect=false"
|
452 |
+
payload = {}
|
453 |
+
headers = {
|
454 |
+
'X-Request-Id': f'{userId}-{int(time.time())}',
|
455 |
+
'Authorization': f'Bearer {login_identity_token}',
|
456 |
+
'Content-Type': "application/json"
|
457 |
+
}
|
458 |
+
response = requests.get(url, headers=headers, data=payload).json()
|
459 |
+
# print(request_id, "response")
|
460 |
+
link = response['statement_links'][0]['url']
|
461 |
+
# print(f'link is : {link}')
|
462 |
+
return link
|
463 |
+
except Exception as e:
|
464 |
+
print(request_id, f"Error in get_pdf")
|
465 |
+
print(request_id, e)
|
466 |
+
# finverse_connector.update_Status(
|
467 |
+
# request_id, "50200", userId, institution_id)
|
468 |
+
#
|
469 |
+
|
470 |
+
def statements_fetch(userId, institution_id, login_identity_token, request_id):
|
471 |
+
try:
|
472 |
+
# logger(request_id, "fetching statements from finverse")
|
473 |
+
url = f"https://api.prod.finverse.net/statements"
|
474 |
+
|
475 |
+
payload = {}
|
476 |
+
headers = {
|
477 |
+
'X-Request-Id': f'{userId}-{int(time.time())}',
|
478 |
+
'Authorization': f'Bearer {login_identity_token}',
|
479 |
+
'Content-Type': 'application/json'
|
480 |
+
}
|
481 |
+
|
482 |
+
response = requests.get(url, headers=headers, data=payload).json()
|
483 |
+
print(request_id, f"The response is :\n{response}")
|
484 |
+
statement = response["statements"]
|
485 |
+
print(f'statement: {statement[0]["id"]}')
|
486 |
+
# for statement in response['statements']:
|
487 |
+
# # name = statement['name'] + ' ' + statement['date']
|
488 |
+
if statement != None:
|
489 |
+
statement = get_pdf(userId, statement[0]["id"], login_identity_token, request_id)
|
490 |
+
return statement, 200
|
491 |
+
else:
|
492 |
+
print('no statement')
|
493 |
+
return 0
|
494 |
+
except Exception as e:
|
495 |
+
print(request_id, f"Error in statements_fetch")
|
496 |
+
print(request_id, e)
|
497 |
+
# finverse_connector.update_Status(
|
498 |
+
# request_id, "50200", userId, institution_id)
|
499 |
+
return 500
|
500 |
+
|
501 |
+
|
502 |
+
def check_if_token_exists(user_id, institution_id, request_id):
|
503 |
+
"""
|
504 |
+
Searches: if user exists in user_and_login_identity table
|
505 |
+
and if value of *refresh_allowed* is 1
|
506 |
+
Section 1 in the Flowchart
|
507 |
+
"""
|
508 |
+
# db_name, table_name, restriction, restriction_value, attribute_name
|
509 |
+
user_details = get_attribute_2('ipygg', 'user_and_login_identity', 'user_id', user_id, 'institution_id', institution_id, '*')
|
510 |
+
if user_details != "":
|
511 |
+
print(user_details)
|
512 |
+
# refresh_token = user_details[6]
|
513 |
+
refresh_allowed = user_details[-1]
|
514 |
+
print(refresh_allowed)
|
515 |
+
if refresh_allowed == "1":
|
516 |
+
return user_details
|
517 |
+
else:
|
518 |
+
return 0
|
519 |
+
else:
|
520 |
+
return 0
|
521 |
+
|
522 |
+
|
523 |
+
def statements_report(userId, institutionId, requestId, date):
|
524 |
+
try:
|
525 |
+
# finverse_connector.add_status(
|
526 |
+
# requestId, "10000", userId, institutionId, 'PDFs', date)
|
527 |
+
customer_token = getCustomerToken(requestId, userId)
|
528 |
+
print(customer_token)
|
529 |
+
|
530 |
+
# addRequestLog(requestId, f"Fetching PDFs for {userId} with {institutionId} institutionID")
|
531 |
+
# userInfo = check_if_token_exists(
|
532 |
+
# userId, institutionId, requestId)
|
533 |
+
userInfo = 1
|
534 |
+
|
535 |
+
print(f'userInfo: {userInfo}')
|
536 |
+
|
537 |
+
if userInfo == 0:
|
538 |
+
# createUserAccount()
|
539 |
+
print(requestId, "User does not have any accounts")
|
540 |
+
return 500 # user does not exist
|
541 |
+
else:
|
542 |
+
# refresh_token = userInfo[-2]
|
543 |
+
search_url = getLinkToken(customer_token, requestId, userId)
|
544 |
+
driver = webdriver.Chrome()
|
545 |
+
driver.get(search_url)
|
546 |
+
wait = WebDriverWait(driver, 50).until(EC.url_contains("code="))
|
547 |
+
url = driver.current_url
|
548 |
+
time.sleep(5)
|
549 |
+
url_list = url.split("code=",1)[1]
|
550 |
+
code = url_list[:26]
|
551 |
+
print(f'code is {code}')
|
552 |
+
# webbrowser.open(search_url)
|
553 |
+
# where user input
|
554 |
+
# code = input("Enter code:")
|
555 |
+
time.sleep(5)
|
556 |
+
login_identity_token = getLinkCode(requestId,userId,code,customer_token)
|
557 |
+
|
558 |
+
pdf_link = statements_fetch(userId, institutionId, login_identity_token, requestId)
|
559 |
+
if pdf_link!= 500:
|
560 |
+
# pdf_link = get_pdf(userId, statement_list['id'], login_identity_token, requestId)
|
561 |
+
webbrowser.open(pdf_link[0])
|
562 |
+
return pdf_link
|
563 |
+
else:
|
564 |
+
return 500
|
565 |
+
|
566 |
+
except Exception as e:
|
567 |
+
print(requestId, "Fetching PDFs failed")
|
568 |
+
print(requestId, e)
|
569 |
+
print(e)
|
570 |
+
return 500 # Fetching error
|
571 |
+
|
572 |
+
# pdf_link = get_pdf(userId="1001001", statement_id="01H4RJ02B5Q7ZTX9S3TPFWNQT0", login_identity_token="eyJhbGciOiJSUzI1NiIsImtpZCI6Imp3dC9jcnlwdG9LZXlzL2p3dC1rZXkvY3J5cHRvS2V5VmVyc2lvbnMvMSIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwczovL2FwaS5wcm9kLmZpbnZlcnNlLm5ldCIsImNsaWVudElkIjoiMDFHMUc1TjdLTVlaNEZDVlo3NkU3VDZORjAiLCJjdXN0b21lckFwcElkIjoiMDFHMUc1TjdOMkc3UVNLNDNXUDZLR002UjYiLCJleHAiOjE2ODg3NDk5NDMsImlhdCI6MTY4ODc0NjM0MywiaXNzIjoiZmludmVyc2UtcHJvZCIsImxvZ2luSWRlbnRpdHlJZCI6IjAxSDRSSFpLV0ZNUVI5MEZXMTNIQUFQNks1Iiwic2NvcGUiOiJhY2NvdW50IHRyYW5zYWN0aW9uIGlkZW50aXR5IGxvZ2luSWRlbnRpdHk6c2VsZiBsb2dpbklkZW50aXR5OmRlbGV0ZSBsb2dpbklkZW50aXR5OnVwZGF0ZSBsaW5rOnRva2VuIHN0YXRlbWVudDpyZWFkIGFjY291bnRfbnVtYmVyIGJhbGFuY2VfaGlzdG9yeSBwYXltZW50OmdldCBpbmNvbWU6Z2V0IGNvbXBvc2l0ZV9zdGF0ZW1lbnQiLCJzdWIiOiIwMUg0UkhaS1dGTVFSOTBGVzEzSEFBUDZLNSJ9.AbmNVOWDAQm9wnxLqpQuHpSBWp7L1IRCXNAeT-FWaXO3hsYOCzsfyA1DObn1XOHbGV-HoYXHGPrDThORrfFOBKjH6yW7-W41UQDjlRIGVsXxYUwJsHAV8ytuDoyIk8rqoPVNALHsU4rOzXYIpplI-7HMgYAzjRXAP50x0KL4Ks5pyGYokmnq2nElKi_nC1XFlsPjYuVnJ_88xZF6hOugoZgU8Mq4P59RUTUUksTWWW8BthCKr1sakKYcBEbIp2efv8Upfc_feY-z4j4qI1V_PHiXUIatqr71kOMbJ3xNkhzEIXNh_SdK4u3C7SW1ZZhivTaFakTUhMazvg3xtalUZq_EZViLg4OJTmUB1gjYEBoPMpUwlQcJXgXpwtUh-9cEo99ufw0oB9trc4ocdfMYzw0kavfmPZqPHgu3HdX7Ofi0Fxgh8-AvRqlr3eVjmL54u8js5A9n43LaZ7oQgOxs82mm0XfAeX_alhzID3IVo-ZDpGkQ8hV-19-X2APhSt75tCMwFvHBfDd8QRfSyCPL0Yqi01jzT1TiVyP7yV2RjaDJQPcXk81nbgEGL_kwfLbCjpWeZSoJi15vAEhEGpM7pnuRHplLq59v6ARCfU1kRXI1Huva5S5X196Cfk7T9gBtqfUkT3oETNIkR8UxQmwp5BQHXtKMcMU8C4h4W3WAsd8", request_id="request1234")
|
573 |
+
# webbrowser.open(pdf_link)
|
574 |
+
# print(f'{pdf_link} finished')
|
575 |
+
|
576 |
+
def get_today_date():
|
577 |
+
today = date.today()
|
578 |
+
return str(today)
|
579 |
+
|
580 |
+
# Example for what will be returned
|
581 |
+
# 2023-06-29
|
582 |
+
|
583 |
+
################### MAIN ############################
|
584 |
+
def get_bs(userId="1836660007822863504716", institutionId="00000005",requestId="request1234"):
|
585 |
+
standard_names = {'boch': "Consolidated Statement",
|
586 |
+
'hangseng': "Statement of",
|
587 |
+
'hsbc': "Statement - HSBC One Account",
|
588 |
+
'sc': "statementOfAccount"}
|
589 |
+
|
590 |
+
# records = statements_report("1836660007822863504716","00000005","request1234","05072023")
|
591 |
+
# userId, institutionId, requestId, date
|
592 |
+
pdf_link = statements_report(userId, institutionId, requestId, str(get_today_date()))
|
593 |
+
# get_pdf(userId, institution_id, records[0]['id'], login_identity_token, request_id)
|
594 |
+
if pdf_link != 500:
|
595 |
+
print(f"successfully get bank report with link: {pdf_link}")
|
596 |
+
|
597 |
+
def get_transactions(user_id, login_identity_token):
|
598 |
+
"""
|
599 |
+
Returns: *customer token* for generating link token
|
600 |
+
Uses: *Client ID*, *Client secret* (Defined in constants.py)
|
601 |
+
Url: "https://api.prod.finverse.net/transactions?offset=0&limit=100"
|
602 |
+
Section 0 in the Flowchart
|
603 |
+
"""
|
604 |
+
url = "https://api.prod.finverse.net/transactions?offset=0&limit=100"
|
605 |
+
# payload = json.dumps({
|
606 |
+
# "client_id": f"{CLIENT_ID}",
|
607 |
+
# "client_secret": f"{CLIENT_SECRET}",
|
608 |
+
# "grant_type": "client_credentials"
|
609 |
+
# })
|
610 |
+
headers = {
|
611 |
+
'X-Request-Id': f'{user_id}-{int(time.time())}',
|
612 |
+
'Authorization': f'Bearer {login_identity_token}',
|
613 |
+
'Content-Type': 'application/json'
|
614 |
+
}
|
615 |
+
response = requests.request(
|
616 |
+
"GET", url, headers=headers).json()
|
617 |
+
|
618 |
+
transaction_record = response["transactions"]
|
619 |
+
|
620 |
+
print(f"getting transactions: {transaction_record}")
|
621 |
+
|
622 |
+
return response["transactions"]
|
623 |
+
|
624 |
+
|
625 |
+
def past_transactions(userId="1836660007822863504716", institutionId="00000005",requestId="request1234"):
|
626 |
+
try:
|
627 |
+
# finverse_connector.add_status(
|
628 |
+
# requestId, "10000", userId, institutionId, 'PDFs', date)
|
629 |
+
customer_token = getCustomerToken(requestId, userId)
|
630 |
+
print(customer_token)
|
631 |
+
|
632 |
+
# addRequestLog(requestId, f"Fetching PDFs for {userId} with {institutionId} institutionID")
|
633 |
+
# userInfo = check_if_token_exists(
|
634 |
+
# userId, institutionId, requestId)
|
635 |
+
userInfo = 1
|
636 |
+
print(f'userInfo: {userInfo}')
|
637 |
+
|
638 |
+
if userInfo == 0:
|
639 |
+
# createUserAccount()
|
640 |
+
print(requestId, "User does not have any accounts")
|
641 |
+
return 500 # user does not exist
|
642 |
+
else:
|
643 |
+
search_url = getLinkToken(customer_token, requestId, userId)
|
644 |
+
driver = webdriver.Chrome()
|
645 |
+
driver.get(search_url)
|
646 |
+
wait = WebDriverWait(driver, 50).until(EC.url_contains("code="))
|
647 |
+
url = driver.current_url
|
648 |
+
time.sleep(5)
|
649 |
+
url_list = url.split("code=",1)[1]
|
650 |
+
code = url_list[:26]
|
651 |
+
print(f'code is {code}')
|
652 |
+
time.sleep(5)
|
653 |
+
login_identity_token = getLinkCode(requestId,userId,code,customer_token)
|
654 |
+
|
655 |
+
transaction_record = get_transactions(userId, login_identity_token)
|
656 |
+
# transaction_record = transaction_record[:10]
|
657 |
+
print('printing transactions records')
|
658 |
+
item_num = 0
|
659 |
+
for item in transaction_record:
|
660 |
+
if str(datetime.date.today().year) in item["created_at"]:
|
661 |
+
item_num = item_num + 1
|
662 |
+
# print(item)
|
663 |
+
transaction_data = {
|
664 |
+
"endpoint": "SBT",
|
665 |
+
"apiType": "store_finverse_verif",
|
666 |
+
"requestId": "request1234",
|
667 |
+
"userId": userId # a string
|
668 |
+
}
|
669 |
+
if (item_num >= 100):
|
670 |
+
print("user has more than 100 transactions in the past year")
|
671 |
+
transaction_data["finverseVerif"] = "True" # a string "True" or "False"
|
672 |
+
# post true
|
673 |
+
else:
|
674 |
+
print("user has no more than 100 transactions in the past year")
|
675 |
+
transaction_data["finverseVerif"] = "False" # a string "True" or "False"
|
676 |
+
# post false
|
677 |
+
generate_request(transaction_data)
|
678 |
+
|
679 |
+
except Exception as e:
|
680 |
+
print(requestId, "Fetching transactions failed")
|
681 |
+
print(requestId, e)
|