-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mccy_accounts.go
364 lines (297 loc) · 13.4 KB
/
mccy_accounts.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package clearbank
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/google/uuid"
)
type MCCYAccountsClient interface {
// accounts
FetchMCCYAccount(ctx context.Context, accountID uuid.UUID) (MCCYAccountResponse, error)
FetchMCCYAccounts(ctx context.Context, pageNum int, pageSize int) (MCCYAccountsResponse, error)
CreateMCCYAccount(ctx context.Context, payload CreateMCCYAccountPayload) (MCCYAccountResponse, error)
UpdateMCCYAccount(ctx context.Context, accountID uuid.UUID, payload UpdateMCCYAccountPayload) (MCCYAccountResponse, error)
UpdateMCCYAccountStatus(ctx context.Context, accountID uuid.UUID, payload UpdateMCCYAccountStatusPayload) (MCCYAccountResponse, error)
EnableMCCYAccountCurrency(ctx context.Context, accountID uuid.UUID, payload EnableMCCYAccountCurrencyPayload) error
CloseMCCYAccount(ctx context.Context, accountID uuid.UUID, reason MCCYAccountCloseReason) error
FetchMCCYAccountBalances(ctx context.Context, accountID uuid.UUID) (MCCYAccountBalancesResponse, error)
// virtual accounts
FetchMCCYVirtualAccount(ctx context.Context, virtualAccountID uuid.UUID) (MCCYVirtualAccountResponse, error)
FetchMCCYVirtualAccounts(ctx context.Context, pageNum int, pageSize int) (MCCYVirtualAccountsResponse, error)
CreateMCCYVirtualAccount(ctx context.Context, payload CreateMCCYVirtualAccountPayload) (MCCYVirtualAccountResponse, error)
UpdateMCCYVirtualAccount(ctx context.Context, virtualAccountID uuid.UUID, payload UpdateMCCYVirtualAccountPayload) (MCCYVirtualAccountResponse, error)
UpdateMCCYVirtualAccountStatus(ctx context.Context, virtualAccountID uuid.UUID, payload UpdateMCCYVirtualAccountStatusPayload) (MCCYVirtualAccountResponse, error)
CloseMCCYVirtualAccount(ctx context.Context, virtualAccountID uuid.UUID, reason MCCYAccountCloseReason) error
}
type MCCYAccountKind string
const (
MCCYAccountKindYourFunds MCCYAccountKind = "YourFunds"
MCCYAccountKindGeneralSegregated MCCYAccountKind = "GeneralSegregated"
MCCYAccountKindDesignatedSegregated MCCYAccountKind = "DesignatedSegregated"
MCCYAccountKindGeneralClient MCCYAccountKind = "GeneralClient"
MCCYAccountKindDesignatedClient MCCYAccountKind = "DesignatedClient"
)
type MCCYAccountStatus string
type MCCYAccountStatusReason string
const (
MCCYAccountStatusActive MCCYAccountStatus = "Active"
MCCYAccountStatusSuspended MCCYAccountStatus = "Suspended"
MCCYAccountStatusClosed MCCYAccountStatus = "Closed"
MCCYAccountStatusNotProvided MCCYAccountStatus = "NotProvided"
MCCYAccountStatusReasonAccountHolderBankrupt MCCYAccountStatusReason = "AccountHolderBankrupt"
MCCYAccountStatusReasonDissatisfiedCustomer MCCYAccountStatusReason = "DissatisfiedCustomer"
MCCYAccountStatusReasonFinancialCrime MCCYAccountStatusReason = "FinancialCrime"
MCCYAccountStatusReasonFraudFirstParty MCCYAccountStatusReason = "FraudFirstParty"
MCCYAccountStatusReasonFraudThirdParty MCCYAccountStatusReason = "FraudThirdParty"
MCCYAccountStatusReasonOther MCCYAccountStatusReason = "Other"
)
type MCCYAccountIdentifier struct {
Identifier string `json:"identifier"`
Kind string `json:"kind"`
}
type MCCYAccountResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Label string `json:"label"`
Owner string `json:"owner"`
Kind MCCYAccountKind `json:"kind"`
Currencies []string `json:"currencies"`
Identifiers []MCCYAccountIdentifier `json:"identifiers"`
Status MCCYAccountStatus `json:"status"`
StatusReason string `json:"statusReason"`
StatusInformation string `json:"statusInformation"`
}
// GetIdentifier returns first identifier.
func (r MCCYAccountResponse) GetIdentifier() string {
if len(r.Identifiers) > 0 {
return r.Identifiers[0].Identifier
}
return ""
}
func (c *client) FetchMCCYAccount(ctx context.Context, accountID uuid.UUID) (data MCCYAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/v2/Accounts/%s/MC", accountID.String()), nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type MCCYAccountsResponse struct {
Accounts []MCCYAccountResponse `json:"accounts"`
}
func (c *client) FetchMCCYAccounts(ctx context.Context, pageNum int, pageSize int) (data MCCYAccountsResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, "/mccy/v1/Accounts", nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.AddQueryParam("pageNumber", strconv.Itoa(pageNum))
req.AddQueryParam("pageSize", strconv.Itoa(pageSize))
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type RoutingCodeKind string
const RoutingCodeKindGB RoutingCodeKind = "GBSortCode"
type CreateMCCYAccountPayload struct {
Label string `json:"label"`
Owner string `json:"owner"`
Kind MCCYAccountKind `json:"kind"`
Currencies []string `json:"currencies"`
RoutingCode struct {
Code string `json:"code"`
Kind RoutingCodeKind `json:"kind"`
} `json:"routingCode"`
}
func (c *client) CreateMCCYAccount(ctx context.Context, payload CreateMCCYAccountPayload) (data MCCYAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/mccy/v1/Accounts", payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusCreated)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type UpdateMCCYAccountPayload struct {
Label string `json:"label"`
Owner string `json:"owner"`
}
func (c *client) UpdateMCCYAccount(ctx context.Context, accountID uuid.UUID, payload UpdateMCCYAccountPayload) (data MCCYAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPatch, fmt.Sprintf("/mccy/v1/Accounts/%s", accountID.String()), payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type UpdateMCCYAccountStatusPayload struct {
Status string `json:"status"`
StatusReason string `json:"statusReason,omitempty"`
Information string `json:"information,omitempty"`
}
func (c *client) UpdateMCCYAccountStatus(ctx context.Context, accountID uuid.UUID, payload UpdateMCCYAccountStatusPayload) (data MCCYAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPatch, fmt.Sprintf("/mccy/v1/Accounts/%s/status", accountID.String()), payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type EnableMCCYAccountCurrencyPayload struct {
Currency string `json:"currency"`
}
func (c *client) EnableMCCYAccountCurrency(ctx context.Context, accountID uuid.UUID, payload EnableMCCYAccountCurrencyPayload) error {
req, err := c.newRequest(ctx, http.MethodPost, fmt.Sprintf("/mccy/v1/Accounts/%s/currencies", accountID.String()), payload)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
return c.do(ctx, req)
}
type MCCYAccountCloseReason string
const (
MCCYAccountCloseReasonOther MCCYAccountCloseReason = "Other"
MCCYAccountCloseReasonAccountHolderDeceased MCCYAccountCloseReason = "AccountHolderDeceased"
MCCYAccountCloseReasonAccountSwitched MCCYAccountCloseReason = "AccountSwitched"
MCCYAccountCloseReasonCompanyNoLongerTrading MCCYAccountCloseReason = "CompanyNoLongerTrading"
MCCYAccountCloseReasonDuplicateAccount MCCYAccountCloseReason = "DuplicateAccount"
)
func (c *client) CloseMCCYAccount(ctx context.Context, accountID uuid.UUID, reason MCCYAccountCloseReason) error {
req, err := c.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/mccy/v1/Accounts/%s", accountID.String()), nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.AddQueryParam("accountCloseReason", string(reason))
req.ExpectStatus(http.StatusNoContent)
return c.do(ctx, req)
}
type MCCYAccountBalance struct {
Currency string `json:"currency"`
Available float64 `json:"available"`
Actual float64 `json:"actual"`
}
type MCCYAccountBalancesResponse struct {
Balances []MCCYAccountBalance `json:"balances"`
}
func (m *MCCYAccountBalancesResponse) UnmarshalJSON(data []byte) error {
type payload struct {
Balances map[string]struct {
Available float64 `json:"Available"`
Actual float64 `json:"Actual"`
} `json:"balances"`
}
var p payload
if err := json.Unmarshal(data, &p); err != nil {
return fmt.Errorf("unable to unmarshal MCCY account balances: %w", err)
}
for currency, balance := range p.Balances {
m.Balances = append(m.Balances, MCCYAccountBalance{
Currency: currency,
Available: balance.Available,
Actual: balance.Actual,
})
}
return nil
}
func (c *client) FetchMCCYAccountBalances(ctx context.Context, accountID uuid.UUID) (data MCCYAccountBalancesResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/mccy/v1/Accounts/%s/balances", accountID.String()), nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type MCCYVirtualAccountResponse struct {
ID uuid.UUID `json:"id"`
AccountID uuid.UUID `json:"accountId"`
Owner string `json:"owner"`
Identifiers []MCCYAccountIdentifier `json:"identifiers"`
Status MCCYAccountStatus `json:"status"`
StatusReason string `json:"statusReason"`
StatusInformation string `json:"statusInformation"`
}
// GetIdentifier returns first identifier.
func (r MCCYVirtualAccountResponse) GetIdentifier() string {
if len(r.Identifiers) > 0 {
return r.Identifiers[0].Identifier
}
return ""
}
func (c *client) FetchMCCYVirtualAccount(ctx context.Context, virtualAccountID uuid.UUID) (data MCCYVirtualAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/mccy/v1/VirtualAccounts/%s", virtualAccountID.String()), nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type MCCYVirtualAccountsResponse struct {
VirtualAccounts []MCCYVirtualAccountResponse `json:"virtualAccounts"`
}
func (c *client) FetchMCCYVirtualAccounts(ctx context.Context, pageNum int, pageSize int) (data MCCYVirtualAccountsResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, "/mccy/v1/VirtualAccounts", nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.AddQueryParam("pageNumber", strconv.Itoa(pageNum))
req.AddQueryParam("pageSize", strconv.Itoa(pageSize))
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type CreateMCCYVirtualAccountPayload struct {
AccountID string `json:"accountId"`
VirtualAccount struct {
Owner string `json:"owner"`
} `json:"virtualAccount"`
}
func (c *client) CreateMCCYVirtualAccount(ctx context.Context, payload CreateMCCYVirtualAccountPayload) (data MCCYVirtualAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/mccy/v1/VirtualAccounts", payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusCreated)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type UpdateMCCYVirtualAccountPayload struct {
Owner string `json:"owner"`
}
func (c *client) UpdateMCCYVirtualAccount(ctx context.Context, virtualAccountID uuid.UUID, payload UpdateMCCYVirtualAccountPayload) (data MCCYVirtualAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPatch, fmt.Sprintf("/mccy/v1/VirtualAccounts/%s", virtualAccountID.String()), payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type UpdateMCCYVirtualAccountStatusPayload struct {
Status string `json:"status"`
StatusReason string `json:"statusReason,omitempty"`
Information string `json:"information,omitempty"`
}
func (c *client) UpdateMCCYVirtualAccountStatus(ctx context.Context, virtualAccountID uuid.UUID, payload UpdateMCCYVirtualAccountStatusPayload) (data MCCYVirtualAccountResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPatch, fmt.Sprintf("/mccy/v1/VirtualAccounts/%s/status", virtualAccountID.String()), payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
func (c *client) CloseMCCYVirtualAccount(ctx context.Context, virtualAccountID uuid.UUID, reason MCCYAccountCloseReason) error {
req, err := c.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/mccy/v1/VirtualAccounts/%s", virtualAccountID.String()), nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.AddQueryParam("accountCloseReason", string(reason))
req.ExpectStatus(http.StatusNoContent)
return c.do(ctx, req)
}