forked from njern/gonexmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
364 lines (305 loc) · 9.21 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
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 nexmo
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
// Verification wraps a client to be able to use local verify methods.
type Verification struct {
client *Client
}
// MarshalJSON returns a byte slice with the serialized JSON of the
// VerifyMessageRequest struct.
func (m *VerifyMessageRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
VerifyMessageRequest
}{
APIKey: m.apiKey,
APISecret: m.apiSecret,
VerifyMessageRequest: *m,
})
}
// VerifyMessageRequest is the request struct for initiating the verification process
// for a phone number.
type VerifyMessageRequest struct {
apiKey string
apiSecret string
Number string `json:"number"`
Brand string `json:"brand"`
SenderID string `json:"sender_id,omitempty"`
Country string `json:"country,omitempty"`
Language string `json:"lg,omitempty"`
CodeLength int `json:"code_length,omitempty"`
PINExpiry int `json:"pin_expiry,omitempty"`
NextEventWait int `json:"next_event_wait,omitempty"`
}
// VerifyMessageResponse is the struct for the response from the verify
// endpoint.
type VerifyMessageResponse struct {
Status ResponseCode `json:"status,string"`
RequestID string `json:"request_id"`
ErrorText string `json:"error_text"`
}
// Send makes the actual HTTP request to the endpoint and returns the
// response.
func (c *Verification) Send(m *VerifyMessageRequest) (*VerifyMessageResponse, error) {
if len(m.Number) == 0 {
return nil, errors.New("Invalid Number field specified")
}
if len(m.Brand) == 0 {
return nil, errors.New("Invalid Brand field specified")
}
var verifyMessageResponse *VerifyMessageResponse
if !c.client.useOauth {
m.apiKey = c.client.apiKey
m.apiSecret = c.client.apiSecret
}
var r *http.Request
buf, err := json.Marshal(m)
if err != nil {
return nil, errors.New("invalid message struct - can not convert to JSON")
}
b := bytes.NewBuffer(buf)
r, err = http.NewRequest("POST", apiRootv2+"/verify/json", b)
if err != nil {
return nil, err
}
r.Header.Add("Accept", "application/json")
r.Header.Add("Content-Type", "application/json")
resp, err := c.client.HTTPClient.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &verifyMessageResponse)
if err != nil {
return nil, err
}
return verifyMessageResponse, nil
}
// MarshalJSON implements the json.Marshaler interface
func (m *VerifyCheckRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
VerifyCheckRequest
}{
APIKey: m.apiKey,
APISecret: m.apiSecret,
VerifyCheckRequest: *m,
})
}
// A VerifyCheckRequest is sent to Nexmo
// when we want to verify a user has the
// phone number he says he does.
type VerifyCheckRequest struct {
apiKey string
apiSecret string
RequestID string `json:"request_id"`
Code string `json:"code"`
IPAddress string `json:"ip_address,omitempty"`
}
// A VerifyCheckResponse is received from Nexmo
// after verifying a user has the
// phone number he says he does.
type VerifyCheckResponse struct {
Status ResponseCode `json:"status,string"`
EventID string `json:"event_id"`
Price string `json:"price"`
Currency string `json:"currency"`
ErrorText string `json:"error_text"`
}
// Check (by sending a PIN to a user) whether a user can be contacted at his given phone number.
// https://developer.nexmo.com/api/verify#verify-check
func (c *Verification) Check(m *VerifyCheckRequest) (*VerifyCheckResponse, error) {
if len(m.RequestID) == 0 {
return nil, errors.New("Invalid RequestID field specified")
}
if len(m.Code) == 0 {
return nil, errors.New("Invalid Code field specified")
}
var verifyCheckResponse *VerifyCheckResponse
if !c.client.useOauth {
m.apiKey = c.client.apiKey
m.apiSecret = c.client.apiSecret
}
var r *http.Request
buf, err := json.Marshal(m)
if err != nil {
return nil, errors.New("invalid message struct - unable to convert to JSON")
}
b := bytes.NewBuffer(buf)
r, err = http.NewRequest("POST", apiRootv2+"/verify/check/json", b)
if err != nil {
return nil, err
}
r.Header.Add("Accept", "application/json")
r.Header.Add("Content-Type", "application/json")
resp, err := c.client.HTTPClient.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &verifyCheckResponse)
if err != nil {
return nil, err
}
return verifyCheckResponse, nil
}
// MarshalJSON implements the json.Marshaler interface
func (m *VerifySearchRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
VerifySearchRequest
}{
APIKey: m.apiKey,
APISecret: m.apiSecret,
VerifySearchRequest: *m,
})
}
// A VerifySearchRequest is sent to Nexmo
// when searching for the status of a Verify
// request.
type VerifySearchRequest struct {
apiKey string
apiSecret string
RequestID string `json:"request_id,omitempty"`
}
// A VerifySearchResponse is received from Nexmo in
// response to a VerifySearchRequest
type VerifySearchResponse struct {
RequestID string `json:"request_id"`
AccountID string `json:"account_id"`
Number string `json:"number"`
SenderID string `json:"sender_id"`
DateSubmitted string `json:"date_submitted"`
DateFinalized string `json:"date_finalized"`
FirstEventDate string `json:"first_event_date"`
LastEventDate string `json:"last_event_date"`
Status string `json:"status"`
Checks []struct {
DateReceived string `json:"date_received"`
Code string `json:"code"`
Status string `json:"status"`
IPAddress string `json:"ip_address,omitempty"`
} `json:"checks"`
Price string `json:"price"`
Currency string `json:"currency"`
ErrorText string `json:"error_text"`
}
// Search sends the verify search request to Nexmo.
// https://developer.nexmo.com/api/verify#verify-search
func (c *Verification) Search(m *VerifySearchRequest) (*VerifySearchResponse, error) {
var verifySearchResponse *VerifySearchResponse
if !c.client.useOauth {
m.apiKey = c.client.apiKey
m.apiSecret = c.client.apiSecret
}
var r *http.Request
buf, err := json.Marshal(m)
if err != nil {
return nil, errors.New("invalid message struct - unable to convert to JSON")
}
b := bytes.NewBuffer(buf)
r, err = http.NewRequest("POST", apiRootv2+"/verify/search/json", b)
if err != nil {
return nil, err
}
r.Header.Add("Accept", "application/json")
r.Header.Add("Content-Type", "application/json")
resp, err := c.client.HTTPClient.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &verifySearchResponse)
if err != nil {
return nil, err
}
return verifySearchResponse, nil
}
// MarshalJSON implements the json.Marshaler interface
func (m *VerifyControlRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
APIKey string `json:"api_key"`
APISecret string `json:"api_secret"`
VerifyControlRequest
}{
APIKey: m.apiKey,
APISecret: m.apiSecret,
VerifyControlRequest: *m,
})
}
// VerifyControlRequest is the request struct for control verificaion such as cancel verification request
// and trigger next verification process
type VerifyControlRequest struct {
apiKey string
apiSecret string
RequestID string `json:"request_id"`
Command string `json:"cmd"`
}
// VerifyControlResponse is received from Nexmo in
// response to a VerifyControlRequest
type VerifyControlResponse struct {
Status ResponseCode `json:"status,string"`
Command string `json:"command"`
ErrorText string `json:"error_text"`
}
// Control the progress of Verify Requests
// https://developer.nexmo.com/api/verify#verify-control
func (c *Verification) Control(m *VerifyControlRequest) (*VerifyControlResponse, error) {
if len(m.RequestID) == 0 {
return nil, errors.New("Invalid Request ID field specified")
}
if len(m.Command) == 0 {
return nil, errors.New("Invalid Command field specified")
}
var verifyControlResponse *VerifyControlResponse
if !c.client.useOauth {
m.apiKey = c.client.apiKey
m.apiSecret = c.client.apiSecret
}
var r *http.Request
buf, err := json.Marshal(m)
if err != nil {
return nil, errors.New("invalid message struct - unable to convert to JSON")
}
b := bytes.NewBuffer(buf)
r, err = http.NewRequest("POST", apiRootv2+"/verify/control/json", b)
if err != nil {
return nil, err
}
r.Header.Add("Accept", "application/json")
r.Header.Add("Content-Type", "application/json")
resp, err := c.client.HTTPClient.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &verifyControlResponse)
if err != nil {
return nil, err
}
return verifyControlResponse, nil
}