-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfidor_bank.py
More file actions
130 lines (101 loc) · 5.26 KB
/
fidor_bank.py
File metadata and controls
130 lines (101 loc) · 5.26 KB
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
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, session, url_for, render_template
from requests.auth import HTTPBasicAuth
import requests
import json
app = Flask(__name__)
app.config['SESSION_TYPE'] = 'memcached'
app.config['SECRET_KEY'] = 'bcda4021155052d1d9ce82375ce201e1'
client_id = "b332054ba4e97102"
client_secret = "bcda4021155052d1d9ce82375ce201e1"
authorization_base_url = 'https://apm.tp.sandbox.fidor.com/oauth/authorize'
token_url = 'https://apm.tp.sandbox.fidor.com/oauth/token'
redirect_uri = 'http://localhost:5000/callback'
@app.route('/', methods=["GET"])
@app.route('/index', methods=["GET"])
def default():
fidor = OAuth2Session(client_id, redirect_uri=redirect_uri)
authorization_url, state = fidor.authorization_url(authorization_base_url)
session['oauth_state'] = state
print("authorization URL is =" + authorization_url)
return redirect(authorization_url)
@app.route("/callback", methods=["GET"])
def callback():
# step 2: retrieving an access token
# the user has been redirected back from the provider to your registered
# callback URL. With this redirectly comes an authorization code included
# in the redirect URL. We will use that to obtain an access token.
fidor = OAuth2Session(state=session['oauth_state'])
authorizationCode = request.args.get('code')
body = 'grant_type="authorization_code&code='+authorizationCode+ \
'&redirect_uri='+redirect_uri+'&client_id=' +client_id
auth = HTTPBasicAuth(client_id, client_secret)
token = fidor.fetch_token(token_url, auth=auth, code=authorizationCode, body=body, method='POST')
# At this point you can fetch protected resources but lets save
# the token and show how this is done from a persisted token
session['oauth_token'] = token
return redirect(url_for('.services'))
@app.route("/services", methods=["GET"])
def services():
# fetching a protected resource using an OAuth 2 token.
try:
token = session['oauth_token']
url = "https://api.tp.sandbox.fidor.com/accounts"
payload = ""
headers = {
'Accept': "application/vnd.fidor.de;version=1;text/json",
'Authorization': "Bearer "+token["access_token"],
'Cache-Control': "no-cache",
'Postman-Token': "fb619961-bd74-4155-87ff-5bb213d111d0,72bfb6b1-2d45-4ef3-bc7b-0f83b3bea239",
}
response = requests.request("GET", url, data=payload, headers=headers)
print("services= " + response.text)
customersAccount = json.loads(response.text)
customerDetails = customersAccount['data'][0]
customerInformation = customerDetails['customers'][0]
session['fidor_customer'] = customersAccount
return render_template('services.html', fID=customerInformation["id"],
fFirstName=customerInformation["first_name"], fLastName=customerInformation["last_name"],
fAccountNo=customerDetails["account_number"], fBalance=(customerDetails["balance"]/100))
except KeyError:
print("Key error in services-to return back to index")
return redirect(url_for('default'))
@app.route("/bank_transfer", methods=["GET"])
def transfer():
try:
customersAccount = session['fidor_customer']
customersDetails = customersAccount['data'][0]
return render_template('internal_transfer.html', fFIDORID=customersDetails["id"],
fAccountNo=customersDetails["account_number"],fBalance=(customersDetails["balance"]/100))
except KeyError:
print("Key error in bank_transfer-to reutn back to index")
return redirect(url_for('.index'))
@app.route("/process", methods=["POST"])
def process():
if request.method == "POST":
token = session['oauth_token']
customersAccount = session['fidor_customer']
customerDetails = customersAccount['data'][0]
fidorID = customerDetails['id']
custEmail = request.form['customerEmailAdd']
transferAmt = int(float(request.form['transferAmount'])*100)
transferRemarks = request.form['transferRemarks']
transactionID = request.form['transactionID']
url = 'https://api.tp.sandbox.fidor.com/internal_transfers'
payload = "{\n\t\"account_id\": \""+fidorID+"\",\n\t\"receiver\": \""+ \
custEmail+"\" ,\n\t\"external_uid\": \""+transactionID+"\",\n\t\"amount\": "+ \
str(transferAmt)+",\n\t\"subject\": \""+transferRemarks+"\"\n}\n"
headers = {
'Accept': "application/vnd.fidor.de; version=1,text/json",
'Content-Type': "application/json",
'Authorization': "Bearer "+token["access_token"],
'cache-control': "no-cache",
'Postman-Token': "fb619961-bd74-4155-87ff-5bb213d111d0,72bfb6b1-2d45-4ef3-bc7b-0f83b3bea239",
}
response = requests.request("POST", url, data=payload, headers=headers)
print("process"+response.text)
transactionDetails = json.loads(response.text)
return render_template('transfer_result.html', fTransactionID=transactionDetails["id"],
custEmail=transactionDetails["receiver"], fRemarks=transactionDetails["subject"],
famount=(float(transactionDetails["amount"])/100),
fRecipientName=transactionDetails["recipient_name"])