Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ public String getHouseNumberOrName() {
}

public void setHouseNumberOrName(final String houseNumberOrName) {
// Adyen needs houseNumberOrName to not be blank in US https://docs.adyen.com/developers/api-reference/common-api/address
this.houseNumberOrName = houseNumberOrName != null ? houseNumberOrName : "";
this.houseNumberOrName = houseNumberOrName;
}

public String getCity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,52 @@ private void setShopperInteraction(final PaymentInfo paymentInfo, final PaymentR
}

private void setBillingAddress(final PaymentInfo paymentInfo, final PaymentRequest paymentRequest) {
final Address address = new Address();
address.setStreet(paymentInfo.getStreet());
address.setHouseNumberOrName(paymentInfo.getHouseNumberOrName());
address.setCity(paymentInfo.getCity());
address.setPostalCode(paymentInfo.getPostalCode());
address.setStateOrProvince(paymentInfo.getStateOrProvince());

final String street = paymentInfo.getStreet();
final String houseNumberOrName = paymentInfo.getHouseNumberOrName();
final String city = paymentInfo.getCity();
final String postalCode = paymentInfo.getPostalCode();
final String stateOrProvince = paymentInfo.getStateOrProvince();
final String country = paymentInfo.getCountry();
final String adjustedCountry;
if ("UK".equalsIgnoreCase(paymentInfo.getCountry())) {
if ("UK".equalsIgnoreCase(country)) {
// Passing UK will result in: validation 134 Billing address problem (Country UK invalid)
adjustedCountry = "GB";
} else if ("QC".equalsIgnoreCase(paymentInfo.getCountry())) {
} else if ("QC".equalsIgnoreCase(country)) {
// Passing QC will result in: validation 134 Billing address problem (Country QC invalid)
adjustedCountry = "CA";
} else {
adjustedCountry = paymentInfo.getCountry();
adjustedCountry = country;
}
address.setCountry(adjustedCountry);

// Required by Adyen: house number and city
// house number can be passed in the street or in the houseNumberOrName field
if ((address.getStreet() != null || address.getHouseNumberOrName() != null) && address.getCity() != null) {
// Adyen validation docs:
// - https://docs.adyen.com/api-reference/payments-api/paymentrequest/
// - https://docs.adyen.com/developers/api-reference/common-api/address
// TL;DR: the billing address per se is optional, but when sending it, the country is always mandatory,
// while the remaining fields must either be sent all or none (although in our experience the ZIP always has
// been treated as optional by Adyen: maybe it is mandatory only for US and CA)
// TODO? Introduce some data validation
final boolean stateProvinceValid = !("US".equals(adjustedCountry) || "CA".equals(adjustedCountry))
|| stateOrProvince != null;

final boolean addressComplete = street != null && houseNumberOrName != null && city != null
&& postalCode != null && stateProvinceValid;

final boolean addressEmpty = street == null && houseNumberOrName == null && city == null && postalCode == null
&& stateOrProvince == null;

final boolean addressValid = adjustedCountry != null && (addressComplete || addressEmpty);

// TODO: validate the right format for fields

if (addressValid) {
final Address address = new Address();
address.setStreet(street);
address.setHouseNumberOrName(houseNumberOrName);
address.setCity(city);
address.setPostalCode(postalCode);
address.setStateOrProvince(stateOrProvince);
address.setCountry(adjustedCountry);

paymentRequest.setBillingAddress(address);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2014-2019 Groupon, Inc
* Copyright 2014-2019 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package org.killbill.billing.plugin.adyen.client.payment.converter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.killbill.adyen.payment.PaymentRequest;
import org.killbill.billing.plugin.adyen.client.model.PaymentInfo;
import org.killbill.billing.plugin.adyen.client.model.paymentinfo.Card;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import static org.testng.Assert.*;

public class PaymentInfoConverterTest {

@DataProvider
public static Object[][] paymentInfoProvider() {
// Testing only address values, by now
// Map initialization in Java is even more verbose...
return new String[][]{
{"street:address1", "houseNumberOrName:address2", "city:city", "postalCode:00000", "stateOrProvince:SOP", "country:CO", "Y"}, // All fields present
{"country:CO", "Y"}, // Only Country

{"street:address1", "houseNumberOrName:address2", "city:city", "postalCode:00000", "stateOrProvince:SOP", "N"}, // Missing Country

{"houseNumberOrName:address2", "city:city", "postalCode:00000", "stateOrProvince:SOP", "country:CO", "N"}, // Missing address field
{"street:address1", "city:city", "postalCode:00000", "stateOrProvince:SOP", "country:CO", "N"},
{"street:address1", "houseNumberOrName:address2", "postalCode:00000", "stateOrProvince:SOP", "country:CO", "N"},
{"street:address1", "houseNumberOrName:address2", "city:city", "stateOrProvince:SOP", "country:CO", "N"},

{"street:address1", "houseNumberOrName:address2", "city:city", "postalCode:00000", "country:CO", "Y"}, // No State for Country other than US/CA
{"street:address1", "houseNumberOrName:address2", "city:city", "postalCode:00000", "country:US", "N"}, // Missing State for US/CA
{"street:address1", "houseNumberOrName:address2", "city:city", "postalCode:00000", "country:CA", "N"},

// TODO? New test cases if/when data validation is introduced
};
}

@Test(dataProvider = "paymentInfoProvider")
public void testConvertPaymentInfoToPaymentRequest(final String[] paramsValue)
throws NoSuchFieldException, IllegalAccessException {

final Card pi = buildPaymentInfo(paramsValue);
final boolean expectedAddrPresent = "Y".equals(paramsValue[paramsValue.length - 1]);

final PaymentRequest pr = new PaymentInfoConverter<Card>().convertPaymentInfoToPaymentRequest(pi);
final boolean addrPresent = pr.getBillingAddress() != null;

assertEquals(addrPresent, expectedAddrPresent);

}

private static Card buildPaymentInfo(final String[] values)
throws IllegalAccessException, NoSuchFieldException {

final Card pi = new Card();

for (int i = 0; i < values.length - 1; i++) {
final String[] pair = values[i].split(":", 2);

final Field field = PaymentInfo.class.getDeclaredField(pair[0]);
field.setAccessible(true);
field.set(pi, "null".equals(pair[1]) ? null : pair[1]);
}

return pi;
}
}