WorldpayKlarnaCheckoutStepController extends AbstractController { @RequestMapping(value = "/klarna/confirmation", method = RequestMethod.GET) @RequireHardLogIn public String doHandleKlarnaConfirmation(final Model model, final RedirectAttributes redirectAttributes) { try { final KlarnaRedirectAuthoriseResult klarnaRedirectAuthoriseResult = worldpayKlarnaPaymentCheckoutFacade.checkKlarnaOrderStatus(); worldpayHostedOrderFacade.completeRedirectAuthorise(klarnaRedirectAuthoriseResult); checkoutFacade.placeOrder(); final String decodedHTMLContent = klarnaRedirectAuthoriseResult.getDecodedHTMLContent(); model.addAttribute(KLARNA_RESPONSE_PAGE_DATA_PARAM, decodedHTMLContent); return worldpayAddonEndpointService.getKlarnaResponsePage(); } catch (WorldpayException | InvalidCartException e) { LOG.error("Failed to place Order", e); GlobalMessages.addErrorMessage(model, CHECKOUT_PLACE_ORDER_FAILED); return doHostedOrderPageError(ERROR.name(), redirectAttributes); } }  @RequestMapping(value = "/klarna/confirmation", method = RequestMethod.GET) @RequireHardLogIn String doHandleKlarnaConfirmation(final Model model, final RedirectAttributes redirectAttributes);  }
@Test public void shouldHandleKlarnaConfirmation() throws WorldpayException, InvalidCartException { when(worldpayKlarnaPaymentCheckoutFacade.checkKlarnaOrderStatus()).thenReturn(klarnaRedirectAuthoriseResponse); when(klarnaRedirectAuthoriseResponse.getDecodedHTMLContent()).thenReturn(KLARNA_CONTENT_PAGE); when(worldpayAddonEndpointServiceMock.getKlarnaResponsePage()).thenReturn(KLARNA_PAGE_REDIRECT); final String result = testObj.doHandleKlarnaConfirmation(modelMock, redirectAttributesMock); assertEquals(KLARNA_PAGE_REDIRECT, result); verify(worldpayHostedOrderFacadeMock).completeRedirectAuthorise(klarnaRedirectAuthoriseResponse); verify(modelMock).addAttribute(KLARNA_VIEW_DATA, KLARNA_CONTENT_PAGE); verify(checkoutFacadeMock).placeOrder(); }
@Test public void shouldNotPlaceOrderWhenKlarnaConfirmationIsNotAuthorised() throws WorldpayException, InvalidCartException { when(worldpayKlarnaPaymentCheckoutFacade.checkKlarnaOrderStatus()).thenThrow(new WorldpayException("Chan chan")); final String result = testObj.doHandleKlarnaConfirmation(modelMock, redirectAttributesMock); assertEquals(format(CHOOSE_PAYMENT_REDIRECT_URL, AuthorisedStatus.ERROR.name()), result); verify(modelMock, never()).addAttribute(eq(KLARNA_VIEW_DATA), anyString()); verify(checkoutFacadeMock, never()).placeOrder(); }
@Test public void shouldRedirectToErrorPageWhenPlaceOrderFails() throws WorldpayException, InvalidCartException { when(worldpayKlarnaPaymentCheckoutFacade.checkKlarnaOrderStatus()).thenReturn(klarnaRedirectAuthoriseResponse); when(checkoutFacadeMock.placeOrder()).thenThrow(new InvalidCartException("Chan chan")); final String result = testObj.doHandleKlarnaConfirmation(modelMock, redirectAttributesMock); verify(modelMock, never()).addAttribute(KLARNA_VIEW_DATA, KLARNA_PAGE_REDIRECT); assertEquals(format(CHOOSE_PAYMENT_REDIRECT_URL, AuthorisedStatus.ERROR.name()), result); }