Skip to content

Commit

Permalink
Merge pull request #2428 from alphagov/PP-6712_do_not_use_EpdqTemplat…
Browse files Browse the repository at this point in the history
…eData_for_authorisation_requests

PP-6712 Don’t use EpdqTemplateData for authorisation requests
  • Loading branch information
alexbishop1 authored Jul 3, 2020
2 parents 69a52f4 + 656808b commit e6fb528
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,6 @@ private GatewayOrder buildQueryOrderRequestFor(ChargeEntity charge) {
}

private GatewayOrder buildAuthoriseOrder(CardAuthorisationGatewayRequest request, String frontendUrl) {
EpdqTemplateData templateData = new EpdqTemplateData();
templateData.setOrderId(request.getChargeExternalId());
templateData.setPassword(request.getGatewayAccount().getCredentials().get(CREDENTIALS_PASSWORD));
templateData.setUserId(request.getGatewayAccount().getCredentials().get(CREDENTIALS_USERNAME));
templateData.setMerchantCode(request.getGatewayAccount().getCredentials().get(CREDENTIALS_MERCHANT_ID));
templateData.setDescription(request.getDescription());
templateData.setAmount(request.getAmount());
templateData.setAuthCardDetails(request.getAuthCardDetails());

EpdqPayloadDefinitionForNewOrder epdqPayloadDefinition;

if (request.getGatewayAccount().isRequires3ds()) {
Expand All @@ -313,7 +304,12 @@ private GatewayOrder buildAuthoriseOrder(CardAuthorisationGatewayRequest request
epdqPayloadDefinition = new EpdqPayloadDefinitionForNewOrder();
}

epdqPayloadDefinition.setEpdqTemplateData(templateData);
epdqPayloadDefinition.setOrderId(request.getChargeExternalId());
epdqPayloadDefinition.setPassword(request.getGatewayAccount().getCredentials().get(CREDENTIALS_PASSWORD));
epdqPayloadDefinition.setUserId(request.getGatewayAccount().getCredentials().get(CREDENTIALS_USERNAME));
epdqPayloadDefinition.setPspId(request.getGatewayAccount().getCredentials().get(CREDENTIALS_MERCHANT_ID));
epdqPayloadDefinition.setAmount(request.getAmount());
epdqPayloadDefinition.setAuthCardDetails(request.getAuthCardDetails());
epdqPayloadDefinition.setShaInPassphrase(request.getGatewayAccount().getCredentials().get(CREDENTIALS_SHA_IN_PASSPHRASE));
return epdqPayloadDefinition.createGatewayOrder();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.apache.http.message.BasicNameValuePair;
import uk.gov.pay.connector.gateway.GatewayOrder;
import uk.gov.pay.connector.gateway.epdq.EpdqSha512SignatureGenerator;
import uk.gov.pay.connector.gateway.epdq.EpdqTemplateData;
import uk.gov.pay.connector.gateway.epdq.SignatureGenerator;
import uk.gov.pay.connector.gateway.model.OrderRequestType;

Expand All @@ -27,9 +26,7 @@ public abstract class EpdqPayloadDefinition {
* parlance) seems to encode to %92 — makes us believe that they do
*/
public static final Charset EPDQ_APPLICATION_X_WWW_FORM_URLENCODED_CHARSET = Charset.forName("windows-1252");

protected EpdqTemplateData epdqTemplateData;


protected String shaInPassphrase;

protected abstract List<NameValuePair> extract();
Expand All @@ -50,14 +47,6 @@ public GatewayOrder createGatewayOrder() {

protected abstract OrderRequestType getOrderRequestType();

public void setEpdqTemplateData(EpdqTemplateData epdqTemplateData) {
this.epdqTemplateData = epdqTemplateData;
}

protected EpdqTemplateData getEpdqTemplateData() {
return epdqTemplateData;
}

public void setShaInPassphrase(String shaInPassphrase) {
this.shaInPassphrase = shaInPassphrase;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.apache.http.NameValuePair;
import uk.gov.pay.commons.model.SupportedLanguage;
import uk.gov.pay.connector.common.model.domain.Address;
import uk.gov.pay.connector.gateway.epdq.EpdqTemplateData;

import java.time.Clock;
import java.time.ZoneId;
Expand Down Expand Up @@ -48,7 +47,7 @@ public class EpdqPayloadDefinitionForNew3ds2Order extends EpdqPayloadDefinitionF
private final static Pattern NUMBER_FROM_0_TO_999999 = Pattern.compile("0|[1-9][0-9]{0,5}");
private final static Pattern NUMBER_FROM_MINUS_999_TO_999 = Pattern.compile("-[1-9][0-9]{0,2}|0|[1-9][0-9]{0,2}");
private final static Set<String> VALID_SCREEN_COLOR_DEPTHS = Set.of("1", "2", "4", "8", "15", "16", "24", "32");

private final boolean sendPayerIpAddressToGateway;
private final SupportedLanguage paymentLanguage;
private final Clock clock;
Expand All @@ -63,74 +62,72 @@ public EpdqPayloadDefinitionForNew3ds2Order(String frontendUrl, boolean sendPaye
@Override
public List<NameValuePair> extract() {
List<NameValuePair> nameValuePairs = super.extract();
var templateData = getEpdqTemplateData();
EpdqParameterBuilder parameterBuilder = newParameterBuilder(nameValuePairs)
.add(BROWSER_COLOR_DEPTH, getBrowserColorDepth(templateData))
.add(BROWSER_LANGUAGE, getBrowserLanguage(templateData))
.add(BROWSER_SCREEN_HEIGHT, getBrowserScreenHeight(templateData))
.add(BROWSER_SCREEN_WIDTH, getBrowserScreenWidth(templateData))
.add(BROWSER_TIMEZONE, getBrowserTimezone(templateData))
.add(BROWSER_ACCEPT_HEADER, getBrowserAcceptHeader(templateData))
.add(BROWSER_USER_AGENT, getBrowserUserAgent(templateData))
.add(BROWSER_COLOR_DEPTH, getBrowserColorDepth())
.add(BROWSER_LANGUAGE, getBrowserLanguage())
.add(BROWSER_SCREEN_HEIGHT, getBrowserScreenHeight())
.add(BROWSER_SCREEN_WIDTH, getBrowserScreenWidth())
.add(BROWSER_TIMEZONE, getBrowserTimezone())
.add(BROWSER_ACCEPT_HEADER, getBrowserAcceptHeader())
.add(BROWSER_USER_AGENT, getBrowserUserAgent())
.add(BROWSER_JAVA_ENABLED, "false");

templateData.getAuthCardDetails().getAddress().map(Address::getCity)
getAuthCardDetails().getAddress().map(Address::getCity)
.filter(city -> city.length() <= ECOM_BILLTO_POSTAL_CITY_MAX_LENGTH)
.ifPresent(city -> parameterBuilder.add(ECOM_BILLTO_POSTAL_CITY, city));

templateData.getAuthCardDetails().getAddress().map(Address::getCountry)
getAuthCardDetails().getAddress().map(Address::getCountry)
.filter(country -> country.length() <= ECOM_BILLTO_POSTAL_COUNTRYCODE_MAX_LENGTH)
.ifPresent(country -> parameterBuilder.add(ECOM_BILLTO_POSTAL_COUNTRYCODE, country));

templateData.getAuthCardDetails().getAddress().map(Address::getLine1)
getAuthCardDetails().getAddress().map(Address::getLine1)
.filter(addressLine1 -> addressLine1.length() <= ECOM_BILLTO_POSTAL_STREET_LINE1_MAX_LENGTH)
.ifPresent(addressLine1 -> parameterBuilder.add(ECOM_BILLTO_POSTAL_STREET_LINE1, addressLine1));

templateData.getAuthCardDetails().getAddress().map(Address::getLine2)
getAuthCardDetails().getAddress().map(Address::getLine2)
.filter(addressLine2 -> addressLine2.length() <= ECOM_BILLTO_POSTAL_STREET_LINE2_MAX_LENGTH)
.ifPresent(addressLine2 -> parameterBuilder.add(ECOM_BILLTO_POSTAL_STREET_LINE2, addressLine2));

templateData.getAuthCardDetails().getAddress().map(Address::getPostcode)
getAuthCardDetails().getAddress().map(Address::getPostcode)
.filter(addressPostCode -> addressPostCode.length() <= ECOM_BILLTO_POSTAL_POSTALCODE_MAX_LENGTH)
.ifPresent(addressPostCode -> parameterBuilder.add(ECOM_BILLTO_POSTAL_POSTALCODE, addressPostCode));

if (sendPayerIpAddressToGateway) {
templateData.getAuthCardDetails().getIpAddress().ifPresent(ipAddress -> parameterBuilder.add(REMOTE_ADDR, ipAddress));
getAuthCardDetails().getIpAddress().ifPresent(ipAddress -> parameterBuilder.add(REMOTE_ADDR, ipAddress));
}

return parameterBuilder.build();
}

private String getBrowserTimezone(EpdqTemplateData templateData) {
return templateData.getAuthCardDetails().getJsTimezoneOffsetMins()
private String getBrowserTimezone() {
return getAuthCardDetails().getJsTimezoneOffsetMins()
.filter(timezoneOffsetMins -> NUMBER_FROM_MINUS_999_TO_999.matcher(timezoneOffsetMins).matches())
.map(Integer::parseInt)
.filter(timezoneOffsetMins -> timezoneOffsetMins >= -840 && timezoneOffsetMins <= 720)
.map(timezoneOffsetMins -> Integer.toString(timezoneOffsetMins))
.orElseGet(this::getDefaultBrowserOffsetInMinutes);
}

private String getBrowserScreenWidth(EpdqTemplateData templateData) {
return templateData.getAuthCardDetails().getJsScreenWidth()
private String getBrowserScreenWidth() {
return getAuthCardDetails().getJsScreenWidth()
.filter(screenWidth -> NUMBER_FROM_0_TO_999999.matcher(screenWidth).matches())
.map(Integer::parseInt)
.filter(screenWidth -> screenWidth >= 0 && screenWidth <= 999999)
.map(screenWidth -> Integer.toString(screenWidth))
.orElse(DEFAULT_BROWSER_SCREEN_WIDTH);
}

private String getBrowserScreenHeight(EpdqTemplateData templateData) {
return templateData.getAuthCardDetails().getJsScreenHeight()
private String getBrowserScreenHeight() {
return getAuthCardDetails().getJsScreenHeight()
.filter(screenHeight -> NUMBER_FROM_0_TO_999999.matcher(screenHeight).matches())
.map(Integer::parseInt)
.filter(screenHeight -> screenHeight >= 0 && screenHeight <= 999999)
.map(screenHeight -> Integer.toString(screenHeight))
.orElse(DEFAULT_BROWSER_SCREEN_HEIGHT);
}

private String getBrowserLanguage(EpdqTemplateData templateData) {
return templateData
.getAuthCardDetails()
private String getBrowserLanguage() {
return getAuthCardDetails()
.getJsNavigatorLanguage()
.map(Locale::forLanguageTag)
.map(Locale::toLanguageTag)
Expand All @@ -145,8 +142,8 @@ private String getDefaultBrowserLanguage() {
return paymentLanguage.toString();
}

private String getBrowserColorDepth(EpdqTemplateData templateData) {
return templateData.getAuthCardDetails().getJsScreenColorDepth()
private String getBrowserColorDepth() {
return getAuthCardDetails().getJsScreenColorDepth()
.filter(VALID_SCREEN_COLOR_DEPTHS::contains).orElse(DEFAULT_BROWSER_COLOR_DEPTH);
}

Expand All @@ -157,13 +154,13 @@ private String getDefaultBrowserOffsetInMinutes() {
return String.valueOf(currentUkOffsetMinsInJavaScriptFormatWithAheadOfUtcNegative);
}

String getBrowserAcceptHeader(EpdqTemplateData templateData) {
String acceptHeader = super.getBrowserAcceptHeader(templateData);
String getBrowserAcceptHeader() {
String acceptHeader = super.getBrowserAcceptHeader();
return acceptHeader.length() > BROWSER_ACCEPT_MAX_LENGTH ? DEFAULT_BROWSER_ACCEPT_HEADER : acceptHeader;
}

String getBrowserUserAgent(EpdqTemplateData templateData) {
String userAgent = super.getBrowserUserAgent(templateData);
String getBrowserUserAgent() {
String userAgent = super.getBrowserUserAgent();
return userAgent.length() > BROWSER_USER_AGENT_MAX_LENGTH ? DEFAULT_BROWSER_USER_AGENT : userAgent;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package uk.gov.pay.connector.gateway.epdq.payload;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import uk.gov.pay.connector.common.model.domain.Address;
import uk.gov.pay.connector.gateway.epdq.EpdqTemplateData;
import uk.gov.pay.connector.gateway.model.OrderRequestType;

import java.util.List;
Expand Down Expand Up @@ -36,51 +33,43 @@ public EpdqPayloadDefinitionForNew3dsOrder(String frontendUrl) {

@Override
public List<NameValuePair> extract() {
var templateData = getEpdqTemplateData();
templateData.setFrontendUrl(frontendUrl);
String frontend3dsIncomingUrl = String.format("%s/card_details/%s/3ds_required_in/epdq", templateData.getFrontendUrl(), templateData.getOrderId());
String frontend3dsIncomingUrl = String.format("%s/card_details/%s/3ds_required_in/epdq", frontendUrl, getOrderId());

EpdqParameterBuilder epdqParameterBuilder = newParameterBuilder()
.add(ACCEPTURL_KEY, frontend3dsIncomingUrl)
.add(AMOUNT_KEY, templateData.getAmount())
.add(CARD_NO_KEY, templateData.getAuthCardDetails().getCardNo())
.add(CARDHOLDER_NAME_KEY, templateData.getAuthCardDetails().getCardHolder())
.add(AMOUNT_KEY, getAmount())
.add(CARD_NO_KEY, getAuthCardDetails().getCardNo())
.add(CARDHOLDER_NAME_KEY, getAuthCardDetails().getCardHolder())
.add(COMPLUS_KEY, "")
.add(CURRENCY_KEY, "GBP")
.add(CVC_KEY, templateData.getAuthCardDetails().getCvc())
.add(CVC_KEY, getAuthCardDetails().getCvc())
.add(DECLINEURL_KEY, frontend3dsIncomingUrl + "?status=declined")
.add(EXCEPTIONURL_KEY, frontend3dsIncomingUrl + "?status=error")
.add(EXPIRY_DATE_KEY, templateData.getAuthCardDetails().getEndDate())
.add(EXPIRY_DATE_KEY, getAuthCardDetails().getEndDate())
.add(FLAG3D_KEY, "Y")
.add(HTTPACCEPT_KEY, getBrowserAcceptHeader(templateData))
.add(HTTPUSER_AGENT_KEY, getBrowserUserAgent(templateData))
.add(HTTPACCEPT_KEY, getBrowserAcceptHeader())
.add(HTTPUSER_AGENT_KEY, getBrowserUserAgent())
.add(LANGUAGE_URL, "en_GB")
.add(OPERATION_KEY, getOperationType())
.add(ORDER_ID_KEY, templateData.getOrderId());

if (templateData.getAuthCardDetails().getAddress().isPresent()) {
Address address = templateData.getAuthCardDetails().getAddress().get();
String addressLines = concatAddressLines(address.getLine1(), address.getLine2());
.add(ORDER_ID_KEY, getOrderId());

getAuthCardDetails().getAddress().ifPresent(address -> {
String addressLines = concatAddressLines(address.getLine1(), address.getLine2());
epdqParameterBuilder.add(OWNER_ADDRESS_KEY, addressLines)
.add(OWNER_COUNTRY_CODE_KEY, address.getCountry())
.add(OWNER_TOWN_KEY, address.getCity())
.add(OWNER_ZIP_KEY, address.getPostcode());
}
});

epdqParameterBuilder.add(PARAMPLUS_URL, "")
.add(PSPID_KEY, templateData.getMerchantCode())
.add(PSWD_KEY, templateData.getPassword())
.add(USERID_KEY, templateData.getUserId())
.add(PSPID_KEY, getPspId())
.add(PSWD_KEY, getPassword())
.add(USERID_KEY, getUserId())
.add(WIN3DS_URL, "MAINW");

return epdqParameterBuilder.build();
}

private static String concatAddressLines(String addressLine1, String addressLine2) {
return StringUtils.isBlank(addressLine2) ? addressLine1 : addressLine1 + ", " + addressLine2;
}

@Override
public String getOperationType() {
return "RES";
Expand All @@ -91,14 +80,14 @@ protected OrderRequestType getOrderRequestType() {
return OrderRequestType.AUTHORISE_3DS;
}

String getBrowserAcceptHeader(EpdqTemplateData templateData) {
return Optional.ofNullable(templateData.getAuthCardDetails().getAcceptHeader())
String getBrowserAcceptHeader() {
return Optional.ofNullable(getAuthCardDetails().getAcceptHeader())
.filter(not(String::isEmpty))
.orElse(DEFAULT_BROWSER_ACCEPT_HEADER);
}

String getBrowserUserAgent(EpdqTemplateData templateData) {
return Optional.ofNullable(templateData.getAuthCardDetails().getUserAgentHeader())
String getBrowserUserAgent() {
return Optional.ofNullable(getAuthCardDetails().getUserAgentHeader())
.filter(not(String::isEmpty))
.orElse(DEFAULT_BROWSER_USER_AGENT);
}
Expand Down
Loading

0 comments on commit e6fb528

Please sign in to comment.