forked from guitmz/n26
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
348 lines (316 loc) · 12.2 KB
/
api.go
File metadata and controls
348 lines (316 loc) · 12.2 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
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
package n26
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"
"net/url"
)
const apiURL = "https://api.tech26.de"
type Auth struct {
UserName string
Password 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"`
}
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 int64 `json:"expirationDate"`
CardType string `json:"cardType"`
Status string `json:"status"`
CardProduct interface{} `json:"cardProduct"`
CardProductType string `json:"cardProductType"`
PinDefined interface{} `json:"pinDefined"`
CardActivated interface{} `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 Client http.Client
func NewClient(a Auth) (*Client, error) {
c := oauth2.Config{
ClientID: "android",
ClientSecret: "secret",
Endpoint: oauth2.Endpoint{TokenURL: apiURL + "/oauth/token"},
}
ctx := context.Background()
tok, err := c.PasswordCredentialsToken(ctx, a.UserName, a.Password)
if err != nil {
return nil, err
}
return (*Client)(c.Client(ctx, tok)), nil
}
func (c *Client) n26Request(endpoint string, params map[string]string) []byte {
u, _ := url.ParseRequestURI(apiURL)
u.Path = endpoint
u.RawQuery = mapToQuery(params).Encode()
res, _ := (*http.Client)(c).Get(u.String())
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
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("/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("/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("/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("/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("/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("/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("/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() (*Transactions, error) {
return auth.GetTransactions(TimeStamp{}, TimeStamp{})
}
// 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) (*Transactions, error) {
params := map[string]string{}
//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("/api/smrt/transactions", params)
transactions := &Transactions{}
if err := json.Unmarshal(body, &transactions); err != nil {
return nil, err
}
return transactions, nil
}
func (auth *Client) GetStatements(retType string) (string, *Statements) {
body := auth.n26Request("/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(fmt.Sprintf("%s%s", "/api/statements/", ID), nil)
ioutil.WriteFile(
fmt.Sprintf("%s.pdf", ID),
body,
0750,
)
}
func check(e error) {
if e != nil {
panic(e)
}
}