/**
  * @return void
  */
 public function prepare()
 {
     $config = $this->getData('config');
     if (isset($config['options'])) {
         $options = [];
         foreach ($config['options'] as $option) {
             $option['url'] = $this->urlBuilder->getUrl($option['url']);
             $options[] = $option;
         }
         $config['options'] = $options;
     }
     $store = $this->storeManager->getStore();
     $config['validationEnabled'] = $this->config->isAddressValidationEnabled($store);
     $hasChoice = $this->config->allowUserToChooseAddress($store);
     if ($hasChoice) {
         $instructions = $this->config->getAddressValidationInstructionsWithChoice($store);
     } else {
         $instructions = $this->config->getAddressValidationInstructionsWithOutChoice($store);
     }
     $config['instructions'] = $instructions;
     $config['errorInstructions'] = $this->config->getAddressValidationErrorInstructions($store);
     $config['countriesEnabled'] = $this->config->getAddressValidationCountriesEnabled($store);
     $config['baseUrl'] = $this->urlBuilder->getUrl(self::VALIDATE_ADDRESS_PATH);
     $this->setData('config', $config);
     parent::prepare();
 }
 public function aroundSaveAddressInformation(\Magento\Checkout\Model\ShippingInformationManagement $subject, \Closure $proceed, $cartId, ShippingInformationInterface $addressInformation)
 {
     // Only validate address if module is enabled
     $quote = $this->quoteRepository->getActive($cartId);
     $storeId = $quote->getStoreId();
     if (!$this->config->isModuleEnabled($storeId)) {
         $paymentDetails = $proceed($cartId, $addressInformation);
         $this->ensureTaxCalculationSuccess($storeId);
         return $paymentDetails;
     }
     // Only validate address if address validation is enabled
     if (!$this->config->isAddressValidationEnabled($storeId)) {
         $paymentDetails = $proceed($cartId, $addressInformation);
         $this->ensureTaxCalculationSuccess($storeId);
         return $paymentDetails;
     }
     // If quote is virtual, getShippingAddress will return billing address, so no need to check if quote is virtual
     $shippingAddress = $addressInformation->getShippingAddress();
     $shippingInformationExtension = $addressInformation->getExtensionAttributes();
     $errorMessage = null;
     $validAddress = null;
     $customerAddress = null;
     $quoteAddress = null;
     $shouldValidateAddress = true;
     if (!is_null($shippingInformationExtension)) {
         $shouldValidateAddress = $shippingInformationExtension->getShouldValidateAddress();
     }
     $customerAddressId = $shippingAddress->getCustomerAddressId();
     $enabledAddressValidationCountries = explode(',', $this->config->getAddressValidationCountriesEnabled($storeId));
     if (!in_array($shippingAddress->getCountryId(), $enabledAddressValidationCountries)) {
         $shouldValidateAddress = false;
     }
     if ($shouldValidateAddress) {
         try {
             $validAddress = $this->validationInteraction->validateAddress($shippingAddress, $storeId);
         } catch (AddressValidateException $e) {
             $errorMessage = $e->getMessage();
         } catch (\SoapFault $e) {
             // If there is a SoapFault, it will have already been logged, so just disable address validation, as we
             // don't want to display SoapFault error message to user
             $shouldValidateAddress = false;
         } catch (\Exception $e) {
             $this->avaTaxLogger->error('Error in validating address in aroundSaveAddressInformation: ' . $e->getMessage());
             // Continue without address validation
             $shouldValidateAddress = false;
         }
     }
     // Determine which address to save to the customer or shipping addresses
     if (!is_null($validAddress)) {
         $quoteAddress = $validAddress;
     } else {
         $quoteAddress = $shippingAddress;
     }
     try {
         /*
          * Regardless of whether address was validated by AvaTax, if the address is a customer address then we need
          * to save that address on the customer record. The reason for this is that when a user is on the "Review
          * & Payments" step and they are selecting between "Valid" and "Original" address options, the selected
          * address information is submitted to this API so that the customer address is updated and tax
          * calculation is affected accordingly.
          */
         if ($customerAddressId) {
             // Update the customer address
             $customerAddress = $this->customerAddressRepository->getById($customerAddressId);
             $mergedCustomerAddress = $this->addressInteraction->copyQuoteAddressToCustomerAddress($quoteAddress, $customerAddress);
             $this->customerAddressRepository->save($mergedCustomerAddress);
         } else {
             // Update the shipping address
             $addressInformation->setShippingAddress($quoteAddress);
         }
     } catch (\Exception $e) {
         // There may be scenarios in which the above address updating may fail, in which case we should just do
         // nothing
         $this->avaTaxLogger->error('Error in saving address: ' . $e->getMessage());
         // Continue without address validation
         $shouldValidateAddress = false;
     }
     $returnValue = $proceed($cartId, $addressInformation);
     $this->ensureTaxCalculationSuccess($storeId);
     if (!$shouldValidateAddress) {
         return $returnValue;
     }
     $paymentDetailsExtension = $returnValue->getExtensionAttributes();
     if (is_null($paymentDetailsExtension)) {
         $paymentDetailsExtension = $this->paymentDetailsExtensionFactory->create();
     }
     if (!is_null($validAddress)) {
         $paymentDetailsExtension->setValidAddress($validAddress);
     } else {
         $paymentDetailsExtension->setErrorMessage($errorMessage);
     }
     $paymentDetailsExtension->setOriginalAddress($shippingAddress);
     $returnValue->setExtensionAttributes($paymentDetailsExtension);
     return $returnValue;
 }
 /**
  * @return mixed
  */
 public function getCountriesEnabled()
 {
     return $this->config->getAddressValidationCountriesEnabled($this->_storeManager->getStore());
 }