-
Notifications
You must be signed in to change notification settings - Fork 0
/
debit_cards.go
426 lines (349 loc) · 12.7 KB
/
debit_cards.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// Handles all DebitCard operations
type debitCardService service
// DebitCard data type
type DebitCard struct {
UID string `json:"uid,omitempty"`
ExternalUID string `json:"external_uid,omitempty"`
CustomerUID string `json:"customer_uid,omitempty"`
PoolUID string `json:"pool_uid,omitempty"`
SyntheticAccountUID string `json:"synthetic_account_uid,omitempty"`
CustodialAccountUID string `json:"custodial_account_uid,omitempty"`
CardArtworkUID string `json:"card_artwork_uid,omitempty"`
CardLastFourDigits string `json:"card_last_four_digits,omitempty"`
Status string `json:"status,omitempty"`
Type string `json:"type,omitempty"`
ReadyToUse bool `json:"ready_to_use,omitempty"`
LockReason string `json:"lock_reason,omitempty"`
IssuedOn string `json:"issued_on,omitempty"`
LockedAt time.Time `json:"locked_at,omitempty"`
ClosedAt time.Time `json:"closed_at,omitempty"`
LatestShippingAddress *DebitCardShippingAddress `json:"latest_shipping_address,omitempty"`
}
// DebitCardShippingAddress is an optional field used to specify the shipping address for a physical Debit Card.
type DebitCardShippingAddress struct {
Street1 string `json:"street1,omitempty"`
Street2 string `json:"street2,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
PostalCode string `json:"postal_code,omitempty"`
}
// DebitCardAccessToken contains the token necessary to retrieve a virtual Debit Card image.
type DebitCardAccessToken struct {
Token string `json:"token"`
ConfigID string `json:"config_id"`
}
// DebitCardListParams builds the query parameters used in querying Debit Cards
type DebitCardListParams struct {
CustomerUID string `url:"customer_uid,omitempty" json:"customer_uid,omitempty"`
ExternalUID string `url:"external_uid,omitempty" json:"external_uid,omitempty"`
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
Offset int `url:"offset,omitempty" json:"offset,omitempty"`
PoolUID string `url:"pool_uid,omitempty" json:"pool_uid,omitempty"`
Locked bool `url:"locked,omitempty" json:"locked,omitempty"`
Status string `url:"status,omitempty" json:"status,omitempty"`
}
// DebitCardCreateParams are the body params used when creating a new Debit Card
type DebitCardCreateParams struct {
ExternalUID string `json:"external_uid,omitempty"`
CardArtworkUID string `json:"card_artwork_uid,omitempty"`
CustomerUID string `json:"customer_uid"`
PoolUID string `json:"pool_uid"`
ShippingAddress *DebitCardShippingAddress `json:"shipping_address,omitempty"`
}
// DebitCardActivateParams are the body params used when activating a new Debit Card
type DebitCardActivateParams struct {
CardLastFourDigits string `json:"card_last_four_digits"`
CVV string `json:"cvv"`
ExpiryDate string `json:"expiry_date"`
}
// DebitCardLockParams are the body params used when locking a Debit Card
type DebitCardLockParams struct {
LockReason string `json:"lock_reason"`
}
// DebitCardReissueParams are the body params used when reissuing a Debit Card
type DebitCardReissueParams struct {
CardArtworkUID string `json:"card_artwork_uid,omitempty"`
ReissueReason string `json:"reissue_reason"`
ShippingAddress *DebitCardShippingAddress `json:"shipping_address,omitempty"`
}
// DebitCardGetPINTokenParams are the query params used fetching a Debit Card PIN reset token
type DebitCardGetPINTokenParams struct {
ForceReset bool `url:"force_reset" json:"force_reset"`
}
// VirtualDebitCardMigrateParams are the body params used when migrating a Virtual Debit Card
type VirtualDebitCardMigrateParams struct {
ExternalUID string `json:"external_uid,omitempty"`
CardArtworkUID string `json:"card_artwork_uid,omitempty"`
ShippingAddress *DebitCardShippingAddress `json:"shipping_address,omitempty"`
}
// VirtualDebitCardQueryParams are the query params used to retrieve a virtual Debit Card image
type VirtualDebitCardQueryParams struct {
Token string `url:"token" json:"token"`
Config string `url:"config" json:"config"`
}
// DebitCardListResponse is an API response containing a list of Debit Cards
type DebitCardListResponse struct {
ListResponse
Data []*DebitCard `json:"data"`
}
// DebitCardPINTokenResponse is an API response containing a token necessary to change a Debit Card's PIN
type DebitCardPINTokenResponse struct {
PinChangeToken string `json:"pin_change_token"`
}
// List retrieves a list of Debit Cards filtered by the given parameters
func (d *debitCardService) List(ctx context.Context, params *DebitCardListParams) (*DebitCardListResponse, error) {
// Build DebitCardListParams into query string params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := d.client.doRequest(ctx, http.MethodGet, "debit_cards", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCardListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Create is used to a new Debit Card and attach it to the supplied Customer and Pool
func (d *debitCardService) Create(ctx context.Context, params *DebitCardCreateParams) (*DebitCard, error) {
if params.CustomerUID == "" || params.PoolUID == "" {
return nil, fmt.Errorf("CustomerUID and PoolUID are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := d.client.doRequest(ctx, http.MethodPost, "debit_cards", nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCard{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Get returns a single DebitCard
func (d *debitCardService) Get(ctx context.Context, uid string) (*DebitCard, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := d.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("debit_cards/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCard{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Activate a Debit Card
func (d *debitCardService) Activate(ctx context.Context, uid string, params *DebitCardActivateParams) (*DebitCard, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
if params.CardLastFourDigits == "" || params.CVV == "" || params.ExpiryDate == "" {
return nil, fmt.Errorf("CardLastFourDigits, CVV and ExpiryDate are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := d.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("debit_cards/%s/activate", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCard{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Lock will temporarily lock the Debit Card
func (d *debitCardService) Lock(ctx context.Context, uid string, params *DebitCardLockParams) (*DebitCard, error) {
if uid == "" || params.LockReason == "" {
return nil, fmt.Errorf("UID and LockReason are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := d.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("debit_cards/%s/lock", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCard{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Unlock will attempt to remove a lock placed on a Debit Card
func (d *debitCardService) Unlock(ctx context.Context, uid string) (*DebitCard, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := d.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("debit_cards/%s/unlock", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCard{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Reissue a Debit Card that is lost or stolen, or when it has suffered damage
func (d *debitCardService) Reissue(ctx context.Context, uid string, params *DebitCardReissueParams) (*DebitCard, error) {
if uid == "" || params.ReissueReason == "" {
return nil, fmt.Errorf("UID and ReissueReason are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := d.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("debit_cards/%s/reissue", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCard{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// GetPINToken is used to retrieve a token necessary to change a Debit Card's PIN
func (d *debitCardService) GetPINToken(ctx context.Context, uid string, params *DebitCardGetPINTokenParams) (*DebitCardPINTokenResponse, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := d.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("debit_cards/%s/pin_change_token", uid), v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCardPINTokenResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// GetAccessToken is used to retrieve the configuration ID and token necessary to retrieve a virtual Debit Card image
func (d *debitCardService) GetAccessToken(ctx context.Context, uid string) (*DebitCardAccessToken, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := d.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("debit_cards/%s/access_token", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCardAccessToken{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// MigrateVirtualDebitCard will result in a physical version of the virtual debit card being issued to a Customer
func (d *debitCardService) MigrateVirtualDebitCard(ctx context.Context, uid string, params *VirtualDebitCardMigrateParams) (*DebitCard, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := d.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("debit_cards/%s/migrate", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &DebitCard{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// GetVirtualDebitCardImage is used to retrieve a virtual Debit Card image
func (d *debitCardService) GetVirtualDebitCardImage(ctx context.Context, params *VirtualDebitCardQueryParams) (*http.Response, error) {
if params.Config == "" || params.Token == "" {
return nil, fmt.Errorf("Config and Token params are required")
}
v, err := query.Values(params)
if err != nil {
return nil, err
}
// TODO: Does this require a different Accept header type (image/jpeg)?
res, err := d.client.doRequest(ctx, http.MethodGet, "assets/virtual_card_image", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
return res, nil
}