WorldpayCaptureRefundAction extends AbstractSimpleDecisionAction<ReturnProcessModel> { @Override public Transition executeAction(final ReturnProcessModel process) { LOG.debug("Process: " + process.getCode() + " in step " + getClass().getSimpleName()); final ReturnRequestModel returnRequest = process.getReturnRequest(); final List<PaymentTransactionModel> transactions = returnRequest.getOrder().getPaymentTransactions(); if (transactions.isEmpty()) { LOG.info("Unable to refund for ReturnRequest " + returnRequest.getCode() + ", no PaymentTransactions found"); setReturnRequestStatus(returnRequest, PAYMENT_REVERSAL_FAILED); return NOK; } final PaymentTransactionModel transaction = transactions.get(0); final BigDecimal customRefundAmount = refundAmountCalculationService.getCustomRefundAmount(returnRequest); BigDecimal amountToRefund; if (customRefundAmount != null && customRefundAmount.compareTo(BigDecimal.ZERO) > 0) { amountToRefund = customRefundAmount; } else { amountToRefund = refundAmountCalculationService.getOriginalRefundAmount(returnRequest); } Transition result; try { final PaymentTransactionEntryModel refundPaymentTransactionEntry = getPaymentService().refundFollowOn(transaction, amountToRefund); returnRequest.setPaymentTransactionEntry(refundPaymentTransactionEntry); setReturnRequestStatus(returnRequest, PAYMENT_REVERSAL_PENDING); result = OK; } catch (final AdapterException e) { LOG.error(format("Unable to refund for ReturnRequest [{0}]", returnRequest.getCode()), e); setReturnRequestStatus(returnRequest, PAYMENT_REVERSAL_FAILED); result = NOK; } return result; }  @Override Transition executeAction(final ReturnProcessModel process); @Required void setPaymentService(final PaymentService paymentService); @Required void setRefundAmountCalculationService(final RefundAmountCalculationService refundAmountCalculationService);  }
@Test public void shouldReturnNOKWhenThereAreNoTransactions() { when(returnRequestMock.getOrder().getPaymentTransactions()).thenReturn(emptyList()); final Transition transition = testObj.executeAction(returnProcessModelMock); verify(returnRequestMock).setStatus(PAYMENT_REVERSAL_FAILED); verify(modelServiceMock).save(returnRequestMock); assertEquals(NOK, transition); }
@Test public void shouldReturnOKWhenRefundFollowOnIsSuccessfulWithCustomRefundAmount() { when(returnRequestMock.getOrder().getPaymentTransactions()).thenReturn(singletonList(paymentTransactionMock)); when(refundAmountCalculationServiceMock.getCustomRefundAmount(returnRequestMock)).thenReturn(CUSTOM_REFUND_AMOUNT); when(paymentServiceMock.refundFollowOn(paymentTransactionMock, CUSTOM_REFUND_AMOUNT)).thenReturn(paymentTransactionEntryMock); final Transition transition = testObj.executeAction(returnProcessModelMock); verify(returnRequestMock).setStatus(PAYMENT_REVERSAL_PENDING); verify(returnRequestMock).setPaymentTransactionEntry(paymentTransactionEntryMock); verify(modelServiceMock).save(returnRequestMock); assertEquals(OK, transition); }
@Test public void shouldReturnOKWhenRefundFollowOnIsSuccessfulWithOriginalAmount() { when(returnRequestMock.getOrder().getPaymentTransactions()).thenReturn(singletonList(paymentTransactionMock)); when(refundAmountCalculationServiceMock.getCustomRefundAmount(returnRequestMock)).thenReturn(null); when(refundAmountCalculationServiceMock.getOriginalRefundAmount(returnRequestMock)).thenReturn(ORIGINAL_REFUND_AMOUNT); when(paymentServiceMock.refundFollowOn(paymentTransactionMock, ORIGINAL_REFUND_AMOUNT)).thenReturn(paymentTransactionEntryMock); final Transition transition = testObj.executeAction(returnProcessModelMock); verify(returnRequestMock).setStatus(PAYMENT_REVERSAL_PENDING); verify(returnRequestMock).setPaymentTransactionEntry(paymentTransactionEntryMock); verify(modelServiceMock).save(returnRequestMock); assertEquals(OK, transition); }
@Test public void shouldReturnNOKWhenRefundFollowOnFails() { when(returnRequestMock.getOrder().getPaymentTransactions()).thenReturn(singletonList(paymentTransactionMock)); when(refundAmountCalculationServiceMock.getCustomRefundAmount(returnRequestMock)).thenReturn(null); when(refundAmountCalculationServiceMock.getOriginalRefundAmount(returnRequestMock)).thenReturn(ORIGINAL_REFUND_AMOUNT); when(paymentServiceMock.refundFollowOn(paymentTransactionMock, ORIGINAL_REFUND_AMOUNT)).thenThrow(new AdapterException("errorMessage")); final Transition transition = testObj.executeAction(returnProcessModelMock); verify(returnRequestMock).setStatus(PAYMENT_REVERSAL_FAILED); verify(returnRequestMock, never()).setPaymentTransactionEntry(any(PaymentTransactionEntryModel.class)); verify(modelServiceMock).save(returnRequestMock); assertEquals(NOK, transition); }