Skip to content

Commit

Permalink
Merge pull request #2044 from alphagov/PP-6075-rationalise-charge-ent…
Browse files Browse the repository at this point in the history
…ity-constructors

PP-6075 Better rationalise ChargeEntity constructors
  • Loading branch information
stephencdaly authored Jan 28, 2020
2 parents 70b0ac7 + de80d87 commit dc14335
Show file tree
Hide file tree
Showing 56 changed files with 309 additions and 160 deletions.
218 changes: 183 additions & 35 deletions src/main/java/uk/gov/pay/connector/charge/model/domain/ChargeEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

import static java.lang.String.format;
import static net.logstash.logback.argument.StructuredArguments.kv;
import static uk.gov.pay.commons.model.Source.CARD_EXTERNAL_TELEPHONE;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURED;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURE_SUBMITTED;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.UNDEFINED;
Expand Down Expand Up @@ -167,42 +168,23 @@ public ChargeEntity() {
//for jpa
}

public ChargeEntity(Long amount, String returnUrl, String description, ServicePaymentReference reference,
GatewayAccountEntity gatewayAccount, String email, SupportedLanguage language,
boolean delayedCapture, ExternalMetadata externalMetadata, Source source) {
this(amount, UNDEFINED, returnUrl, description, reference, gatewayAccount, email, ZonedDateTime.now(ZoneId.of("UTC")), language, delayedCapture, externalMetadata, source);
}

public ChargeEntity(Long amount,
ServicePaymentReference reference,
String description,
ChargeStatus status,
String email,
CardDetailsEntity cardDetails,
ExternalMetadata externalMetadata,
GatewayAccountEntity gatewayAccount,
String gatewayTransactionId,
SupportedLanguage language,
Source source) {
this.amount = amount;
this.reference = reference;
this.description = description;
this.status = status.getValue();
this.email = email;
this.createdDate = ZonedDateTime.now(ZoneId.of("UTC"));
this.cardDetails = cardDetails;
this.externalMetadata = externalMetadata;
this.gatewayAccount = gatewayAccount;
this.externalId = RandomIdGenerator.newId();
this.gatewayTransactionId = gatewayTransactionId;
this.language = language;
this.source = source;
}

