Skip to content

Commit

Permalink
PP-6712 Make subclasses of EpdqPayloadDefinition not be singletons
Browse files Browse the repository at this point in the history
Make subclasses of EpdqPayloadDefinition not be singletons for they
will soon hold state (except for
EpdqPayloadDefinitionForNew3dsOrder and
EpdqPayloadDefinitionForNew3ds2Order, which already aren’t
singletons and hold state).

Fun fact: despite being annotated with @singleton,
EpdqPayloadDefinitionForRefundOrder wasn’t a singleton.
  • Loading branch information
alexbishop1 committed Jul 1, 2020
1 parent 7859cda commit 7b17b81
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ public class EpdqCaptureHandler implements CaptureHandler {

private final GatewayClient client;
private final Map<String, String> gatewayUrlMap;
private final EpdqPayloadDefinitionForCaptureOrder epdqPayloadDefinitionForCaptureOrder;

public EpdqCaptureHandler(GatewayClient client,
Map<String, String> gatewayUrlMap,
EpdqPayloadDefinitionForCaptureOrder epdqPayloadDefinitionForCaptureOrder) {
public EpdqCaptureHandler(GatewayClient client, Map<String, String> gatewayUrlMap) {
this.client = client;
this.gatewayUrlMap = gatewayUrlMap;
this.epdqPayloadDefinitionForCaptureOrder = epdqPayloadDefinitionForCaptureOrder;
}

@Override
Expand All @@ -58,6 +54,6 @@ private GatewayOrder buildCaptureOrder(CaptureGatewayRequest request) {
templateData.setMerchantCode(request.getGatewayAccount().getCredentials().get(CREDENTIALS_MERCHANT_ID));
templateData.setTransactionId(request.getTransactionId());

return epdqPayloadDefinitionForCaptureOrder.createGatewayOrder(templateData);
return new EpdqPayloadDefinitionForCaptureOrder().createGatewayOrder(templateData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import uk.gov.pay.connector.gateway.epdq.model.response.EpdqAuthorisationResponse;
import uk.gov.pay.connector.gateway.epdq.model.response.EpdqCancelResponse;
import uk.gov.pay.connector.gateway.epdq.model.response.EpdqQueryResponse;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinition;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCancelOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCaptureOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNew3ds2Order;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNew3dsOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNewOrder;
Expand Down Expand Up @@ -82,35 +80,21 @@ public class EpdqPaymentProvider implements PaymentProvider {
private final GatewayClient cancelClient;
private final ExternalRefundAvailabilityCalculator externalRefundAvailabilityCalculator;
private final Map<String, String> gatewayUrlMap;
private final EpdqPayloadDefinitionForCancelOrder epdqPayloadDefinitionForCancelOrder;
private final EpdqPayloadDefinitionForNewOrder epdqPayloadDefinitionForNewOrder;
private final EpdqPayloadDefinitionForQueryOrder epdqPayloadDefinitionForQueryOrder;
private final EpdqCaptureHandler epdqCaptureHandler;
private final EpdqRefundHandler epdqRefundHandler;
private final Clock clock;

@Inject
public EpdqPaymentProvider(ConnectorConfiguration configuration,
GatewayClientFactory gatewayClientFactory,
Environment environment,
EpdqPayloadDefinitionForCancelOrder epdqPayloadDefinitionForCancelOrder,
EpdqPayloadDefinitionForCaptureOrder epdqPayloadDefinitionForCaptureOrder,
EpdqPayloadDefinitionForNewOrder epdqPayloadDefinitionForNewOrder,
EpdqPayloadDefinitionForQueryOrder epdqPayloadDefinitionForQueryOrder,
Clock clock) {

public EpdqPaymentProvider(ConnectorConfiguration configuration, GatewayClientFactory gatewayClientFactory, Environment environment, Clock clock) {
gatewayUrlMap = configuration.getGatewayConfigFor(EPDQ).getUrls();
this.epdqPayloadDefinitionForCancelOrder = epdqPayloadDefinitionForCancelOrder;
this.epdqPayloadDefinitionForNewOrder = epdqPayloadDefinitionForNewOrder;
this.epdqPayloadDefinitionForQueryOrder = epdqPayloadDefinitionForQueryOrder;
authoriseClient = gatewayClientFactory.createGatewayClient(EPDQ, AUTHORISE, environment.metrics());
cancelClient = gatewayClientFactory.createGatewayClient(EPDQ, CANCEL, environment.metrics());
GatewayClient captureClient = gatewayClientFactory.createGatewayClient(EPDQ, CAPTURE, environment.metrics());
GatewayClient refundClient = gatewayClientFactory.createGatewayClient(EPDQ, REFUND, environment.metrics());
frontendUrl = configuration.getLinks().getFrontendUrl();
metricRegistry = environment.metrics();
externalRefundAvailabilityCalculator = new EpdqExternalRefundAvailabilityCalculator();
epdqCaptureHandler = new EpdqCaptureHandler(captureClient, gatewayUrlMap, epdqPayloadDefinitionForCaptureOrder);
epdqCaptureHandler = new EpdqCaptureHandler(captureClient, gatewayUrlMap);
epdqRefundHandler = new EpdqRefundHandler(refundClient, gatewayUrlMap);
this.clock = clock;
}
Expand Down Expand Up @@ -294,7 +278,7 @@ private GatewayOrder buildQueryOrderRequestFor(Auth3dsResponseGatewayRequest req
templateData.setShaInPassphrase(request.getGatewayAccount().getCredentials().get(CREDENTIALS_SHA_IN_PASSPHRASE));
templateData.setUserId(request.getGatewayAccount().getCredentials().get(CREDENTIALS_USERNAME));
templateData.setMerchantCode(request.getGatewayAccount().getCredentials().get(CREDENTIALS_MERCHANT_ID));
return epdqPayloadDefinitionForQueryOrder.createGatewayOrder(templateData);
return new EpdqPayloadDefinitionForQueryOrder().createGatewayOrder(templateData);
}

private GatewayOrder buildQueryOrderRequestFor(ChargeEntity charge) {
Expand All @@ -304,7 +288,7 @@ private GatewayOrder buildQueryOrderRequestFor(ChargeEntity charge) {
templateData.setShaInPassphrase(charge.getGatewayAccount().getCredentials().get(CREDENTIALS_SHA_IN_PASSPHRASE));
templateData.setUserId(charge.getGatewayAccount().getCredentials().get(CREDENTIALS_USERNAME));
templateData.setMerchantCode(charge.getGatewayAccount().getCredentials().get(CREDENTIALS_MERCHANT_ID));
return epdqPayloadDefinitionForQueryOrder.createGatewayOrder(templateData);
return new EpdqPayloadDefinitionForQueryOrder().createGatewayOrder(templateData);
}

private GatewayOrder buildAuthoriseOrder(CardAuthorisationGatewayRequest request, String frontendUrl) {
Expand All @@ -318,18 +302,18 @@ private GatewayOrder buildAuthoriseOrder(CardAuthorisationGatewayRequest request
templateData.setAmount(request.getAmount());
templateData.setAuthCardDetails(request.getAuthCardDetails());

EpdqPayloadDefinition epdqPayloadDefinition;
EpdqPayloadDefinitionForNewOrder epdqPayloadDefinition;

if (request.getGatewayAccount().isRequires3ds()) {
if (request.getGatewayAccount().getIntegrationVersion3ds() == 2) {
epdqPayloadDefinition = new EpdqPayloadDefinitionForNew3ds2Order(frontendUrl, request.getGatewayAccount().isSendPayerIpAddressToGateway(), request.getCharge().getLanguage(), clock);
} else {
epdqPayloadDefinition = new EpdqPayloadDefinitionForNew3dsOrder(frontendUrl);
}
} else {
epdqPayloadDefinition = epdqPayloadDefinitionForNewOrder;
epdqPayloadDefinition = new EpdqPayloadDefinitionForNewOrder();
}

return epdqPayloadDefinition.createGatewayOrder(templateData);
}

Expand All @@ -345,6 +329,6 @@ private GatewayOrder buildCancelOrder(CancelGatewayRequest request) {
templateData::setTransactionId,
() -> templateData.setOrderId(request.getExternalChargeId()));

return epdqPayloadDefinitionForCancelOrder.createGatewayOrder(templateData);
return new EpdqPayloadDefinitionForCancelOrder().createGatewayOrder(templateData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import uk.gov.pay.connector.gateway.model.OrderRequestType;

import javax.inject.Singleton;

@Singleton
public class EpdqPayloadDefinitionForRefundOrder extends EpdqPayloadDefinitionForMaintenanceOrder {

@Override
protected String getOperationType() {
return "RFD";
Expand All @@ -15,4 +13,5 @@ protected String getOperationType() {
protected OrderRequestType getOrderRequestType() {
return OrderRequestType.REFUND;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
import uk.gov.pay.connector.gateway.GatewayClientFactory;
import uk.gov.pay.connector.gateway.GatewayOperation;
import uk.gov.pay.connector.gateway.PaymentGatewayName;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCancelOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCaptureOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNewOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForQueryOrder;
import uk.gov.pay.connector.gateway.model.Auth3dsResult;
import uk.gov.pay.connector.gateway.model.AuthCardDetails;
import uk.gov.pay.connector.gateway.model.GatewayError;
Expand Down Expand Up @@ -123,10 +119,6 @@ public void setup() {
configuration,
gatewayClientFactory,
environment,
new EpdqPayloadDefinitionForCancelOrder(),
new EpdqPayloadDefinitionForCaptureOrder(),
new EpdqPayloadDefinitionForNewOrder(),
new EpdqPayloadDefinitionForQueryOrder(),
Clock.fixed(Instant.parse("2020-01-01T10:10:10.100Z"), ZoneOffset.UTC));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import uk.gov.pay.connector.gateway.GatewayClient;
import uk.gov.pay.connector.gateway.GatewayException.GatewayErrorException;
import uk.gov.pay.connector.gateway.GatewayOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCaptureOrder;
import uk.gov.pay.connector.gateway.model.request.CaptureGatewayRequest;
import uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity;

Expand All @@ -26,13 +25,13 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.when;
import static uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity;
import static uk.gov.pay.connector.gateway.model.ErrorType.GATEWAY_ERROR;
import static uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_MERCHANT_ID;
import static uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_PASSWORD;
import static uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_SHA_IN_PASSPHRASE;
import static uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_USERNAME;
import static uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity.Type.TEST;
import static uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity;
import static uk.gov.pay.connector.util.TestTemplateResourceLoader.load;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -47,7 +46,7 @@ public class EpdqCaptureHandlerTest {

@Before
public void setup() {
epdqCaptureHandler = new EpdqCaptureHandler(client, emptyMap(), new EpdqPayloadDefinitionForCaptureOrder());
epdqCaptureHandler = new EpdqCaptureHandler(client, emptyMap());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
import uk.gov.pay.connector.gateway.GatewayOperation;
import uk.gov.pay.connector.gateway.PaymentGatewayName;
import uk.gov.pay.connector.gateway.epdq.EpdqPaymentProvider;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCancelOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCaptureOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNewOrder;
import uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForQueryOrder;
import uk.gov.pay.connector.gateway.model.Auth3dsResult;
import uk.gov.pay.connector.gateway.model.AuthCardDetails;
import uk.gov.pay.connector.gateway.model.request.Auth3dsResponseGatewayRequest;
Expand Down Expand Up @@ -137,10 +133,6 @@ public void setUp() {
mockConnectorConfiguration,
mockGatewayClientFactory,
mockEnvironment,
new EpdqPayloadDefinitionForCancelOrder(),
new EpdqPayloadDefinitionForCaptureOrder(),
new EpdqPayloadDefinitionForNewOrder(),
new EpdqPayloadDefinitionForQueryOrder(),
Clock.fixed(Instant.parse("2020-01-01T10:10:10.100Z"), ZoneOffset.UTC));
}

Expand Down

0 comments on commit 7b17b81

Please sign in to comment.