forked from phypoh/receipt-hackers-monzo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
243 lines (205 loc) · 8.88 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import json
import uuid
import requests
import config
import oauth2
import receipt_types
from utils import error
class ReceiptsClient:
''' An example single-account client of the Monzo Transaction Receipts API.
For the underlying OAuth2 implementation, see oauth2.OAuth2Client.
'''
def __init__(self):
self._api_client = oauth2.OAuth2Client()
self._api_client_ready = False
self._account_id = None
self.transactions = []
def do_auth(self):
''' Perform OAuth2 flow mostly on command-line and retrieve information of the
authorised user's current account information, rather than from joint account,
if present.
'''
print("Starting OAuth2 flow...")
self._api_client.start_auth()
print("OAuth2 flow completed, testing API call...")
response = self._api_client.test_api_call()
if "authenticated" in response:
print("API call test successful!")
else:
error("OAuth2 flow seems to have failed.")
self._api_client_ready = True
print("Retrieving account information...")
success, response = self._api_client.api_get("accounts", {})
if not success or "accounts" not in response or len(response["accounts"]) < 1:
error("Could not retrieve accounts information")
# We will be operating on personal account only.
for account in response["accounts"]:
if "type" in account and account["type"] == "uk_retail":
self._account_id = account["id"]
print("Retrieved account information.")
return
if self._account_id is None:
error("Could not find a personal account")
def list_transactions(self):
''' An example call to the end point documented in
https://docs.monzo.com/#list-transactions, other requests
can be implemented in a similar fashion.
'''
if self._api_client is None or not self._api_client_ready:
error("API client not initialised.")
# Our call is not paginated here with e.g. "limit": 10, which will be slow for
# accounts with a lot of transactions.
success, response = self._api_client.api_get("transactions", {
"account_id": self._account_id,
})
if not success or "transactions" not in response:
error("Could not list past transactions ({})".format(response))
self.transactions = response["transactions"]
print("All transactions loaded.")
def read_receipt(self, receipt_id):
''' Retrieve receipt for a transaction with an external ID of our choosing.
'''
success, response = self._api_client.api_get("transaction-receipts", {
"external_id": receipt_id,
})
if not success:
error("Failed to load receipt: {}".format(response))
print("Receipt read: {}".format(response))
def example_add_receipt_data(self):
''' An example in which we add receipt data to the latest transaction
of the account, with fabricated information. You can set varying
receipts data on the same transaction again and again to test it
if you need to.
'''
if len(self.transactions) == 0:
error("No transactions found, either it was not loaded with list_transactions() or there's no transaction in the Monzo account :/")
most_recent_transaction = self.transactions[-1]
print("Using most recent transaction to attach receipt: {}".format(most_recent_transaction))
# Using a random receipt ID we generate as external ID
receipt_id = uuid.uuid4().hex
# Price amounts are in the number of pences.
example_sub_item_1 = receipt_types.SubItem("Oranges loose", 1.5, "kg", 30, "GBP", 0)
example_sub_item_2 = receipt_types.SubItem("Organic oranges", 1, "kg", 20, "GBP", 0)
example_items = [receipt_types.Item("Selected oranges", 2.5, "kg", 50, "GBP", 0, [example_sub_item_1,
example_sub_item_2])]
if abs(most_recent_transaction["amount"]) > 269:
example_items.append(receipt_types.Item("Excess fare", 1, "", abs(most_recent_transaction["amount"])
- 269, "GBP", 20, []))
example_payments = [receipt_types.Payment("card", "123321", "1234", "A10B2C", "", "", "", "",
abs(most_recent_transaction["amount"]), "GBP")]
example_taxes = [receipt_types.Tax("VAT", 0, "GBP", "12345678")]
example_receipt = receipt_types.Receipt("", receipt_id, most_recent_transaction["id"],
abs(most_recent_transaction["amount"]), "GBP", example_payments, example_taxes, example_items)
example_receipt_marshaled = example_receipt.marshal()
print("Uploading receipt data to API: ", json.dumps(example_receipt_marshaled, indent=4, sort_keys=True))
print("")
success, response = self._api_client.api_put("transaction-receipts/", example_receipt_marshaled)
if not success:
error("Failed to upload receipt: {}".format(response))
print("Successfully uploaded receipt {}: {}".format(receipt_id, response))
return receipt_id
def example_register_webhook(self, incoming_endpoint):
'''
This is an example on registering a webhook with Monzo for Monzo's server to call your own
backend service endpoint when certain events happen on an account. This is useful if you
deploy an API client as a backend service with an incoming interface exposed to the internet.
Your backend code can then, for example, attach receipts to new transactions in an event-
driven manner. For more details, see https://docs.monzo.com/#webhooks
'''
print("Listing webhooks on account")
success, response = self._api_client.api_get("webhooks", {
"account_id": self._account_id,
})
if not success:
error("Failed to list webhooks: {}".format(response))
print("Existing webhooks: ", response)
print("Registering a webhook with callback URL {} ...".format(incoming_endpoint))
success, response = self._api_client.api_post("webhooks", {
"account_id": self._account_id,
"url": incoming_endpoint,
})
if not success or "webhook" not in response:
error("Failed to register webhook: {}".format(response))
print("Successfully registered webhooks ", response)
return response["webhook"]["id"]
if __name__ == "__main__":
client = ReceiptsClient()
client.do_auth()
# x1 = json.dumps({
# "transaction_id": "tx_00009ext4tuShVylmolJDd",
# "external_id": "test-receipt-1",
# "total": 1299,
# "currency": "GBP",
# "items": [
# {
# "description": "Bananas, 70p per kg",
# "quantity": 18.56,
# "unit": "kg",
# "amount": 70,
# "currency": "GBP"
# }
# ]
# }
# )
x1 = json.dumps({
"transaction_id": "tx_00009ezCURJniYB2dnnopt",
"external_id": "test-receipt-3",
"total": 1,
"currency": "GBP",
"items": [
{
"description": "Strawberries, 70p per kg",
"quantity": 18.56,
"unit": "kg",
"amount": 70,
"currency": "GBP"
}
],
"taxes": [
{
"description": "VAT",
"amount": 0,
"currency": "GBP",
"tax_number": "945719291",
}
],
"payments": [
{
"type": "card",
"bin": "543210",
"last_four": "0987",
"auth_code": "123456",
"aid": "",
"mid": "",
"tid": "",
"amount": 1000,
"currency": "GBP"
},
{
"type": "cash",
"amount": 1000,
"currency": "GBP"
},
{
"type": "gift_card",
"gift_card_type": "One4all",
"amount": 1000,
"currency": "GBP"
}
]
}
)
client.list_transactions()
import pdb; pdb.set_trace()
# client._api_client.api_put("transaction-receipts/", x1)
# receipt_id = client.example_add_receipt_data()
# client.read_receipt(receipt_id)
# client.example_register_webhook("https://example.com/webhook_callback")
# if __name__ == "__main__":
# client = ReceiptsClient()
# client.do_auth()
# client.list_transactions()
# receipt_id = client.example_add_receipt_data()
# client.read_receipt(receipt_id)
# client.example_register_webhook("https://example.com/webhook_callback")
# The webhook endpoint used should be an HTTP-style server served by your own app server.