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.AbstractMethod; 025import com.nexmo.client.HttpWrapper; 026import com.nexmo.client.auth.SignatureAuthMethod; 027import com.nexmo.client.auth.TokenAuthMethod; 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.apache.http.HttpResponse; 031import org.apache.http.client.methods.RequestBuilder; 032import org.apache.http.impl.client.BasicResponseHandler; 033 034import java.io.IOException; 035import java.io.UnsupportedEncodingException; 036 037public class Psd2Method extends AbstractMethod<Psd2Request, VerifyResponse> { 038 private static final Log LOG = LogFactory.getLog(Psd2Method.class); 039 040 private static final String PATH = "/verify/psd2/json"; 041 private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{TokenAuthMethod.class}; 042 043 public Psd2Method(HttpWrapper wrapper) { 044 super(wrapper); 045 } 046 047 @Override 048 protected Class[] getAcceptableAuthMethods() { 049 return ALLOWED_AUTH_METHODS; 050 } 051 052 @Override 053 public RequestBuilder makeRequest(Psd2Request request) throws UnsupportedEncodingException { 054 RequestBuilder builder = RequestBuilder 055 .post(httpWrapper.getHttpConfig().getApiBaseUri() + PATH) 056 .addParameter("number", request.getNumber()) 057 .addParameter("payee", request.getPayee()) 058 .addParameter("amount", Double.toString(request.getAmount())); 059 060 //add additional parameters if they are present 061 if(request.getWorkflow() != null){ 062 builder.addParameter("workflow_id", Integer.toString(request.getWorkflow().getId())); 063 } 064 065 optionalParams(builder, "code_length", request.getLength()); 066 optionalParams(builder, "lg", request.getDashedLocale()); 067 optionalParams(builder, "country", request.getCountry()); 068 optionalParams(builder, "pin_expiry", request.getPinExpiry()); 069 optionalParams(builder, "next_event_wait", request.getNextEventWait()); 070 071 return builder; 072 073 } 074 075 @Override 076 public VerifyResponse parseResponse(HttpResponse response) throws IOException { 077 return VerifyResponse.fromJson(new BasicResponseHandler().handleResponse(response)); 078 } 079 080 private RequestBuilder optionalParams(RequestBuilder builder, String paramName, Object value){ 081 if (value != null) { 082 builder.addParameter(paramName, value + ""); 083 } 084 return null; 085 } 086}