Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pay 5323 fixes #1173

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fcbf5c7
Remapped checkRefundsAgainstRemission method
arbab-cog Apr 11, 2022
bbd1681
Update CaseControllerTest.java
arbab-cog Apr 12, 2022
744f452
LOGS ADDED
arbab-cog Apr 12, 2022
c1b862a
Update build.gradle
arbab-cog Apr 12, 2022
6f49d68
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 12, 2022
a31dec5
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 12, 2022
eaa1d8f
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 12, 2022
dc9ec2f
rework on checkRefundAgainstRemissionV2 method
arbab-cog Apr 12, 2022
8c5785a
fix
arbab-cog Apr 12, 2022
510305d
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 12, 2022
771e5c2
LOGS ADDED
arbab-cog Apr 12, 2022
854427b
remappped method
arbab-cog Apr 13, 2022
dc7b51f
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 13, 2022
2ef5c8b
null pointer exception
arbab-cog Apr 13, 2022
bcc33e1
fixed null pointer
arbab-cog Apr 13, 2022
0989aa0
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 13, 2022
5bca4fe
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 13, 2022
3c8e8f0
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 14, 2022
2e16abc
code clean up
arbab-cog Apr 14, 2022
dcdfe2b
checkRefundAgainstRemissionFeeApportionV2 added
arbab-cog Apr 14, 2022
b50b3bf
LOGS ADDED
arbab-cog Apr 14, 2022
09392b2
loop fix
arbab-cog Apr 14, 2022
dad6923
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 14, 2022
fea0209
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 14, 2022
c881064
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 14, 2022
639705b
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 14, 2022
b456d32
Update PaymentRefundsServiceImpl.java
arbab-cog Apr 14, 2022
edf8335
Bumping chart version/ fixing aliases
hmcts-jenkins-a-to-c[bot] Oct 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ public class FeeDto {
private Date dateReceiptProcessed;

private Boolean remissionEnable;

private boolean issueRefundAddRefundAddRemission;

private boolean addRemission;

@Override
public String toString() {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public PaymentGroupResponse retrieveCasePaymentGroups(@PathVariable(name = "ccdc

PaymentGroupResponse paymentGroupResponse = new PaymentGroupResponse(paymentGroups);

paymentGroupResponse = paymentRefundsService.checkRefundAgainstRemission(headers, paymentGroupResponse, ccdCaseNumber);
paymentGroupResponse = paymentRefundsService.checkRefundAgainstRemissionV2(headers, paymentGroupResponse, ccdCaseNumber);

return paymentGroupResponse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ResponseEntity<PaymentGroupDto> retrieveApportionDetails(@PathVariable("p

}
PaymentGroupDto paymentGroupDto = paymentGroupDtoMapper.toPaymentGroupDto(paymentFeeLink);
paymentGroupDto = paymentRefundsService.checkRefundAgainstRemissionFeeApportion(headers, paymentGroupDto, paymentReference);
paymentGroupDto = paymentRefundsService.checkRefundAgainstRemissionFeeApportionV2(headers, paymentGroupDto, paymentReference);
return new ResponseEntity<>(paymentGroupDto, HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ ResponseEntity<RefundResponse> createAndValidateRetrospectiveRemissionRequest(

PaymentGroupResponse checkRefundAgainstRemission(MultiValueMap<String, String> headers, PaymentGroupResponse paymentGroupResponse, String ccdCaseNumber);


PaymentGroupResponse checkRefundAgainstRemissionV2(MultiValueMap<String, String> headers, PaymentGroupResponse paymentGroupResponse, String ccdCaseNumber);

PaymentGroupDto checkRefundAgainstRemissionFeeApportion(MultiValueMap<String, String> headers, PaymentGroupDto paymentGroupDto, String paymentRef);

PaymentGroupDto checkRefundAgainstRemissionFeeApportionV2(MultiValueMap<String, String> headers, PaymentGroupDto paymentGroupDto, String paymentRef);


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.hmcts.payment.api.service;

import java.math.BigInteger;
import java.time.temporal.ChronoUnit;

import org.apache.commons.lang3.EnumUtils;
Expand All @@ -19,8 +20,9 @@
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import uk.gov.hmcts.payment.api.contract.FeeDto;
import uk.gov.hmcts.payment.api.configuration.LaunchDarklyFeatureToggler;
import uk.gov.hmcts.payment.api.contract.FeeDto;
import uk.gov.hmcts.payment.api.contract.PaymentDto;
import uk.gov.hmcts.payment.api.contract.RefundsFeeDto;
import uk.gov.hmcts.payment.api.dto.*;
import uk.gov.hmcts.payment.api.exception.InvalidPartialRefundRequestException;
Expand All @@ -34,6 +36,7 @@

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -207,6 +210,258 @@ public ResponseEntity updateTheRemissionAmount(String paymentReference, Resubmit
return new ResponseEntity<>(null, HttpStatus.OK);
}


public boolean checkRefundsRole(PaymentGroupDto paymentGroupDto){
for(PaymentDto payment : paymentGroupDto.getPayments()){
if(payment.getRefundEnable()){
return true;
}
}
return false;
}

public BigDecimal getAvailableBalance(PaymentGroupDto paymentGroupDto, RefundListDtoResponse refundListDtoResponse){

BigDecimal totalPaymentAmount = new BigDecimal(BigInteger.ZERO);
BigDecimal totalRefundAmount = new BigDecimal(BigInteger.ZERO);

//Goes through each payment in this paymentDto, and get the total payment amount
for (PaymentDto payment : paymentGroupDto.getPayments()) {

if(payment.getStatus() == "Success") {
totalPaymentAmount = totalPaymentAmount.add(payment.getAmount());
}
//if theres a refund available for this payment, get the total refund amount
if (refundListDtoResponse != null) {
for (RefundDto refundDto : refundListDtoResponse.getRefundList()) {

//Condition to check that a valid refund corresponding with the payment reference is considered only
if (refundDto.getPaymentReference().equals(payment.getPaymentReference())
&& (refundDto.getRefundStatus().getName().equals("Accepted") || refundDto.getRefundStatus().getName().equals("Approved")))
totalRefundAmount = totalRefundAmount.add(refundDto.getAmount());
}
}
}
return totalPaymentAmount.subtract(totalRefundAmount);
}

public List<String> getAllRefundedFeeIds(RefundListDtoResponse refundListDtoResponse){
List<String> refundedFees = new ArrayList<String>();

if(refundListDtoResponse !=null) {
for (RefundDto refundDto : refundListDtoResponse.getRefundList()) {
if (refundDto.getReason().equals("Retrospective remission")) {
refundedFees = Arrays.asList(refundDto.getFeeIds().split(","));
}
}
}
return refundedFees;
}



public PaymentGroupDto checkRefundAgainstRemissionFeeApportionV2(MultiValueMap<String, String> headers,
PaymentGroupDto paymentGroupDto, String paymentReference) {
//check roles
if(isContainsPaymentsRefundRole()) {

Payment payment = paymentRepository.findByReference(paymentReference).orElseThrow(PaymentNotFoundException::new);
BigDecimal balanceAvailable;
Boolean refundRole;

//get the RefundListDtoResponse by calling refunds app
RefundListDtoResponse refundListDtoResponse = getRefundsFromRefundService(payment.getCcdCaseNumber(), headers);
LOG.info("refundListDtoResponse : {}", refundListDtoResponse);

//gets a list of all the refunded fee ids for this case
List<String> refundedFees= getAllRefundedFeeIds(refundListDtoResponse);

for(PaymentDto paymentDto : paymentGroupDto.getPayments()){
LOG.info("INSIDE MAIN LOOP");

boolean activeRemission = false;
refundRole = checkRefundsRole(paymentGroupDto);
balanceAvailable = getAvailableBalance(paymentGroupDto, refundListDtoResponse);

//Main check: if the payment has refund role and there is available balance
if(refundRole && balanceAvailable.compareTo(BigDecimal.ZERO) > 0){
LOG.info("BALANCE IS AVAILABLE");

//Goes through each fee in a paymentGroupDto
for (FeeDto fee : paymentGroupDto.getFees()) {
LOG.info("INSIDE FEE LOOP");

//Check that there is a remission object
if(!paymentGroupDto.getRemissions().isEmpty()){
LOG.info("THERE IS A REMISSION");

//Goes through each remission in paymentGroupDto
for (RemissionDto remission : paymentGroupDto.getRemissions()) {
LOG.info("INSIDE REMISSION LOOP");

//Makes sure that the fee ID matches with the fee ID in remission
if (fee.getId() == remission.getFeeId()) {
LOG.info("FEE ID MATCHES REMISSION FEE ID");


//IF THERE IS NO PROCESSED REFUND FOR THE FEE BUT THERE IS AN ACTIVE REMISSION
if (!refundedFees.stream().anyMatch(fee.getId().toString()::equals)
&& fee.getId() == remission.getFeeId()) {
LOG.info("ENTERED NO PROCESSED REFUND IF");

activeRemission =true;
fee.setAddRemission(false);
remission.setAddRefund(true);
paymentDto.setIssueRefund(false);
}
//IF THERE IS A PROCESSED REFUND FOR THE FEE
else if (refundedFees.stream().anyMatch(fee.getId().toString()::equals)) {
LOG.info("ENTERED PROCESSED REFUND ELSEIF");

fee.setAddRemission(false);
remission.setAddRefund(false);
paymentDto.setIssueRefund(true);

}
//NO PROCESSED OR OUTSTANDING REMISSION
else if (!refundedFees.stream().anyMatch(fee.getId().toString()::equals)
&& fee.getId() != remission.getFeeId()) {
LOG.info("ENTERED NO PROCESSED OR OUTSTANDING REFUND ELSEIF");

fee.setAddRemission(true);
remission.setAddRefund(false);
paymentDto.setIssueRefund(true);
}
}
//If the fee does not have a remission, check if theres any other active remissions in this payment group
else{
LOG.info("FEE ID DOESNT MATCH REMISSION FEE ID");
if(activeRemission){
fee.setAddRemission(false);
}else {
fee.setAddRemission(true);
}
}
}
}
else{
LOG.info("THERE IS NO REMISSION");

paymentDto.setIssueRefund(true);
fee.setAddRemission(true);
}
}

for (FeeDto fee : paymentGroupDto.getFees()) {
if(activeRemission){
fee.setAddRemission(false);
}
}
}
}
}
return paymentGroupDto;
}

@Override
public PaymentGroupResponse checkRefundAgainstRemissionV2(MultiValueMap<String, String> headers,
PaymentGroupResponse paymentGroupResponse, String ccdCaseNumber) {
//check roles
if(isContainsPaymentsRefundRole()) {

BigDecimal balanceAvailable;
Boolean refundRole;

//get the RefundListDtoResponse by calling refunds app
RefundListDtoResponse refundListDtoResponse = getRefundsFromRefundService(ccdCaseNumber, headers);
LOG.info("refundListDtoResponse : {}", refundListDtoResponse);

//gets a list of all the refunded fee ids for this case
List<String> refundedFees= getAllRefundedFeeIds(refundListDtoResponse);

for(PaymentGroupDto paymentGroupDto : paymentGroupResponse.getPaymentGroups()){

boolean activeRemission = false;
refundRole = checkRefundsRole(paymentGroupDto);
balanceAvailable = getAvailableBalance(paymentGroupDto, refundListDtoResponse);

//Main check: if the payment has refund role and there is available balance
if(refundRole && balanceAvailable.compareTo(BigDecimal.ZERO) > 0){

//Goes through each fee in a paymentGroupDto
for (FeeDto fee : paymentGroupDto.getFees()) {
//Check that there is a remission object
if(!paymentGroupDto.getRemissions().isEmpty()){
//Goes through each remission in paymentGroupDto
for (RemissionDto remission : paymentGroupDto.getRemissions()) {
//Makes sure that the fee ID matches with the fee ID in remission
if (fee.getId() == remission.getFeeId()) {

//IF THERE IS NO PROCESSED REFUND FOR THE FEE BUT THERE IS AN ACTIVE REMISSION
if (!refundedFees.stream().anyMatch(fee.getId().toString()::equals)
&& fee.getId() == remission.getFeeId()) {
LOG.info("ENTERED NO PROCESSED REFUND IF");

activeRemission =true;
fee.setAddRemission(false);
remission.setAddRefund(true);
for (PaymentDto payment : paymentGroupDto.getPayments()) {
payment.setIssueRefund(false);
}
}
//IF THERE IS A PROCESSED REFUND FOR THE FEE
else if (refundedFees.stream().anyMatch(fee.getId().toString()::equals)) {
LOG.info("ENTERED PROCESSED REFUND ELSEIF");

fee.setAddRemission(false);
remission.setAddRefund(false);
for (PaymentDto payment : paymentGroupDto.getPayments()) {
payment.setIssueRefund(true);
}
}
//NO PROCESSED OR OUTSTANDING REMISSION
else if (!refundedFees.stream().anyMatch(fee.getId().toString()::equals)
&& fee.getId() != remission.getFeeId()) {
LOG.info("ENTERED NO PROCESSED OR OUTSTANDING REFUND ELSEIF");

fee.setAddRemission(true);
remission.setAddRefund(false);
for (PaymentDto payment : paymentGroupDto.getPayments()) {
payment.setIssueRefund(true);
}
}
}
//If the fee does not have a remission, check if theres any other active remissions in this payment group
else{
if(activeRemission){
fee.setAddRemission(false);
}else {
fee.setAddRemission(true);
}
}
}
}
else{
for (PaymentDto payment : paymentGroupDto.getPayments()) {
payment.setIssueRefund(true);
}
fee.setAddRemission(true);
}
}

for (FeeDto fee : paymentGroupDto.getFees()) {
if(activeRemission){
fee.setAddRemission(false);
}
}
}
}
}
return paymentGroupResponse;
}



@Override
public PaymentGroupResponse checkRefundAgainstRemission(MultiValueMap<String, String> headers,
PaymentGroupResponse paymentGroupResponse, String ccdCaseNumber) {
Expand Down Expand Up @@ -371,7 +626,8 @@ private RefundListDtoResponse getRefundsFromRefundService(String ccdCaseNumber,

} catch (HttpClientErrorException e) {

LOG.error("client err ", e);
LOG.error("client err ", e.getStatusText());


refundListDtoResponse = null;

Expand Down
Loading