WorldpayIframeCheckoutStepController extends WorldpayChoosePaymentMethodCheckoutStepController { @RequestMapping(value = "/add-payment-details", method = POST) @RequireHardLogIn public String addPaymentDetails(final Model model, @Valid final PaymentDetailsForm paymentDetailsForm, final BindingResult bindingResult, final RedirectAttributes redirectAttrs) throws CMSItemNotFoundException { getPaymentDetailsFormValidator().validate(paymentDetailsForm, bindingResult); if (addGlobalErrors(model, bindingResult)) { return handleFormErrors(model, paymentDetailsForm); } handleAndSaveAddresses(paymentDetailsForm); final String shopperBankCode = paymentDetailsForm.getShopperBankCode(); worldpayCartFacade.resetDeclineCodeAndShopperBankOnCart(shopperBankCode); final String paymentMethod = paymentDetailsForm.getPaymentMethod(); if (isAPM(paymentMethod)) { redirectAttrs.addFlashAttribute(PAYMENT_METHOD_PARAM, paymentMethod); redirectAttrs.addFlashAttribute(SHOPPER_BANK_CODE, shopperBankCode); return getRedirectToPaymentMethod(); } try { final AdditionalAuthInfo additionalAuthInfo = createAdditionalAuthInfo(paymentDetailsForm.getSaveInAccount(), paymentMethod); final PaymentData paymentData = getWorldpayHostedOrderFacade().redirectAuthorise(additionalAuthInfo); redirectAttrs.addFlashAttribute(PAYMENT_DATA, paymentData); redirectAttrs.addFlashAttribute(SHOW_NGPP_IFRAME, true); } catch (final WorldpayException e) { redirectAttrs.addFlashAttribute(SHOW_NGPP_IFRAME, false); GlobalMessages.addFlashMessage(redirectAttrs, GlobalMessages.ERROR_MESSAGES_HOLDER, COMMUNICATION_ERROR); LOGGER.error("An error occurred during Worldpay redirect authorise: " + e.getMessage(), e); } return REDIRECT_CHECKOUT_MULTI_WORLDPAY_IFRAME_ADD_PAYMENT_DETAILS; }  @RequestMapping(value = "/add-payment-details", method = POST) @RequireHardLogIn String addPaymentDetails(final Model model, @Valid final PaymentDetailsForm paymentDetailsForm, final BindingResult bindingResult, final RedirectAttributes redirectAttrs);  }
@Test public void addPaymentDetailsShouldReturnIframePage() throws WorldpayException, CMSItemNotFoundException { when(worldpayHostedOrderFacadeMock.redirectAuthorise(additionalAuthInfoMock)).thenReturn(paymentDataMock); final String result = testObj.addPaymentDetails(modelMock, paymentDetailsFormMock, bindingResultMock, redirectAttrbsMock); verify(worldpayCartFacadeMock).resetDeclineCodeAndShopperBankOnCart(BANK_CODE); verify(testObj).handleAndSaveAddresses(paymentDetailsFormMock); verify(redirectAttrbsMock).addFlashAttribute(PAYMENT_DATA, paymentDataMock); verify(redirectAttrbsMock, never()).addFlashAttribute(eq(SHOPPER_BANK_CODE), any()); verify(redirectAttrbsMock).addFlashAttribute(SHOW_NGPP_IFRAME, true); verify(paymentDetailsFormValidatorMock).validate(paymentDetailsFormMock, bindingResultMock); assertEquals(REDIRECT_CHECKOUT_MULTI_WORLDPAY_IFRAME_ADD_PAYMENT_DETAILS, result); }
@Test public void addPaymentDetailsShouldReturnBillingPageWithErrors() throws WorldpayException, CMSItemNotFoundException { when(worldpayHostedOrderFacadeMock.redirectAuthorise(additionalAuthInfoMock)).thenThrow(new WorldpayException(EXCEPTION_MESSAGE)); final String result = testObj.addPaymentDetails(modelMock, paymentDetailsFormMock, bindingResultMock, redirectAttrbsMock); verify(paymentDetailsFormValidatorMock).validate(paymentDetailsFormMock, bindingResultMock); verify(worldpayCartFacadeMock).resetDeclineCodeAndShopperBankOnCart(BANK_CODE); verify(testObj).handleAndSaveAddresses(paymentDetailsFormMock); verify(redirectAttrbsMock, never()).addFlashAttribute(eq(PAYMENT_DATA), any()); verify(redirectAttrbsMock, never()).addFlashAttribute(eq(PAYMENT_METHOD_PARAM), any()); verify(redirectAttrbsMock, never()).addFlashAttribute(eq(SHOPPER_BANK_CODE), any()); verify(redirectAttrbsMock).addFlashAttribute(SHOW_NGPP_IFRAME, false); assertEquals(REDIRECT_CHECKOUT_MULTI_WORLDPAY_IFRAME_ADD_PAYMENT_DETAILS, result); }
@Test public void addPaymentDetailsShouldReturnErrorsWhenValidationFails() throws CMSItemNotFoundException { when(testObj.addGlobalErrors(modelMock, bindingResultMock)).thenReturn(true); doReturn(ERROR_VIEW).when(testObj).handleFormErrors(modelMock, paymentDetailsFormMock); testObj.addPaymentDetails(modelMock, paymentDetailsFormMock, bindingResultMock, redirectAttrbsMock); verify(testObj).handleFormErrors(modelMock, paymentDetailsFormMock); }
@Test public void addPaymentDetailsShouldReturnFullPageRedirectForAPM() throws CMSItemNotFoundException { doReturn(PAGE_REDIRECT).when(testObj).getRedirectToPaymentMethod(); when(apmConfigurationServiceMock.getAllApmPaymentTypeCodes()).thenReturn(singleton(PAYMENT_METHOD)); final String result = testObj.addPaymentDetails(modelMock, paymentDetailsFormMock, bindingResultMock, redirectAttrbsMock); assertEquals(PAGE_REDIRECT, result); final InOrder inOrder = inOrder(paymentDetailsFormValidatorMock, testObj, worldpayCartFacadeMock, redirectAttrbsMock); inOrder.verify(paymentDetailsFormValidatorMock).validate(paymentDetailsFormMock, bindingResultMock); inOrder.verify(testObj).handleAndSaveAddresses(paymentDetailsFormMock); inOrder.verify(worldpayCartFacadeMock).resetDeclineCodeAndShopperBankOnCart(BANK_CODE); inOrder.verify(redirectAttrbsMock).addFlashAttribute(SHOPPER_BANK_CODE, BANK_CODE); }