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.fasterxml.jackson.annotation.JsonValue;
025
026import java.util.Locale;
027
028/**
029 * Base request class for {@link VerifyRequest} and {@link Psd2Request}
030 * @since 5.5.0
031 */
032public class BaseRequest {
033
034    private final String number;
035    private Integer length;
036    private Locale locale;
037    private String country;
038    private Integer pinExpiry;
039    private Integer nextEventWait;
040
041    public BaseRequest(String number, Integer length, Locale locale) {
042        this.number = number;
043        this.length = length;
044        this.locale = locale;
045        country = null;
046        pinExpiry = null;
047        nextEventWait = null;
048    }
049
050    protected BaseRequest(String number, Integer length, Locale locale, String country, Integer pinExpiry, Integer nextEventWait) {
051        this.number = number;
052        this.length = length;
053        this.locale = locale;
054        this.country = country;
055        this.pinExpiry = pinExpiry;
056        this.nextEventWait = nextEventWait;
057    }
058
059    /**
060     * @return the recipient's phone number provided in the constructor, in
061     * <a href="https://en.wikipedia.org/wiki/E.164">E.164</a> format.
062     */
063    public String getNumber() {
064        return number;
065    }
066
067    /**
068     * @return the length of the verification code to be sent to the user, specified in some {@link VerifyRequest}
069     * constructors. {@code -1} indicates the default length will be used.
070     */
071    public Integer getLength() {
072        return length;
073    }
074
075    /**
076     *
077     * @param length the length of the verification code to be sent to the user. Options are either 4 or 6.
078     * @deprecated since 5.5.0 use {@link VerifyRequest.Builder} to create a 2FA verification request or
079     *             {@link Psd2Request.Builder} to create a PSD2 verification request
080     */
081    @Deprecated
082    public void setLength(Integer length) {
083        this.length = length;
084    }
085
086    /**
087     * @return the default locale used for verification. If this value is {@code null}, the locale will be determined
088     * from the country code included in {@code number}
089     */
090    public Locale getLocale() {
091        return locale;
092    }
093
094
095    /**
096     *
097     * @param locale Override the default locale used for verification. By default the locale is determined
098     *               from the country code included in {@code number}
099     * @deprecated since 5.5.0 use {@link VerifyRequest.Builder} to create a 2FA verification request or
100     *             {@link Psd2Request.Builder} to create a PSD2 verification request
101     */
102    public void setLocale(Locale locale) {
103        this.locale = locale;
104    }
105
106    /**
107     * @return the default locale used for verification in snake case.
108     * Ex: {@code en-gb}
109     * If this value is {@code null}, the locale will be determined
110     * from the country code included in {@code number}
111     */
112    public String getDashedLocale(){
113        if(locale != null){
114           return locale.toLanguageTag().toLowerCase();
115        }
116        else return null;
117    }
118
119    /**
120     * The country for the destination phone number.
121     *
122     * @return a String containing a 2-character country code
123     */
124    public String getCountry() {
125        return country;
126    }
127
128    /**
129     * The country for the destination phone number.
130     * <p>
131     * If you wish to used localised number formats or you are not sure if number is correctly formatted, set this to a
132     * two-character country code. For example, GB, US. Verify will work out the international phone number for you.
133     * </p>
134     *
135     * @param country  a String containing a 2-character country code
136     * @deprecated     since 5.5.0 use {@link VerifyRequest.Builder} to create a 2FA verification request or
137     *                 {@link Psd2Request.Builder} to create a PSD2 verification request
138     */
139    public void setCountry(String country) {
140        this.country = country;
141    }
142
143    /**
144     * @return PIN expiry time in seconds
145     */
146    public Integer getPinExpiry() {
147        return pinExpiry;
148    }
149
150    /**
151     * @param pinExpiry PIN expiry time in seconds.
152     * @deprecated since 5.5.0 use {@link VerifyRequest.Builder} to create a 2FA verification request or
153     *             {@link Psd2Request.Builder} to create a PSD2 verification request
154     */
155    public void setPinExpiry(Integer pinExpiry) {
156        this.pinExpiry = pinExpiry;
157    }
158
159    /**
160     * @return the wait time between attempts to deliver the PIN. An Integer between 600-900, or null.
161     */
162    public Integer getNextEventWait() {
163        return nextEventWait;
164    }
165
166    /**
167     * Set the wait time between attempts to deliver the PIN.
168     *
169     * @param nextEventWait An Integer value between 60 and 900 seconds, or null to use the default duration.
170     * @deprecated since 5.5.0 use {@link VerifyRequest.Builder} to create a 2FA verification request or
171     *             {@link Psd2Request.Builder} to create a PSD2 verification request
172     */
173    public void setNextEventWait(Integer nextEventWait) {
174        this.nextEventWait = nextEventWait;
175    }
176
177    @Override
178    public String toString() {
179        return  "number='" + number + '\'' +
180                ", length=" + length +
181                ", locale=" + locale +
182                ", country='" + country + '\'' +
183                ", pinExpiry=" + pinExpiry +
184                ", nextEventWait=" + nextEventWait;
185    }
186}