This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.go
265 lines (227 loc) · 8.63 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
package sbanken
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/engvik/sbanken-go/internal/transport"
"github.com/engvik/sbanken-go/internal/unmarshal"
)
var (
ErrTransactionUnmarshal = errors.New("error unmarshaling json")
)
// Transaction represents a transaction.
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html#/Transactions
type Transaction struct {
CardDetails CardDetails `json:"cardDetails"`
TransactionDetails TransactionDetails `json:"transactionDetails"`
AccountingDate string `json:"accountingDate"`
InterestDate string `json:"interestDate"`
OtherAccountNumber string `json:"otherAccountNumber"`
Text string `json:"text"`
TransactionType string `json:"transactionType"`
TransactionTypeText string `json:"transactionTypeText"`
ReservationType string `json:"reservationType"`
TransactionID string `json:"transactionId"`
Source string `json:"source"`
Amount float32 `json:"amount"`
TransactionTypeCode int `json:"transactionTypeCode"`
IsReservation bool `json:"isReservation"`
CardDetailsSpecified bool `json:"cardDetailsSpecified"`
OtherAccountNumberSpecified bool `json:"otherAccountNumberSpecified"`
TransactionDetailSpecified bool `json:"transactionDetailSpecified"`
}
// UnmarshalJSON is implemented due to a bug in sbankens API. The Source field
// should contain a string, but contain an int. The custom unmarshaler supports
// both string and ints for this fields.
func (t *Transaction) UnmarshalJSON(data []byte) error {
// Temporary internal structs that supports both ints and strings for
// the Source field.
tmpTransaction := struct {
CardDetails CardDetails `json:"cardDetails"`
TransactionDetails TransactionDetails `json:"transactionDetails"`
AccountingDate string `json:"accountingDate"`
InterestDate string `json:"interestDate"`
OtherAccountNumber string `json:"otherAccountNumber"`
Text string `json:"text"`
TransactionType string `json:"transactionType"`
TransactionTypeText string `json:"transactionTypeText"`
ReservationType string `json:"reservationType"`
TransactionID string `json:"transactionId"`
Amount float32 `json:"amount"`
TransactionTypeCode int `json:"transactionTypeCode"`
IsReservation bool `json:"isReservation"`
CardDetailsSpecified bool `json:"cardDetailsSpecified"`
OtherAccountNumberSpecified bool `json:"otherAccountNumberSpecified"`
TransactionDetailSpecified bool `json:"transactionDetailSpecified"`
Source interface{} `json:"source"`
}{}
if err := json.Unmarshal(data, &tmpTransaction); err != nil {
return err
}
// Get and set proper source value.
sourceString, sourceInt, err := unmarshal.GetIntStringField(tmpTransaction.Source)
if err != nil {
return err
}
if sourceString != "" {
t.Source = sourceString
} else {
if sourceInt == 0 {
t.Source = "AccountStatement"
} else if sourceInt == 1 {
t.Source = "Archive"
} else {
return ErrTransactionUnmarshal
}
}
// Set fields of correct type
t.CardDetails = tmpTransaction.CardDetails
t.TransactionDetails = tmpTransaction.TransactionDetails
t.AccountingDate = tmpTransaction.AccountingDate
t.InterestDate = tmpTransaction.InterestDate
t.OtherAccountNumber = tmpTransaction.OtherAccountNumber
t.Text = tmpTransaction.Text
t.TransactionType = tmpTransaction.TransactionType
t.TransactionTypeText = tmpTransaction.TransactionTypeText
t.ReservationType = tmpTransaction.ReservationType
t.TransactionID = tmpTransaction.TransactionID
t.Amount = tmpTransaction.Amount
t.TransactionTypeCode = tmpTransaction.TransactionTypeCode
t.IsReservation = tmpTransaction.IsReservation
t.CardDetailsSpecified = tmpTransaction.CardDetailsSpecified
t.OtherAccountNumberSpecified = tmpTransaction.OtherAccountNumberSpecified
t.TransactionDetailSpecified = tmpTransaction.TransactionDetailSpecified
return nil
}
// CardDetails contains card details about the card used.
type CardDetails struct {
CardNumber string `json:"cardNumber"`
MerchantCategoryCode string `json:"merchantCategoryCode"`
MerchantCategoryDescription string `json:"merchantCategoryDescription"`
MerchantCity string `json:"merchantCity"`
MerchantName string `json:"merchantName"`
OriginalCurrencyCode string `json:"originalCurrencyCode"`
PurchaseDate string `json:"purchaseDate"`
TransactionID string `json:"transactionId"`
CurrencyAmount float32 `json:"currencyAmount"`
CurrencyRate float32 `json:"currencyRate"`
}
// TransactionDetails contains transaction details.
type TransactionDetails struct {
ID string `json:"transactionId"`
FormattedAccountNumber string `json:"formattedAccountNumber"`
CID string `json:"cid"`
AmountDescription string `json:"amountDescription"`
ReceiverName string `json:"receiverName"`
PayerName string `json:"payerName"`
RegistrationDate string `json:"registrationDate"`
NumericReference int `json:"numericReference"`
}
// TransactionListQuery represents query parameters for querying transactions.
type TransactionListQuery struct {
StartDate time.Time
EndDate time.Time
Index string
Length string
}
// QueryString translates the query into a query string.
func (q *TransactionListQuery) QueryString(u string) (string, error) {
parsedURL, err := url.Parse(u)
if err != nil {
return u, err
}
query := parsedURL.Query()
if !q.StartDate.IsZero() {
query.Add("startDate", q.StartDate.Format(time.RFC3339))
}
if !q.EndDate.IsZero() {
query.Add("endDate", q.EndDate.Format(time.RFC3339))
}
if q.Index != "" {
query.Add("index", q.Index)
}
if q.Length != "" {
query.Add("length", q.Length)
}
return query.Encode(), nil
}
// ListTransactions returns the latest transactions of the given account.
func (c *Client) ListTransactions(ctx context.Context, accountID string, q *TransactionListQuery) ([]Transaction, error) {
if accountID == "" {
return nil, ErrMissingAccountID
}
url := fmt.Sprintf("%s/v2/Transactions/%s", c.bankBaseURL, accountID)
if q != nil {
qs, err := q.QueryString(url)
if err != nil {
return nil, fmt.Errorf("QueryString: %w", err)
}
url = fmt.Sprintf("%s?%s", url, qs)
}
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
data := struct {
Transactions []Transaction `json:"items"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Transactions, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Transactions, &Error{
"ListTransactions",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Transactions, nil
}
// ListArchivedTransactions returns archived transactions.
func (c *Client) ListArchivedTransactions(ctx context.Context, accountID string, q *TransactionListQuery) ([]Transaction, error) {
if accountID == "" {
return nil, ErrMissingAccountID
}
url := fmt.Sprintf("%s/v2/Transactions/archive/%s", c.bankBaseURL, accountID)
if q != nil {
qs, err := q.QueryString(url)
if err != nil {
return nil, fmt.Errorf("QueryString: %w", err)
}
url = fmt.Sprintf("%s?%s", url, qs)
}
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
data := struct {
Transactions []Transaction `json:"items"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Transactions, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Transactions, &Error{
"ListTransactions",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Transactions, nil
}