This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
verify.go
292 lines (237 loc) · 8.27 KB
/
verify.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
package vonage
import (
"context"
"encoding/json"
"io/ioutil"
"github.com/antihax/optional"
"github.com/vonage/vonage-go-sdk/internal/verify"
)
// VerifyClient for working with the Verify API
type VerifyClient struct {
Config *verify.Configuration
apiKey string
apiSecret string
}
// NewVerifyClient Creates a new Verify Client, supplying an Auth to work with
func NewVerifyClient(Auth Auth) *VerifyClient {
client := new(VerifyClient)
creds := Auth.GetCreds()
client.apiKey = creds[0]
client.apiSecret = creds[1]
client.Config = verify.NewConfiguration()
client.Config.UserAgent = GetUserAgent()
return client
}
// VerifyOpts holds all the optional arguments for the verify request function
type VerifyOpts struct {
Country string
SenderID string
CodeLength int32
Lg string
PinExpiry int32
NextEventWait int32
WorkflowID int32
}
type VerifyRequestResponse struct {
// The unique ID of the Verify request. You need this `request_id` for the Verify check.
RequestId string
Status string
}
type VerifyErrorResponse struct {
RequestId string `json:"request_id"`
Status string `json:"status"`
ErrorText string `json:"error_text,omitempty"`
}
// Request a number is verified for ownership
func (client *VerifyClient) Request(number string, brand string, opts VerifyOpts) (VerifyRequestResponse, VerifyErrorResponse, error) {
// create the client
verifyClient := verify.NewAPIClient(client.Config)
// set up and then parse the options
verifyOpts := verify.VerifyRequestOpts{}
if opts.CodeLength != 0 {
verifyOpts.CodeLength = optional.NewInt32(opts.CodeLength)
}
if opts.Lg != "" {
verifyOpts.Lg = optional.NewString(opts.Lg)
}
if opts.WorkflowID != 0 {
verifyOpts.WorkflowId = optional.NewInt32(opts.WorkflowID)
}
if opts.SenderID != "" {
verifyOpts.SenderId = optional.NewString(opts.SenderID)
}
ctx := context.Background()
result, resp, err := verifyClient.DefaultApi.VerifyRequest(ctx, "json", client.apiKey, client.apiSecret, number, brand, &verifyOpts)
// catch HTTP errors
if err != nil {
return VerifyRequestResponse{}, VerifyErrorResponse{}, err
}
// non-zero statuses are also errors
if result.Status != "0" {
data, _ := ioutil.ReadAll(resp.Body)
var errResp VerifyErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return VerifyRequestResponse(result), errResp, nil
}
}
return VerifyRequestResponse(result), VerifyErrorResponse{}, nil
}
type VerifyCheckResponse struct {
RequestId string
EventId string
Status string
Price string
Currency string
EstimatedPriceMessagesSent string
}
// Check the user-supplied code for this request ID
func (client *VerifyClient) Check(requestID string, code string) (VerifyCheckResponse, VerifyErrorResponse, error) {
// create the client
verifyClient := verify.NewAPIClient(client.Config)
// set up and then parse the options
verifyOpts := verify.VerifyCheckOpts{}
ctx := context.Background()
result, resp, err := verifyClient.DefaultApi.VerifyCheck(ctx, "json", client.apiKey, client.apiSecret, requestID, code, &verifyOpts)
// catch HTTP errors
if err != nil {
return VerifyCheckResponse{}, VerifyErrorResponse{}, err
}
// non-zero statuses are also errors
if result.Status != "0" {
data, _ := ioutil.ReadAll(resp.Body)
var errResp VerifyErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return VerifyCheckResponse(result), errResp, nil
}
}
return VerifyCheckResponse(result), VerifyErrorResponse{}, nil
}
type VerifySearchResponse struct {
RequestId string
AccountId string
Status string
Number string
Price string
Currency string
SenderId string
DateSubmitted string
DateFinalized string
FirstEventDate string
LastEventDate string
Checks []verify.SearchResponseChecks
Events []verify.SearchResponseEvents
EstimatedPriceMessagesSent string
}
// Search for an earlier request by id
func (client *VerifyClient) Search(requestID string) (VerifySearchResponse, VerifyErrorResponse, error) {
// create the client
verifyClient := verify.NewAPIClient(client.Config)
// set up and then parse the options
verifyOpts := verify.VerifySearchOpts{}
verifyOpts.RequestId = optional.NewString(requestID)
ctx := context.Background()
result, resp, err := verifyClient.DefaultApi.VerifySearch(ctx, "json", client.apiKey, client.apiSecret, &verifyOpts)
// catch HTTP errors
if err != nil {
return VerifySearchResponse{}, VerifyErrorResponse{}, err
}
// search failed if we didn't get a request ID
if result.RequestId == "" {
data, _ := ioutil.ReadAll(resp.Body)
var errResp VerifyErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return VerifySearchResponse{}, errResp, nil
}
}
return VerifySearchResponse(result), VerifyErrorResponse{}, nil
}
type VerifyControlResponse struct {
Status string
Command string
}
// Cancel an in-progress request (check API docs for when this is possible)
func (client *VerifyClient) Cancel(requestID string) (VerifyControlResponse, VerifyErrorResponse, error) {
// create the client
verifyClient := verify.NewAPIClient(client.Config)
ctx := context.Background()
result, resp, err := verifyClient.DefaultApi.VerifyControl(ctx, "json", client.apiKey, client.apiSecret, requestID, "cancel")
// catch HTTP errors
if err != nil {
return VerifyControlResponse{}, VerifyErrorResponse{}, err
}
// search statuses are strings
if result.Status != "0" {
data, _ := ioutil.ReadAll(resp.Body)
var errResp VerifyErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return VerifyControlResponse(result), errResp, nil
}
}
return VerifyControlResponse(result), VerifyErrorResponse{}, nil
}
// TriggerNextEvent moves on to the next event in the workflow
func (client *VerifyClient) TriggerNextEvent(requestID string) (VerifyControlResponse, VerifyErrorResponse, error) {
// create the client
verifyClient := verify.NewAPIClient(client.Config)
ctx := context.Background()
result, resp, err := verifyClient.DefaultApi.VerifyControl(ctx, "json", client.apiKey, client.apiSecret, requestID, "trigger_next_event")
// catch HTTP errors
if err != nil {
return VerifyControlResponse{}, VerifyErrorResponse{}, err
}
// search statuses are strings
if result.Status != "0" {
data, _ := ioutil.ReadAll(resp.Body)
var errResp VerifyErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return VerifyControlResponse(result), errResp, nil
}
}
return VerifyControlResponse(result), VerifyErrorResponse{}, nil
}
// VerifyPsd2Opts holds all the optional arguments for the verify psd2 function
type VerifyPsd2Opts struct {
Country string
CodeLength int32
Lg string
PinExpiry int32
NextEventWait int32
WorkflowID int32
}
// Psd2 requests a user confirm a payment with amount and payee
func (client *VerifyClient) Psd2(number string, payee string, amount float64, opts VerifyPsd2Opts) (VerifyRequestResponse, VerifyErrorResponse, error) {
// create the client
verifyClient := verify.NewAPIClient(client.Config)
// set up and then parse the options
verifyOpts := verify.VerifyRequestWithPSD2Opts{}
if opts.CodeLength != 0 {
verifyOpts.CodeLength = optional.NewInt32(opts.CodeLength)
}
if opts.Lg != "" {
verifyOpts.Lg = optional.NewString(opts.Lg)
}
if opts.WorkflowID != 0 {
verifyOpts.WorkflowId = optional.NewInt32(opts.WorkflowID)
}
ctx := context.Background()
result, resp, err := verifyClient.DefaultApi.VerifyRequestWithPSD2(ctx, "json", client.apiKey, client.apiSecret, number, payee, float32(amount), &verifyOpts)
// catch HTTP errors
if err != nil {
return VerifyRequestResponse{}, VerifyErrorResponse{}, err
}
// non-zero statuses are also errors
if result.Status != "0" {
data, _ := ioutil.ReadAll(resp.Body)
var errResp VerifyErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return VerifyRequestResponse(result), errResp, nil
}
}
return VerifyRequestResponse(result), VerifyErrorResponse{}, nil
}