-
Notifications
You must be signed in to change notification settings - Fork 21
/
hello_payments_v1.4.py
149 lines (124 loc) · 4.83 KB
/
hello_payments_v1.4.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script will execute a payment transation via the OpenBankProject API
Note: in order to use this example, you need to have at least one account
that you can send money from (i.e. be the owner).
Configure settings.py to suit your needs.
"""
import json
import sys
from oauth_dance import get_api
from settings import (
API_BASE_URL,
MY_BANK,
COUNTERPARTY_BANK,
COUNTERPARTY_ACCOUNT_ID,
PAYMENT_CURRENCY,
PAYMENT_VALUE,
PAYMENT_DESCRIPTION,
)
URL_MY_BANK = '{}/banks/{}'.format(API_BASE_URL, MY_BANK)
URL_PRIVATE_ACCOUNTS = '{}/accounts/private'.format(URL_MY_BANK)
FMT_URL_TRANSACTIONS = '{}/accounts/{}/owner/transactions'
FMT_URL_REQUEST_TYPES = '{}/accounts/{}/owner/transaction-request-types'
FMT_URL_TRANSACTION_REQUESTS = \
FMT_URL_REQUEST_TYPES + '/{}/transaction-requests'
FMT_URL_TRANSACTION_CHALLENGE = \
FMT_URL_REQUEST_TYPES + '/sandbox/transaction-requests/{}/challenge'
TRANSACTIONS_LIMIT = '25'
def get_account(api):
"""Get my account (id) from the API"""
# Print some account info
print('Getting private accounts ...')
response = api.get(URL_PRIVATE_ACCOUNTS)
data = response.json()
for account in data['accounts']:
print(' Available account id: {}'.format(account['id']))
# Just picking first account
my_account = data['accounts'][0]['id']
print("Picking account: {}".format(my_account))
return my_account
def print_transactions(api, my_account):
"""Prints number of transactions on my account in the API"""
url_transactions = FMT_URL_TRANSACTIONS.format(URL_MY_BANK, my_account)
headers = {
'obp_limit': TRANSACTIONS_LIMIT,
}
print("Getting transactions for my account ...")
response = api.get(url_transactions, headers=headers)
transactions = response.json()['transactions']
print("Got {} transactions".format(len(transactions)))
def get_challenge_type(api, my_account):
"""Gets the challenge type to use for the payment from the API"""
url_request_types = FMT_URL_REQUEST_TYPES.format(URL_MY_BANK, my_account)
print("Getting challenge request types ...")
response = api.get(url_request_types)
challenge_type = response.json()['transaction_request_types'][0]['value']
print("Got type {}".format(challenge_type))
return challenge_type
def initiate_transaction(api, my_account):
"""
Initiates a transaction request on the API
Either payment will be processed directly or a challenge is returned.
"""
challenge_type = get_challenge_type(api, my_account)
url_transaction_requests = FMT_URL_TRANSACTION_REQUESTS.format(
URL_MY_BANK, my_account, challenge_type)
data = json.dumps({
'to': {
'account_id': COUNTERPARTY_ACCOUNT_ID,
'bank_id': COUNTERPARTY_BANK,
},
'value': {
'currency': PAYMENT_CURRENCY,
'amount': PAYMENT_VALUE,
},
'description': PAYMENT_DESCRIPTION,
'challenge_type': challenge_type,
})
headers = {'content-type': 'application/json'}
print("Initiating transaction request ...")
response = api.post(url_transaction_requests, data=data, headers=headers)
response = response.json()
return response
def answer_challenge(api, my_account, challenge_query, transaction_request_id):
"""Answers the API's transaction request challenge"""
print("Challenge query is {}".format(challenge_query))
url_transaction_challenge = FMT_URL_TRANSACTION_CHALLENGE.format(
URL_MY_BANK, my_account, transaction_request_id)
data = json.dumps({
'id': challenge_query,
'answer': '123456', # any number works in sandbox mode
})
headers = {'content-type': 'application/json'}
print("Answering challenge ...")
response = api.post(url_transaction_challenge, data=data, headers=headers)
response = response.json()
if 'error' in response:
sys.exit('Got an error: ' + str(response))
return response
def pay(api, my_account):
"""Handles the payment process"""
response = initiate_transaction(api, my_account)
if 'error' in response:
sys.exit("Got an error: " + str(response))
if response['challenge'] is not None:
print('This request is challenged, you have {} attempt(s)'.format(
response['challenge']['allowed_attempts']))
response = answer_challenge(
api,
my_account,
response['challenge']['id'],
response['id']['value'],
)
print('Transaction status: {}'.format(response['status']))
print('Created transaction id: {}'.format(response['transaction_ids']))
def hello_payment():
"""Say hello, Payment!"""
api = get_api()
my_account = get_account(api)
print_transactions(api, my_account)
pay(api, my_account)
if __name__ == '__main__':
hello_payment()