-
Notifications
You must be signed in to change notification settings - Fork 21
/
api.go
439 lines (396 loc) · 15.1 KB
/
api.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package n26
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"golang.org/x/oauth2"
)
const apiURL = "https://api.tech26.de"
type Auth struct {
UserName string
Password string
DeviceToken string
}
type Balance struct {
AvailableBalance float64 `json:"availableBalance"`
UsableBalance float64 `json:"usableBalance"`
IBAN string `json:"iban"`
BIC string `json:"bic"`
BankName string `json:"bankName"`
Seized bool `json:"seized"`
ID string `json:"id"`
}
type PersonalInfo struct {
ID string `json:"id"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
KycFirstName string `json:"kycFirstName"`
KycLastName string `json:"kycLastName"`
Title string `json:"title"`
Gender string `json:"gender"`
BirthDate int64 `json:"birthDate"`
SignupCompleted bool `json:"signupCompleted"`
Nationality string `json:"nationality"`
MobilePhoneNumber string `json:"mobilePhoneNumber"`
ShadowUserID string `json:"shadowUserId"`
TransferWiseTermsAccepted bool `json:"transferWiseTermsAccepted"`
IDNowToken string `json:"idNowToken"`
}
type Token struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
MfaToken string `json:"mfaToken"`
}
type Statuses struct {
ID string `json:"id"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
SingleStepSignup int64 `json:"singleStepSignup"`
EmailValidationInitiated int64 `json:"emailValidationInitiated"`
EmailValidationCompleted int64 `json:"emailValidationCompleted"`
ProductSelectionCompleted int64 `json:"productSelectionCompleted"`
PhonePairingInitiated int64 `json:"phonePairingInitiated"`
PhonePairingCompleted int64 `json:"phonePairingCompleted"`
KycInitiated int64 `json:"kycInitiated"`
KycCompleted int64 `json:"kycCompleted"`
KycWebIDInitiated int64 `json:"kycWebIDInitiated"`
KycWebIDCompleted int64 `json:"kycWebIDCompleted"`
CardActivationCompleted int64 `json:"cardActivationCompleted"`
PinDefinitionCompleted int64 `json:"pinDefinitionCompleted"`
BankAccountCreationInitiated int64 `json:"bankAccountCreationInitiated"`
BankAccountCreationSucceded int64 `json:"bankAccountCreationSucceded"`
FlexAccount bool `json:"flexAccount"`
}
type Addresses struct {
Paging struct {
TotalResults int `json:"totalResults"`
} `json:"paging"`
Data []struct {
AddressLine1 string `json:"addressLine1"`
StreetName string `json:"streetName"`
HouseNumberBlock string `json:"houseNumberBlock"`
ZipCode string `json:"zipCode"`
CityName string `json:"cityName"`
CountryName string `json:"countryName"`
Type string `json:"type"`
ID string `json:"id"`
} `json:"data"`
}
type Barzahlen struct {
DepositAllowance string `json:"depositAllowance"`
WithdrawAllowance string `json:"withdrawAllowance"`
RemainingAmountMonth string `json:"remainingAmountMonth"`
FeeRate string `json:"feeRate"`
Cash26WithdrawalsCount string `json:"cash26WithdrawalsCount"`
Cash26WithdrawalsSum string `json:"cash26WithdrawalsSum"`
AtmWithdrawalsCount string `json:"atmWithdrawalsCount"`
AtmWithdrawalsSum string `json:"atmWithdrawalsSum"`
MonthlyDepositFeeThreshold string `json:"monthlyDepositFeeThreshold"`
Success bool `json:"success"`
}
type Cards []struct {
ID string `json:"id"`
PublicToken interface{} `json:"publicToken"`
Pan interface{} `json:"pan"`
MaskedPan string `json:"maskedPan"`
ExpirationDate TimeStamp `json:"expirationDate"`
CardType string `json:"cardType"`
Status string `json:"status"`
CardProduct interface{} `json:"cardProduct"`
CardProductType string `json:"cardProductType"`
PinDefined TimeStamp `json:"pinDefined"`
CardActivated TimeStamp `json:"cardActivated"`
UsernameOnCard string `json:"usernameOnCard"`
ExceetExpressCardDelivery interface{} `json:"exceetExpressCardDelivery"`
Membership interface{} `json:"membership"`
ExceetActualDeliveryDate interface{} `json:"exceetActualDeliveryDate"`
ExceetExpressCardDeliveryEmailSent interface{} `json:"exceetExpressCardDeliveryEmailSent"`
ExceetCardStatus interface{} `json:"exceetCardStatus"`
ExceetExpectedDeliveryDate interface{} `json:"exceetExpectedDeliveryDate"`
ExceetExpressCardDeliveryTrackingID interface{} `json:"exceetExpressCardDeliveryTrackingId"`
CardSettingsID interface{} `json:"cardSettingsId"`
MptsCard bool `json:"mptsCard"`
}
type Limits []struct {
Limit string `json:"limit"`
Amount float64 `json:"amount"`
}
type Contacts []struct {
UserID string `json:"userId"`
ID string `json:"id"`
Name string `json:"name"`
Subtitle string `json:"subtitle"`
Account struct {
AccountType string `json:"accountType"`
Iban string `json:"iban"`
Bic string `json:"bic"`
} `json:"account"`
}
type Transactions []struct {
ID string `json:"id"`
UserID string `json:"userId"`
Type string `json:"type"`
Amount float64 `json:"amount"`
CurrencyCode string `json:"currencyCode"`
OriginalAmount float64 `json:"originalAmount,omitempty"`
OriginalCurrency string `json:"originalCurrency,omitempty"`
ExchangeRate float64 `json:"exchangeRate,omitempty"`
MerchantCity string `json:"merchantCity,omitempty"`
VisibleTS TimeStamp `json:"visibleTS"`
Mcc int `json:"mcc,omitempty"`
MccGroup int `json:"mccGroup,omitempty"`
MerchantName string `json:"merchantName,omitempty"`
Recurring bool `json:"recurring"`
AccountID string `json:"accountId"`
Category string `json:"category"`
CardID string `json:"cardId,omitempty"`
UserCertified TimeStamp `json:"userCertified"`
Pending bool `json:"pending"`
TransactionNature string `json:"transactionNature"`
CreatedTS TimeStamp `json:"createdTS"`
MerchantCountry int `json:"merchantCountry,omitempty"`
SmartLinkID string `json:"smartLinkId"`
LinkID string `json:"linkId"`
Confirmed TimeStamp `json:"confirmed"`
PartnerBic string `json:"partnerBic,omitempty"`
PartnerBcn string `json:"partnerBcn,omitempty"`
PartnerAccountIsSepa bool `json:"partnerAccountIsSepa,omitempty"`
PartnerName string `json:"partnerName,omitempty"`
PartnerIban string `json:"partnerIban,omitempty"`
PartnerAccountBan string `json:"partnerAccountBan,omitempty"`
ReferenceText string `json:"referenceText,omitempty"`
UserAccepted int64 `json:"userAccepted,omitempty"`
SmartContactID string `json:"smartContactId,omitempty"`
}
type Statements []struct {
ID string `json:"id"`
URL string `json:"url"`
VisibleTS int64 `json:"visibleTS"`
Month int `json:"month"`
Year int `json:"year"`
}
type Spaces struct {
Spaces []struct {
Balance struct {
AvailableBalance float64 `json:"availableBalance"`
OverdraftAmount interface{} `json:"overdraftAmount"`
} `json:"balance"`
Color string `json:"color"`
Goal interface{} `json:"goal"`
ID string `json:"id"`
ImageURL string `json:"imageUrl"`
IsCardAttached bool `json:"isCardAttached"`
IsPrimary bool `json:"isPrimary"`
Name string `json:"name"`
} `json:"spaces"`
TotalBalance float64 `json:"totalBalance"`
UserFeatures struct {
AvailableSpaces int `json:"availableSpaces"`
CanUpgrade bool `json:"canUpgrade"`
} `json:"userFeatures"`
}
type Client http.Client
type TokenSource struct {
AccessToken string
}
func (t *TokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
}
func NewClient(a Auth) (*Client, error) {
token := &Token{}
err := token.GetMFAToken(a.UserName, a.Password, a.DeviceToken)
check(err)
err = token.requestMfaApproval(a.DeviceToken)
check(err)
tokenSource := &TokenSource{
AccessToken: token.AccessToken,
}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
return (*Client)(oauthClient), nil
}
func (c *Client) n26RawRequest(requestMethod, endpoint string, params map[string]string, callback func(io.Reader) error) error {
var req *http.Request
var err error
u, _ := url.ParseRequestURI(apiURL)
u.Path = endpoint
u.RawQuery = mapToQuery(params).Encode()
switch requestMethod {
case http.MethodGet:
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
check(err)
case http.MethodPost:
req, err = http.NewRequest(http.MethodPost, u.String(), nil)
check(err)
}
res, err := (*http.Client)(c).Do(req)
check(err)
defer res.Body.Close()
return callback(res.Body)
}
func (c *Client) n26Request(requestMethod, endpoint string, params map[string]string) []byte {
var body []byte
err := c.n26RawRequest(requestMethod, endpoint, params, func(r io.Reader) error {
var err error
body, err = ioutil.ReadAll(r)
return err
})
check(err)
return body
}
func mapToQuery(params map[string]string) url.Values {
values := url.Values{}
for k, v := range params {
values.Set(k, v)
}
return values
}
func (auth *Client) GetBalance(retType string) (string, *Balance) {
body := auth.n26Request(http.MethodGet, "/api/accounts", nil)
balance := &Balance{}
check(json.Unmarshal(body, &balance))
identedJSON, _ := json.MarshalIndent(&balance, "", " ")
if retType == "json" {
return string(identedJSON), balance
}
return "", balance
}
func (auth *Client) GetInfo(retType string) (string, *PersonalInfo) {
body := auth.n26Request(http.MethodGet, "/api/me", nil)
info := &PersonalInfo{}
check(json.Unmarshal(body, &info))
identedJSON, _ := json.MarshalIndent(&info, "", " ")
if retType == "json" {
return string(identedJSON), info
}
return "", info
}
func (auth *Client) GetStatus(retType string) (string, *Statuses) {
body := auth.n26Request(http.MethodGet, "/api/me/statuses", nil)
status := &Statuses{}
check(json.Unmarshal(body, &status))
identedJSON, _ := json.MarshalIndent(&status, "", " ")
if retType == "json" {
return string(identedJSON), status
}
return "", status
}
func (auth *Client) GetAddresses(retType string) (string, *Addresses) {
body := auth.n26Request(http.MethodGet, "/api/addresses", nil)
addresses := &Addresses{}
check(json.Unmarshal(body, &addresses))
identedJSON, _ := json.MarshalIndent(&addresses, "", " ")
if retType == "json" {
return string(identedJSON), addresses
}
return "", addresses
}
func (auth *Client) GetCards(retType string) (string, *Cards) {
body := auth.n26Request(http.MethodGet, "/api/v2/cards", nil)
cards := &Cards{}
check(json.Unmarshal(body, &cards))
identedJSON, _ := json.MarshalIndent(&cards, "", " ")
if retType == "json" {
return string(identedJSON), cards
}
return "", cards
}
func (auth *Client) GetLimits(retType string) (string, *Limits) {
body := auth.n26Request(http.MethodGet, "/api/settings/account/limits", nil)
limits := &Limits{}
check(json.Unmarshal(body, &limits))
identedJSON, _ := json.MarshalIndent(&limits, "", " ")
if retType == "json" {
return string(identedJSON), limits
}
return "", limits
}
func (auth *Client) GetContacts(retType string) (string, *Contacts) {
body := auth.n26Request(http.MethodGet, "/api/smrt/contacts", nil)
contacts := &Contacts{}
check(json.Unmarshal(body, &contacts))
identedJSON, _ := json.MarshalIndent(&contacts, "", " ")
if retType == "json" {
return string(identedJSON), contacts
}
return "", contacts
}
func (auth *Client) GetLastTransactions(limit string) (*Transactions, error) {
return auth.GetTransactions(TimeStamp{}, TimeStamp{}, limit)
}
// Get transactions for the given time window.
// Use the zero values for the time stamps if no restrictions are
// desired (use the defaults on the server)
func (auth *Client) GetTransactions(from, to TimeStamp, limit string) (*Transactions, error) {
params := map[string]string{
"limit": limit,
}
//Filter is applied only if both values are set
if !from.IsZero() && !to.IsZero() {
params["from"] = fmt.Sprint(from.AsMillis())
params["to"] = fmt.Sprint(to.AsMillis())
}
body := auth.n26Request(http.MethodGet, "/api/smrt/transactions", params)
transactions := &Transactions{}
if err := json.Unmarshal(body, &transactions); err != nil {
return nil, err
}
return transactions, nil
}
// Get transactions for the given time window as N26 CSV file. Stored as 'smrt_statement.csv'
func (auth *Client) GetSmartStatementCsv(from, to TimeStamp, reader func(io.Reader) error) error {
//Filter is applied only if both values are set
if from.IsZero() || to.IsZero() {
return errors.New("Start and end time must be set")
}
return auth.n26RawRequest(http.MethodGet, fmt.Sprintf("/api/smrt/reports/%v/%v/statements", from.AsMillis(), to.AsMillis()), nil, reader)
}
func (auth *Client) GetStatements(retType string) (string, *Statements) {
body := auth.n26Request(http.MethodGet, "/api/statements", nil)
statements := &Statements{}
check(json.Unmarshal(body, &statements))
identedJSON, _ := json.MarshalIndent(&statements, "", " ")
if retType == "json" {
return string(identedJSON), statements
}
return "", statements
}
func (auth *Client) GetStatementPDF(ID string) {
body := auth.n26Request(http.MethodGet, fmt.Sprintf("/api/statements/%s", ID), nil)
ioutil.WriteFile(
fmt.Sprintf("%s.pdf", ID),
body,
0750,
)
}
func (auth *Client) BlockCard(ID string) {
_ = auth.n26Request(http.MethodPost, fmt.Sprintf("/api/cards/%s/block", ID), nil)
fmt.Printf("\nYour card with ID: %s is DISABLED\n\n", ID)
}
func (auth *Client) UnblockCard(ID string) {
_ = auth.n26Request(http.MethodPost, fmt.Sprintf("/api/cards/%s/unblock", ID), nil)
fmt.Printf("\nYour card with ID: %s is ACTIVE\n\n", ID)
}
func (auth *Client) GetSpaces(retType string) (string, *Spaces) {
body := auth.n26Request(http.MethodGet, "/api/spaces", nil)
spaces := &Spaces{}
check(json.Unmarshal(body, &spaces))
identedJSON, _ := json.MarshalIndent(&spaces, "", " ")
if retType == "json" {
return string(identedJSON), spaces
}
return "", spaces
}
func check(e error) {
if e != nil {
log.Fatal(e.Error())
}
}