-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarling.go
274 lines (220 loc) · 7.06 KB
/
starling.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
266
267
268
269
270
271
272
273
274
package starlinggo
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
var BaseUrl = "https://api.starlingbank.com/api/v2/"
var datetimeFmt = "2006-01-02T15:04:05.000Z"
var dateFmt = "2006-01-02"
type amount struct {
Currency string `json:"curency"`
Pence float64 `json:"minorUnits"`
}
type latestDDPayment struct {
LastAmount amount `json:"lastAmount"`
LastDate string `json:"lastDate"`
}
type directDebit struct {
Status string `json:"status"`
Payee string `json:"originatorName"`
LatestPayment latestDDPayment `json:"lastPayment"`
}
type directDebits struct {
Mandates []directDebit `json:"mandates"`
}
type recurringPayment struct {
Payee string `json:"counterPartyName"`
Status string `json:"status"`
LastAmount amount `json:"latestPaymentAmount"`
LastDate string `json:"latestPaymentDate"`
}
type recurringPayments struct {
RecurringPayments []recurringPayment `json:"recurringPayments"`
}
type standingOrder struct {
Reference string `json:"reference"`
Amount amount `json:"amount"`
CancelDate string `json:"cancelledAt"`
NextDate string `json:"nextdate"`
}
type standingOrders struct {
StandingOrders []standingOrder `json:"standingOrders"`
}
type balances struct {
EffectiveBalance amount `json:"effectiveBalance"`
ClearedBalance amount `json:"clearedBalance"`
Amount amount `json:"amount"`
}
type transaction struct {
Amount amount `json:"amount"`
Direction string `json:"direction"`
TransactionDt string `json:"transactionTime"`
PartyName string `json:"counterPartyName"`
Reference string `json:"reference"`
}
type transactions struct {
Transactions []transaction `json:"feedItems"`
}
type accounts struct {
Accounts []accountDetial `json:"accounts"`
}
type accountDetial struct {
AccountUid string `json:"accountUid"`
AccountType string `json:"accountType"`
CategoryUid string `json:"defaultCategory"`
Name string `json:"name"`
}
// type Accounter interface {
// getDirectDebits()
// getRecurringPayments()
// getStandingOrders()
// GetBalance()
// getTransactionsSince()
// leftToPayReport()
// getLastPayDay()
// }
type Account struct {
Token, AccountUid, CategoryUid string
}
// HTTPClient interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
var Client HTTPClient
func init() {
Client = &http.Client{}
}
// Initialise and return an account
func AccountInit(token string) Account {
au, cu := getPrimaryAccountDetails(token)
acc := Account{Token: token, AccountUid: au, CategoryUid: cu}
return acc
}
// Generic get function that makes the request to starling api
func get(endpoint string, token string) []byte {
req, _ := http.NewRequest("GET", BaseUrl+endpoint, nil)
req.Header.Set("Authorization", "Bearer "+token)
resp, err := Client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
return body
}
// Function to gather additional account info for intialisation
func getPrimaryAccountDetails(token string) (string, string) {
resp := get("accounts", token)
var accs accounts
fmt.Println(string(resp))
if err := json.Unmarshal(resp, &accs); err != nil {
log.Fatal(err.Error())
}
var accUid, catUid string
for _, acc := range accs.Accounts {
if acc.AccountType == "PRIMARY" {
accUid, catUid = acc.AccountUid, acc.CategoryUid
}
}
return accUid, catUid
}
// Function to collect a list of all active direct debits for the account
func (x Account) getDirectDebits() []directDebit {
resp := get("direct-debit/mandates", x.Token)
var dds directDebits
if err := json.Unmarshal(resp, &dds); err != nil {
log.Fatal(err)
}
return dds.Mandates
}
func (x Account) getRecurringPayments() []recurringPayment {
resp := get(fmt.Sprintf("accounts/%s/recurring-payment", x.AccountUid), x.Token)
var rps recurringPayments
if err := json.Unmarshal(resp, &rps); err != nil {
log.Fatal(err)
}
return rps.RecurringPayments
}
func (x Account) getStandingOrders() []standingOrder {
resp := get(fmt.Sprintf("payments/local/account/%s/category/%s/standing-orders", x.AccountUid, x.CategoryUid), x.Token)
var sos standingOrders
if err := json.Unmarshal(resp, &sos); err != nil {
log.Fatal(err)
}
return sos.StandingOrders
}
// Function to return the effective balance of the account in pounds
func (x Account) GetBalance() float64 {
resp := get(fmt.Sprintf("accounts/%s/balance", x.AccountUid), x.Token)
var bal balances
if err := json.Unmarshal(resp, &bal); err != nil {
log.Fatal(err)
}
return bal.EffectiveBalance.Pence / 100.00
}
func (x Account) getTransactionsSince(since time.Time) transactions {
resp := get(fmt.Sprintf("feed/account/%s/category/%s?changesSince=%s", x.AccountUid, x.CategoryUid, since.Format(datetimeFmt)), x.Token)
var t transactions
if err := json.Unmarshal(resp, &t); err != nil {
log.Fatal(err)
}
return t
}
func getLastPayDay(ts []transaction, ref string) time.Time {
var dt time.Time
for _, t := range ts {
if t.Direction == "IN" && t.Reference == ref {
dt, _ = time.Parse(datetimeFmt, t.TransactionDt)
}
}
return dt
}
func writeRepTab(status string, payee string, amnt float64, dt string) string {
return fmt.Sprintf("<tr><td>%-20s</td><td>%-30s</td><td>£%8.2f</td><td>%s</td></tr>", status, payee, amnt, dt)
}
func leftToPayReport(dd []directDebit, rp []recurringPayment, so []standingOrder, payDate time.Time) (float64, string) {
total := 0.00
report := ""
for _, e := range dd {
dt, _ := time.Parse(dateFmt, e.LatestPayment.LastDate)
if e.Status == "LIVE" && dt.Before(payDate) {
report += writeRepTab("ACTIVE", e.Payee, e.LatestPayment.LastAmount.Pence/100.00, dt.Format(dateFmt))
total += e.LatestPayment.LastAmount.Pence
}
}
for _, e := range rp {
dt, _ := time.Parse(datetimeFmt, e.LastDate)
if e.Status == "ACTIVE" && dt.Before(payDate) {
report += writeRepTab(e.Status, e.Payee, e.LastAmount.Pence/100.00, dt.Format(dateFmt))
total += e.LastAmount.Pence
}
}
for _, e := range so {
dt, _ := time.Parse(datetimeFmt, e.NextDate)
if e.CancelDate != "" && dt.Before(payDate.AddDate(0, 1, 0)) && dt.After(payDate) {
report += writeRepTab("ACTIVE", e.Reference, e.Amount.Pence/100.00, dt.Format(dateFmt))
total += e.Amount.Pence
}
}
return float64(total) / 100.00, report
}
func (x Account) Report(payRef string) string {
now := time.Now()
since := now.AddDate(0, -1, 0)
trans := x.getTransactionsSince(since).Transactions
payDay := getLastPayDay(trans, payRef)
dd, so, rp, bal := x.getDirectDebits(), x.getStandingOrders(), x.getRecurringPayments(), x.GetBalance()
toPay, toPayRep := leftToPayReport(dd, rp, so, payDay)
report := fmt.Sprintf("<head><style>table, th, td {border: 1px solid black;}</style></head><body><table>%s</table>", toPayRep)
// add invisable uncode char to force utf-8 as gmail is dumb
report += fmt.Sprintf("<p>\u200BLast pay day %s</p>", payDay.Format(dateFmt))
report += fmt.Sprintf("<p>Balance £%6.2f<br>To Pay £%6.2f<br>Effective balance £%6.2f</p></body>", bal, toPay, bal-toPay)
return report
}