-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathusers.go
440 lines (323 loc) · 14.9 KB
/
users.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package synapse
import (
"github.com/mitchellh/mapstructure"
)
/*********** GLOBAL VARIABLES ***********/
/********** TYPES **********/
type (
// User represents a single user object
User struct {
AuthKey string
UserID string `mapstructure:"_id"`
RefreshToken string `mapstructure:"refresh_token"`
Response interface{}
request Request
}
// Users represents a collection of user objects
Users struct {
Limit int64 `mapstructure:"limit"`
Page int64 `mapstructure:"page"`
PageCount int64 `mapstructure:"page_count"`
UsersCount int64 `mapstructure:"users_count"`
Users []User `mapstructure:"users"`
}
)
/********** METHODS **********/
func (u *User) do(method, url, data string, params []string) (map[string]interface{}, error) {
var response []byte
var err error
switch method {
case "GET":
response, err = u.request.Get(url, params)
case "POST":
response, err = u.request.Post(url, data, params)
case "PATCH":
response, err = u.request.Patch(url, data, params)
case "DELETE":
response, err = u.request.Delete(url)
}
switch err.(type) {
case *ActionPending:
return readStream(response), err
case *UnauthorizedAction:
res, authErr := u.Authenticate(`{ "refresh_token": "`+u.RefreshToken+`" }`, u.request.fingerprint, u.request.ipAddress)
if authErr != nil {
return res, authErr
}
u.request.authKey = u.AuthKey
return u.do(method, url, data, params)
// case *IncorrectValues:
// _, err := u.GetRefreshToken()
// if err != nil {
// return nil, err
// }
// _, err = u.Authenticate(`{ "refresh_token": "` + u.RefreshToken + `" }`)
// if err != nil {
// return nil, err
// }
// u.request.authKey = u.AuthKey
// return u.do(method, url, data, params)
}
return readStream(response), err
}
/********** AUTHENTICATION **********/
// Authenticate returns an oauth key and sets it to the user object
// If the refresh token is expired the API will automatically send a new one
// Capture refresh token every time
func (u *User) Authenticate(data, fingerprint, ipAddress string) (map[string]interface{}, error) {
log.info("========== AUTHENTICATE ==========")
url := buildURL(path["auth"], u.UserID)
u.request.updateRequest(u.request.clientID, u.request.clientSecret, fingerprint, ipAddress)
res, err := u.do("POST", url, data, nil)
if res["refresh_token"] != nil {
u.RefreshToken = res["refresh_token"].(string)
}
if res["oauth_key"] != nil {
u.AuthKey = res["oauth_key"].(string)
u.request.authKey = res["oauth_key"].(string)
}
return res, err
}
// GetRefreshToken performs a GET request and returns a new refresh token
func (u *User) GetRefreshToken() (map[string]interface{}, error) {
log.info("========== GET REFRESH TOKEN ==========")
url := buildURL(path["users"], u.UserID)
res, err := u.do("GET", url, "", nil)
if res["refresh_token"] != nil {
u.RefreshToken = res["refresh_token"].(string)
}
return res, err
}
// RegisterFingerprint submits a new fingerprint and triggers the MFA flow
func (u *User) RegisterFingerprint(fp string) (map[string]interface{}, error) {
log.info("========== REGISTER FINGERPRINT ==========")
url := buildURL(path["auth"], u.UserID)
u.request.fingerprint = fp
data := `{ "refresh_token": "` + u.RefreshToken + `" }`
res, err := u.do("POST", url, data, nil)
return res, err
}
// Select2FA sends the 2FA device selection to the API
func (u *User) Select2FA(device string) (map[string]interface{}, error) {
log.info("========== SELECT 2FA DEVICE ==========")
url := buildURL(path["auth"], u.UserID)
data := `{ "refresh_token": "` + u.RefreshToken + `", "phone_number": "` + device + `" }`
res, err := u.do("POST", url, data, nil)
return res, err
}
// SubmitMFA submits the access token and mfa answer
func (u *User) SubmitMFA(data string) (map[string]interface{}, error) {
log.info("========== SUBMIT MFA RESPONSE ==========")
url := buildURL(path["users"], u.UserID, path["nodes"])
return u.do("POST", url, data, nil)
}
// VerifyPIN sends the requested pin to the API to complete the 2FA process
func (u *User) VerifyPIN(pin string) (map[string]interface{}, error) {
log.info("========== VERIFY PIN ==========")
url := buildURL(path["auth"], u.UserID)
data := `{ "refresh_token": "` + u.RefreshToken + `", "validation_pin": "` + pin + `" }`
res, err := u.do("POST", url, data, nil)
return res, err
}
/********** NODE **********/
// GetNodes returns all of the nodes associated with a user
func (u *User) GetNodes(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET USER NODES ==========")
url := buildURL(path["users"], u.UserID, path["nodes"])
return u.do("GET", url, "", queryParams)
}
// GetNode returns a single node object
func (u *User) GetNode(nodeID string) (map[string]interface{}, error) {
log.info("========== GET NODE ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID)
res, err := u.do("GET", url, "", nil)
return res, err
}
// CreateNode creates a node depending on the type of node specified
func (u *User) CreateNode(data string, idempotencyKey ...string) (map[string]interface{}, error) {
log.info("========== CREATE NODE ==========")
url := buildURL(path["users"], u.UserID, path["nodes"])
return u.do("POST", url, data, idempotencyKey)
}
// UpdateNode updates a node
func (u *User) UpdateNode(nodeID, data string) (map[string]interface{}, error) {
log.info("========== UPDATE NODE ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID)
return u.do("PATCH", url, data, nil)
}
// DeleteNode deletes a node
func (u *User) DeleteNode(nodeID string) (map[string]interface{}, error) {
log.info("========== DELETE NODE ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID)
return u.do("DELETE", url, "", nil)
}
/********** NODE (OTHER) **********/
// VerifyMicroDeposit verifies micro-deposit amounts for a node
func (u *User) VerifyMicroDeposit(nodeID, data string) (map[string]interface{}, error) {
log.info("========== VERIFY MICRO-DEPOSITS ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID)
return u.do("PATCH", url, data, nil)
}
// ReinitiateMicroDeposits reinitiates micro-deposits for an ACH-US node with AC/RT
func (u *User) ReinitiateMicroDeposits(nodeID string) (map[string]interface{}, error) {
log.info("========== RE-INITIATE MICRO-DEPOSITS ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID) + "?resend_micro=YES"
data := `{"resend_micro":"YES"}`
return u.do("PATCH", url, data, nil)
}
// DEPRECATED
// ResetCardNode resets the debit card number, card cvv, and expiration date
func (u *User) ResetCardNode(nodeID string) (map[string]interface{}, error) {
log.info("========== RESET CARD ==========)")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID) + "?reset=YES"
return u.do("PATCH", url, "", nil)
}
// DEPRECATED, use User.ShipCard instead
// ShipCardNode ships a physical debit card out to the user
func (u *User) ShipCardNode(nodeID, data string) (map[string]interface{}, error) {
log.info("========== SHIP CARD ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID) + "?ship=YES"
return u.do("PATCH", url, data, nil)
}
// DEPRECATED
// GetApplePayToken generates tokenized info for Apple Wallet
func (u *User) GetApplePayToken(nodeID, data string) (map[string]interface{}, error) {
log.info("========== GET APPLE PAY TOKEN ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, "applepay")
return u.do("PATCH", url, data, nil)
}
/********** STATEMENT **********/
// GetStatements gets all of the user statements
func (u *User) GetStatements(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET USER STATEMENTS ==========")
url := buildURL(path["users"], u.UserID, path["statements"])
return u.do("GET", url, "", queryParams)
}
// GetNodeStatements gets all of the node statements
func (u *User) GetNodeStatements(nodeID string, queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET NODE STATEMENTS ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["statements"])
return u.do("GET", url, "", queryParams)
}
// DEPRECATED - statements are automatically generated
// CreateNodeStatements creates ad-hoc statements for the specified node
func (u *User) CreateNodeStatements(nodeID, data string, idempotencyKey ...string) (map[string]interface{}, error) {
log.info("========== CREATE AD-HOC STATEMENT ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["statements"])
return u.do("POST", url, data, idempotencyKey)
}
/********** SUBNET **********/
// GetSubnets gets all subnets associated with a user
func (u *User) GetSubnets(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET USER SUBNETS ==========")
url := buildURL(path["users"], u.UserID, path["subnets"])
return u.do("GET", url, "", queryParams)
}
// GetNodeSubnets gets all subnets associated with a node
func (u *User) GetNodeSubnets(nodeID string, queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET NODE SUBNETS ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["subnets"])
return u.do("GET", url, "", queryParams)
}
// GetSubnet gets a single subnet object
func (u *User) GetSubnet(nodeID, subnetID string) (map[string]interface{}, error) {
log.info("========== GET SUBNET ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["subnets"], subnetID)
return u.do("GET", url, "", nil)
}
// CreateSubnet creates a subnet object
func (u *User) CreateSubnet(nodeID, data string, idempotencyKey ...string) (map[string]interface{}, error) {
log.info("========== CREATE SUBNET ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["subnets"])
return u.do("POST", url, data, idempotencyKey)
}
// UpdateSubnet updates a subnet object
func (u *User) UpdateSubnet(nodeID, subnetID, data string) (map[string]interface{}, error) {
log.info("========== UPDATE SUBNET ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["subnets"], subnetID)
return u.do("PATCH", url, data, nil)
}
// ShipCard ships a physical debit card out to the user
func (u *User) ShipCard(nodeID, subnetID, data string) (map[string]interface{}, error) {
log.info("========== SHIP CARD SUBNET ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["subnets"], subnetID, "ship")
return u.do("PATCH", url, data, nil)
}
// PushToWallet Mobile Wallets are digital wallets held on mobile devices that transact with merchants by turning Native Card Primary Account Numbers (PANs) into digital tokens
func (u *User) PushToWallet(nodeID, subnetID, data string) (map[string]interface{}, error) {
log.info("========== PUSH TO MOBILE WALLET ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["subnets"], subnetID, "push")
return u.do("POST", url, data, nil)
}
// GetCardShipment gets card shipment details
func (u *User) GetCardShipment(nodeID, subnetID string) (map[string]interface{}, error) {
log.info("========== GET CARD SHIPMENT DETAILS ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["subnets"], subnetID, "ship")
return u.do("GET", url, "", nil)
}
/********** TRANSACTION **********/
// GetTransactions returns transactions associated with a user
func (u *User) GetTransactions(queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET USER TRANSACTIONS ==========")
url := buildURL(path["users"], u.UserID, path["transactions"])
return u.do("GET", url, "", queryParams)
}
// GetNodeTransactions returns transactions associated with a node
func (u *User) GetNodeTransactions(nodeID string, queryParams ...string) (map[string]interface{}, error) {
log.info("========== GET NODE TRANSACTIONS ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["transactions"])
return u.do("GET", url, "", queryParams)
}
// GetTransaction returns a specific transaction associated with a node
func (u *User) GetTransaction(nodeID, transactionID string) (map[string]interface{}, error) {
log.info("========== GET TRANSACTION ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["transactions"], transactionID)
return u.do("GET", url, "", nil)
}
// CreateTransaction creates a transaction for the specified node
func (u *User) CreateTransaction(nodeID, data string, idempotencyKey ...string) (map[string]interface{}, error) {
log.info("========== CREATE TRANSACTION ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["transactions"])
return u.do("POST", url, data, idempotencyKey)
}
// CancelTransaction deletes/cancels a transaction
func (u *User) CancelTransaction(nodeID, transactionID string) (map[string]interface{}, error) {
log.info("========== CANCEL TRANSACTION ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["transactions"], transactionID)
return u.do("DELETE", url, "", nil)
}
// CommentOnTransactionStatus adds comment to the transaction status
func (u *User) CommentOnTransactionStatus(nodeID, transactionID, comment string, idempotencyKey ...string) (map[string]interface{}, error) {
log.info("========== COMMENT ON TRANSACTION STATUS ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["transactions"], transactionID)
data := `{"comment":"` + comment + `"}`
return u.do("PATCH", url, data, idempotencyKey)
}
// DisputeTransaction disputes a transaction for a user
func (u *User) DisputeTransaction(nodeID, transactionID, data string) (map[string]interface{}, error) {
log.info("========== DISPUTE TRANSACTION ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID, path["transactions"], transactionID, "dispute")
return u.do("PATCH", url, data, nil)
}
// CreateDummyTransaction triggers external dummy transactions on internal accounts
func (u *User) CreateDummyTransaction(nodeID string, queryParams ...string) (map[string]interface{}, error) {
log.info("========== CREATE DUMMY TRANSACTION ==========")
url := buildURL(path["users"], u.UserID, path["nodes"], nodeID) + "/dummy-tran"
return u.do("GET", url, "", queryParams)
}
/********** USER **********/
// Update updates a single user and returns the updated user information
func (u *User) Update(data string) (*User, error) {
log.info("========== UPDATE USER ==========")
url := buildURL(path["users"], u.UserID)
res, err := u.do("PATCH", url, data, nil)
mapstructure.Decode(res, u)
u.Response = res
return u, err
}
// CreateUBO creates and uploads an Ultimate Beneficial Ownership (UBO) and REG GG form as a physical document under the Business’s base document
func (u *User) CreateUBO(data string) (map[string]interface{}, error) {
log.info("========== CREATE UBO ==========")
url := buildURL(path["users"], u.UserID, "ubo")
return u.do("PATCH", url, data, nil)
}