From 470d9f18e68407c78327ec0eccc063537b242873 Mon Sep 17 00:00:00 2001 From: Dominic Belcher Date: Fri, 28 Jun 2024 09:28:47 +0100 Subject: [PATCH] PP-12826 Update RefundRequest class to record --- .../connector/refund/model/RefundRequest.java | 41 ++++++------------- .../refund/resource/RefundsResource.java | 2 +- .../refund/service/RefundService.java | 16 ++++---- 3 files changed, 21 insertions(+), 38 deletions(-) diff --git a/src/main/java/uk/gov/pay/connector/refund/model/RefundRequest.java b/src/main/java/uk/gov/pay/connector/refund/model/RefundRequest.java index 13e32ff7a1..36cd6b0e02 100644 --- a/src/main/java/uk/gov/pay/connector/refund/model/RefundRequest.java +++ b/src/main/java/uk/gov/pay/connector/refund/model/RefundRequest.java @@ -3,46 +3,29 @@ import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.v3.oas.annotations.media.Schema; -public class RefundRequest { - +public record RefundRequest ( @JsonProperty("amount") @Schema(example = "3444", required = true, description = "Amount to refund in pence") - private long amount; + long amount, @JsonProperty("refund_amount_available") @Schema(example = "30000", required = true, description = "Total amount still available before issuing the refund") - private long amountAvailableForRefund; + long amountAvailableForRefund, @JsonProperty("user_external_id") @Schema(example = "3444", description = "The ID of the user who issued the refund") - private String userExternalId; + String userExternalId, @JsonProperty("user_email") @Schema(example = "joeb@example.org", description = "Email address of the user refunding payment") - private String userEmail; - - public RefundRequest() { - } - + String userEmail +) { public RefundRequest(long amount, long amountAvailableForRefund, String userExternalId) { - this.amount = amount; - this.amountAvailableForRefund = amountAvailableForRefund; - this.userExternalId = userExternalId; - } - - public long getAmount() { - return amount; - } - - public long getAmountAvailableForRefund() { - return amountAvailableForRefund; - } - - public String getUserExternalId() { - return userExternalId; - } - - public String getUserEmail() { - return userEmail; + this( + amount, + amountAvailableForRefund, + userExternalId, + null + ); } } diff --git a/src/main/java/uk/gov/pay/connector/refund/resource/RefundsResource.java b/src/main/java/uk/gov/pay/connector/refund/resource/RefundsResource.java index 678939256c..e9e83d2d56 100644 --- a/src/main/java/uk/gov/pay/connector/refund/resource/RefundsResource.java +++ b/src/main/java/uk/gov/pay/connector/refund/resource/RefundsResource.java @@ -84,7 +84,7 @@ public Response submitRefund(@Parameter(example = "1", description = "Gateway ac @Parameter(example = "2c6vtn9pth38ppbmnt20d57t49", description = "Charge external ID") @PathParam("chargeId") String chargeExternalId, RefundRequest refundRequest, @Context UriInfo uriInfo) { - validateRefundRequest(refundRequest.getAmount()); + validateRefundRequest(refundRequest.amount()); ChargeRefundResponse refundServiceResponse = chargeService.findCharge(chargeExternalId) .map(charge -> refundService.doRefund(accountId, charge, refundRequest)) diff --git a/src/main/java/uk/gov/pay/connector/refund/service/RefundService.java b/src/main/java/uk/gov/pay/connector/refund/service/RefundService.java index a50cafe897..060db65869 100644 --- a/src/main/java/uk/gov/pay/connector/refund/service/RefundService.java +++ b/src/main/java/uk/gov/pay/connector/refund/service/RefundService.java @@ -117,10 +117,10 @@ public RefundEntity createRefund(Charge charge, GatewayAccountEntity gatewayAcco charge.getGatewayTransactionId(), gatewayAccountEntity.getId(), availableAmount, - refundRequest.getAmount(), + refundRequest.amount(), charge.getPaymentGatewayName(), gatewayAccountEntity.getType(), - refundRequest.getUserExternalId()); + refundRequest.userExternalId()); return refundEntity; } @@ -203,8 +203,8 @@ private RefundStatus determineRefundStatus(GatewayRefundResponse gatewayRefundRe @Transactional @SuppressWarnings("WeakerAccess") public RefundEntity createRefundEntity(RefundRequest refundRequest, GatewayAccountEntity gatewayAccountEntity, Charge charge) { - RefundEntity refundEntity = new RefundEntity(refundRequest.getAmount(), - refundRequest.getUserExternalId(), refundRequest.getUserEmail(), charge.getExternalId()); + RefundEntity refundEntity = new RefundEntity(refundRequest.amount(), + refundRequest.userExternalId(), refundRequest.userEmail(), charge.getExternalId()); transitionRefundState(refundEntity, gatewayAccountEntity, RefundStatus.CREATED, charge); refundDao.persist(refundEntity); @@ -229,15 +229,15 @@ public void transitionRefundState(RefundEntity refundEntity, GatewayAccountEntit } private void checkIfRefundRequestIsInConflictOrTerminate(RefundRequest refundRequest, Charge reloadedCharge, long totalAmountToBeRefunded) { - if (totalAmountToBeRefunded != refundRequest.getAmountAvailableForRefund()) { + if (totalAmountToBeRefunded != refundRequest.amountAvailableForRefund()) { logger.info("Refund request has a mismatch on amount available for refund - charge_external_id={}, amount_actually_available_for_refund={}, refund_amount_available_in_request={}", - reloadedCharge.getExternalId(), totalAmountToBeRefunded, refundRequest.getAmountAvailableForRefund()); + reloadedCharge.getExternalId(), totalAmountToBeRefunded, refundRequest.amountAvailableForRefund()); throw RefundException.refundAmountAvailableMismatchException("Refund Amount Available Mismatch"); } } private void checkIfRefundAmountWithinLimitOrTerminate(RefundRequest refundRequest, Charge reloadedCharge, ExternalChargeRefundAvailability refundAvailability, GatewayAccountEntity gatewayAccount, long totalAmountToBeRefunded) { - if (totalAmountToBeRefunded - refundRequest.getAmount() < 0) { + if (totalAmountToBeRefunded - refundRequest.amount() < 0) { logger.info("Charge doesn't have sufficient amount for refund - charge_external_id={}, status={}, refund_status={}, account_id={}, operation_type=Refund, provider={}, provider_type={}, amount_available_refund={}, amount_requested_refund={}", reloadedCharge.getExternalId(), @@ -247,7 +247,7 @@ private void checkIfRefundAmountWithinLimitOrTerminate(RefundRequest refundReque reloadedCharge.getPaymentGatewayName(), gatewayAccount.getType(), totalAmountToBeRefunded, - refundRequest.getAmount()); + refundRequest.amount()); throw RefundException.notAvailableForRefundException("Not sufficient amount available for refund", NOT_SUFFICIENT_AMOUNT_AVAILABLE); }