def getTransactionHistory(uid): connector_settings = polish_api_data token = queries.getTokenByUid(uid) if token: connector_settings['accessToken'] = token api_client = enablebanking.ApiClient('Alior', connector_settings=connector_settings) # Create client instance. aisp_api = enablebanking.AISPApi(api_client) user_config = queries.getUserInvoiceConfigByUid(uid) date_from = queries.fetchLastUserTransaction(uid).timestamp print(date_from.strftime('%Y-%m-%d'), datetime.datetime.now().strftime('%Y-%m-%d')) transactions = aisp_api.get_account_transactions(user_config.account_number, date_from=date_from.strftime('%Y-%m-%d'), date_to=datetime.datetime.now().strftime('%Y-%m-%d')) print(transactions.transactions[0]) # for transaction in transactions.transactions: # transaction.entry_reference = 'PL64249010440000420042820498' # user = queries.getUserByAccNo(transaction.entry_reference) # if user: # other_uid = user.uid # ## Mocking some information due to lacking data from API # if random.randint(0, 2) == 1: # queries.addNewTransaction(uid, other_uid, transaction.transaction_amount.amount, f'Opłata za fakturę nr {random.randint(10000, 99999)}', transaction.transaction_amount.currency) # else: # queries.addNewTransaction(other_uid, uid, transaction.transaction_amount.amount, f'Opłata za fakturę nr {random.randint(10000, 99999)}', transaction.transaction_amount.currency) return None
def keyRedirect(): # token = auth_api.make_token(grant_type="authorization_code", # grant type, MUST be set to "authorization_code" # code=parsed_query.get("code"), # # The code received in the query string when redirected from authorization # redirect_uri=REDIRECT_URL) code = request.args.get("code") # print(f"\nCode: {code}\n\n") api_client = enablebanking.ApiClient( "Alior", connector_settings=nordea_settings()) # Create client instance. auth_api = enablebanking.AuthApi( api_client) # Create authentication interface. # auth_url = auth_api.get_auth( # response_type="code", # OAuth2 response type # redirect_uri=REDIRECT_URL, # redirect URI # scope=["aisp"], # API scopes # state="okok").url # state to pass to redirect URL # redirected_url = util.read_redirected_url(auth_url, REDIRECT_URL) # parsed_query = util.parse_redirected_url(redirected_url) # calling helper functions for CLI interaction # logging.info("Parsed query: %s", parsed_query) token = auth_api.make_token( grant_type= "authorization_code", # grant type, MUST be set to "authorization_code" code=code, # The code received in the query string when redirected from authorization redirect_uri=REDIRECT_URL) # logging.info("Token: %s", token) aisp_api = enablebanking.AISPApi( api_client ) # api_client has already accessToken and refreshToken applied after call to makeToken() accounts = aisp_api.get_accounts() x = "Accounts info: %s" % accounts for account in accounts.accounts: transactions = aisp_api.get_account_transactions( account.account_id.iban, date_from=(datetime.datetime.now() - datetime.timedelta(days=89)).strftime('%Y-%m-%d'), date_to=datetime.datetime.now().strftime('%Y-%m-%d')) x += "Transactions info: %s" % transactions balances = aisp_api.get_account_balances(account.account_id.iban) x += "Balances info: %s" % balances # TODO: how to save it somewhere else # do_magic.magic(x) return x
def getAccountBalance(uid): connector_settings = polish_api_data token = queries.getTokenByUid(uid) balance = 15000 if token: connector_settings['accessToken'] = token api_client = enablebanking.ApiClient('Alior', connector_settings=connector_settings) # Create client instance. aisp_api = enablebanking.AISPApi(api_client) user_config = queries.getUserInvoiceConfigByUid(uid) balance = aisp_api.get_account_balances(user_config.account_number) return balance.balances[0].balance_amount.amount
def getUserTokenAndAPI(self, bank, api_client, auth_api, redirected_url): redirect_begin = self.bank2redirect[bank] parsed_query = util.parse_redirected_url(redirected_url) token = auth_api.make_token( grant_type= "authorization_code", # grant type, MUST be set to "authorization_code" code=parsed_query.get("code"), # The code received in the query string when redirected from authorization redirect_uri=redirect_begin) logging.info("Token: %s", token) aisp_api = enablebanking.AISPApi( api_client ) # api_client has already accessToken and refreshToken applied after call to makeToken() return aisp_api, token
def main(): api_client = enablebanking.ApiClient( "Alior", connector_settings=nordea_settings()) # Create client instance. auth_api = enablebanking.AuthApi( api_client) # Create authentication interface. auth_url = auth_api.get_auth( response_type="code", # OAuth2 response type redirect_uri=REDIRECT_URL, # redirect URI scope=["aisp"], # API scopes state="okok", access=enablebanking.Access( valid_until=(datetime.datetime.now() + datetime.timedelta(days=89)).strftime('%Y-%m-%d'), balances=enablebanking.BalancesAccess(), transactions=enablebanking.TransactionsAccess()) ).url # state to pass to redirect URL redirected_url = util.read_redirected_url(auth_url, REDIRECT_URL) parsed_query = util.parse_redirected_url( redirected_url) # calling helper functions for CLI interaction logging.info("Parsed query: %s", parsed_query) token = auth_api.make_token( grant_type= "authorization_code", # grant type, MUST be set to "authorization_code" code=parsed_query.get("code"), # The code received in the query string when redirected from authorization redirect_uri=REDIRECT_URL) logging.info("Token: %s", token) aisp_api = enablebanking.AISPApi( api_client ) # api_client has already accessToken and refreshToken applied after call to makeToken() accounts = aisp_api.get_accounts() logging.info("Accounts info: %s", accounts) for account in accounts.accounts: transactions = aisp_api.get_account_transactions(account.resource_id) logging.info("Transactions info: %s", transactions) balances = aisp_api.get_account_balances(account.resource_id) logging.info("Balances info: %s", balances)
def main(): api_meta = get_connector_meta( CONNECTOR_NAME, CONNECTOR_COUNTRY) # get meta information for current connector api_client = enablebanking.ApiClient( CONNECTOR_NAME, connector_settings=CONNECTOR_SETTINGS) # Create client instance. auth_api = enablebanking.AuthApi( api_client) # Create authentication interface. client_info = enablebanking.ClientInfo() connector_psu_headers = api_meta.required_psu_headers if "psuIpAddress" in connector_psu_headers: client_info.psu_ip_address = "10.10.10.10" if "psuUserAgent" in connector_psu_headers: client_info.psu_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15" auth_api.set_client_info(client_info=client_info) access = enablebanking.Access(valid_until=(datetime.datetime.now() + datetime.timedelta(days=89))) get_auth_params: Dict[str, Any] = {"state": str(uuid.uuid4())} if api_meta.auth_info[0].info.access: get_auth_params["access"] = access if api_meta.auth_info[0].info.user_id_required: get_auth_params["user_id"] = "some_id" if api_meta.auth_info[0].info.password_required: get_auth_params["password"] = "******" auth_response = auth_api.get_auth(**get_auth_params) make_token_response = None if auth_response.url: # if url is returned, then we are doing redirect flow redirected_url = read_redirected_url(auth_response.url, REDIRECT_URL) query_params = auth_api.parse_redirect_url(redirected_url) logging.info("Parsed query: %s", query_params) make_token_response = auth_api.make_token( "authorization_code", # grant type, MUST be set to "authorization_code" query_params.code, auth_env=auth_response.env, ) else: # decoupled flow otherwise # Doing a retry to check periodically whether user has already authorized a consent sleep_time = 3 for _ in range(3): try: make_token_response = auth_api.make_token( "authorization_code", # grant type, MUST be set to "authorization_code" code="", auth_env=auth_response.env, ) except enablebanking.MakeTokenException as e: logging.debug( f"Failed to get authorization code, sleeping for {sleep_time} seconds" ) time.sleep(sleep_time) else: break logging.info("Token: %s", make_token_response) aisp_api = enablebanking.AISPApi( api_client ) # api_client has already accessToken and refreshToken applied after call to makeToken() if api_meta.modify_consents_info[0].info.before_accounts: # bank requires to create a consent explicitly before accessing list of accounts consent = aisp_api.modify_consents(access=access) logging.debug(f"Consent: {consent}") try: # If returned consent has a redirect url, we need to redirect a PSU there consent_url = consent.links.redirect.href redirect_url = read_redirected_url(consent_url, REDIRECT_URL) logging.debug(f"Redirect url: {redirect_url}") except AttributeError: pass accounts = aisp_api.get_accounts() logging.info("Accounts info: %s", accounts) if api_meta.modify_consents_info[0].info.accounts_required: # bank requires to create a consent with account ids specified account_ids = [ enablebanking.AccountIdentification(iban=acc.account_id.iban) for acc in accounts.accounts ] access.accounts = account_ids consent = aisp_api.modify_consents(access=access) logging.debug(f"Consent: {consent}") try: # If returned consent has a redirect url, we need to redirect a PSU there consent_url = consent.links.redirect.href redirect_url = read_redirected_url(consent_url, REDIRECT_URL) logging.debug(f"Redirect url: {redirect_url}") except AttributeError: pass for account in accounts.accounts: transactions = aisp_api.get_account_transactions(account.resource_id) logging.info("Transactions info: %s", transactions) balances = aisp_api.get_account_balances(account.resource_id) logging.info("Balances info: %s", balances)