@RequestMapping(value = "/summary/createUpdateDeliveryAddress.json", method = RequestMethod.POST)
  @RequireHardLogIn
  public String createUpdateDeliveryAddress(
      final Model model, @Valid final AddressForm form, final BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
      model.addAttribute("edit", Boolean.valueOf(StringUtils.isNotBlank(form.getAddressId())));
      // Work out if the address form should be displayed based on the payment type
      final B2BPaymentTypeData paymentType = getCheckoutFacade().getCheckoutCart().getPaymentType();
      final boolean payOnAccount =
          paymentType != null
              && CheckoutPaymentType.ACCOUNT.getCode().equals(paymentType.getCode());
      model.addAttribute("showAddressForm", Boolean.valueOf(!payOnAccount));

      return ControllerConstants.Views.Fragments.SingleStepCheckout.DeliveryAddressFormPopup;
    }

    // create delivery address and set it on cart
    final AddressData addressData = new AddressData();
    addressData.setId(form.getAddressId());
    addressData.setTitleCode(form.getTitleCode());
    addressData.setFirstName(form.getFirstName());
    addressData.setLastName(form.getLastName());
    addressData.setLine1(form.getLine1());
    addressData.setLine2(form.getLine2());
    addressData.setTown(form.getTownCity());
    addressData.setPostalCode(form.getPostcode());
    addressData.setCountry(getI18NFacade().getCountryForIsocode(form.getCountryIso()));
    addressData.setShippingAddress(
        Boolean.TRUE.equals(form.getShippingAddress())
            || Boolean.TRUE.equals(form.getSaveInAddressBook()));

    addressData.setVisibleInAddressBook(
        Boolean.TRUE.equals(form.getSaveInAddressBook())
            || StringUtils.isNotBlank(form.getAddressId()));
    addressData.setDefaultAddress(Boolean.TRUE.equals(form.getDefaultAddress()));

    if (StringUtils.isBlank(form.getAddressId())) {
      getUserFacade().addAddress(addressData);
    } else {
      getUserFacade().editAddress(addressData);
    }

    getCheckoutFacade().setDeliveryAddress(addressData);

    if (getCheckoutFacade().getCheckoutCart().getDeliveryMode() == null) {
      getCheckoutFacade().setDeliveryModeIfAvailable();
    }

    model.addAttribute("createUpdateStatus", "Success");
    model.addAttribute("addressId", addressData.getId());

    return REDIRECT_PREFIX
        + "/checkout/single/summary/getDeliveryAddressForm.json?addressId="
        + addressData.getId()
        + "&createUpdateStatus=Success";
  }
  @RequestMapping(value = "/select-suggested-address", method = RequestMethod.POST)
  public String doSelectSuggestedAddress(
      final AddressForm addressForm, final RedirectAttributes redirectModel) {
    final Set<String> resolveCountryRegions =
        org.springframework.util.StringUtils.commaDelimitedListToSet(
            Config.getParameter("resolve.country.regions"));

    final AddressData selectedAddress = new AddressData();
    selectedAddress.setId(addressForm.getAddressId());
    selectedAddress.setTitleCode(addressForm.getTitleCode());
    selectedAddress.setFirstName(addressForm.getFirstName());
    selectedAddress.setLastName(addressForm.getLastName());
    selectedAddress.setLine1(addressForm.getLine1());
    selectedAddress.setLine2(addressForm.getLine2());
    selectedAddress.setTown(addressForm.getTownCity());
    selectedAddress.setPostalCode(addressForm.getPostcode());
    selectedAddress.setBillingAddress(false);
    selectedAddress.setShippingAddress(true);
    selectedAddress.setVisibleInAddressBook(true);

    final CountryData countryData = i18NFacade.getCountryForIsocode(addressForm.getCountryIso());
    selectedAddress.setCountry(countryData);

    if (resolveCountryRegions.contains(countryData.getIsocode())) {
      if (addressForm.getRegionIso() != null && !StringUtils.isEmpty(addressForm.getRegionIso())) {
        final RegionData regionData =
            getI18NFacade().getRegion(addressForm.getCountryIso(), addressForm.getRegionIso());
        selectedAddress.setRegion(regionData);
      }
    }

    if (resolveCountryRegions.contains(countryData.getIsocode())) {
      if (addressForm.getRegionIso() != null && !StringUtils.isEmpty(addressForm.getRegionIso())) {
        final RegionData regionData =
            getI18NFacade().getRegion(addressForm.getCountryIso(), addressForm.getRegionIso());
        selectedAddress.setRegion(regionData);
      }
    }

    if (Boolean.TRUE.equals(addressForm.getEditAddress())) {
      selectedAddress.setDefaultAddress(
          Boolean.TRUE.equals(addressForm.getDefaultAddress())
              || userFacade.getAddressBook().size() <= 1);
      userFacade.editAddress(selectedAddress);
    } else {
      selectedAddress.setDefaultAddress(
          Boolean.TRUE.equals(addressForm.getDefaultAddress()) || userFacade.isAddressBookEmpty());
      userFacade.addAddress(selectedAddress);
    }

    GlobalMessages.addFlashMessage(
        redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "account.confirmation.address.added");

    return REDIRECT_TO_ADDRESS_BOOK_PAGE;
  }
  @ResponseBody
  @RequestMapping(
      value = "/summary/setDeliveryAddress.json",
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public CartData setDeliveryAddress(@RequestParam(value = "addressId") final String addressId) {
    final AddressData addressData = new AddressData();
    addressData.setId(addressId);

    final CartData cartData = new CartData();
    cartData.setDeliveryAddress(addressData);

    return getB2BCheckoutFacade().updateCheckoutCart(cartData);
  }
  @RequestMapping(
      value = "/remove-address/" + ADDRESS_CODE_PATH_VARIABLE_PATTERN,
      method = {RequestMethod.GET, RequestMethod.POST})
  @RequireHardLogIn
  public String removeAddress(
      @PathVariable("addressCode") final String addressCode,
      final RedirectAttributes redirectModel) {
    final AddressData addressData = new AddressData();
    addressData.setId(addressCode);
    userFacade.removeAddress(addressData);

    GlobalMessages.addFlashMessage(
        redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "account.confirmation.address.removed");
    return REDIRECT_TO_ADDRESS_BOOK_PAGE;
  }
 @RequestMapping(
     value = "/set-default-address/" + ADDRESS_CODE_PATH_VARIABLE_PATTERN,
     method = RequestMethod.GET)
 @RequireHardLogIn
 public String setDefaultAddress(
     @PathVariable("addressCode") final String addressCode,
     final RedirectAttributes redirectModel) {
   final AddressData addressData = new AddressData();
   addressData.setDefaultAddress(true);
   addressData.setVisibleInAddressBook(true);
   addressData.setId(addressCode);
   userFacade.setDefaultAddress(addressData);
   GlobalMessages.addFlashMessage(
       redirectModel,
       GlobalMessages.CONF_MESSAGES_HOLDER,
       "account.confirmation.default.address.changed");
   return REDIRECT_TO_ADDRESS_BOOK_PAGE;
 }
  @RequestMapping(value = "/summary/createUpdatePaymentDetails.json", method = RequestMethod.POST)
  @RequireHardLogIn
  public String createUpdatePaymentDetails(
      final Model model, @Valid final PaymentDetailsForm form, final BindingResult bindingResult) {
    paymentDetailsValidator.validate(form, bindingResult);

    final boolean editMode = StringUtils.isNotBlank(form.getPaymentId());

    if (bindingResult.hasErrors()) {
      model.addAttribute("edit", Boolean.valueOf(editMode));

      return ControllerConstants.Views.Fragments.SingleStepCheckout.PaymentDetailsFormPopup;
    }

    final CCPaymentInfoData paymentInfoData = new CCPaymentInfoData();
    paymentInfoData.setId(form.getPaymentId());
    paymentInfoData.setCardType(form.getCardTypeCode());
    paymentInfoData.setAccountHolderName(form.getNameOnCard());
    paymentInfoData.setCardNumber(form.getCardNumber());
    paymentInfoData.setStartMonth(form.getStartMonth());
    paymentInfoData.setStartYear(form.getStartYear());
    paymentInfoData.setExpiryMonth(form.getExpiryMonth());
    paymentInfoData.setExpiryYear(form.getExpiryYear());
    paymentInfoData.setSaved(Boolean.TRUE.equals(form.getSaveInAccount()));
    paymentInfoData.setIssueNumber(form.getIssueNumber());

    final AddressData addressData;
    if (!editMode && Boolean.FALSE.equals(form.getNewBillingAddress())) {
      addressData = getCheckoutCart().getDeliveryAddress();
      if (addressData == null) {
        GlobalMessages.addErrorMessage(
            model, "checkout.paymentMethod.createSubscription.billingAddress.noneSelected");

        model.addAttribute("edit", Boolean.valueOf(editMode));
        return ControllerConstants.Views.Fragments.SingleStepCheckout.PaymentDetailsFormPopup;
      }

      addressData.setBillingAddress(true); // mark this as billing address
    } else {
      final AddressForm addressForm = form.getBillingAddress();

      addressData = new AddressData();
      if (addressForm != null) {
        addressData.setId(addressForm.getAddressId());
        addressData.setTitleCode(addressForm.getTitleCode());
        addressData.setFirstName(addressForm.getFirstName());
        addressData.setLastName(addressForm.getLastName());
        addressData.setLine1(addressForm.getLine1());
        addressData.setLine2(addressForm.getLine2());
        addressData.setTown(addressForm.getTownCity());
        addressData.setPostalCode(addressForm.getPostcode());
        addressData.setCountry(getI18NFacade().getCountryForIsocode(addressForm.getCountryIso()));
        addressData.setShippingAddress(Boolean.TRUE.equals(addressForm.getShippingAddress()));
        addressData.setBillingAddress(Boolean.TRUE.equals(addressForm.getBillingAddress()));
      }
    }

    paymentInfoData.setBillingAddress(addressData);

    final CCPaymentInfoData newPaymentSubscription =
        getCheckoutFacade().createPaymentSubscription(paymentInfoData);
    if (newPaymentSubscription != null
        && StringUtils.isNotBlank(newPaymentSubscription.getSubscriptionId())) {
      if (Boolean.TRUE.equals(form.getSaveInAccount())
          && getUserFacade().getCCPaymentInfos(true).size() <= 1) {
        getUserFacade().setDefaultPaymentInfo(newPaymentSubscription);
      }
      getCheckoutFacade().setPaymentDetails(newPaymentSubscription.getId());
    } else {
      GlobalMessages.addErrorMessage(model, "checkout.paymentMethod.createSubscription.failed");

      model.addAttribute("edit", Boolean.valueOf(editMode));
      return ControllerConstants.Views.Fragments.SingleStepCheckout.PaymentDetailsFormPopup;
    }

    model.addAttribute("createUpdateStatus", "Success");
    model.addAttribute("paymentId", newPaymentSubscription.getId());

    return REDIRECT_PREFIX
        + "/checkout/single/summary/getPaymentDetailsForm.json?paymentId="
        + paymentInfoData.getId()
        + "&createUpdateStatus=Success";
  }
  @RequestMapping(
      value = "/edit-address/" + ADDRESS_CODE_PATH_VARIABLE_PATTERN,
      method = RequestMethod.POST)
  @RequireHardLogIn
  public String editAddress(
      final AddressForm addressForm,
      final BindingResult bindingResult,
      final Model model,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {
    getAddressValidator().validate(addressForm, bindingResult);
    if (bindingResult.hasErrors()) {
      GlobalMessages.addErrorMessage(model, "form.global.error");
      storeCmsPageInModel(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpAddressFormAfterError(addressForm, model);
      return ControllerConstants.Views.Pages.Account.AccountEditAddressPage;
    }

    model.addAttribute("metaRobots", "no-index,no-follow");

    final AddressData newAddress = new AddressData();
    newAddress.setId(addressForm.getAddressId());
    newAddress.setTitleCode(addressForm.getTitleCode());
    newAddress.setFirstName(addressForm.getFirstName());
    newAddress.setLastName(addressForm.getLastName());
    newAddress.setLine1(addressForm.getLine1());
    newAddress.setLine2(addressForm.getLine2());
    newAddress.setTown(addressForm.getTownCity());
    newAddress.setPostalCode(addressForm.getPostcode());
    newAddress.setBillingAddress(false);
    newAddress.setShippingAddress(true);
    newAddress.setVisibleInAddressBook(true);
    newAddress.setCountry(getI18NFacade().getCountryForIsocode(addressForm.getCountryIso()));

    if (addressForm.getRegionIso() != null && !StringUtils.isEmpty(addressForm.getRegionIso())) {
      newAddress.setRegion(
          getI18NFacade().getRegion(addressForm.getCountryIso(), addressForm.getRegionIso()));
    }

    if (Boolean.TRUE.equals(addressForm.getDefaultAddress())
        || userFacade.getAddressBook().size() <= 1) {
      newAddress.setDefaultAddress(true);
      newAddress.setVisibleInAddressBook(true);
    }

    final AddressVerificationResult<AddressVerificationDecision> verificationResult =
        getAddressVerificationFacade().verifyAddressData(newAddress);
    final boolean addressRequiresReview =
        getAddressVerificationResultHandler()
            .handleResult(
                verificationResult,
                newAddress,
                model,
                redirectModel,
                bindingResult,
                getAddressVerificationFacade().isCustomerAllowedToIgnoreAddressSuggestions(),
                "checkout.multi.address.updated");

    if (addressRequiresReview) {
      model.addAttribute(
          "regions", getI18NFacade().getRegionsForCountryIso(addressForm.getCountryIso()));
      model.addAttribute("country", addressForm.getCountryIso());
      model.addAttribute("edit", Boolean.TRUE);
      storeCmsPageInModel(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ADD_EDIT_ADDRESS_CMS_PAGE));
      return ControllerConstants.Views.Pages.Account.AccountEditAddressPage;
    }

    userFacade.editAddress(newAddress);

    return REDIRECT_TO_ADDRESS_BOOK_PAGE;
  }