forked from cloudmode/go-primetrust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcash-transactions.go
227 lines (191 loc) · 7.01 KB
/
cash-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
package primetrust
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/cloudmode/go-primetrust/models"
"github.com/fatih/color"
"github.com/tidwall/gjson"
)
func GetCashTransaction(transactionID string) (*models.CashTransaction, error) {
apiUrl := fmt.Sprintf("%s/cash-transactions/%s", _apiPrefix, transactionID)
color.Green("GetCashTransactions:%v", apiUrl)
req, err := http.NewRequest("GET", apiUrl, nil)
req.Header.Add("Authorization", _jwt)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)
response := models.CashTransaction{}
if err := json.Unmarshal(body, &response); err != nil {
color.Red("body:%v", string(body))
return nil, errors.New("unmarshal error")
}
return &response, nil
}
// GetCashTransactionsPage returns a single page of transactions using the USD filter
func GetCashTransactionsPage(accountID string, number, size int64) (*models.CashTransactionsResponse, error) {
filter := "filter[currency-type eq]=USD"
apiURL := fmt.Sprintf("%s/cash-transactions?%s&page[number]=1&page[size]=20&include=account-cash-transfer-from,account-cash-transfer-to&sort=-created-at&account.id=%s",
_apiPrefix, url.PathEscape(filter), accountID)
color.Red("GetCashTransactionsPage:%v", apiURL)
req, err := http.NewRequest("GET", apiURL, nil)
req.Header.Add("Authorization", _jwt)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
color.Red("error getting accountID:%s:%s", filter)
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
color.Red("wetf:%v", res.Status)
return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)
//color.Red("body:%s", body)
transactions := models.CashTransactionsResponse{}
if err := json.Unmarshal(body, &transactions); err != nil {
return nil, errors.New("unmarshal error")
}
color.Red("GetCashTransactionsPage:%v", PrettyPrint(transactions))
return &transactions, nil
}
// GetCashTransactions returns all cash transactions between from and to
func GetCashTransactions(accountID string, from, to time.Time) (*models.CashTransactionsResponse, error) {
filter := fmt.Sprintf("filter[created-at gte]=%s&filter[created-at lte]=%s",
from.Format(time.RFC3339), to.Format(time.RFC3339))
color.Blue("filter:%s", filter)
apiURL := fmt.Sprintf("%s/cash-transactions?%s&page[number]=1&page[size]=1000&include=account-cash-transfer-from,account-cash-transfer-to&sort=-created-at&account.id=%s",
_apiPrefix, url.PathEscape(filter), accountID)
//apiURL = url.QueryEscape(apiURL)
req, err := http.NewRequest("GET", apiURL, nil)
req.Header.Add("Authorization", _jwt)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
color.Red("error getting accountID:%s:%s", filter)
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
color.Red("wetf:%v", res.Status)
return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)
//color.Red("body:%s", body)
transactions := models.CashTransactionsResponse{}
if err := json.Unmarshal(body, &transactions); err != nil {
return nil, errors.New("unmarshal error")
}
color.Red("GetTransactions:%v", PrettyPrint(transactions))
return &transactions, nil
}
// NewFundsTransfer ...
func NewFundsTransfer(from, to, reference string, amount float64) *models.FundTransfer {
attrs := models.FundTransferAttributes{FromAccountID: from, ToAccountID: to, Amount: amount, Reference: reference, CurrencyType: "USD"}
data := models.FundTransferData{Type: "account-cash-transfers", Attributes: attrs}
ft := models.FundTransfer{Data: data}
return &ft
}
// GetFundsTransfer from funds-transfer-id
func GetFundsTransfer(fundsTransferID string) (*models.FundsTransfer, error) {
apiUrl := fmt.Sprintf("%s/funds-transfers/%s", _apiPrefix, fundsTransferID)
req, err := http.NewRequest("GET", apiUrl, nil)
req.Header.Add("Authorization", _jwt)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
body, _ := ioutil.ReadAll(res.Body)
response := models.FundsTransfer{}
if err := json.Unmarshal(body, &response); err != nil {
color.Red("GetFundsTransfer unmarshal error:%v", err)
return nil, errors.New("unmarshal error")
}
return &response, nil
}
// FundsTransfer ...
// trying a JSON parsing trick from https://stackoverflow.com/questions/35583735/unmarshaling-into-an-interface-and-then-performing-type-assertion
// because Included is an array of CashTransfer and CashTransaction
func FundsTransfer(from, to, amount, reference string) (*models.CashTransfer, error) {
tAmount, err := strconv.ParseFloat(amount, 64)
if err != nil {
return nil, err
}
ft := NewFundsTransfer(from, to, reference, tAmount)
jsonData := new(bytes.Buffer)
json.NewEncoder(jsonData).Encode(ft)
apiURL := fmt.Sprintf("%s/account-cash-transfers?include=to-account-cash-totals,from-account-cash-totals,to-cash-transaction,from-cash-transaction", _apiPrefix)
//color.Green("FundsTransfer:%v", apiUrl)
req, err := http.NewRequest("POST", apiURL, jsonData)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", _jwt)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %s", res.Status, string(body))
}
response := models.CashTransfer{}
if err := json.Unmarshal(body, &response); err != nil {
color.Black("FundsTransfer unmarshal error %v", string(body))
return nil, err
}
included := gjson.ParseBytes(body).Get("included")
for _, raw := range included.Array() {
t := raw.Get("type")
r := raw.Get("relationships.account.links.related")
if strings.Contains(r.String(), from) {
if t.String() == "account-cash-totals" {
if err := json.Unmarshal([]byte(raw.Raw), &response.FromCashData); err != nil {
color.Red("shit:%v", err)
}
color.Green("CashTotal:%v", PrettyPrint(response.FromCashData))
} else if t.String() == "cash-transactions" {
if err := json.Unmarshal([]byte(raw.Raw), &response.FromCashTransaction); err != nil {
color.Red("shit:%v", err)
}
}
} else if strings.Contains(r.String(), to) {
if t.String() == "account-cash-totals" {
if err := json.Unmarshal([]byte(raw.Raw), &response.ToCashData); err != nil {
color.Red("shit:%v", err)
}
} else if t.String() == "cash-transactions" {
if err := json.Unmarshal([]byte(raw.Raw), &response.ToCashTransaction); err != nil {
color.Red("shit:%v", err)
}
}
}
}
color.Red("FundsTransfer:response:%+v", PrettyPrint(response))
return &response, nil
}
// PrettyPrint ...
func PrettyPrint(thing interface{}) string {
s, _ := json.MarshalIndent(thing, "", "\t")
return string(s)
}