Skip to content

Commit

Permalink
Merge pull request #2561 from alphagov/pp_6988_expunge_refunds
Browse files Browse the repository at this point in the history
PP-6988 Fix ledger refund transaction mapping
  • Loading branch information
kbottla authored Sep 25, 2020
2 parents 157d929 + a654c13 commit 7834390
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class LedgerTransaction {
private String walletType;
@JsonProperty("metadata")
private Map<String, Object> externalMetaData;
private String userEmail;
private String userExternalId;
private String refundedBy;
private String refundedByUserEmail;
private String parentTransactionId;

public LedgerTransaction() {
Expand Down Expand Up @@ -257,20 +257,20 @@ public void setExternalMetaData(Map<String, Object> externalMetaData) {
this.externalMetaData = externalMetaData;
}

public String getUserEmail() {
return userEmail;
public String getRefundedBy() {
return refundedBy;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
public void setRefundedBy(String refundedBy) {
this.refundedBy = refundedBy;
}

public String getUserExternalId() {
return userExternalId;
public String getRefundedByUserEmail() {
return refundedByUserEmail;
}

public void setUserExternalId(String userExternalId) {
this.userExternalId = userExternalId;
public void setRefundedByUserEmail(String refundedByUserEmail) {
this.refundedByUserEmail = refundedByUserEmail;
}

public String getParentTransactionId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public static Refund from(LedgerTransaction ledgerTransaction) {
ledgerTransaction.getTransactionId(),
ledgerTransaction.getAmount(),
ExternalRefundStatus.fromPublicStatusLabel(ledgerTransaction.getState().getStatus()),
ledgerTransaction.getUserExternalId(),
ledgerTransaction.getUserEmail(),
ledgerTransaction.getRefundedBy(),
ledgerTransaction.getRefundedByUserEmail(),
ledgerTransaction.getParentTransactionId(),
ledgerTransaction.getGatewayTransactionId(),
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public ParityCheckStatus checkParity(RefundEntity refundEntity, LedgerTransactio
fieldsMatch = fieldsMatch && isEquals(refundEntity.getChargeExternalId(), transaction.getParentTransactionId(), "parent_transaction_id");
fieldsMatch = fieldsMatch && isEquals(refundEntity.getAmount(), transaction.getAmount(), "amount");
fieldsMatch = fieldsMatch && isEquals(refundEntity.getGatewayTransactionId(), transaction.getGatewayTransactionId(), "gateway_transaction_id");
fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserExternalId(), transaction.getUserExternalId(), "user_external_id");
fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserEmail(), transaction.getUserEmail(), "user_email");
fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserExternalId(), transaction.getRefundedBy(), "refunded_by");
fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserEmail(), transaction.getRefundedByUserEmail(), "refunded_by_user_email");

String refundCreatedEventDate = getRefundCreatedDate(refundEntity.getExternalId())
.map(ISO_INSTANT_MILLISECOND_PRECISION::format).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_GET_REFUNDS_FOR_PAYMENT;
import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_GET_TRANSACTION;
import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_PAYMENT_TRANSACTION;
import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_REFUND_TRANSACTION;
import static uk.gov.pay.connector.util.TestTemplateResourceLoader.load;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -60,8 +61,8 @@ public void setUp() {
}

@Test
public void getTransaction_shouldSerialiseLedgerTransaction() throws JsonProcessingException {
when(mockResponse.readEntity(LedgerTransaction.class)).thenReturn(objectMapper.readValue(load(LEDGER_GET_TRANSACTION), LedgerTransaction.class));
public void getTransaction_shouldSerialiseLedgerPaymentTransactionCorrectly() throws JsonProcessingException {
when(mockResponse.readEntity(LedgerTransaction.class)).thenReturn(objectMapper.readValue(load(LEDGER_PAYMENT_TRANSACTION), LedgerTransaction.class));

String externalId = "external-id";
Optional<LedgerTransaction> mayBeTransaction = ledgerService.getTransaction("external-id");
Expand All @@ -74,6 +75,26 @@ public void getTransaction_shouldSerialiseLedgerTransaction() throws JsonProcess
assertThat(transaction.getExternalMetaData(), is(notNullValue()));
}

@Test
public void getTransaction_shouldSerialiseLedgerRefundTransactionCorrectly() throws JsonProcessingException {
when(mockResponse.readEntity(LedgerTransaction.class))
.thenReturn(objectMapper.readValue(load(LEDGER_REFUND_TRANSACTION), LedgerTransaction.class));

String externalId = "external-id";
Optional<LedgerTransaction> mayBeTransaction = ledgerService.getTransaction("external-id");

assertThat(mayBeTransaction.isPresent(), is(true));
LedgerTransaction transaction = mayBeTransaction.get();
assertThat(transaction.getTransactionId(), is(externalId));
assertThat(transaction.getAmount(), is(1000L));
assertThat(transaction.getGatewayTransactionId(), is("re_1H6zDTHj08j2jFuBtG6maiHK11"));
assertThat(transaction.getParentTransactionId(), is("64pcdagc9c13vgi7n904aio3n9"));
assertThat(transaction.getRefundedBy(), is("refunded-by-external-id"));
assertThat(transaction.getRefundedByUserEmail(), is("[email protected]"));
assertThat(transaction.getCreatedDate(), is("2020-07-20T13:39:38.940Z"));
assertThat(transaction.getState().getStatus(), is("success"));
}

@Test
public void getRefundsFromLedgerShouldSerialiseResponseCorrectly() throws JsonProcessingException {
when(mockResponse.readEntity(RefundTransactionsForPayment.class)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ private Map<String, Object> refundEntityToLedgerTransaction(RefundEntity refund,
Optional.ofNullable(refund.getChargeExternalId()).ifPresent(value -> map.put("parent_transaction_id", value));
Optional.of(refund.getAmount()).ifPresent(value -> map.put("amount", String.valueOf(value)));
Optional.ofNullable(refund.getGatewayTransactionId()).ifPresent(value -> map.put("gateway_transaction_id", value));
Optional.ofNullable(refund.getUserExternalId()).ifPresent(value -> map.put("user_external_id", value));
Optional.ofNullable(refund.getUserEmail()).ifPresent(value -> map.put("user_email", value));
Optional.ofNullable(refund.getUserExternalId()).ifPresent(value -> map.put("refunded_by", value));
Optional.ofNullable(refund.getUserEmail()).ifPresent(value -> map.put("refunded_by_user_email", value));
Optional.ofNullable(refundCreatedEventDate).ifPresent(value -> map.put("created_date",
ISO_INSTANT_MILLISECOND_PRECISION.format(value)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public class LedgerTransactionFixture {
private ZonedDateTime capturedDate;
private ChargeResponse.RefundSummary refundSummary;
private String parentTransactionId;
private String userEmail;
private String userExternalId;
private String refundedBy;
private String refundedByUserEmail;

public static LedgerTransactionFixture aValidLedgerTransaction() {
return new LedgerTransactionFixture();
Expand Down Expand Up @@ -149,8 +149,8 @@ public static LedgerTransactionFixture from(RefundEntity refundEntity) {
.withExternalId(refundEntity.getExternalId())
.withGatewayTransactionId(refundEntity.getGatewayTransactionId())
.withParentTransactionId(refundEntity.getChargeExternalId())
.withUserEmail(refundEntity.getUserEmail())
.withUserExternalId(refundEntity.getUserExternalId());
.withRefundedByUserEmail(refundEntity.getUserEmail())
.withRefundedBy(refundEntity.getUserExternalId());

return ledgerTransactionFixture;
}
Expand Down Expand Up @@ -204,8 +204,8 @@ public LedgerTransaction build() {
ledgerTransaction.setRefundSummary(refundSummary);

ledgerTransaction.setParentTransactionId(parentTransactionId);
ledgerTransaction.setUserEmail(userEmail);
ledgerTransaction.setUserExternalId(userExternalId);
ledgerTransaction.setRefundedBy(refundedBy);
ledgerTransaction.setRefundedByUserEmail(refundedByUserEmail);

return ledgerTransaction;
}
Expand Down Expand Up @@ -340,13 +340,13 @@ public LedgerTransactionFixture withParentTransactionId(String chargeExternalId)
return this;
}

public LedgerTransactionFixture withUserEmail(String userEmail) {
this.userEmail = userEmail;
public LedgerTransactionFixture withRefundedBy(String userExternalId) {
this.refundedBy = userExternalId;
return this;
}

public LedgerTransactionFixture withUserExternalId(String userExternalId) {
this.userExternalId = userExternalId;
public LedgerTransactionFixture withRefundedByUserEmail(String userEmail) {
this.refundedByUserEmail = userEmail;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public void parityCheck_shouldReturnDataMismatchIfRefundDoesNotMatchWithLedger()

@Test
public void parityCheck_shouldReturnDataMismatchIfUserEmailDoesNotMatchWithLedger() {
LedgerTransaction transaction = from(refundEntity).withUserEmail(null).build();
LedgerTransaction transaction = from(refundEntity).withRefundedByUserEmail(null).build();
assertParityCheckStatus(transaction, DATA_MISMATCH);
}

@Test
public void parityCheck_shouldReturnDataMismatchIfUserExternalIdDoesNotMatchWithLedger() {
LedgerTransaction transaction = from(refundEntity).withUserExternalId(null).build();
LedgerTransaction transaction = from(refundEntity).withRefundedBy(null).build();
assertParityCheckStatus(transaction, DATA_MISMATCH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public class TestTemplateResourceLoader {
public static final String SQS_SEND_MESSAGE_RESPONSE = TEMPLATE_BASE_NAME + "/sqs/send-message-response.xml";
public static final String SQS_ERROR_RESPONSE = TEMPLATE_BASE_NAME + "/sqs/error-response.xml";

public static final String LEDGER_GET_TRANSACTION = TEMPLATE_BASE_NAME + "/ledger/transaction.json";
public static final String LEDGER_PAYMENT_TRANSACTION = TEMPLATE_BASE_NAME + "/ledger/payment_transaction.json";
public static final String LEDGER_REFUND_TRANSACTION = TEMPLATE_BASE_NAME + "/ledger/refund_transaction.json";
public static final String LEDGER_GET_REFUNDS_FOR_PAYMENT = TEMPLATE_BASE_NAME + "/ledger/refunds_for_payment.json";

public static String load(String location) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/templates/ledger/refund_transaction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"gateway_account_id": "1821",
"amount": 1000,
"state": {
"finished": true,
"status": "success"
},
"created_date": "2020-07-20T13:39:38.940Z",
"gateway_transaction_id": "re_1H6zDTHj08j2jFuBtG6maiHK11",
"transaction_type": "REFUND",
"transaction_id": "external-id",
"parent_transaction_id": "64pcdagc9c13vgi7n904aio3n9",
"refunded_by": "refunded-by-external-id",
"refunded_by_user_email": "[email protected]"
}

0 comments on commit 7834390

Please sign in to comment.