-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomers.go
391 lines (327 loc) · 12.8 KB
/
customers.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
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
"github.com/rizefinance/rize-go-sdk/internal"
)
// Handles all Customer related functionality
type customerService service
// Customer data type
type Customer struct {
UID string `json:"uid,omitempty"`
ExternalUID string `json:"external_uid,omitempty"`
ActivatedAt time.Time `json:"activated_at,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
CustomerType string `json:"customer_type,omitempty"`
Email string `json:"email,omitempty"`
Details *CustomerDetails `json:"details,omitempty"`
KYCStatus string `json:"kyc_status,omitempty"`
KYCStatusReasons []string `json:"kyc_status_reasons,omitempty"`
LockReason string `json:"lock_reason,omitempty"`
LockedAt time.Time `json:"locked_at,omitempty"`
PIIConfirmedAt time.Time `json:"pii_confirmed_at,omitempty"`
PoolUIDs []string `json:"pool_uids,omitempty"`
PrimaryCustomerUID string `json:"primary_customer_uid,omitempty"`
ProfileResponses []*CustomerProfileResponse `json:"profile_responses,omitempty"`
ProgramUID string `json:"program_uid,omitempty"`
SecondaryCustomerUIDs []string `json:"secondary_customer_uids,omitempty"`
Status string `json:"status,omitempty"`
TotalBalance string `json:"total_balance,omitempty"`
}
// CustomerDetails is an object containing the supplied identifying information for the Customer
type CustomerDetails struct {
FirstName string `json:"first_name,omitempty"`
MiddleName string `json:"middle_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Suffix string `json:"suffix,omitempty"`
Phone string `json:"phone,omitempty"`
BusinessName string `json:"business_name,omitempty"`
DOB internal.DOB `json:"dob,omitempty"`
SSN string `json:"ssn,omitempty"`
SSNLastFour string `json:"ssn_last_four,omitempty"`
Address *CustomerAddress `json:"address,omitempty"`
}
// CustomerAddress information
type CustomerAddress 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"`
}
// CustomerProfileResponse contains Profile Response info
type CustomerProfileResponse struct {
ProfileRequirement string `json:"profile_requirement,omitempty"`
ProfileRequirementUID string `json:"profile_requirement_uid,omitempty"`
ProfileResponse *internal.CustomerProfileResponseItem `json:"profile_response,omitempty"`
}
// CustomerListParams builds the query parameters used in querying Customers
type CustomerListParams struct {
UID string `url:"uid,omitempty" json:"uid,omitempty"`
Status string `url:"status,omitempty" json:"status,omitempty"`
IncludeInitiated bool `url:"include_initiated,omitempty" json:"include_initiated"`
KYCStatus string `url:"kyc_status,omitempty" json:"kyc_status,omitempty"`
CustomerType string `url:"customer_type,omitempty" json:"customer_type,omitempty"`
FirstName string `url:"first_name,omitempty" json:"first_name,omitempty"`
LastName string `url:"last_name,omitempty" json:"last_name,omitempty"`
Email string `url:"email,omitempty" json:"email,omitempty"`
Locked bool `url:"locked,omitempty" json:"locked"`
ProgramUID string `url:"program_uid,omitempty" json:"program_uid,omitempty"`
BusinessName string `url:"business_name,omitempty" json:"business_name,omitempty"`
ExternalUID string `url:"external_uid,omitempty" json:"external_uid,omitempty"`
PoolUID string `url:"pool_uid,omitempty" json:"pool_uid,omitempty"`
Limit int `url:"limit,omitempty" json:"limit"`
Offset int `url:"offset,omitempty" json:"offset"`
Sort string `url:"sort,omitempty" json:"sort,omitempty"`
}
// CustomerCreateParams are the body params used when creating a new Customer
type CustomerCreateParams struct {
CustomerType string `json:"customer_type,omitempty"`
PrimaryCustomerUID string `json:"primary_customer_uid,omitempty"`
ExternalUID string `json:"external_uid,omitempty"`
Email string `json:"email,omitempty"`
Details *CustomerDetails `json:"details,omitempty"`
}
// CustomerUpdateParams are the body params used when updating a Customer
type CustomerUpdateParams struct {
Email string `json:"email,omitempty"`
Details *CustomerDetails `json:"details,omitempty"`
ExternalUID string `json:"external_uid,omitempty"`
}
// CustomerDeleteParams are the body params used when deleting/archiving a Customer
type CustomerDeleteParams struct {
ArchiveNote string `json:"archive_note,omitempty"`
}
// CustomerLockParams are the body params used when locking/unlocking a Customer
type CustomerLockParams struct {
LockNote string `json:"lock_note,omitempty"`
LockReason string `json:"lock_reason,omitempty"`
UnlockReason string `json:"unlock_reason,omitempty"`
}
// CustomerProfileResponseParams are the body params used when updating Customer Profile responses
type CustomerProfileResponseParams struct {
ProfileRequirementUID string `json:"profile_requirement_uid"`
ProfileResponse *internal.CustomerProfileResponseItem `json:"profile_response"`
}
// CustomerListResponse is an API response containing a list of Customers
type CustomerListResponse struct {
ListResponse
Data []*Customer `json:"data"`
}
// List retrieves a list of Customers filtered by the given parameters
func (c *customerService) List(ctx context.Context, params *CustomerListParams) (*CustomerListResponse, error) {
// Build CustomerListParams into query string params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodGet, "customers", 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 := &CustomerListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Create is used to initialize a new Customer with an email and external_uid
func (c *customerService) Create(ctx context.Context, params *CustomerCreateParams) (*Customer, error) {
if params.CustomerType == "secondary" && params.PrimaryCustomerUID == "" {
return nil, fmt.Errorf("primary_customer_uid is required for secondary customers")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodPost, "customers", 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 := &Customer{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Get retrieves overall status about a Customer as well as their total Asset Balances across all accounts
func (c *customerService) Get(ctx context.Context, uid string) (*Customer, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := c.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("customers/%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 := &Customer{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Update will submit or update a Customer's personally identifiable information (PII) after they are created
func (c *customerService) Update(ctx context.Context, uid string, params *CustomerUpdateParams) (*Customer, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("customers/%s", 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 := &Customer{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Delete will archive a Customer
func (c *customerService) Delete(ctx context.Context, uid string, params *CustomerDeleteParams) (*http.Response, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodDelete, fmt.Sprintf("customers/%s", uid), nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
return res, nil
}
// ConfirmPIIData is used to explicitly confirm a Customer's PII data is up-to-date in order to add additional products
func (c *customerService) ConfirmPIIData(ctx context.Context, uid string) (*Customer, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := c.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("customers/%s/identity_confirmation", 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 := &Customer{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Lock will freeze all activities relating to the Customer
func (c *customerService) Lock(ctx context.Context, uid string, params *CustomerLockParams) (*Customer, 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 := c.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("customers/%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 := &Customer{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Unlock will remove the Customer lock, returning their state to normal
func (c *customerService) Unlock(ctx context.Context, uid string, params *CustomerLockParams) (*Customer, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("customers/%s/unlock", 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 := &Customer{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// UpdateProfileResponses is used to submit a Customer's Profile Responses to Profile Requirements.
// For most cases, use CustomerProfileResponseItem.Response to submit a string response.
// For ordered list type responses, use CustomerProfileResponseItem.Num0/1/2
func (c *customerService) UpdateProfileResponses(ctx context.Context, uid string, params []*CustomerProfileResponseParams) (*Customer, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
for _, v := range params {
if v.ProfileRequirementUID == "" || (v.ProfileResponse.Response == "" && v.ProfileResponse.Num0 == "") {
return nil, fmt.Errorf("ProfileRequirementUID and ProfileResponse are required")
}
}
// Wrap profile response params in a `details` json object
var details = struct {
Details []*CustomerProfileResponseParams `json:"details"`
}{
Details: params,
}
bytesMessage, err := json.Marshal(&details)
if err != nil {
return nil, err
}
res, err := c.client.doRequest(ctx, http.MethodPut, fmt.Sprintf("customers/%s/update_profile_responses", 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 := &Customer{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}