/**
  * Validate address using AvaTax Address Validation API
  *
  * @param array|\Magento\Customer\Api\Data\AddressInterface|\Magento\Sales\Api\Data\OrderAddressInterface|/AvaTax/ValidAddress|\Magento\Customer\Api\Data\AddressInterface|\Magento\Quote\Api\Data\AddressInterface|\Magento\Sales\Api\Data\OrderAddressInterface|array|null
  * @param $storeId
  * @return array|\Magento\Customer\Api\Data\AddressInterface|\Magento\Sales\Api\Data\OrderAddressInterface|/AvaTax/ValidAddress|\Magento\Customer\Api\Data\AddressInterface|\Magento\Quote\Api\Data\AddressInterface|\Magento\Sales\Api\Data\OrderAddressInterface|array|null
  * @throws AddressValidateException
  * @throws LocalizedException
  */
 public function validateAddress($addressInput, $storeId)
 {
     $returnCoordinates = 1;
     $validateRequest = $this->validateRequestFactory->create(['address' => $this->interactionAddress->getAddress($addressInput), 'textCase' => TextCase::$Mixed ? TextCase::$Mixed : TextCase::$Default, 'coordinates' => $returnCoordinates]);
     $validateResult = $this->addressService->validate($validateRequest, $storeId);
     if ($validateResult->getResultCode() == SeverityLevel::$Success) {
         $validAddresses = $validateResult->getValidAddresses();
         if (isset($validAddresses[0])) {
             $validAddress = $validAddresses[0];
         } else {
             return null;
         }
         // Convert data back to the type it was passed in as
         switch (true) {
             case $addressInput instanceof \Magento\Customer\Api\Data\AddressInterface:
                 $validAddress = $this->interactionAddress->convertAvaTaxValidAddressToCustomerAddress($validAddress, $addressInput);
                 break;
             case $addressInput instanceof \Magento\Quote\Api\Data\AddressInterface:
                 $validAddress = $this->interactionAddress->convertAvaTaxValidAddressToQuoteAddress($validAddress, $addressInput);
                 break;
             default:
                 throw new LocalizedException(__('Input parameter "$addressInput" was not of a recognized/valid type: "%1".', [gettype($addressInput)]));
                 break;
         }
         return $validAddress;
     } else {
         $messages = $validateResult->getMessages();
         $firstMessage = array_shift($messages);
         $message = $firstMessage instanceof \AvaTax\Message ? $firstMessage->getSummary() : self::GENERIC_VALIDATION_MESSAGE;
         throw new AddressValidateException(__($message));
     }
 }
Esempio n. 2
0
 /**
  * Get details for GetTaxRequest
  *
  * @param \Magento\Store\Api\Data\StoreInterface $store
  * @param $address \Magento\Quote\Api\Data\AddressInterface|\Magento\Sales\Api\Data\OrderAddressInterface
  * @param \Magento\Quote\Api\Data\CartInterface|\Magento\Sales\Api\Data\OrderInterface $object
  * @return array
  * @throws LocalizedException
  */
 protected function retrieveGetTaxRequestFields(StoreInterface $store, $address, $object)
 {
     $customerId = $object->getCustomerId();
     $customer = $this->getCustomerById($customerId);
     $storeId = $store->getId();
     if ($this->config->getLiveMode() == Config::API_PROFILE_NAME_PROD) {
         $companyCode = $this->config->getCompanyCode($storeId);
     } else {
         $companyCode = $this->config->getDevelopmentCompanyCode($storeId);
     }
     $businessIdentificationNumber = $this->getBusinessIdentificationNumber($store, $address, $customer);
     $locationCode = $this->config->getLocationCode($store);
     return ['BusinessIdentificationNo' => $businessIdentificationNumber, 'CompanyCode' => $companyCode, 'LocationCode' => $locationCode, 'OriginAddress' => $this->address->getAddress($this->config->getOriginAddress($storeId))];
 }
 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;
 }
 /**
  * Pass all undefined method calls through to AddressService
  *
  * @param $name
  * @param array $arguments
  * @return mixed
  */
 public function __call($name, array $arguments)
 {
     return call_user_func_array([$this->interactionAddress->getAddressService($this->type), $name], $arguments);
 }