Last active
April 16, 2018 20:55
-
-
Save papachristoumarios/6483feda3288973c555b4ea1e532b5e6 to your computer and use it in GitHub Desktop.
NBG openAPI Sandbox Wrappers in Typescript/NodeJS and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# implementation for NBG openAPI v2.0 | |
import requests, json | |
class NBGSandbox: | |
def __init__(self, sandboxid='6916545110343680'): | |
headers = { 'accept': "text/json" } | |
r = requests.get('https://apis.nbg.gr/public/nbgapis/obp/v3.0.1/sandbox/{}'.format(sandboxid)) | |
self.sandbox_data = r.json() | |
self.id = self.sandbox_data['SandboxID'] | |
self.users = {} | |
self.accounts = {} | |
for user in self.sandbox_data['Users']: | |
self.users[user['user_id']] = user | |
self.accounts[user['user_id']] = [] | |
for p in user['UserPermissions']: | |
self.accounts[user['user_id']].append(p['AccountID']) | |
def balance(self, user_id, account_id): | |
assert (account_id in self.accounts[user_id]) | |
user = self.users[user_id] | |
headers = { | |
'accept' : 'text/json', | |
'application_id' : 'testg', | |
'sandbox_id' : self.id, | |
'user_id': user_id, | |
'username' : user['username'], | |
'provider_id' : user['provider_id'], | |
'provider' : user['provider'] | |
} | |
req = "https://apis.nbg.gr/public/nbgapis/obp/v3.0.1/banks/DB173089-A8FE-43F1-8947-F1B2A8699829/accounts/{}/owner/account".format(account_id) | |
r = requests.get(req, headers=headers).json() | |
if 'error' in r.keys(): | |
raise Exception(r['error']) | |
else: | |
result = r['balance'] | |
result['IBAN'] = r['IBAN'] | |
return result | |
def pay(self, sender_id, account_id, to_iban, amount): | |
assert(account_id in self.accounts[sender_id]) | |
sender = self.users[sender_id] | |
headers = { | |
'accept': 'application/json', | |
'context-type' : 'application/json', | |
'sandbox_id' : self.id, | |
'application_id' : 'testg', | |
'user_id' : sender['user_id'], | |
'username' : sender['username'], | |
'provider_id' : sender['provider_id'], | |
'provider' : sender['provider'] | |
} | |
params = {'value': | |
{'amount': str(amount), 'currency': 'EUR'}, | |
'to': {'iban': to_iban }, | |
'charge_policy': 'OUR', | |
'description': 'Payment'} | |
req = "https://apis.nbg.gr/public/nbgapis/obp/v3.0.1/banks/DB173089-A8FE-43F1-8947-F1B2A8699829/accounts/{}/owner/transaction-request-types/sepa/transaction-requests".format(account_id) | |
r = requests.post(req, params=params, headers=headers).json() | |
if 'error' in r.keys(): | |
raise Exception(r['error']) | |
else: | |
return (r) | |
sandbox = NBGSandbox() | |
ids = ['c1b4b5c4-ba09-4a90-badf-e4a852a1e495', '5c9cfd23-4fab-4b4e-ac81-fe878a19cb68'] | |
x = sandbox.users[ids[0]] | |
y = sandbox.users[ids[1]] | |
ax = sandbox.accounts[ids[0]][0] | |
ay = sandbox.accounts[ids[1]][0] | |
bx = sandbox.balance(ids[0], ax) | |
by = sandbox.balance(ids[1], ay) | |
print(bx) | |
print(by) | |
#res = sandbox.pay(ids[0], ax, by['IBAN'], 10) | |
#print(res) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as request from 'request'; | |
import * as rp from 'request-promise'; | |
export class NBGSandbox { | |
id : string; | |
private sandboxData : object; | |
private clientSecret: string; | |
private clientID: string; | |
public users : object; | |
public accounts: object; | |
private auth: object; | |
constructor(sandboxID : string, | |
clientId : string, | |
clientSecret : string) { | |
this.id = sandboxID; | |
this.clientID = clientId; | |
this.clientSecret = clientSecret; | |
this.auth = { | |
'x-ibm-client-secret': this.clientSecret, | |
'x-ibm-client-id': this.clientID, | |
} | |
this.sandboxData = {}; | |
} | |
async init() { | |
let options = { method: 'GET', | |
url: 'https://apis.nbg.gr/public/sandbox/obp/v3.0.2/sandbox/' + this.id, | |
headers: | |
{ 'accept' : 'text/json', | |
'x-ibm-client-secret': this.clientSecret, | |
'x-ibm-client-id': this.clientID, | |
} }; | |
await rp(options).then((body) => { | |
this.sandboxData = JSON.parse(body); | |
}).catch((err) => { }); | |
this.users = {} | |
this.accounts = {} | |
for (let i = 0; i < this.sandboxData['Users'].length; i++) { | |
let user = this.sandboxData['Users'][i]; | |
this.users[user['user_id']] = user; | |
this.accounts[user['user_id']] = []; | |
for (let j = 0; j < user['UserPermissions'].length; j++) { | |
let p = user['UserPermissions'][j] | |
this.accounts[user['user_id']].push(p['AccountID']); | |
} | |
} | |
} | |
async balance(user_id : string, account_id : string) { | |
let user = this.users[user_id] | |
let options = { | |
method: 'GET', | |
url: "https://apis.nbg.gr/public/sandbox/obp/v3.0.2/my/banks/DB173089-A8FE-43F1-8947-F1B2A8699829/accounts/" + account_id + "/account", | |
headers : { | |
provider : user['provider'], | |
provider_id : user['provider_id'], | |
username : user['username'], | |
user_id: user_id, | |
application_id : 'testg', | |
sandbox_id: this.id, | |
accept : 'text/json', | |
'x-ibm-client-secret': this.clientSecret, | |
'x-ibm-client-id': this.clientID, | |
} }; | |
let result = {} | |
await rp(options).then((data) => { | |
let r = JSON.parse(data); | |
result = r['balance']; | |
result['IBAN'] = r['IBAN']; | |
}).catch((err) => { | |
console.log(err); | |
result = null; | |
}) | |
return result; | |
} | |
async pay(sender_id : string , account_id : string, to_iban : string, amount : number) { | |
let sender = this.users[sender_id] | |
let options = { | |
method : 'POST', | |
url : "https://apis.nbg.gr/public/sandbox/obp/v3.0.2/banks/DB173089-A8FE-43F1-8947-F1B2A8699829/accounts/" + account_id + "/owner/transaction-request-types/sepa/transaction-requests", | |
headers : { | |
'accept': 'text/json', | |
'context-type' : 'text/json', | |
'sandbox_id' : this.id, | |
'application_id' : 'testg', | |
'user_id' : sender['user_id'], | |
'username' : sender['username'], | |
'provider_id' : sender['provider_id'], | |
'provider' : sender['provider'], | |
'x-ibm-client-secret': this.clientSecret, | |
'x-ibm-client-id': this.clientID, | |
}, | |
body : {value: {amount: amount, currency: 'EUR'}, | |
to: {iban: to_iban }, | |
charge_policy: 'OUR', | |
description: 'Payment' | |
}, | |
json : true, | |
} | |
let result = {} | |
await rp(options).then((data) => { | |
result = data; | |
}).catch((err) => { | |
console.log(err); | |
result = null; | |
}); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment