Skip to content

Commit

Permalink
Merge pull request #2553 from alphagov/PP-6994-use-external-refund-st…
Browse files Browse the repository at this point in the history
…ate-to-calculate-refundability

PP-6994 Use external refund status to calculate refundability
  • Loading branch information
stephencdaly authored Sep 23, 2020
2 parents ba8abcf + a0e9c4b commit a882a7d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import uk.gov.pay.connector.charge.model.domain.Charge;
import uk.gov.pay.connector.charge.model.domain.ChargeEntity;
import uk.gov.pay.connector.refund.model.domain.Refund;
import uk.gov.pay.connector.refund.model.domain.RefundEntity;
import uk.gov.pay.connector.refund.model.domain.RefundStatus;

import java.util.List;

import static uk.gov.pay.connector.common.model.api.ExternalRefundStatus.EXTERNAL_SUBMITTED;
import static uk.gov.pay.connector.common.model.api.ExternalRefundStatus.EXTERNAL_SUCCESS;

/**
* Holder for utility methods used to calculate refund amounts
*/
Expand All @@ -27,7 +28,7 @@ public static long getTotalAmountAvailableToBeRefunded(ChargeEntity chargeEntity

public static long getRefundedAmount(List<Refund> refundList) {
return refundList.stream()
.filter(p -> List.of(RefundStatus.CREATED, RefundStatus.REFUND_SUBMITTED, RefundStatus.REFUNDED).contains(p.getStatus()))
.filter(p -> List.of(EXTERNAL_SUBMITTED, EXTERNAL_SUCCESS).contains(p.getExternalStatus()))
.mapToLong(Refund::getAmount)
.sum();
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/uk/gov/pay/connector/refund/model/domain/Refund.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.gov.pay.connector.refund.model.domain;

import uk.gov.pay.connector.common.model.api.ExternalRefundStatus;

import java.util.Objects;

public class Refund {
Expand All @@ -9,13 +11,18 @@ public class Refund {
private String userEmail;
private String gatewayTransactionId;
private String chargeExternalId;
private RefundStatus status;
private ExternalRefundStatus externalStatus;
private boolean historic;

public Refund(String externalId, Long amount, RefundStatus status, String userExternalId, String userEmail, String gatewayTransactionId, String chargeExternalId, boolean historic) {
public Refund(String externalId, Long amount,
ExternalRefundStatus externalStatus,
String userExternalId, String userEmail,
String gatewayTransactionId,
String chargeExternalId,
boolean historic) {
this.externalId = externalId;
this.amount = amount;
this.status = status;
this.externalStatus = externalStatus;
this.userExternalId = userExternalId;
this.userEmail = userEmail;
this.gatewayTransactionId = gatewayTransactionId;
Expand All @@ -27,7 +34,7 @@ public static Refund from(RefundEntity refundEntity) {
return new Refund(
refundEntity.getExternalId(),
refundEntity.getAmount(),
refundEntity.getStatus(),
refundEntity.getStatus().toExternal(),
refundEntity.getUserExternalId(),
refundEntity.getUserEmail(),
refundEntity.getGatewayTransactionId(),
Expand Down Expand Up @@ -56,8 +63,8 @@ public String getUserExternalId() {
return userExternalId;
}

public RefundStatus getStatus() {
return status;
public ExternalRefundStatus getExternalStatus() {
return externalStatus;
}

public Long getAmount() {
Expand All @@ -80,7 +87,7 @@ public boolean equals(Object obj) {
Refund refund = (Refund) obj;
return Objects.equals(externalId, refund.externalId) &&
Objects.equals(amount, refund.amount) &&
Objects.equals(status, refund.status) &&
Objects.equals(externalStatus, refund.externalStatus) &&
Objects.equals(gatewayTransactionId, refund.gatewayTransactionId) &&
Objects.equals(historic, refund.historic) &&
Objects.equals(chargeExternalId, refund.chargeExternalId) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ public void shouldFindRefundsGivenValidCharge() {
assertThat(refunds.size(), is(2));
assertThat(refunds.get(0).getChargeExternalId(), is(charge.getExternalId()));
assertThat(refunds.get(0).getAmount(), is(refundOne.getAmount()));
assertThat(refunds.get(0).getStatus(), is(RefundStatus.CREATED));
assertThat(refunds.get(1).getStatus(), is(RefundStatus.REFUND_SUBMITTED));
assertThat(refunds.get(0).getExternalStatus(), is(RefundStatus.CREATED.toExternal()));
assertThat(refunds.get(1).getExternalStatus(), is(RefundStatus.REFUND_SUBMITTED.toExternal()));
assertThat(refunds.get(1).getAmount(), is(refundTwo.getAmount()));
}

Expand Down

0 comments on commit a882a7d

Please sign in to comment.