-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanking.go
243 lines (195 loc) · 5.36 KB
/
banking.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
package inter
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type Banking struct {
client *Client
token Token
}
func NewBanking(client *Client, token Token) *Banking {
return &Banking{
client: client,
token: token,
}
}
type Balance struct {
Available float32
Limit float32
CheckOnHold float32
JudiciallyBlocked float32
AdministrativelyBlocked float32
}
func (b *Banking) Balance(ctx context.Context, date time.Time) (Balance, error) {
endpoint := fmt.Sprintf("%s/banking/v2/saldo", b.client.apiBaseUrl)
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return Balance{}, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", b.token.Data))
q := url.Values{}
q.Add("dataSaldo", date.Format(time.DateOnly))
req.URL.RawQuery = q.Encode()
resp, err := b.client.Do(req)
if err != nil {
return Balance{}, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Balance{}, err
}
if resp.StatusCode != http.StatusOK {
return Balance{}, errors.New(string(data))
}
return parseApiBalance(data)
}
type apiBalance struct {
Available float32 `json:"disponivel"`
Limit float32 `json:"limite"`
CheckOnHold float32 `json:"bloqueadoCheque"`
JudiciallyBlocked float32 `json:"bloqueadoJudicialmente"`
AdministrativelyBlocked float32 `json:"bloqueadoAdministrativo"`
}
func parseApiBalance(d []byte) (Balance, error) {
var tmp apiBalance
err := json.Unmarshal(d, &tmp)
if err != nil {
return Balance{}, err
}
return Balance{
Available: tmp.Available,
Limit: tmp.Limit,
CheckOnHold: tmp.CheckOnHold,
JudiciallyBlocked: tmp.JudiciallyBlocked,
AdministrativelyBlocked: tmp.AdministrativelyBlocked,
}, nil
}
type TransactionType int
const (
PixTransactionType = TransactionType(iota + 1)
PagamentoTransactionType
TransferenciaTransactionType
)
func (t TransactionType) String() string {
switch t {
case PixTransactionType:
return "pix"
case PagamentoTransactionType:
return "pagamento"
case TransferenciaTransactionType:
return "transferencia"
}
return "unknow"
}
type TransactionOperation int
const (
CreditTransactionOperation = TransactionOperation(iota + 1)
DebitTransactionOperation
)
func (t TransactionOperation) String() string {
switch t {
case CreditTransactionOperation:
return "credit"
case DebitTransactionOperation:
return "debit"
}
return "invalid"
}
type Transaction struct {
Date time.Time
Type TransactionType
Operation TransactionOperation
Value float32
Title string
Description string
}
func (b *Banking) Transactions(ctx context.Context, start, end time.Time) ([]Transaction, error) {
endpoint := fmt.Sprintf("%s/banking/v2/extrato", b.client.apiBaseUrl)
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return []Transaction{}, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", b.token.Data))
q := url.Values{}
q.Add("dataInicio", start.Format(time.DateOnly))
q.Add("dataFim", end.Format(time.DateOnly))
req.URL.RawQuery = q.Encode()
resp, err := b.client.Do(req)
if err != nil {
return []Transaction{}, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []Transaction{}, err
}
if resp.StatusCode != http.StatusOK {
return []Transaction{}, errors.New(string(data))
}
return parseApiTransactions(data)
}
type apiTransaction struct {
Date string `json:"dataEntrada"`
Type string `json:"tipoTransacao"`
Operation string `json:"tipoOperacao"`
Value string `json:"valor"`
Title string `json:"titulo"`
Description string `json:"descricao"`
}
type apiTransactions struct {
Transactions []apiTransaction `json:"transacoes"`
}
var apiTransactionTypeMap map[string]TransactionType = map[string]TransactionType{
"PIX": PixTransactionType,
"PAGAMENTO": PagamentoTransactionType,
"TRANSFERENCIA": TransferenciaTransactionType,
}
var apiTransactionOperationMap map[string]TransactionOperation = map[string]TransactionOperation{
"C": CreditTransactionOperation,
"D": DebitTransactionOperation,
}
func transactionFromApi(a apiTransaction) (Transaction, error) {
date, err := time.Parse(time.DateOnly, a.Date)
if err != nil {
return Transaction{}, err
}
value, err := strconv.ParseFloat(a.Value, 32)
if err != nil {
return Transaction{}, err
}
return Transaction{
Date: date,
Type: apiTransactionTypeMap[a.Type],
Operation: apiTransactionOperationMap[a.Operation],
Value: float32(value),
Title: strings.TrimSpace(a.Title),
Description: strings.TrimSpace(a.Description),
}, nil
}
func parseApiTransactions(d []byte) ([]Transaction, error) {
var tmp apiTransactions
err := json.Unmarshal(d, &tmp)
if err != nil {
return []Transaction{}, err
}
transactions := make([]Transaction, 0, len(tmp.Transactions))
for _, v := range tmp.Transactions {
t, err := transactionFromApi(v)
if err != nil {
return []Transaction{}, err
}
transactions = append(transactions, t)
}
return transactions, nil
}