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 * Describes a PSD2 verify request when passed to {@link com.nexmo.client.verify.Psd2Endpoint}
030 * @since 5.5.0
031 */
032public class Psd2Request extends BaseRequest {
033
034    private Double amount;
035    private String payee;
036    private Workflow workflow;
037
038
039    private Psd2Request(Builder builder){
040        super(builder.number,builder.length, builder.locale, builder.country, builder.pinExpiry, builder.nextEventWait);
041        amount = builder.amount;
042        payee = builder.payee;
043        workflow = builder.workflow;
044    }
045
046    /**
047     * @return The decimal amount of the payment to be confirmed, in Euros
048     */
049    public Double getAmount() {
050        return amount;
051    }
052
053    /**
054     * @return An alphanumeric string to indicate to the user the name of the recipient that they are confirming a payment to.
055     */
056    public String getPayee() {
057        return this.payee;
058    }
059
060    /**
061     * @return The predefined sequence of SMS and TTS (Text To Speech) actions to use in order to convey the PIN to your
062     * user.
063     */
064    public Workflow getWorkflow() {
065        return workflow;
066    }
067
068    /**
069     * Enumeration representing different verification workflows.
070     * <p>
071     * See: https://developer.nexmo.com/verify/guides/workflows-and-events for more details.
072     */
073    public enum Workflow {
074        /**
075         * The default workflow.
076         */
077        SMS_TTS_TTS(1),
078        SMS_SMS_TTS(2),
079        TTS_TTS(3),
080        SMS_SMS(4),
081        SMS_TTS(5),
082        SMS(6),
083        TTS(7);
084
085        private final int id;
086
087        Workflow(int id) {
088            this.id = id;
089        }
090
091        @JsonValue
092        public int getId() {
093            return id;
094        }
095    }
096
097    /**
098     * @return A new Builder to start building.
099     */
100    public static Builder builder(String number, Double amount, String payee) {
101        return new Builder(number, amount, payee);
102    }
103
104    @Override
105    public String toString() {
106        return "Psd2Request{" +
107                super.toString() +
108                ", amount=" + amount +
109                ", payee='" + payee + '\'' +
110                ", workflow=" + workflow +
111                '}';
112    }
113
114    public static class Builder {
115
116        private Double amount;
117        private String payee;
118        private Workflow workflow;
119        private String number;
120        private Locale locale;
121        private Integer length;
122        private Integer pinExpiry;
123        private Integer nextEventWait;
124        private String country;
125
126        /**
127         * @param number The recipient's phone number in <a href="https://en.wikipedia.org/wiki/E.164">E.164</a>
128         *               format.
129         * @param amount The decimal amount of the payment to be confirmed, in Euros.
130         * @param payee  An alphanumeric string to indicate to the user the name of the recipient that they
131         *               are confirming a payment to.
132         */
133        public Builder(String number, Double amount, String payee){
134            this.number = number;
135            this.payee = payee;
136            this.amount = amount;
137        }
138
139        /**
140         * @param workflow Selects the predefined sequence of SMS and TTS (Text To Speech) actions to use
141         *                 in order to convey the PIN to your user. For example, an id of 1 identifies the
142         *                 workflow SMS - TTS - TTS. For a list of all workflows and their associated ids,
143         *                 please visit the
144         *                 <a href="https://developer.nexmo.com/verify/guides/workflows-and-events">developer portal.</a>
145         * @return {@link Builder}
146         */
147        public Builder workflow(Workflow workflow){
148            this.workflow = workflow;
149            return this;
150        }
151
152        /**
153         * @param locale (optional) Override the default locale used for verification. By default the locale is determined
154         *        from the country code included in {@code number}
155         * @return {@link Builder}
156         */
157        public Builder locale(Locale locale) {
158            this.locale = locale;
159            return this;
160        }
161
162        /**
163         * @param length (optional) The length of the verification code to be sent to the user. Must be either 4 or 6. Use
164         *               -1 to use the default value.
165         * @return {@link Builder}
166         */
167        public Builder length(Integer length) {
168            this.length = length;
169            return this;
170        }
171
172        /**
173         * @param pinExpiry (optional) the PIN validity time from generation, in seconds. Default is 300 seconds
174         * @return {@link Builder}
175         */
176        public Builder pinExpiry(Integer pinExpiry) {
177            this.pinExpiry = pinExpiry;
178            return this;
179        }
180
181        /**
182         * @param nextEventWait (optional) the wait time between attempts to deliver the PIN. A number between 600-900.
183         * @return {@link Builder}
184         */
185        public Builder nextEventWait(Integer nextEventWait) {
186            this.nextEventWait = nextEventWait;
187            return this;
188        }
189
190        /**
191         * The country for the destination phone number.
192         * <p>
193         * If you wish to used localised number formats or you are not sure if number is correctly formatted, set this to a
194         * two-character country code. For example, GB, US. Verify will work out the international phone number for you.
195         * </p>
196         *
197         * @param country  a String containing a 2-character country code
198         * @return {@link Builder}
199         */
200        public Builder country(String country) {
201            this.country = country;
202            return this;
203        }
204
205        public Psd2Request build() {
206            return new Psd2Request(this);
207        }
208
209    }
210}