DefaultWorldpayomsOrderModificationRefundProcessStrategy implements WorldpayOrderModificationRefundProcessStrategy { @Override public boolean processRefundFollowOn(final OrderModel orderModel, final OrderNotificationMessage notificationMessage) { final String refundReference = notificationMessage.getPaymentReply().getRefundReference(); if (refundReference != null) { final List<ReturnRequestModel> returnRequests = orderModel.getReturnRequests(); if (CollectionUtils.isNotEmpty(returnRequests)) { final Optional<ReturnRequestModel> returnRequest = findMatchingReturnRequest(refundReference, returnRequests); if (returnRequest.isPresent()) { final ReturnRequestModel matchingReturnRequest = returnRequest.get(); matchingReturnRequest.setStatus(PAYMENT_REVERSED); modelService.save(matchingReturnRequest); final Collection<ReturnProcessModel> returnProcesses = matchingReturnRequest.getReturnProcess(); final ReturnProcessModel returnProcessModel = returnProcesses.iterator().next(); triggerOrderProcessEvent(REFUND_FOLLOW_ON, returnProcessModel); return true; } } } else { LOG.warn("Refund reference is null. The refund will never be processed against any of the return requests of the order"); } return false; }  @Override boolean processRefundFollowOn(final OrderModel orderModel, final OrderNotificationMessage notificationMessage); @Required void setBusinessProcessService(final BusinessProcessService businessProcessService); @Required void setModelService(final ModelService modelService);  }
@Test public void shouldTriggerReturnProcessWhenNotificationIsREFUND() { testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); verify(businessProcessServiceMock).triggerEvent(RETURN_PROCESS_CODE + "_" + REFUND_FOLLOW_ON.toString()); }
@Test public void shouldMarkReturnRequestAsRefunded() { testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); verify(returnRequestMock1).setStatus(PAYMENT_REVERSED); verify(modelServiceMock).save(returnRequestMock1); }
@Test public void shouldTriggerReturnProcessWhenNotificationIsREFUNDAndThereAreMultipleReturnRequest() { when(orderModelMock.getReturnRequests()).thenReturn(Arrays.asList(returnRequestMock1, returnRequestMock2)); when(returnRequestMock2.getReturnProcess()).thenReturn(singletonList(refundWaitingReturnProcessMock2)); when(refundWaitingReturnProcessMock2.getCode()).thenReturn(RETURN_PROCESS_CODE + "_INVALID"); testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); verify(businessProcessServiceMock).triggerEvent(RETURN_PROCESS_CODE + "_" + REFUND_FOLLOW_ON.toString()); }
@Test public void shouldNotTriggerReturnProcessWhenRefundReferenceIsNotEqualToTransactionEntryCode() { when(paymentTransactionEntryMock.getCode()).thenReturn(REFUND_REFERENCE + "_INVALID"); testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); verifyZeroInteractions(businessProcessServiceMock); }
@Test public void shouldNotTriggerReturnProcessWhenNoReturnRequestsOnOrder() { when(orderModelMock.getReturnRequests()).thenReturn(null); testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); verifyZeroInteractions(businessProcessServiceMock); }
@Test public void shouldTriggerCorrectProcessForCancelledReturnRequest() { when(returnRequestMock1.getStatus()).thenReturn(CANCELED); final boolean result = testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); verifyZeroInteractions(businessProcessServiceMock); assertFalse(result); }
@Test public void shouldTriggerCorrectProcessWhenOneIsCancelled() { when(orderModelMock.getReturnRequests()).thenReturn(Arrays.asList(returnRequestMock1, returnRequestMock2)); when(returnRequestMock1.getStatus()).thenReturn(CANCELED); when(returnRequestMock2.getStatus()).thenReturn(PAYMENT_REVERSAL_PENDING); when(returnRequestMock2.getReturnProcess()).thenReturn(singletonList(refundWaitingReturnProcessMock2)); when(refundWaitingReturnProcessMock2.getCode()).thenReturn(RETURN_PROCESS_CODE); final boolean result = testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); verify(businessProcessServiceMock).triggerEvent(RETURN_PROCESS_CODE + "_" + REFUND_FOLLOW_ON.toString()); verifyNoMoreInteractions(businessProcessServiceMock); assertTrue(result); }
@Test public void shoudReturnFalseWhenRefundReferenceIsNull() throws Exception { when(orderNotificationMessageMock.getPaymentReply().getRefundReference()).thenReturn(null); final boolean result = testObj.processRefundFollowOn(orderModelMock, orderNotificationMessageMock); assertFalse(result); }