This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtransactions.go
339 lines (279 loc) · 9.98 KB
/
transactions.go
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package starling
import (
"context"
"net/http"
)
// Transaction represents the details of a transaction.
type Transaction struct {
UID string `json:"id"`
Currency string `json:"currency"`
Amount float64 `json:"amount"`
Direction string `json:"direction"`
Created string `json:"created"`
Narrative string `json:"narrative"`
Source string `json:"source"`
Balance float64 `json:"balance,omitempty"`
}
// Transactions is a list of transaction summaries.
type transactions struct {
Transactions []Transaction `json:"transactions"`
}
// HALTransactions is a HAL wrapper around the Transactions type.
type halTransactions struct {
Embedded *transactions `json:"_embedded"`
}
// DDTransaction represents the details of a direct debit transaction.
type DDTransaction struct {
UID string `json:"id"`
Currency string `json:"currency"`
Amount float64 `json:"amount"`
Direction string `json:"direction"`
Created string `json:"created"`
Narrative string `json:"narrative"`
Source string `json:"source"`
MandateUID string `json:"mandateId"`
Type string `json:"type"`
MerchantUID string `json:"merchantId"`
MerchantLocationUID string `json:"merchantLocationId"`
SpendingCategory string `json:"spendingCategory"`
}
// ddTransactions is a list of transaction summaries.
type ddTransactions struct {
Transactions []DDTransaction `json:"transactions"`
}
// halDDTransactions is a HAL wrapper around the Transactions type.
type halDDTransactions struct {
Embedded *ddTransactions `json:"_embedded"`
}
// MastercardTransaction represents the details of a card transaction
type MastercardTransaction struct {
Transaction
Method string `json:"mastercardTransactionMethod"`
Status string `json:"status"`
SourceAmount float64 `json:"sourceAmount"`
SourceCurrency string `json:"sourceCurrency"`
MerchantUID string `json:"merchantId"`
SpendingCategory string `json:"spendingCategory"`
Country string `json:"country"`
POSTimestamp string `json:"posTimestamp"`
AuthorisationCode string `json:"authorisationCode"`
EventUID string `json:"eventUid"`
Receipt Receipt `json:"receipt"`
CardLast4 string `json:"cardLast4"`
}
// MastercardTransactions is a list of Mastercard transactions
type mastercardTransactions struct {
Transactions []MastercardTransaction `json:"transactions"`
}
// HALMastercardTransactions is a HAL wrapper around the Transactions type.
type halMastercardTransactions struct {
Embedded *mastercardTransactions `json:"_embedded"`
}
// Transactions returns a list of transaction summaries for the current user. It accepts optional
// time.Time values to request transactions within a given date range. If these values are not provided
// the API returns the last 100 transactions.
func (c *Client) Transactions(ctx context.Context, dr *DateRange) ([]Transaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions", nil)
if err != nil {
return nil, nil, err
}
if dr != nil {
q := req.URL.Query()
q.Add("from", dr.From.Format("2006-01-02"))
q.Add("to", dr.To.Format("2006-01-02"))
req.URL.RawQuery = q.Encode()
}
hTxns := new(halTransactions)
resp, err := c.Do(ctx, req, &hTxns)
if hTxns == nil {
return nil, resp, err
}
if hTxns.Embedded == nil {
return nil, resp, err
}
return hTxns.Embedded.Transactions, resp, err
}
// Transaction returns an individual transaction for the current customer.
func (c *Client) Transaction(ctx context.Context, uid string) (*Transaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/"+uid, nil)
if err != nil {
return nil, nil, err
}
txn := new(Transaction)
resp, err := c.Do(ctx, req, txn)
if err != nil {
return nil, resp, err
}
return txn, resp, nil
}
// DDTransactions returns a list of direct debit transactions for the current user. It accepts optional
// time.Time values to request transactions within a given date range. If these values are not provided
// the API returns the last 100 transactions.
func (c *Client) DDTransactions(ctx context.Context, dr *DateRange) ([]DDTransaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/direct-debit", nil)
if err != nil {
return nil, nil, err
}
if dr != nil {
q := req.URL.Query()
q.Add("from", dr.From.Format("2006-01-02"))
q.Add("to", dr.To.Format("2006-01-02"))
req.URL.RawQuery = q.Encode()
}
var halResp *halDDTransactions
var txns *ddTransactions
resp, err := c.Do(ctx, req, &halResp)
if err != nil {
return nil, resp, err
}
if halResp.Embedded != nil {
txns = halResp.Embedded
}
return txns.Transactions, resp, nil
}
// DDTransaction returns an individual transaction for the current customer.
func (c *Client) DDTransaction(ctx context.Context, uid string) (*DDTransaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/direct-debit/"+uid, nil)
if err != nil {
return nil, nil, err
}
ddTxn := new(DDTransaction)
resp, err := c.Do(ctx, req, ddTxn)
if err != nil {
return nil, resp, err
}
return ddTxn, resp, nil
}
// SetDDSpendingCategory updates the spending category for a given direct debit.
func (c *Client) SetDDSpendingCategory(ctx context.Context, uid, cat string) (*http.Response, error) {
reqCat := SpendingCategory{SpendingCategory: cat}
req, err := c.NewRequest("PUT", "/api/v1/transactions/direct-debit/"+uid, reqCat)
if err != nil {
return nil, err
}
resp, err := c.Do(ctx, req, nil)
return resp, err
}
// FPSTransactionsIn returns a list of inbound Faster Payments transaction summaries for the current user. It accepts
// optional time.Time values to request transactions within a given date range. If these values are not provided
// the API returns the last 100 transactions.
func (c *Client) FPSTransactionsIn(ctx context.Context, dr *DateRange) ([]Transaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/fps/in", nil)
if err != nil {
return nil, nil, err
}
if dr != nil {
q := req.URL.Query()
q.Add("from", dr.From.Format("2006-01-02"))
q.Add("to", dr.To.Format("2006-01-02"))
req.URL.RawQuery = q.Encode()
}
var halResp *halTransactions
var txns *transactions
resp, err := c.Do(ctx, req, &halResp)
if err != nil {
return nil, resp, err
}
if halResp.Embedded != nil {
txns = halResp.Embedded
}
return txns.Transactions, resp, nil
}
// FPSTransactionIn returns an individual transaction for the current customer.
func (c *Client) FPSTransactionIn(ctx context.Context, uid string) (*Transaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/fps/in/"+uid, nil)
if err != nil {
return nil, nil, err
}
txn := new(Transaction)
resp, err := c.Do(ctx, req, txn)
if err != nil {
return nil, resp, err
}
return txn, resp, err
}
// FPSTransactionsOut returns a list of inbound Faster Payments transaction summaries for the current user. It accepts
// optional time.Time values to request transactions within a given date range. If these values are not provided
// the API returns the last 100 transactions.
func (c *Client) FPSTransactionsOut(ctx context.Context, dr *DateRange) ([]Transaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/fps/out", nil)
if err != nil {
return nil, nil, err
}
if dr != nil {
q := req.URL.Query()
q.Add("from", dr.From.Format("2006-01-02"))
q.Add("to", dr.To.Format("2006-01-02"))
req.URL.RawQuery = q.Encode()
}
var halResp *halTransactions
var txns *transactions
resp, err := c.Do(ctx, req, &halResp)
if err != nil {
return nil, resp, err
}
if halResp.Embedded != nil {
txns = halResp.Embedded
}
return txns.Transactions, resp, nil
}
// FPSTransactionOut returns an individual transaction for the current customer.
func (c *Client) FPSTransactionOut(ctx context.Context, uid string) (*Transaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/fps/out/"+uid, nil)
if err != nil {
return nil, nil, err
}
txn := new(Transaction)
resp, err := c.Do(ctx, req, txn)
if err != nil {
return nil, resp, err
}
return txn, resp, err
}
// MastercardTransactions returns a list of transaction summaries for the current user. It accepts optional
// time.Time values to request transactions within a given date range. If these values are not provided
// the API returns the last 100 transactions.
func (c *Client) MastercardTransactions(ctx context.Context, dr *DateRange) ([]MastercardTransaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/mastercard", nil)
if err != nil {
return nil, nil, err
}
if dr != nil {
q := req.URL.Query()
q.Add("from", dr.From.Format("2006-01-02"))
q.Add("to", dr.To.Format("2006-01-02"))
req.URL.RawQuery = q.Encode()
}
hTxns := new(halMastercardTransactions)
resp, err := c.Do(ctx, req, &hTxns)
if hTxns == nil {
return nil, resp, err
}
if hTxns.Embedded == nil {
return nil, resp, err
}
return hTxns.Embedded.Transactions, resp, err
}
// MastercardTransaction returns an individual mastercard transaction for the current customer.
func (c *Client) MastercardTransaction(ctx context.Context, uid string) (*MastercardTransaction, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/transactions/mastercard/"+uid, nil)
if err != nil {
return nil, nil, err
}
txn := new(MastercardTransaction)
resp, err := c.Do(ctx, req, txn)
if err != nil {
return nil, resp, err
}
return txn, resp, nil
}
// SetMastercardSpendingCategory updates the spending category for a given mastercard transaction.
func (c *Client) SetMastercardSpendingCategory(ctx context.Context, uid, cat string) (*http.Response, error) {
reqCat := SpendingCategory{SpendingCategory: cat}
req, err := c.NewRequest("PUT", "/api/v1/transactions/mastercard/"+uid, reqCat)
if err != nil {
return nil, err
}
resp, err := c.Do(ctx, req, nil)
return resp, err
}