DefaultWorldpayPlaceOrderFromNotificationStrategy implements WorldpayPlaceOrderFromNotificationStrategy { @Override public void placeOrderFromNotification(final WorldpayOrderModificationModel orderModificationModel, final CartModel cart) { final OrderNotificationMessage notificationMessage = orderModificationSerialiser.deserialise(orderModificationModel.getOrderNotificationMessage()); final Amount amount = notificationMessage.getPaymentReply().getAmount(); final ImpersonationContext context = new ImpersonationContext(); context.setOrder(cart); context.setSite(cart.getSite()); context.setUser(cart.getUser()); impersonationService.executeInContext(context, (ImpersonationService.Executor<Void, ImpersonationService.Nothing>) () -> placeOrderInContext(orderModificationModel, cart, notificationMessage, amount) ); }  @Override void placeOrderFromNotification(final WorldpayOrderModificationModel orderModificationModel, final CartModel cart); @Required void setWorldpayRedirectOrderService(final WorldpayRedirectOrderService worldpayRedirectOrderService); @Required void setWorldpayOrderService(final WorldpayOrderService worldpayOrderService); @Required void setImpersonationService(final ImpersonationService impersonationService); @Required void setCartService(final CartService cartService); @Required void setCommerceCheckoutService(final CommerceCheckoutService commerceCheckoutService); @Required void setOrderModificationSerialiser(final OrderModificationSerialiser orderModificationSerialiser); @Required void setModelService(final ModelService modelService); @Required void setWorldpayOrderNotificationHandler(final WorldpayOrderNotificationHandler worldpayOrderNotificationHandler);  }
@Test public void placeOrderFromNotification() throws InvalidCartException { when(orderNotificationMessageMock.getPaymentReply().getAmount()).thenReturn(amountMock); when(commerceCheckoutServiceMock.placeOrder(commerceCheckoutParameterCaptor.capture())).thenReturn(commerceOrderResultMock); when(commerceOrderResultMock.getOrder()).thenReturn(orderModelMock); when(orderNotificationMessageMock.getMerchantCode()).thenReturn(MERCHANT_CODE); when(worldpayOrderServiceMock.convertAmount(amountMock)).thenReturn(TEN); testObj.placeOrderFromNotification(worldpayOrderModificationModelMock, cartModelMock); final InOrder inOrder = Mockito.inOrder(cartServiceMock, worldpayRedirectOrderServiceMock, worldpayOrderNotificationHandlerMock); inOrder.verify(cartServiceMock).setSessionCart(cartModelMock); inOrder.verify(worldpayRedirectOrderServiceMock).completeConfirmedRedirectAuthorise(TEN, MERCHANT_CODE, cartModelMock); inOrder.verify(worldpayOrderNotificationHandlerMock).setNonDefectiveAndProcessed(worldpayOrderModificationModelMock); verify(modelServiceMock).refresh(orderModelMock); verify(cartServiceMock).removeSessionCart(); final CommerceCheckoutParameter value = commerceCheckoutParameterCaptor.getValue(); assertThat(value.isEnableHooks()).isTrue(); assertThat(value.getCart()).isEqualTo(cartModelMock); }
@Test public void placeOrderFromNotificationShouldMarkNotificationAsDefectiveWhenThereIsAnErrorPlacingTheOrder() throws InvalidCartException { when(orderNotificationMessageMock.getPaymentReply().getAmount()).thenReturn(amountMock); when(commerceCheckoutServiceMock.placeOrder(commerceCheckoutParameterCaptor.capture())).thenThrow(new InvalidCartException("Invalid cart exception")); when(commerceOrderResultMock.getOrder()).thenReturn(orderModelMock); when(orderNotificationMessageMock.getMerchantCode()).thenReturn(MERCHANT_CODE); when(worldpayOrderServiceMock.convertAmount(amountMock)).thenReturn(TEN); testObj.placeOrderFromNotification(worldpayOrderModificationModelMock, cartModelMock); verify(worldpayOrderNotificationHandlerMock).setDefectiveReason(worldpayOrderModificationModelMock, DefectiveReason.ERROR_PLACING_ORDER); }