/**
  * {@inheritDoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function saveAddressInformation($cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
 {
     $address = $addressInformation->getShippingAddress();
     $carrierCode = $addressInformation->getShippingCarrierCode();
     $methodCode = $addressInformation->getShippingMethodCode();
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     if ($quote->isVirtual()) {
         throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping address is not applicable.'));
     }
     if (0 == $quote->getItemsCount()) {
         throw new InputException(__('Shipping method is not applicable for empty cart'));
     }
     $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
     $sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
     $customerAddressId = $address->getCustomerAddressId();
     $this->addressValidator->validate($address);
     $quote->setShippingAddress($address);
     $address = $quote->getShippingAddress();
     if ($customerAddressId) {
         $addressData = $this->addressRepository->getById($customerAddressId);
         $address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
     }
     $address->setSameAsBilling($sameAsBilling);
     $address->setSaveInAddressBook($saveInAddressBook);
     $address->setCollectShippingRates(true);
     if (!$address->getCountryId()) {
         throw new StateException(__('Shipping address is not set'));
     }
     $address->setShippingMethod($carrierCode . '_' . $methodCode);
     try {
         $address->save();
         $address->collectTotals();
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save address. Please, check input data.'));
     }
     if (!$address->getShippingRateByCode($address->getShippingMethod())) {
         throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode));
     }
     if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
         throw new InputException($this->scopeConfig->getValue('sales/minimum_order/error_message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $quote->getStoreId()));
     }
     try {
         $address->save();
         $quote->collectTotals();
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save shipping information. Please, check input data.'));
     }
     /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
     $paymentDetails = $this->paymentDetailsFactory->create();
     $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
     $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
     return $paymentDetails;
 }
 /**
  * {@inheritDoc}
  */
 public function saveAddressInformation($cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
 {
     $address = $addressInformation->getShippingAddress();
     $billingAddress = $addressInformation->getBillingAddress();
     $carrierCode = $addressInformation->getShippingCarrierCode();
     $methodCode = $addressInformation->getShippingMethodCode();
     if (!$address->getCountryId()) {
         throw new StateException(__('Shipping address is not set'));
     }
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     $quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
     $this->validateQuote($quote);
     $quote->setIsMultiShipping(false);
     if ($billingAddress) {
         $quote->setBillingAddress($billingAddress);
     }
     try {
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save shipping information. Please check input data.'));
     }
     $shippingAddress = $quote->getShippingAddress();
     if (!$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) {
         throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode));
     }
     /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
     $paymentDetails = $this->paymentDetailsFactory->create();
     $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
     $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
     return $paymentDetails;
 }
 /**
  *Set additional information for shipping address
  *
  * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
  * @param callable $proceed
  *
  * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function aroundSaveAddressInformation(\Magento\Checkout\Model\ShippingInformationManagement $subject, $proceed, $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
 {
     try {
         $carrierCode = $addressInformation->getShippingCarrierCode();
         $methodCode = $addressInformation->getShippingMethodCode();
         $shippingMethod = $carrierCode . '_' . $methodCode;
         $quote = $this->quoteRepository->getActive($cartId);
         $address = $quote->getShippingAddress();
         $validation = $this->checkoutSession->getShipAddressValidation();
         if (is_array($validation) && isset($validation['key'])) {
             if (isset($validation['validation_status'])) {
                 $additionalDetail['address_valid'] = $validation['validation_status'];
                 $address->setValidationStatus($validation['validation_status']);
             }
             if (isset($validation['destination_type'])) {
                 $additionalDetail['destination_type'] = $validation['destination_type'];
                 $address->setDestinationType($validation['destination_type']);
             }
             $this->checkoutSession->setShipAddressValidation(null);
         }
         $address->save();
         $additionalDetail = new \Magento\Framework\DataObject();
         $extAttributes = $addressInformation->getShippingAddress()->getExtensionAttributes();
         //push out event so other modules can save their data TODO add carrier_group_id
         $this->eventManager->dispatch('shipperhq_additional_detail_checkout', ['address_extn_attributes' => $extAttributes, 'additional_detail' => $additionalDetail, 'carrier_code' => $carrierCode]);
         $additionalDetailArray = $additionalDetail->convertToArray();
         $this->shipperLogger->postDebug('ShipperHQ Shipper', 'processing additional detail ', $additionalDetail);
         $this->carrierGroupHelper->saveCarrierGroupInformation($address, $shippingMethod, $additionalDetailArray);
     } catch (\Exception $e) {
         $this->shipperLogger->postCritical('Shipperhq_Shipper', 'Shipping Information Plugin', 'Exception raised ' . $e->getMessage());
     }
     $result = $proceed($cartId, $addressInformation);
     if ($address->getCustomerId()) {
         $customerAddresses = $quote->getCustomer()->getAddresses();
         foreach ($customerAddresses as $oneAddress) {
             if ($oneAddress->getId() == $address->getCustomerAddressId()) {
                 if ($address->getValidationStatus()) {
                     $oneAddress->setCustomAttribute('validation_status', $address->getValidationStatus());
                 }
                 if ($address->getDestinationType()) {
                     $oneAddress->setCustomAttribute('destination_type', $address->getDestinationType());
                 }
                 $this->addressRepository->save($oneAddress);
             }
         }
     }
     return $result;
 }