// Only the ChargeEntityFixture should directly call this constructor
public ChargeEntity(Long amount, ChargeStatus status, String returnUrl, String description, ServicePaymentReference reference,
GatewayAccountEntity gatewayAccount, String email, ZonedDateTime createdDate, SupportedLanguage language,
boolean delayedCapture, ExternalMetadata externalMetadata, Source source) {
ChargeEntity(
Long amount,
ChargeStatus status,
String returnUrl,
String description,
ServicePaymentReference reference,
GatewayAccountEntity gatewayAccount,
String email,
ZonedDateTime createdDate,
SupportedLanguage language,
boolean delayedCapture,
ExternalMetadata externalMetadata,
Source source,
String gatewayTransactionId,
CardDetailsEntity cardDetails
) {
this.amount = amount;
this.status = status.getValue();
this.returnUrl = returnUrl;
Expand All @@ -216,6 +198,8 @@ public ChargeEntity(Long amount, ChargeStatus status, String returnUrl, String d
this.delayedCapture = delayedCapture;
this.externalMetadata = externalMetadata;
this.source = source;
this.gatewayTransactionId = gatewayTransactionId;
this.cardDetails = cardDetails;
}

public Long getId() {
Expand Down Expand Up @@ -433,4 +417,168 @@ public Source getSource() {
public void setSource(Source source) {
this.source = source;
}

public static final class WebChargeEntityBuilder {
private Long amount;
private String returnUrl;
private String email;
private GatewayAccountEntity gatewayAccount;
private String description;
private ServicePaymentReference reference;
private SupportedLanguage language;
private boolean delayedCapture;
private ExternalMetadata externalMetadata;
private Source source;

private WebChargeEntityBuilder() {
}

public static WebChargeEntityBuilder aWebChargeEntity() {
return new WebChargeEntityBuilder();
}

public WebChargeEntityBuilder withAmount(Long amount) {
this.amount = amount;
return this;
}

public WebChargeEntityBuilder withReturnUrl(String returnUrl) {
this.returnUrl = returnUrl;
return this;
}

public WebChargeEntityBuilder withEmail(String email) {
this.email = email;
return this;
}

public WebChargeEntityBuilder withGatewayAccount(GatewayAccountEntity gatewayAccount) {
this.gatewayAccount = gatewayAccount;
return this;
}

public WebChargeEntityBuilder withDescription(String description) {
this.description = description;
return this;
}

public WebChargeEntityBuilder withReference(ServicePaymentReference reference) {
this.reference = reference;
return this;
}

public WebChargeEntityBuilder withLanguage(SupportedLanguage language) {
this.language = language;
return this;
}

public WebChargeEntityBuilder withDelayedCapture(boolean delayedCapture) {
this.delayedCapture = delayedCapture;
return this;
}

public WebChargeEntityBuilder withExternalMetadata(ExternalMetadata externalMetadata) {
this.externalMetadata = externalMetadata;
return this;
}

public WebChargeEntityBuilder withSource(Source source) {
this.source = source;
return this;
}

public ChargeEntity build() {
return new ChargeEntity(
amount,
UNDEFINED,
returnUrl,
description,
reference,
gatewayAccount,
email,
ZonedDateTime.now(ZoneId.of("UTC")),
language,
delayedCapture,
externalMetadata,
source,
null,
null);
}
}

public static final class TelephoneChargeEntityBuilder {
private Long amount;
private String gatewayTransactionId;
private String email;
private CardDetailsEntity cardDetails;
private GatewayAccountEntity gatewayAccount;
private String description;
private ServicePaymentReference reference;
private ExternalMetadata externalMetadata;

private TelephoneChargeEntityBuilder() {
}

public static TelephoneChargeEntityBuilder aTelephoneChargeEntity() {
return new TelephoneChargeEntityBuilder();
}

public TelephoneChargeEntityBuilder withAmount(Long amount) {
this.amount = amount;
return this;
}

public TelephoneChargeEntityBuilder withGatewayTransactionId(String gatewayTransactionId) {
this.gatewayTransactionId = gatewayTransactionId;
return this;
}

public TelephoneChargeEntityBuilder withEmail(String email) {
this.email = email;
return this;
}

public TelephoneChargeEntityBuilder withCardDetails(CardDetailsEntity cardDetails) {
this.cardDetails = cardDetails;
return this;
}

public TelephoneChargeEntityBuilder withGatewayAccount(GatewayAccountEntity gatewayAccount) {
this.gatewayAccount = gatewayAccount;
return this;
}

public TelephoneChargeEntityBuilder withDescription(String description) {
this.description = description;
return this;
}

public TelephoneChargeEntityBuilder withReference(ServicePaymentReference reference) {
this.reference = reference;
return this;
}

public TelephoneChargeEntityBuilder withExternalMetadata(ExternalMetadata externalMetadata) {
this.externalMetadata = externalMetadata;
return this;
}

public ChargeEntity build() {
return new ChargeEntity(
amount,
UNDEFINED,
null,
description,
reference,
gatewayAccount,
email,
ZonedDateTime.now(ZoneId.of("UTC")),
SupportedLanguage.ENGLISH,
false,
externalMetadata,
CARD_EXTERNAL_TELEPHONE,
gatewayTransactionId,
cardDetails);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@
import static javax.ws.rs.HttpMethod.POST;
import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static uk.gov.pay.commons.model.Source.CARD_EXTERNAL_TELEPHONE;
import static uk.gov.pay.connector.charge.model.ChargeResponse.aChargeResponseBuilder;
import static uk.gov.pay.connector.charge.model.domain.ChargeEntity.TelephoneChargeEntityBuilder.aTelephoneChargeEntity;
import static uk.gov.pay.connector.charge.model.domain.ChargeEntity.WebChargeEntityBuilder.aWebChargeEntity;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.AUTHORISATION_ERROR;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.AUTHORISATION_REJECTED;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.AWAITING_CAPTURE_REQUEST;
Expand All @@ -88,7 +89,6 @@
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.CREATED;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.ENTERING_CARD_DETAILS;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.PAYMENT_NOTIFICATION_CREATED;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.UNDEFINED;
import static uk.gov.pay.connector.charge.model.domain.ChargeStatus.fromString;
import static uk.gov.pay.connector.common.model.domain.NumbersInStringsSanitizer.sanitize;

Expand Down Expand Up @@ -156,20 +156,17 @@ private Optional<ChargeEntity> createCharge(TelephoneChargeCreateRequest telepho
null
);

ChargeEntity chargeEntity = new ChargeEntity(
telephoneChargeRequest.getAmount(),
ServicePaymentReference.of(telephoneChargeRequest.getReference()),
telephoneChargeRequest.getDescription(),
UNDEFINED,
telephoneChargeRequest.getEmailAddress().orElse(null),
cardDetails,
storeExtraFieldsInMetaData(telephoneChargeRequest),
gatewayAccount,
telephoneChargeRequest.getProviderId(),
SupportedLanguage.ENGLISH,
CARD_EXTERNAL_TELEPHONE
);

ChargeEntity chargeEntity = aTelephoneChargeEntity()
.withAmount(telephoneChargeRequest.getAmount())
.withDescription(telephoneChargeRequest.getDescription())
.withReference(ServicePaymentReference.of(telephoneChargeRequest.getReference()))
.withGatewayAccount(gatewayAccount)
.withEmail(telephoneChargeRequest.getEmailAddress().orElse(null))
.withExternalMetadata(storeExtraFieldsInMetaData(telephoneChargeRequest))
.withGatewayTransactionId(telephoneChargeRequest.getProviderId())
.withCardDetails(cardDetails)
.build();

chargeDao.persist(chargeEntity);
transitionChargeState(chargeEntity, PAYMENT_NOTIFICATION_CREATED);
transitionChargeState(chargeEntity, internalChargeStatus(telephoneChargeRequest.getPaymentOutcome().getCode().orElse(null)));
Expand Down Expand Up @@ -209,17 +206,20 @@ private Optional<ChargeEntity> createCharge(ChargeCreateRequest chargeRequest, L
? chargeRequest.getLanguage()
: SupportedLanguage.ENGLISH;

ChargeEntity chargeEntity = new ChargeEntity(
chargeRequest.getAmount(),
chargeRequest.getReturnUrl(),
chargeRequest.getDescription(),
ServicePaymentReference.of(chargeRequest.getReference()),
gatewayAccount,
chargeRequest.getEmail(),
language,
chargeRequest.isDelayedCapture(),
chargeRequest.getExternalMetadata().orElse(null),
chargeRequest.getSource());


ChargeEntity chargeEntity = aWebChargeEntity()
.withAmount(chargeRequest.getAmount())
.withReturnUrl(chargeRequest.getReturnUrl())
.withDescription(chargeRequest.getDescription())
.withReference(ServicePaymentReference.of(chargeRequest.getReference()))
.withGatewayAccount(gatewayAccount)
.withEmail(chargeRequest.getEmail())
.withLanguage(language)
.withDelayedCapture(chargeRequest.isDelayedCapture())
.withExternalMetadata(chargeRequest.getExternalMetadata().orElse(null))
.withSource(chargeRequest.getSource())
.build();

chargeRequest.getPrefilledCardHolderDetails()
.map(this::createCardDetailsEntity)
Expand Down Expand Up @@ -772,7 +772,7 @@ private ExternalMetadata storeExtraFieldsInMetaData(TelephoneChargeCreateRequest

return new ExternalMetadata(telephoneJSON);
}

private String checkAndGetTruncatedValue(String processorId, String field, String value) {
if (value.length() > 50) {
logger.info("Telephone payment {} - {} field is longer than 50 characters and has been truncated and stored. Actual value is {}", processorId, field, value);
Expand Down
Loading

0 comments on commit dc14335

Please sign in to comment.