-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoints.go
233 lines (190 loc) · 5.85 KB
/
endpoints.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
// Copyright 2020 Joseph Cobhams. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE.md file.
//
package gomono
import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)
//Auth Endpoints
//ExchangeToken - https://docs.mono.co/reference#authentication-endpoint
func (g *gomono) ExchangeToken(code string) (string, error) {
if code == "" {
return "", errors.New("gomono: Code cannot be blank")
}
payload, err := g.preparePayload(map[string]string{"code": code})
if err != nil {
return "", err
}
respTarget := make(map[string]string)
err = g.makeRequest("POST", fmt.Sprintf("%v/account/auth", g.apiUrl), payload, nil, &respTarget)
if err != nil {
return "", err
}
return respTarget["id"], nil
}
//Account Endpoints
//Information - https://docs.mono.co/reference#bank-account-details
func (g *gomono) Information(id string) (*InformationResponse, error) {
if id == "" {
return nil, errors.New("gomono: ID is required")
}
var respTarget InformationResponse
err := g.makeRequest("GET", fmt.Sprintf("%v/accounts/%v", g.apiUrl, id), nil, nil, &respTarget)
if err != nil {
return nil, err
}
return &respTarget, nil
}
//Statement - https://docs.mono.co/reference#bank-statement
func (g *gomono) Statement(id, period, output string) (*StatementResponse, error) {
if id == "" {
return nil, errors.New("gomono: ID is required")
}
output = strings.ToLower(output)
if output != "" && output != "pdf" && output != "json" {
return nil, errors.New("gomono: only json or pdf output supported")
}
params := url.Values{}
if output != "" {
params.Add("output", output)
}
if period != "" {
params.Add("period", period)
}
endpoint := fmt.Sprintf("%v/accounts/%v/statement?%v", g.apiUrl, id, params.Encode())
var result StatementResponse
switch output {
case "pdf":
var pdfRespTarget StatementResponsePdf
err := g.makeRequest("GET", endpoint, nil, nil, &pdfRespTarget)
if err != nil {
return nil, err
}
result.PDF = &pdfRespTarget
case "json":
var jsonRespTarget StatementResponseJson
err := g.makeRequest("POST", endpoint, nil, nil, &jsonRespTarget)
if err != nil {
return nil, err
}
result.JSON = &jsonRespTarget
}
return &result, nil
}
//PdfStatementJobStatus - https://docs.mono.co/reference#poll-statement-status
func (g *gomono) PdfStatementJobStatus(id, jobId string) (*StatementResponsePdf, error) {
if id == "" {
return nil, errors.New("gomono: ID is required")
}
if jobId == "" {
return nil, errors.New("gomono: JOBID is required")
}
var respTarget StatementResponsePdf
err := g.makeRequest("GET", fmt.Sprintf("%v/accounts/%v/statement/jobs/%v?", g.apiUrl, id, jobId), nil, nil, &respTarget)
if err != nil {
return nil, err
}
return &respTarget, nil
}
// User Endpoints
//Transactions - https://docs.mono.co/reference#poll-statement-status
func (g *gomono) Transactions(id, start, end, narration, tnxType string, paginate bool) (*TransactionsResponse, error) {
if id == "" {
return nil, errors.New("gomono: ID is required")
}
params := url.Values{}
if start != "" {
params.Add("start", start)
}
if end != "" {
params.Add("end", end)
}
if narration != "" {
params.Add("narration", narration)
}
if tnxType != "" {
params.Add("type", tnxType)
}
params.Add("paginate", strconv.FormatBool(paginate))
var respTarget TransactionsResponse
err := g.makeRequest("GET", fmt.Sprintf("%v/accounts/%v/transactions?%v", g.apiUrl, id, params.Encode()), nil, nil, &respTarget)
if err != nil {
return nil, err
}
return &respTarget, nil
}
//CreditTransactions - https://docs.mono.co/reference#credits
func (g *gomono) CreditTransactions(id string) (*TransactionByTypeResponse, error) {
return g.transactionByType(id, "credit")
}
//DebitTransactions - https://docs.mono.co/reference#debits
func (g *gomono) DebitTransactions(id string) (*TransactionByTypeResponse, error) {
return g.transactionByType(id, "debit")
}
func (g *gomono) transactionByType(id, tnxType string) (*TransactionByTypeResponse, error) {
if id == "" {
return nil, errors.New("gomono: ID is required")
}
var respTarget TransactionByTypeResponse
err := g.makeRequest("GET", fmt.Sprintf("%v/accounts/%v/%v", g.apiUrl, id, tnxType), nil, nil, &respTarget)
if err != nil {
return nil, err
}
return &respTarget, nil
}
//Income - https://docs.mono.co/reference#income
func (g *gomono) Income(id string) (*IncomeResponse, error) {
if id == "" {
return nil, errors.New("gomono: ID is required")
}
var respTarget IncomeResponse
err := g.makeRequest("GET", fmt.Sprintf("%v/accounts/%v/income", g.apiUrl, id), nil, nil, &respTarget)
if err != nil {
return nil, err
}
return &respTarget, nil
}
//Identity - https://docs.mono.co/reference#identity
func (g *gomono) Identity(id string) (*IdentityResponse, error) {
if id == "" {
return nil, errors.New("gomono: ID is required")
}
var respTarget IdentityResponse
err := g.makeRequest("GET", fmt.Sprintf("%v/accounts/%v/identity", g.apiUrl, id), nil, nil, &respTarget)
if err != nil {
return nil, err
}
return &respTarget, nil
}
//MISC Endpoints
//Institutions - https://docs.mono.co/reference#list-institutions
func (g *gomono) Institutions() (*InstitutionsResponse, error) {
var respTarget []Institution
err := g.makeRequest("GET", fmt.Sprintf("%v/coverage", g.apiUrl), nil, nil, &respTarget)
if err != nil {
return nil, err
}
return &InstitutionsResponse{
Institutions: respTarget,
}, nil
}
func (g *gomono) LookupBVN(bvn string) (*IdentityResponse, error) {
if bvn == "" {
return nil, errors.New("gomono: BVN is required")
}
payload, err := g.preparePayload(map[string]string{"bvn": bvn})
if err != nil {
return nil, err
}
var respTarget IdentityResponse
err = g.makeRequest("POST", fmt.Sprintf("%v/v1/lookup/bvn/identity", g.apiUrl), payload, nil, &respTarget)
if err != nil {
return nil, err
}
return &respTarget, nil
}