001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.verify;
023
024import com.nexmo.client.*;
025
026import java.util.Locale;
027
028/**
029 * A client for talking to the Nexmo Verify API. The standard way to obtain an instance of this class is to use {@link
030 * NexmoClient#getVerifyClient()}.
031 * <p>
032 * Send a verification request with a call to {@link #verify}, confirm the code entered by the user with {@link #check},
033 * and search in-progress or completed verification requests with {@link #search}
034 * <p>
035 * More information on method parameters can be found at Nexmo website:
036 * <a href="https://docs.nexmo.com/verify">https://docs.nexmo.com/verify</a>
037 */
038public class VerifyClient extends AbstractClient {
039
040    private CheckEndpoint check;
041    private VerifyEndpoint verify;
042    private SearchEndpoint search;
043    private ControlEndpoint control;
044    private Psd2Endpoint psd2;
045
046    /**
047     * Constructor.
048     *
049     * @param httpWrapper (required) shared HTTP wrapper object used for making REST calls.
050     */
051    public VerifyClient(HttpWrapper httpWrapper) {
052        super(httpWrapper);
053
054        this.check = new CheckEndpoint(httpWrapper);
055        this.search = new SearchEndpoint(httpWrapper);
056        this.verify = new VerifyEndpoint(httpWrapper);
057        this.control = new ControlEndpoint(httpWrapper);
058        this.psd2 = new Psd2Endpoint(httpWrapper);
059    }
060
061    /**
062     * Send a verification request to a phone number.
063     *
064     * @param number (required) The recipient's phone number in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a>
065     *               format.
066     * @param brand  (required) The name of the company or app to be verified for. Must not be longer than 18
067     *               characters.
068     *
069     * @return a VerifyResponse representing the response received from the Verify API call.
070     *
071     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
072     * @throws NexmoResponseParseException if the response from the API could not be parsed.
073     */
074    public VerifyResponse verify(final String number, final String brand) throws NexmoResponseParseException, NexmoClientException {
075        return this.verify.verify(number, brand);
076    }
077
078    /**
079     * Send a verification request to a phone number with a pin verification workflow
080     *
081     * @param number (required) The recipient's phone number in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a>
082     *               format.
083     * @param brand  (required) The name of the company or app to be verified for. Must not be longer than 18
084     *               characters.
085     * @param workflow <a href="https://developer.nexmo.com/verify/guides/workflows-and-events">workflow</a>
086     *                 to use for sending verification pin
087     *
088     * @return a VerifyResponse representing the response received from the Verify API call.
089     *
090     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
091     * @throws NexmoResponseParseException if the response from the API could not be parsed.
092     * @since 5.5.0
093     */
094    public VerifyResponse verify(final String number, final String brand, VerifyRequest.Workflow workflow)
095            throws NexmoResponseParseException, NexmoClientException {
096        return this.verify.verify(number, brand, workflow);
097    }
098
099
100
101    /**
102     * Send a verification request to a phone number.
103     *
104     * @param number (required) The recipient's phone number in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a>
105     *               format.
106     * @param brand  (required) The name of the company or app to be verified for. Must not be longer than 18
107     *               characters.
108     * @param from   (optional The Nexmo number to use as the sender for the verification SMS message and calls, in
109     *               <a href="https://en.wikipedia.org/wiki/E.164">E.164</a> format.
110     *
111     * @return a VerifyResponse representing the response received from the Verify API call.
112     *
113     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
114     * @throws NexmoResponseParseException if the response from the API could not be parsed.
115     */
116    public VerifyResponse verify(final String number,
117                                 final String brand,
118                                 final String from) throws NexmoClientException, NexmoResponseParseException {
119        return this.verify.verify(number, brand, from);
120    }
121
122    /**
123     * Send a verification request to a phone number.
124     *
125     * @param number (required) The recipient's phone number in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a>
126     *               format.
127     * @param brand  (required) The name of the company or app to be verified for. Must not be longer than 18
128     *               characters.
129     * @param from   (optional The Nexmo number to use as the sender for the verification SMS message and calls, in
130     *               <a href="https://en.wikipedia.org/wiki/E.164">E.164</a> format.
131     * @param length (optional) The length of the verification code to be sent to the user. Must be either 4 or 6. Use
132     *               -1 to use the default value.
133     * @param locale (optional) Override the default locale used for verification. By default the locale is determined
134     *               from the country code included in {@code number}
135     *
136     * @return a VerifyResponse representing the response received from the Verify API call.
137     *
138     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
139     * @throws NexmoResponseParseException if the response from the API could not be parsed.
140     */
141    public VerifyResponse verify(final String number,
142                                 final String brand,
143                                 final String from,
144                                 final int length,
145                                 final Locale locale) throws NexmoClientException, NexmoResponseParseException {
146        return this.verify.verify(number, brand, from, length, locale);
147    }
148
149    /**
150     * Send a verification request to a phone number.
151     *
152     * @param number (required) The recipient's phone number in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a>
153     *               format.
154     * @param brand  (required) The name of the company or app to be verified for. Must not be longer than 18
155     *               characters.
156     * @param from   (optional The Nexmo number to use as the sender for the verification SMS message and calls, in
157     *               <a href="https://en.wikipedia.org/wiki/E.164">E.164</a> format.
158     * @param length (optional) The length of the verification code to be sent to the user. Must be either 4 or 6. Use
159     *               -1 to use the default value.
160     * @param locale (optional) Override the default locale used for verification. By default the locale is determined
161     *               from the country code included in {@code number}
162     * @param type   (optional) If provided, restrict the verification to the specified network type. Contact
163     *               support@nexmo.com to enable this feature.
164     *
165     * @return a VerifyResponse representing the response received from the Verify API call.
166     *
167     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
168     * @throws NexmoResponseParseException if the response from the API could not be parsed.
169     */
170    public VerifyResponse verify(final String number,
171                                 final String brand,
172                                 final String from,
173                                 final int length,
174                                 final Locale locale,
175                                 final VerifyRequest.LineType type) throws NexmoClientException {
176        return this.verify.verify(number, brand, from, length, locale, type);
177    }
178
179    /**
180     * Send a verification request to a phone number.
181     *
182     * @param request validation request for the 2FA verification.
183     * @return a VerifyResponse representing the response received from the Verify API call.
184     *
185     * @throws NexmoClientException if there was a problem with the Nexmo request or response objects.
186     * @throws NexmoResponseParseException if the response from the API could not be parsed.
187     *
188     */
189    public VerifyResponse verify(VerifyRequest request) throws NexmoClientException, NexmoResponseParseException {
190        return this.verify.verify(request);
191    }
192
193    /**
194     * Validate a code provided by a user in response to a call from {@link #verify}.
195     *
196     * @param requestId (required) The requestId returned by the {@code verify} call.
197     * @param code      (required) The code entered by the user.
198     *
199     * @return a CheckResponse representing the response received from the API call.
200     *
201     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
202     * @throws NexmoResponseParseException if the response from the API could not be parsed.
203     */
204    public CheckResponse check(final String requestId, final String code) throws NexmoClientException, NexmoResponseParseException {
205        return this.check.check(requestId, code);
206    }
207
208    /**
209     * Validate a code provided by a user in response to a call from {@link #verify}.
210     *
211     * @param requestId (required) The requestId returned by the {@code verify} call.
212     * @param code      (required) The code entered by the user.
213     * @param ipAddress (optional) The IP address obtained from the HTTP request made when the user entered their code.
214     *
215     * @return a CheckResponse representing the response received from the API call.
216     *
217     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
218     * @throws NexmoResponseParseException if the response from the API could not be parsed.
219     */
220    public CheckResponse check(final String requestId,
221                               final String code,
222                               final String ipAddress) throws NexmoClientException, NexmoResponseParseException {
223        return this.check.check(requestId, code, ipAddress);
224    }
225
226    /**
227     * Search for a previous verification request.
228     *
229     * @param requestId The requestId of a single Verify request to be looked up.
230     *
231     * @return A SearchVerifyResponse containing the details of the Verify request that was looked up, or {@code null}
232     * if no record was found.
233     *
234     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
235     * @throws NexmoResponseParseException if the response from the API could not be parsed.
236     */
237    public SearchVerifyResponse search(String requestId) throws NexmoClientException, NexmoResponseParseException {
238        return this.search.search(requestId);
239    }
240
241    /**
242     * Search for a previous verification request.
243     *
244     * @param requestIds The requestIds of Verify requests to be looked up.
245     *
246     * @return An array SearchVerifyResponse for each record that was found.
247     *
248     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
249     * @throws NexmoResponseParseException if the response from the API could not be parsed.
250     */
251    public SearchVerifyResponse search(String... requestIds) throws NexmoClientException, NexmoResponseParseException {
252        return this.search.search(requestIds);
253    }
254
255    /**
256     * Advance a current verification request to the next stage in the process.
257     *
258     * @param requestId The requestId of the ongoing verification request.
259     *
260     * @return A {@link ControlResponse} representing the response from the API.
261     *
262     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
263     * @throws NexmoResponseParseException if the response from the API could not be parsed.
264     */
265    public ControlResponse advanceVerification(String requestId) throws NexmoClientException, NexmoResponseParseException {
266        return this.control.execute(new ControlRequest(requestId, VerifyControlCommand.TRIGGER_NEXT_EVENT));
267    }
268
269    /**
270     * Cancel a current verification request.
271     *
272     * @param requestId The requestId of the ongoing verification request.
273     *
274     * @return A {@link ControlResponse} representing the response from the API.
275     *
276     * @throws NexmoClientException        if there was a problem with the Nexmo request or response objects.
277     * @throws NexmoResponseParseException if the response from the API could not be parsed.
278     */
279    public ControlResponse cancelVerification(String requestId) throws NexmoClientException, NexmoResponseParseException {
280        return this.control.execute(new ControlRequest(requestId, VerifyControlCommand.CANCEL));
281    }
282
283    /**
284     * Send a PSD2 compliant payment token to a user for payment authorization
285     *
286     * @param number Telephone number to verify, in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a> format
287     * @param amount payment amount
288     * @param payee name of the person the payment is for. Name will be included in the message
289     *
290     * @return A {@link VerifyResponse} representing the response from the API.
291     *
292     * @throws NexmoClientException          if there was a problem with the Nexmo request or response objects.
293     * @throws NexmoResponseParseException   if the response from the API could not be parsed.
294     *
295     * @since 5.5.0
296     */
297    public VerifyResponse psd2Verify(String number, Double amount, String payee) throws NexmoClientException, NexmoResponseParseException {
298        return this.psd2.psd2Verify(number, amount, payee);
299    }
300
301    /**
302     * Send a PSD2 compliant payment token to a user for payment authorization with a pin verification workflow
303     *
304     * @param number   telephone number to verify, in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a> format
305     * @param amount   payment amount
306     * @param payee    name of the person the payment is for. Name will be included in the message
307     * @param workflow <a href="https://developer.nexmo.com/verify/guides/workflows-and-events">workflow</a>
308     *                 to use for sending verification pin
309     *
310     * @return A {@link VerifyResponse} representing the response from the API.
311     *
312     * @throws NexmoClientException          if there was a problem with the Nexmo request or response objects.
313     * @throws NexmoResponseParseException   if the response from the API could not be parsed.
314     *
315     * @since 5.5.0
316     */
317    public VerifyResponse psd2Verify(String number, Double amount, String payee, Psd2Request.Workflow workflow)
318            throws NexmoClientException, NexmoResponseParseException {
319        return this.psd2.psd2Verify(number, amount, payee, workflow);
320    }
321
322    /**
323     * Send a PSD2 verification request to a phone number with optional parameters
324     *
325     * @param psd2Request request to to send PSD2 verification to a phone.
326     *
327     * @return A VerifyResponse representing the response from the API.
328     *
329     * @throws NexmoClientException          if there was a problem with the Nexmo request or response objects.
330     * @throws NexmoResponseParseException   if the response from the API could not be parsed.
331     *
332     * @since 5.5.0
333     */
334    public VerifyResponse psd2Verify(Psd2Request psd2Request) throws NexmoClientException, NexmoResponseParseException {
335        return this.psd2.psd2Verify(psd2Request);
336    }
337
338
339}