-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
135 lines (109 loc) · 3.45 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
package ziggy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"time"
)
// TransactionStatus describes the current state of a transaction.
type TransactionStatus string
const (
TransactionStatusPosted = "POSTED"
)
// TransactionType describes the kind of the transaction.
type TransactionType string
const (
TransactionTypeCredit = "CREDIT"
TransactionTypeDebit = "DEBIT"
)
// Transaction describes some activity which has occurred on an account.
type Transaction struct {
AccountID string `json:"accountId"`
Type TransactionType `json:"type"`
Status TransactionStatus `json:"status"`
Description string `json:"description"`
CardNumber string `json:"cardNumber"`
PostingDate time.Time `json:"postingDate"`
ValueDate time.Time `json:"valueDate"`
ActionDate time.Time `json:"actionDate"`
TransactionDate time.Time `json:"transactionDate"`
Amount float64 `json:"amount"`
}
// transactionWithoutDate is a type alias for Transaction allowing a custom
// Unmarshaler implementation for only the date values.
type transactionWithoutDate Transaction
// UnmarshalJSON satisfies the json.Unmarshaler interface.
func (t *Transaction) UnmarshalJSON(data []byte) error {
raw := struct {
transactionWithoutDate
PostingDate string `json:"postingDate"`
ValueDate string `json:"valueDate"`
ActionDate string `json:"actionDate"`
TransactionDate string `json:"transactionDate"`
}{}
err := json.Unmarshal(data, &raw)
if err != nil {
return err
}
*t = Transaction(raw.transactionWithoutDate)
t.PostingDate, err = time.Parse("2006-01-02", raw.PostingDate)
if err != nil {
return err
}
t.ValueDate, err = time.Parse("2006-01-02", raw.ValueDate)
if err != nil {
return err
}
t.ActionDate, err = time.Parse("2006-01-02", raw.ActionDate)
if err != nil {
return err
}
t.TransactionDate, err = time.Parse("2006-01-02", raw.TransactionDate)
if err != nil {
return err
}
return nil
}
// TransactionRequest describes the request parameters available for retrieving
// account transactions. All parameters are requires unless specified.
type TransactionsRequest struct {
AccountID string `json:"-"`
// Optional.
StartDate time.Time `json:"fromDate,omitempty"`
// Optional.
EndDate time.Time `json:"toDate,omitempty"`
}
// TransactionsResponse describes the response data returned for retrieving
// account transactions.
type TransactionsResponse struct {
Data struct {
Transactions []Transaction `json:"transactions"`
} `json:"data"`
Links struct {
Self string `json:"self"`
} `json:"links"`
Metadata struct {
TotalPages int64 `json:"totalPages"`
} `json:"meta"`
}
// GetAccountTransactions obtains a specified account's transactions.
func (c *Client) GetAccountTransactions(ctx context.Context,
req *TransactionsRequest) (*TransactionsResponse, error) {
payload, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
res, err := c.transport.Post(ctx, fmt.Sprintf(
"/za/pb/v1/accounts/%s/transactions", req.AccountID), nil,
bytes.NewBuffer(payload))
if err != nil {
return nil, fmt.Errorf("failed to get account transactions: %w", err)
}
var transactions TransactionsResponse
if err = res.JSON(&transactions); err != nil {
return nil, fmt.Errorf("failed to unmarshal transactions response: %w",
err)
}
return &transactions, nil
}