Spaces:
Sleeping
Sleeping
import requests | |
import json | |
import time | |
import datetime | |
import requests | |
import paramiko | |
from dotenv import load_dotenv | |
from selenium import webdriver | |
import webbrowser | |
import os | |
from datetime import date | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from request_json.sbt_request_generator import generate_request | |
load_dotenv() | |
REDIRECT_URL = os.environ.get("REDIRECT_URL") | |
_base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' | |
CUSTOMER_TOKEN = os.environ.get("CUSTOMER_TOKEN") | |
APP_ID = os.environ.get("APP_ID") | |
CLIENT_ID = os.environ.get("CLIENT_ID") | |
CLIENT_SECRET = os.environ.get("CLIENT_SECRET") | |
def connect(): | |
ssh_host = '18.166.103.96' | |
ssh_user = 'ec2-user' | |
ssh_key_path = '.ssh/ipygg-api-test.pem' | |
ssh_client = paramiko.SSHClient() | |
ssh_client.load_system_host_keys() | |
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
ssh_key = paramiko.RSAKey.from_private_key_file(ssh_key_path) | |
ssh_client.connect(ssh_host, username=ssh_user, pkey=ssh_key) | |
print("Successfully connected to the test server") | |
transport = ssh_client.get_transport() | |
if transport.is_active(): | |
print("SSH connection is active") | |
else: | |
print("SSH connection is not active") | |
return ssh_client | |
def command(): | |
mysql_host = 'awseb-e-az6qppyyjf-stack-awsebrdsdatabase-g7iebolqxtzk.ccrm7ijo8xsm.ap-east-1.rds.amazonaws.com' | |
mysql_user = 'iPYGG' | |
mysql_password = 'yt1uQ8aiTd!2cY6K2re342!G6g' | |
mysql_command = f"mysql -h {mysql_host} -u {mysql_user} -p{mysql_password} -e " | |
return mysql_command | |
def initialize_SBTdb(ssh_client): | |
# ssh_client.exec_command(command() + "'DROP DATABASE IF EXISTS test_SBTdb'") | |
try: | |
ssh_client.exec_command(command() + "'CREATE DATABASE IF NOT EXISTS SBTdb'") | |
ssh_client.exec_command(command() + """'USE SBTdb; | |
CREATE TABLE IF NOT EXISTS token_key ( | |
user_id VARCHAR(233) NOT NULL PRIMARY KEY, | |
token_id VARCHAR(233), | |
decryption_key VARBINARY(255), | |
ipfs_link1 VARCHAR(255), | |
ipfs_link2 VARCHAR(255), | |
ipfs_link3 VARCHAR(255), | |
membership_status VARCHAR(255) | |
);'""") | |
except Exception as e: | |
print(e) | |
def close(ssh_client): | |
ssh_client.close() | |
# **************************API calls************************** | |
def store_attribute(db_name, table_name, restriction_name, restriction_value, attribute_name, attribute_value): | |
ssh_client = connect() | |
try: | |
# Check if the row already exists | |
select_query = f"SELECT COUNT(*) AS count FROM {table_name} WHERE {restriction_name}='{restriction_value}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {select_query}\"") | |
count_str = stdout.read().decode() | |
print("output: ", count_str) | |
count_line = count_str.split('\n')[1] | |
count = int(count_line) | |
print("error:", stderr.read().decode()) | |
if count > 0: | |
# Update the existing row | |
update_query = f"UPDATE {table_name} SET {attribute_name}='{attribute_value}' WHERE {restriction_name}='{restriction_value}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {update_query}\"") | |
print("error:", stderr.read().decode()) | |
else: | |
# Insert a new row | |
insert_query = f"INSERT INTO {table_name} ({restriction_name}, {attribute_name}) VALUES ('{restriction_value}', '{attribute_value}')" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {insert_query}\"") | |
print("error:", stderr.read().decode()) | |
except Exception as e: | |
print("ha! Exception!") | |
print(e) | |
close(ssh_client) | |
def get_attribute(db_name, table_name, restriction, restriction_value, attribute_name): | |
ssh_client = connect() | |
try: | |
query = f"SELECT {attribute_name} FROM {table_name} WHERE {restriction}='{restriction_value}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {query}\"") | |
print("output: ", stdout) | |
print("error:", stderr.read().decode()) | |
except Exception as e: | |
print(e) | |
result = stdout.read().decode().strip() | |
rows = result.splitlines() | |
attribute_value = rows[1] | |
close(ssh_client) | |
return attribute_value | |
def get_attribute_2(db_name, table_name, restriction_1, restriction_value_1, restriction_2, restriction_value_2, attribute_name): | |
ssh_client = connect() | |
try: | |
query = f"SELECT {attribute_name} FROM {table_name} WHERE {restriction_1}='{restriction_value_1}' AND {restriction_2}='{restriction_value_2}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE {db_name}; {query}\"") | |
print("output: ", stdout, "end") | |
# print("error:", stderr.read().decode()) | |
except Exception as e: | |
print(e) | |
stdout = "" | |
# rows = stdout.read().decode().strip() | |
result = stdout.read().decode().strip() | |
rows = result.splitlines() | |
# print ("result:", rows) | |
attribute_value = rows[1] | |
# print ("result2:", attribute_value) | |
close(ssh_client) | |
return attribute_value | |
# called through API\metadata | |
def store_ipfs(user_id, ipfs_link, link_num): | |
ssh_client = connect() | |
try: | |
if(link_num==1): | |
store_query = f"INSERT INTO token_key (user_id, ipfs_link1) VALUES ('{user_id}', '{ipfs_link}') ON DUPLICATE KEY UPDATE ipfs_link1 = '{ipfs_link}'" | |
elif(link_num==2): | |
store_query = f"INSERT INTO token_key (user_id, ipfs_link2) VALUES ('{user_id}', '{ipfs_link}') ON DUPLICATE KEY UPDATE ipfs_link2 = '{ipfs_link}'" | |
elif(link_num==3): | |
store_query = f"INSERT INTO token_key (user_id, ipfs_link3) VALUES ('{user_id}', '{ipfs_link}') ON DUPLICATE KEY UPDATE ipfs_link3 = '{ipfs_link}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {store_query}\"") | |
print("error:", stderr.read().decode()) | |
except Exception as e: | |
print(e) | |
close(ssh_client) | |
# called through API\minting | |
def store_token(ipfs_link1, ipfs_link2, ipfs_link3, membership_status, user_id, token_id): | |
ssh_client = connect() | |
# check if the record already exists | |
check_query = f"SELECT COUNT(*) FROM token_key WHERE user_id='{user_id}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} 'USE SBTdb; {check_query}'") | |
count_str = stdout.read().decode() | |
count_line = count_str.split('\n')[1] | |
count = int(count_line) | |
print(f"count: {count}") | |
if count == 0: | |
# if the record doesn't exist, insert a new record | |
try: | |
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}')" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {insert_query}\"") | |
print("error:", stderr.read().decode()) | |
except Exception as e: | |
print(e) | |
else: | |
# if the record exists, update the existing record | |
try: | |
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}'" | |
# sql_query = f"USE SBTdb ;{update_query}" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {update_query}\"") | |
print("error:", stderr.read().decode()) | |
except Exception as e: | |
print(e) | |
close(ssh_client) | |
return token_id | |
# called through API\general, KYC, marketing | |
def get_userinfo(user_id, token_id, type): | |
ssh_client = connect() | |
info = {} | |
try: | |
query = f"SELECT token_id FROM token_key WHERE user_id='{user_id}' " | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE SBTdb; {query}\"") | |
print("error:", stderr.read().decode()) | |
except Exception as e: | |
print(e) | |
token_id_str = stdout.read().decode() | |
print("token_id_str:", token_id_str) | |
token_id_db = token_id_str.split('\n')[1] | |
if token_id_db == token_id: | |
if type == "general": | |
retrieve_query = f"SELECT facebook_id FROM ipygg_users WHERE user_id='{user_id}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE ipygg; {retrieve_query}\"") | |
print("error:", stderr.read().decode()) | |
facebook_id = stdout.read().decode().split('\n')[1] | |
info['facebook_id'] = facebook_id | |
elif type == "KYC": | |
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}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE ipygg; {retrieve_query}\"") | |
print("error:", stderr.read().decode()) | |
info_list = stdout.read().decode().split('\n')[1].split('\t') | |
info = { | |
'email': info_list[0], | |
'salutation': info_list[1], | |
'first_name': info_list[2], | |
'last_name': info_list[3], | |
'birthday': info_list[4], | |
'country': info_list[5], | |
'region': info_list[6], | |
'location': info_list[7], | |
'address': info_list[8], | |
'postcode': info_list[9], | |
'area_code': info_list[10], | |
'phone': info_list[11], | |
'hk_id': info_list[12], | |
'passport': info_list[13] | |
} | |
info['dual_passport'] = False #TODO: add dual_passport to ipygg_users | |
elif type == "marketing": | |
retrieve_query = f"SELECT email, facebook_id, wealth_level FROM ipygg_users WHERE user_id='{user_id}'" | |
stdin, stdout, stderr = ssh_client.exec_command(f"{command()} \"USE ipygg; {retrieve_query}\"") | |
print("error:", stderr.read().decode()) | |
info_list = stdout.read().decode().split('\n')[1].split('\t') | |
info['email'] = info_list[0] | |
info['facebook_id'] = info_list[1] | |
info['wealth_level'] = info_list[2] | |
close(ssh_client) | |
return True, info | |
else: | |
close(ssh_client) | |
return False, info | |
# def update_Status(request_id, status, user_id, institution_id, latest_update_date=time.localtime().tm_mday): | |
# conn, cursor = get_conn_and_cursor(request_id) | |
# try: | |
# sql = "update request_log set status = \"{}\" where user_id = \"{}\" AND " \ | |
# "institution_id = \"{}\" AND request_id = \"{}\";".format( | |
# status, user_id, institution_id, request_id) | |
# # addRequestLog(request_id, sql) | |
# # cursor.execute(sql) | |
# # conn.commit() | |
# except Exception as e: | |
# print(e) | |
# # addRequestLog(request_id, "Could not update user Status") | |
def getCustomerToken(request_id, user_id): | |
""" | |
Returns: *customer token* for generating link token | |
Uses: *Client ID*, *Client secret* (Defined in constants.py) | |
Url: "https://api.prod.finverse.net/auth/customer/token" | |
Section 0 in the Flowchart | |
""" | |
url = "https://api.prod.finverse.net/auth/customer/token" | |
payload = json.dumps({ | |
"client_id": f"{CLIENT_ID}", | |
"client_secret": f"{CLIENT_SECRET}", | |
"grant_type": "client_credentials" | |
}) | |
headers = { | |
'X-Request-Id': f'{user_id}-{int(time.time())}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.request( | |
"POST", url, headers=headers, data=payload).json() | |
# logger( | |
# request_id, f'=============headers X-Request-Id============\n{headers["X-Request-Id"]}') | |
# logger( | |
# request_id, f'============1 post response==========\n{response}') | |
return response["access_token"] | |
def getLinkToken(customer_token, request_id, user_id): | |
url = "https://api.prod.finverse.net/link/token" | |
payload = json.dumps({ | |
"client_id": f"{CLIENT_ID}", | |
"user_id": user_id, | |
"redirect_uri": f"{REDIRECT_URL}", | |
"state": "ipygg_stateparameter", | |
"grant_type": "client_credentials", | |
"response_mode": "form_post", | |
"response_type": "code", | |
"automatic_data_refresh": "", | |
"countries": [], | |
"institution_id": "", | |
"institution_status": "", | |
"language": "", | |
"link_mode": "test", | |
"products_requested": [], | |
"products_supported": [], | |
"ui_mode": "redirect", | |
"user_type": [] | |
}) | |
headers = { | |
'X-Request-Id': f'{user_id}-{int(time.time())}', | |
'Authorization': f'Bearer {customer_token}', | |
'Content-Type': 'application/json' | |
} | |
try: | |
print(request_id, "Trying to get login ID from finverse") | |
response = requests.request("POST", url, headers=headers, data=payload) | |
j = response.json() | |
link_url = j["link_url"] | |
print(request_id, link_url) | |
if 'error' in j.keys(): | |
if j['error']['code'] == 40001: | |
print(j) | |
return 404 | |
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id, | |
# request_id, date) | |
return j['link_url'] | |
except Exception as e: | |
print(request_id, "Unable to refresh") | |
print(request_id, e) | |
def getLoginID(customer_token, request_id, user_id): | |
""" | |
Returns: *link token* | |
Uses: *Customer token*, *login refresh token* | |
Documentation: Data API -> 12-Data Refresh -> POST /auth/token/refresh (1st step in the Refresh flow) | |
URL: https://api.prod.finverse.net/auth/token/refresh | |
Section 9 in the Flowchart | |
""" | |
url = "https://api.prod.finverse.net/login_identity" | |
payload = json.dumps({ | |
}) | |
headers = { | |
'X-Request-Id': f'{user_id}-{int(time.time())}', | |
'Authorization': f'Bearer {customer_token}', | |
'Content-Type': 'application/json' | |
} | |
try: | |
print(request_id, "Trying to get login ID from finverse") | |
response = requests.request("GET", url, headers=headers) | |
j = response.json() | |
login_id = j["login_identity"]["login_identity_id"] | |
print(request_id, login_id) | |
if 'error' in j.keys(): | |
if j['error']['code'] == 40001: | |
print(j) | |
return 404 | |
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id, | |
# request_id, date) | |
return j['access_token'] | |
except Exception as e: | |
print(request_id, "Unable to refresh") | |
print(request_id, e) | |
def getLinkCode(user_id, request_id, code, customer_token): | |
url = "https://api.prod.finverse.net/auth/token" | |
payload = { | |
"client_id": f"{CLIENT_ID}", | |
"code": f"{code}", | |
"redirect_uri": f"{REDIRECT_URL}", | |
"grant_type": "authorization_code" | |
} | |
headers = { | |
'X-Request-Id': f'{user_id}-{int(time.time())}', | |
'Authorization': f'Bearer {customer_token}', | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
try: | |
print(request_id, "Trying to get link code from finverse") | |
response = requests.request("POST", url, headers=headers, data=payload) | |
j = response.json() | |
# print(j) | |
login_id = j["login_identity_id"] | |
login_id_token = j["access_token"] | |
refresh_token = j["refresh_token"] | |
print(f'{request_id}, login id: {login_id} , refresh token: {refresh_token}') | |
if 'error' in j.keys(): | |
if j['error']['code'] == 40001: | |
return 404 | |
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id, | |
# request_id, date) | |
return login_id_token | |
except Exception as e: | |
print(request_id, "Unable to get link code") | |
print(request_id, e) | |
def getRefreshedToken(customer_token, request_id, user_id, refresh_token, institution_id, date, redirect_uri): | |
""" | |
Returns: *link token* | |
Uses: *Customer token*, *login refresh token* | |
Documentation: Data API -> 12-Data Refresh -> POST /auth/token/refresh (1st step in the Refresh flow) | |
URL: https://api.prod.finverse.net/auth/token/refresh | |
Section 9 in the Flowchart | |
""" | |
url = "https://api.prod.finverse.net/auth/token/refresh" | |
payload = json.dumps({ | |
"redirect_uri": f"{redirect_uri}", | |
"grant_type": "client_credentials", | |
"code": "" | |
}) | |
headers = { | |
'X-Request-Id': f'{user_id}-{int(time.time())}', | |
'Authorization': f'Bearer {customer_token}', | |
'Content-Type': 'application/json' | |
} | |
try: | |
print(request_id, "Trying to get refreshed credentials from finverse") | |
response = requests.request("POST", url, headers=headers, data=payload) | |
j = response.json() | |
print(request_id, j) | |
if 'error' in j.keys(): | |
if j['error']['code'] == 40001: | |
return 404 | |
# finverse_connector.update_refresh_token(user_id, j['login_identity_id'], j['refresh_token'], institution_id, | |
# request_id, date) | |
return j['access_token'] | |
except Exception as e: | |
print(request_id, "Unable to refresh") | |
print(request_id, e) | |
def get_pdf(userId, statement_id, login_identity_token, request_id): | |
try: | |
# logger(request_id, f"\nfetching PDF for {statement_id}") | |
url = f"https://api.prod.finverse.net/statements/{statement_id}?redirect=false" | |
payload = {} | |
headers = { | |
'X-Request-Id': f'{userId}-{int(time.time())}', | |
'Authorization': f'Bearer {login_identity_token}', | |
'Content-Type': "application/json" | |
} | |
response = requests.get(url, headers=headers, data=payload).json() | |
# print(request_id, "response") | |
link = response['statement_links'][0]['url'] | |
# print(f'link is : {link}') | |
return link | |
except Exception as e: | |
print(request_id, f"Error in get_pdf") | |
print(request_id, e) | |
# finverse_connector.update_Status( | |
# request_id, "50200", userId, institution_id) | |
# | |
def statements_fetch(userId, institution_id, login_identity_token, request_id): | |
try: | |
# logger(request_id, "fetching statements from finverse") | |
url = f"https://api.prod.finverse.net/statements" | |
payload = {} | |
headers = { | |
'X-Request-Id': f'{userId}-{int(time.time())}', | |
'Authorization': f'Bearer {login_identity_token}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.get(url, headers=headers, data=payload).json() | |
print(request_id, f"The response is :\n{response}") | |
statement = response["statements"] | |
print(f'statement: {statement[0]["id"]}') | |
# for statement in response['statements']: | |
# # name = statement['name'] + ' ' + statement['date'] | |
if statement != None: | |
statement = get_pdf(userId, statement[0]["id"], login_identity_token, request_id) | |
return statement, 200 | |
else: | |
print('no statement') | |
return 0 | |
except Exception as e: | |
print(request_id, f"Error in statements_fetch") | |
print(request_id, e) | |
# finverse_connector.update_Status( | |
# request_id, "50200", userId, institution_id) | |
return 500 | |
def check_if_token_exists(user_id, institution_id, request_id): | |
""" | |
Searches: if user exists in user_and_login_identity table | |
and if value of *refresh_allowed* is 1 | |
Section 1 in the Flowchart | |
""" | |
# db_name, table_name, restriction, restriction_value, attribute_name | |
user_details = get_attribute_2('ipygg', 'user_and_login_identity', 'user_id', user_id, 'institution_id', institution_id, '*') | |
if user_details != "": | |
print(user_details) | |
# refresh_token = user_details[6] | |
refresh_allowed = user_details[-1] | |
print(refresh_allowed) | |
if refresh_allowed == "1": | |
return user_details | |
else: | |
return 0 | |
else: | |
return 0 | |
def statements_report(userId, institutionId, requestId, date): | |
try: | |
# finverse_connector.add_status( | |
# requestId, "10000", userId, institutionId, 'PDFs', date) | |
customer_token = getCustomerToken(requestId, userId) | |
print(customer_token) | |
# addRequestLog(requestId, f"Fetching PDFs for {userId} with {institutionId} institutionID") | |
# userInfo = check_if_token_exists( | |
# userId, institutionId, requestId) | |
userInfo = 1 | |
print(f'userInfo: {userInfo}') | |
if userInfo == 0: | |
# createUserAccount() | |
print(requestId, "User does not have any accounts") | |
return 500 # user does not exist | |
else: | |
# refresh_token = userInfo[-2] | |
search_url = getLinkToken(customer_token, requestId, userId) | |
driver = webdriver.Chrome() | |
driver.get(search_url) | |
wait = WebDriverWait(driver, 50).until(EC.url_contains("code=")) | |
url = driver.current_url | |
time.sleep(5) | |
url_list = url.split("code=",1)[1] | |
code = url_list[:26] | |
print(f'code is {code}') | |
# webbrowser.open(search_url) | |
# where user input | |
# code = input("Enter code:") | |
time.sleep(5) | |
login_identity_token = getLinkCode(requestId,userId,code,customer_token) | |
pdf_link = statements_fetch(userId, institutionId, login_identity_token, requestId) | |
if pdf_link!= 500: | |
# pdf_link = get_pdf(userId, statement_list['id'], login_identity_token, requestId) | |
webbrowser.open(pdf_link[0]) | |
return pdf_link | |
else: | |
return 500 | |
except Exception as e: | |
print(requestId, "Fetching PDFs failed") | |
print(requestId, e) | |
print(e) | |
return 500 # Fetching error | |
# 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") | |
# webbrowser.open(pdf_link) | |
# print(f'{pdf_link} finished') | |
def get_today_date(): | |
today = date.today() | |
return str(today) | |
# Example for what will be returned | |
# 2023-06-29 | |
################### MAIN ############################ | |
def get_bs(userId="1836660007822863504716", institutionId="00000005",requestId="request1234"): | |
standard_names = {'boch': "Consolidated Statement", | |
'hangseng': "Statement of", | |
'hsbc': "Statement - HSBC One Account", | |
'sc': "statementOfAccount"} | |
# records = statements_report("1836660007822863504716","00000005","request1234","05072023") | |
# userId, institutionId, requestId, date | |
pdf_link = statements_report(userId, institutionId, requestId, str(get_today_date())) | |
# get_pdf(userId, institution_id, records[0]['id'], login_identity_token, request_id) | |
if pdf_link != 500: | |
print(f"successfully get bank report with link: {pdf_link}") | |
def get_transactions(user_id, login_identity_token): | |
""" | |
Returns: *customer token* for generating link token | |
Uses: *Client ID*, *Client secret* (Defined in constants.py) | |
Url: "https://api.prod.finverse.net/transactions?offset=0&limit=100" | |
Section 0 in the Flowchart | |
""" | |
url = "https://api.prod.finverse.net/transactions?offset=0&limit=100" | |
# payload = json.dumps({ | |
# "client_id": f"{CLIENT_ID}", | |
# "client_secret": f"{CLIENT_SECRET}", | |
# "grant_type": "client_credentials" | |
# }) | |
headers = { | |
'X-Request-Id': f'{user_id}-{int(time.time())}', | |
'Authorization': f'Bearer {login_identity_token}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.request( | |
"GET", url, headers=headers).json() | |
transaction_record = response["transactions"] | |
print(f"getting transactions: {transaction_record}") | |
return response["transactions"] | |
def past_transactions(userId="1836660007822863504716", institutionId="00000005",requestId="request1234"): | |
try: | |
# finverse_connector.add_status( | |
# requestId, "10000", userId, institutionId, 'PDFs', date) | |
customer_token = getCustomerToken(requestId, userId) | |
print(customer_token) | |
# addRequestLog(requestId, f"Fetching PDFs for {userId} with {institutionId} institutionID") | |
# userInfo = check_if_token_exists( | |
# userId, institutionId, requestId) | |
userInfo = 1 | |
print(f'userInfo: {userInfo}') | |
if userInfo == 0: | |
# createUserAccount() | |
print(requestId, "User does not have any accounts") | |
return 500 # user does not exist | |
else: | |
search_url = getLinkToken(customer_token, requestId, userId) | |
driver = webdriver.Chrome() | |
driver.get(search_url) | |
wait = WebDriverWait(driver, 50).until(EC.url_contains("code=")) | |
url = driver.current_url | |
time.sleep(5) | |
url_list = url.split("code=",1)[1] | |
code = url_list[:26] | |
print(f'code is {code}') | |
time.sleep(5) | |
login_identity_token = getLinkCode(requestId,userId,code,customer_token) | |
transaction_record = get_transactions(userId, login_identity_token) | |
# transaction_record = transaction_record[:10] | |
print('printing transactions records') | |
item_num = 0 | |
for item in transaction_record: | |
if str(datetime.date.today().year) in item["created_at"]: | |
item_num = item_num + 1 | |
# print(item) | |
transaction_data = { | |
"endpoint": "SBT", | |
"apiType": "store_finverse_verif", | |
"requestId": "request1234", | |
"userId": userId # a string | |
} | |
if (item_num >= 100): | |
print("user has more than 100 transactions in the past year") | |
transaction_data["finverseVerif"] = "True" # a string "True" or "False" | |
# post true | |
else: | |
print("user has no more than 100 transactions in the past year") | |
transaction_data["finverseVerif"] = "False" # a string "True" or "False" | |
# post false | |
generate_request(transaction_data) | |
except Exception as e: | |
print(requestId, "Fetching transactions failed") | |
print(requestId, e) |