/**
  * {@inheritDoc}
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
 {
     /** @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.'));
     }
     $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);
     } elseif ($quote->getCustomerId()) {
         $address->setEmail($quote->getCustomerEmail());
     }
     $address->setSameAsBilling($sameAsBilling);
     $address->setSaveInAddressBook($saveInAddressBook);
     $address->setCollectShippingRates(true);
     try {
         $this->totalsCollector->collectAddressTotals($quote, $address);
         $address->save();
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save address. Please, check input data.'));
     }
     if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
         throw new InputException($this->scopeConfig->getValue('sales/minimum_order/error_message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $quote->getStoreId()));
     }
     return $quote->getShippingAddress()->getId();
 }
 /**
  * @param CartInterface $quote
  * @param AddressInterface $address
  * @param bool $useForShipping
  * @return void
  * @throws NoSuchEntityException
  * @throws InputException
  */
 public function save(CartInterface $quote, AddressInterface $address, $useForShipping = false)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $this->addressValidator->validate($address);
     $customerAddressId = $address->getCustomerAddressId();
     $shippingAddress = null;
     $addressData = [];
     if ($useForShipping) {
         $shippingAddress = $address;
     }
     $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
     if ($customerAddressId) {
         try {
             $addressData = $this->addressRepository->getById($customerAddressId);
         } catch (NoSuchEntityException $e) {
             // do nothing if customer is not found by id
         }
         $address = $quote->getBillingAddress()->importCustomerAddressData($addressData);
         if ($useForShipping) {
             $shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData);
             $shippingAddress->setSaveInAddressBook($saveInAddressBook);
         }
     } elseif ($quote->getCustomerId()) {
         $address->setEmail($quote->getCustomerEmail());
     }
     $address->setSaveInAddressBook($saveInAddressBook);
     $quote->setBillingAddress($address);
     if ($useForShipping) {
         $shippingAddress->setSameAsBilling(1);
         $shippingAddress->setCollectShippingRates(true);
         $quote->setShippingAddress($shippingAddress);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
 {
     $quote = $this->quoteRepository->getActive($cartId);
     $this->addressValidator->validate($address);
     $quote->setBillingAddress($address);
     $quote->setDataChanges(true);
     try {
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save address. Please, check input data.'));
     }
     return $quote->getBillingAddress()->getId();
 }
 /**
  * {@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}
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false)
 {
     $quote = $this->quoteRepository->getActive($cartId);
     $this->addressValidator->validate($address);
     $customerAddressId = $address->getCustomerAddressId();
     $shippingAddress = null;
     $addressData = [];
     if ($useForShipping) {
         $shippingAddress = $address;
     }
     $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
     if ($customerAddressId) {
         try {
             $addressData = $this->addressRepository->getById($customerAddressId);
         } catch (NoSuchEntityException $e) {
             // do nothing if customer is not found by id
         }
         $address = $quote->getBillingAddress()->importCustomerAddressData($addressData);
         if ($useForShipping) {
             $shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData);
             $shippingAddress->setSaveInAddressBook($saveInAddressBook);
         }
     } elseif ($quote->getCustomerId()) {
         $address->setEmail($quote->getCustomerEmail());
     }
     $address->setSaveInAddressBook($saveInAddressBook);
     $quote->setBillingAddress($address);
     if ($useForShipping) {
         $shippingAddress->setSameAsBilling(1);
         $shippingAddress->setCollectShippingRates(true);
         $quote->setShippingAddress($shippingAddress);
     }
     $quote->setDataChanges(true);
     $quote->collectTotals();
     try {
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save address. Please, check input data.'));
     }
     return $quote->getBillingAddress()->getId();
 }
 /**
  * {@inheritDoc}
  */
 public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
 {
     /** @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.'));
     }
     $this->addressValidator->validate($address);
     $address->setSameAsBilling(0);
     $address->setCollectShippingRates(true);
     $quote->setShippingAddress($address);
     $quote->setDataChanges(true);
     try {
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save address. Please, check input data.'));
     }
     return $quote->getShippingAddress()->getId();
 }
 public function testValidateWithValidAddress()
 {
     $addressCustomer = 100;
     $addressId = 100;
     $address = $this->getMock('\\Magento\\Quote\\Api\\Data\\AddressInterface');
     $address->expects($this->atLeastOnce())->method('getId')->willReturn($addressId);
     $address->expects($this->atLeastOnce())->method('getCustomerId')->willReturn($addressCustomer);
     /** Customer mock */
     $this->customerFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->customerMock));
     $this->customerMock->expects($this->once())->method('load')->with($addressCustomer);
     $this->customerMock->expects($this->once())->method('getId')->will($this->returnValue($addressCustomer));
     /** Quote address mock */
     $this->addressFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->quoteAddressMock));
     $this->quoteAddressMock->expects($this->once())->method('load')->with($addressId);
     $this->quoteAddressMock->expects($this->once())->method('getId')->will($this->returnValue($addressId));
     $this->quoteAddressMock->expects($this->any())->method('getCustomerId')->will($this->returnValue($addressCustomer));
     /** Validate */
     $this->model->validate($address);
 }
 public function testValidateWithValidAddress()
 {
     $addressCustomer = 100;
     $addressId = 100;
     $customerAddressId = 42;
     $address = $this->getMock('\\Magento\\Quote\\Api\\Data\\AddressInterface');
     $address->expects($this->atLeastOnce())->method('getId')->willReturn($addressId);
     $address->expects($this->atLeastOnce())->method('getCustomerId')->willReturn($addressCustomer);
     $address->expects($this->atLeastOnce())->method('getCustomerAddressId')->willReturn($customerAddressId);
     $customerMock = $this->getMock('\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $customerAddress = $this->getMock('\\Magento\\Quote\\Api\\Data\\AddressInterface');
     $this->customerRepositoryMock->expects($this->once())->method('getById')->willReturn($customerMock);
     $customerMock->expects($this->once())->method('getId')->willReturn($addressCustomer);
     $this->addressRepositoryMock->expects($this->once())->method('getById')->willReturn($this->quoteAddressMock);
     $this->quoteAddressMock->expects($this->any())->method('getCustomerId')->willReturn($addressCustomer);
     $this->customerSessionMock->expects($this->once())->method('getCustomerDataObject')->willReturn($customerMock);
     $customerMock->expects($this->once())->method('getAddresses')->willReturn([$customerAddress]);
     $customerAddress->expects($this->once())->method('getId')->willReturn(42);
     $this->assertTrue($this->model->validate($address));
 }