DefaultWorldpayOrderModificationProcessStrategy implements WorldpayOrderModificationProcessStrategy { @Override public boolean processOrderModificationMessages(final PaymentTransactionType paymentTransactionType) { boolean success = true; final List<WorldpayOrderModificationModel> orderModificationsByType = orderModificationDao.findUnprocessedOrderModificationsByType(paymentTransactionType); for (final WorldpayOrderModificationModel orderModificationModel : orderModificationsByType) { final String worldpayOrderCode = orderModificationModel.getWorldpayOrderCode(); if (REFUSED.equals(paymentTransactionType)) { LOG.info(format("Marking order modification with [{0}] transaction for refused worldpayOrder [{1}] as processed", paymentTransactionType, worldpayOrderCode)); worldpayOrderNotificationHandler.setNonDefectiveAndProcessed(orderModificationModel); } else { success = precessOrderModificationsMessagesNotRefused(paymentTransactionType, orderModificationModel, worldpayOrderCode); } } return success; }  @Override boolean processOrderModificationMessages(final PaymentTransactionType paymentTransactionType); @Required void setOrderModificationDao(OrderModificationDao orderModificationDao); @Required void setBusinessProcessService(BusinessProcessService businessProcessService); @Required void setProcessDefinitionDao(ProcessDefinitionDao processDefinitionDao); @Required void setOrderNotificationService(OrderNotificationService orderNotificationService); @Required void setOrderModificationSerialiser(OrderModificationSerialiser orderModificationSerialiser); @Required void setWorldpayPaymentTransactionService(WorldpayPaymentTransactionService worldpayPaymentTransactionService); @Required void setNonTriggeringOrderStatuses(Set<OrderStatus> nonTriggeringOrderStatuses); @Required void setWorldpayAuthenticatedShopperIdStrategy(final WorldpayAuthenticatedShopperIdStrategy worldpayAuthenticatedShopperIdStrategy); @Required void setWorldpayOrderModificationRefundProcessStrategy(final WorldpayOrderModificationRefundProcessStrategy worldpayOrderModificationRefundProcessStrategy); @Required void setWorldpayCartService(final WorldpayCartService worldpayCartService); @Required void setWorldpayOrderNotificationHandler(final WorldpayOrderNotificationHandler worldpayOrderNotificationHandler); @Required void setWorldpayPlaceOrderFromNotificationStrategy(final WorldpayPlaceOrderFromNotificationStrategy worldpayPlaceOrderFromNotificationStrategy);  }
@Test public void processOrderModificationMessagesShouldProcessIfWorldpayOrderCodeRelatesToOrder() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, CAPTURE, orderModelMock)).thenReturn(true); final boolean result = testObj.processOrderModificationMessages(CAPTURE); assertTrue(result); verify(orderNotificationServiceMock).processOrderNotificationMessage(orderNotificationMessageMock); verify(businessProcessServiceMock).triggerEvent(CAPTURE_BPM_PROCESS_CODE + "_" + CAPTURE.getCode()); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }
@Test public void processOrderModificationMessagesShouldMarkOrderNotificationAsProcessedAndNotTriggerABusinessProcessEventWhenOrderModificationTypeIsSETTLED() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, SETTLED, orderModelMock)).thenReturn(Boolean.TRUE); final boolean result = testObj.processOrderModificationMessages(SETTLED); assertTrue(result); verify(orderNotificationServiceMock).processOrderNotificationMessage(orderNotificationMessageMock); verify(businessProcessServiceMock, never()).triggerEvent(anyString()); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }
@Test public void processOrderModificationMessagesShouldDoNothingIfWorldpayOrderCodeRelatesToCart() { when(paymentTransactionModelMock.getOrder()).thenReturn(cartModelMock); final boolean result = testObj.processOrderModificationMessages(CAPTURE); assertTrue(result); verify(businessProcessServiceMock, never()).triggerEvent(anyString()); verify(orderModificationModelMock, never()).setProcessed(Boolean.TRUE); verify(orderModificationModelMock, never()).setDefective(any(Boolean.class)); verify(modelServiceMock, never()).save(any(WorldpayOrderModificationModel.class)); }
@Test public void processOrderModificationMessagesShouldDoNothingIfNotSingleBusinessProcessFound() { when(processDefinitionDaoMock.findWaitingOrderProcesses(ORDER_CODE, CAPTURE)).thenReturn(asList(authoriseWaitingProcessMock, captureWaitingProcessMock)); when(paymentTransactionModelMock.getOrder()).thenReturn(cartModelMock); final boolean result = testObj.processOrderModificationMessages(CAPTURE); assertTrue(result); verify(businessProcessServiceMock, never()).triggerEvent(anyString()); verify(orderModificationModelMock, never()).setProcessed(Boolean.TRUE); verify(orderModificationModelMock, never()).setDefective(any(Boolean.class)); verify(modelServiceMock, never()).save(any(WorldpayOrderModificationModel.class)); }
@Test public void processOrderModificationMessageShouldProcessNotificationIfPreviousNotificationIsNotPending() { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, CAPTURE, orderModelMock)).thenReturn(Boolean.TRUE); final boolean result = testObj.processOrderModificationMessages(CAPTURE); verify(businessProcessServiceMock).triggerEvent(endsWith(CAPTURE.getCode())); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); verify(processDefinitionDaoMock).findWaitingOrderProcesses(eq(ORDER_CODE), any(PaymentTransactionType.class)); assertTrue(result); }
@Test public void processOrderModificationMessageShouldNotProcessNotificationIfPreviousNotificationIsPending() { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, CAPTURE, orderModelMock)).thenReturn(Boolean.FALSE); final boolean result = testObj.processOrderModificationMessages(CAPTURE); assertTrue(result); verify(businessProcessServiceMock, never()).triggerEvent(anyString()); verify(modelServiceMock, never()).save(any(WorldpayOrderModificationModel.class)); verify(processDefinitionDaoMock, never()).findWaitingOrderProcesses(anyString(), any(PaymentTransactionType.class)); }
@Test public void processOrderModificationMessageShouldProcessRefusedPaymentTransaction() { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, REFUSED, orderModelMock)).thenReturn(Boolean.TRUE); when(authoriseWaitingProcessMock.getCode()).thenReturn(AUTHORISE_PROCESS_CODE); final boolean result = testObj.processOrderModificationMessages(REFUSED); assertTrue(result); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }
@Test public void processOrderModificationMessageShouldSetOrderModificationAsDefectiveWhenAnyExceptionIsThrown() throws WorldpayConfigurationException { doThrow(new IllegalArgumentException(EXCEPTION_MESSAGE)).when(orderNotificationServiceMock).processOrderNotificationMessage(any(OrderNotificationMessage.class)); when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, AUTHORIZATION, orderModelMock)).thenReturn(Boolean.TRUE); final boolean result = testObj.processOrderModificationMessages(AUTHORIZATION); assertFalse(result); verify(worldpayOrderNotificationHandlerMock).setDefectiveReason(orderModificationModelMock, PROCESSING_ERROR); verify(worldpayOrderNotificationHandlerMock).setDefectiveModification(eq(orderModificationModelMock), any(Exception.class), eq(true)); }
@Test public void shouldNotWakeUpOrderProcessWhenOrderStatusIsCancelled() { when(orderModelMock.getStatus()).thenReturn(CANCELLED); when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, AUTHORIZATION, orderModelMock)).thenReturn(Boolean.TRUE); testObj.processOrderModificationMessages(AUTHORIZATION); verify(businessProcessServiceMock, never()).triggerEvent(anyString()); }
@Test public void shouldWakeUpOrderProcessWhenOrderStatusIsNotCancelled() { when(orderModelMock.getStatus()).thenReturn(FRAUD_CHECKED); when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, CAPTURE, orderModelMock)).thenReturn(Boolean.TRUE); testObj.processOrderModificationMessages(CAPTURE); verify(businessProcessServiceMock).triggerEvent(endsWith(CAPTURE.getCode())); }
@Test public void shouldMarkAsProcessedWhenOrderNotificationIsAUTHAndTransactionEntryIsNotPending() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.getNotPendingPaymentTransactionEntriesForType(paymentTransactionModelMock, AUTHORIZATION)).thenReturn(singletonList(paymentTransactionEntryMock)); testObj.processOrderModificationMessages(AUTHORIZATION); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); verify(orderNotificationServiceMock, never()).processOrderNotificationMessage(orderNotificationMessageMock); }
@Test public void markNotificationAsDefectiveWhenContainsTokenReplyWithNonMatchingAuthenticatedShopperId() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, AUTHORIZATION, orderModelMock)).thenReturn(Boolean.TRUE); when(orderModificationSerialiserMock.deserialise(orderModificationModelMock.getOrderNotificationMessage())).thenReturn(orderNotificationMessageMock); when(orderNotificationMessageMock.getTokenReply()).thenReturn(tokenReplyMock); testObj.processOrderModificationMessages(AUTHORIZATION); verify(orderNotificationServiceMock, never()).processOrderNotificationMessage(any(OrderNotificationMessage.class)); verify(businessProcessServiceMock, never()).triggerEvent(anyString()); verify(worldpayOrderNotificationHandlerMock).setDefectiveReason(orderModificationModelMock, DefectiveReason.INVALID_AUTHENTICATED_SHOPPER_ID); }
@Test public void markNotificationAsDefectiveWithInvalidAuthenticatedShopperIdReasonWhenContainsTokenReplyWithNonMatchingAuthenticatedShopperId() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, AUTHORIZATION, orderModelMock)).thenReturn(Boolean.TRUE); when(orderModificationSerialiserMock.deserialise(orderModificationModelMock.getOrderNotificationMessage())).thenReturn(orderNotificationMessageMock); when(orderNotificationMessageMock.getTokenReply()).thenReturn(tokenReplyMock); testObj.processOrderModificationMessages(AUTHORIZATION); verify(orderNotificationServiceMock, never()).processOrderNotificationMessage(any(OrderNotificationMessage.class)); verify(businessProcessServiceMock, never()).triggerEvent(anyString()); verify(worldpayOrderNotificationHandlerMock).setDefectiveReason(orderModificationModelMock, INVALID_AUTHENTICATED_SHOPPER_ID); verify(worldpayOrderNotificationHandlerMock).setDefectiveModification(orderModificationModelMock, null, true); }
@Test public void notificationShouldNotBeMarkedAsDefectiveWhenTokenReplyDoesNotContainAuthenticatedShopperIdAsOrderWasMadeUsingAMerchantToken() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, AUTHORIZATION, orderModelMock)).thenReturn(Boolean.TRUE); when(orderModificationSerialiserMock.deserialise(orderModificationModelMock.getOrderNotificationMessage())).thenReturn(orderNotificationMessageMock); when(orderNotificationMessageMock.getTokenReply()).thenReturn(tokenReplyMock); when(tokenReplyMock.getAuthenticatedShopperID()).thenReturn(null); testObj.processOrderModificationMessages(AUTHORIZATION); verify(orderNotificationServiceMock).processOrderNotificationMessage(any(OrderNotificationMessage.class)); verify(businessProcessServiceMock).triggerEvent(endsWith(AUTHORIZATION.getCode())); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }
@Test public void markNotificationAsProcessedWhenContainsTokenReplyContainsNonMatchingAuthenticatedShopperId() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, AUTHORIZATION, orderModelMock)).thenReturn(Boolean.TRUE); when(orderModificationSerialiserMock.deserialise(orderModificationModelMock.getOrderNotificationMessage())).thenReturn(orderNotificationMessageMock); when(orderNotificationMessageMock.getTokenReply()).thenReturn(tokenReplyMock); when(tokenReplyMock.getAuthenticatedShopperID()).thenReturn(AUTHENTICATED_SHOPPER_ID); testObj.processOrderModificationMessages(AUTHORIZATION); verify(orderNotificationServiceMock).processOrderNotificationMessage(any(OrderNotificationMessage.class)); verify(businessProcessServiceMock).triggerEvent(endsWith(AUTHORIZATION.getCode())); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }
@Test public void markNotificationAsProcessedWhenDoesNotContainTokenReply() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, AUTHORIZATION, orderModelMock)).thenReturn(Boolean.TRUE); when(orderModificationSerialiserMock.deserialise(orderModificationModelMock.getOrderNotificationMessage())).thenReturn(orderNotificationMessageMock); when(orderNotificationMessageMock.getTokenReply()).thenReturn(null); testObj.processOrderModificationMessages(AUTHORIZATION); verify(orderNotificationServiceMock).processOrderNotificationMessage(any(OrderNotificationMessage.class)); verify(businessProcessServiceMock).triggerEvent(endsWith(AUTHORIZATION.getCode())); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }
@Test public void shouldProcessRefundNotification() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, REFUND_FOLLOW_ON, orderModelMock)).thenReturn(Boolean.TRUE); when(worldpayOrderModificationRefundProcessStrategyMock.processRefundFollowOn(orderModelMock, orderNotificationMessageMock)).thenReturn(Boolean.TRUE); testObj.processOrderModificationMessages(REFUND_FOLLOW_ON); verify(orderNotificationServiceMock).processOrderNotificationMessage(orderNotificationMessageMock); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }
@Test public void shouldMarkRefundNotificationAsDefective() { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, REFUND_FOLLOW_ON, orderModelMock)).thenReturn(Boolean.TRUE); when(worldpayOrderModificationRefundProcessStrategyMock.processRefundFollowOn(orderModelMock, orderNotificationMessageMock)).thenReturn(Boolean.FALSE); testObj.processOrderModificationMessages(REFUND_FOLLOW_ON); verify(worldpayOrderNotificationHandlerMock).setDefectiveModification(orderModificationModelMock, null, true); }
@Test public void processOrderModificationMessages_ShouldMarkNotificationAsProcessed_WhenItIsACancelNotification() throws WorldpayConfigurationException { when(worldpayPaymentTransactionServiceMock.isPreviousTransactionCompleted(WORLDPAY_ORDER_CODE, CANCEL, orderModelMock)).thenReturn(Boolean.TRUE); when(orderModificationSerialiserMock.deserialise(orderModificationModelMock.getOrderNotificationMessage())).thenReturn(orderNotificationMessageMock); testObj.processOrderModificationMessages(CANCEL); verify(orderNotificationServiceMock).processOrderNotificationMessage(any(OrderNotificationMessage.class)); verify(businessProcessServiceMock).triggerEvent(endsWith(CANCEL.getCode())); verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(orderModificationModelMock); }