Example #1
0
 /**
  * Address validation
  *
  * @param int $isAddressValidationOn
  * @param int $isAddressActionable
  * @param ValidateResult $result
  * @return array|bool|null
  */
 protected function _addressValidation($isAddressValidationOn, $isAddressActionable, $result)
 {
     if ($isAddressValidationOn == OnePica_AvaTax_Model_Source_Addressvalidation::ENABLED_PREVENT_ORDER) {
         if ($result->getResultCode() == SeverityLevel::$Success) {
             $this->_mageAddress->setAddressValidated(true);
             return true;
         } else {
             $errors = array();
             foreach ($result->getMessages() as $message) {
                 $errors[] = $this->__($message->getSummary());
             }
             return $errors;
         }
     } elseif ($isAddressValidationOn == OnePica_AvaTax_Model_Source_Addressvalidation::ENABLED_ALLOW_ORDER) {
         $this->_mageAddress->setAddressValidated(true);
         if ($result->getResultCode() == SeverityLevel::$Success) {
             return true;
         } else {
             if (!$this->_mageAddress->getAddressNotified()) {
                 $this->_mageAddress->setAddressNotified(true);
                 foreach ($result->getMessages() as $message) {
                     Mage::getSingleton('core/session')->addNotice($this->__($message->getSummary()));
                 }
             }
             return true;
         }
         //a valid address isn't required, but Avalara has to say there is
         //enough info to drill down to a tax jurisdiction to calc on
     } elseif (!$isAddressValidationOn && $isAddressActionable) {
         if ($result->isTaxable()) {
             $this->_mageAddress->setAddressValidated(true);
             return true;
         } else {
             $errors = array();
             foreach ($result->getMessages() as $message) {
                 $errors[] = $this->__($message->getSummary());
             }
             return $errors;
         }
     }
     return null;
 }
Example #2
0
 /**
  * Validates the address with the AvaTax validation API.
  * Returns true on success and an array with an error on failure.
  *
  * @return array|bool
  * @throws OnePica_AvaTax_Model_Avatax_Address_Exception
  */
 public function validate()
 {
     if (!$this->_mageAddress) {
         throw new OnePica_AvaTax_Model_Avatax_Address_Exception($this->__('An address must be set before validation.'));
     }
     /** @var OnePica_AvaTax_Model_Config $config */
     $config = Mage::getSingleton('avatax/config')->init($this->_storeId);
     /** @var Mage_Sales_Model_Quote $quote */
     $quote = $this->_mageAddress->getQuote();
     $isAddressValidationOn = $this->_getDataHelper()->isAddressValidationOn($this->_mageAddress, $this->_storeId);
     $isAddressNormalizationOn = $this->_getDataHelper()->isAddressNormalizationOn($this->_mageAddress, $this->_storeId);
     $isAddressActionable = $this->_getDataHelper()->isAddressActionable($this->_mageAddress, $quote->getStoreId());
     //if there is no use cases for AvaTax services, return address as valid without doing a lookup
     if (!$isAddressValidationOn && !$isAddressNormalizationOn && !$isAddressActionable) {
         return true;
     }
     //lookup in AvaTax (with caching)
     $key = $this->_mageAddress->getCacheHashKey();
     if (array_key_exists($key, $this->_cache)) {
         $result = unserialize($this->_cache[$key]);
     } elseif ($this->_mageAddress->getPostcode() && $this->_mageAddress->getPostcode() != '-') {
         $session = Mage::getSingleton('checkout/session');
         if ($session->getPostType() == 'onepage') {
             $requiredFields = explode(",", $this->getHelper()->getFieldRequiredList());
             $fieldRules = explode(",", $this->getHelper()->getFieldRule());
             foreach ($requiredFields as $field) {
                 $requiredFlag = 0;
                 foreach ($fieldRules as $rule) {
                     if (preg_match("/street\\d/", $field)) {
                         $field = "street";
                     }
                     if ($field == "country") {
                         $field = "country_id";
                     }
                     if ($this->_mageAddress->getData($field) == $rule || !$this->_mageAddress->getData($field)) {
                         $requiredFlag = 1;
                     }
                 }
                 if ($requiredFlag) {
                     $errors = array();
                     $errors[] = $this->__('Invalid ') . $this->__($field);
                     return $errors;
                 }
             }
         }
         $client = $config->getAddressConnection();
         $request = new ValidateRequest($this->_requestAddress, TextCase::$Mixed, 0);
         $request->setTaxability(true);
         $result = $client->Validate($request);
         $this->_log(OnePica_AvaTax_Model_Source_Logtype::VALIDATE, $request, $result, $this->_storeId, $config->getParams());
         $this->_cache[$key] = serialize($result);
     } else {
         $errors = array();
         $errors[] = $this->__('Invalid ZIP/Postal Code.');
         return $errors;
     }
     //normalization
     if ($isAddressNormalizationOn && $result->getResultCode() == SeverityLevel::$Success) {
         $responseAddress = $result->getValidAddresses();
         $responseAddress = array_pop($responseAddress);
         if ($responseAddress instanceof ValidAddress) {
             $this->_responseAddress = $responseAddress;
             $this->_convertResponseAddress();
         } else {
             throw new OnePica_AvaTax_Model_Avatax_Address_Exception($this->__('Invalid response address type.'));
         }
     }
     //validation
     if ($isAddressValidationOn == 1) {
         if ($result->getResultCode() == SeverityLevel::$Success) {
             $this->_mageAddress->setAddressValidated(true);
             return true;
         } else {
             $errors = array();
             foreach ($result->getMessages() as $message) {
                 $errors[] = $this->__($message->getSummary());
             }
             return $errors;
         }
     } elseif ($isAddressValidationOn == 2) {
         $this->_mageAddress->setAddressValidated(true);
         if ($result->getResultCode() == SeverityLevel::$Success) {
             return true;
         } else {
             if (!$this->_mageAddress->getAddressNotified()) {
                 $this->_mageAddress->setAddressNotified(true);
                 foreach ($result->getMessages() as $message) {
                     Mage::getSingleton('core/session')->addNotice($this->__($message->getSummary()));
                 }
             }
             return true;
         }
         //a valid address isn't required, but Avalara has to say there is
         //enough info to drill down to a tax jurisdiction to calc on
     } elseif (!$isAddressValidationOn && $isAddressActionable) {
         if ($result->isTaxable()) {
             $this->_mageAddress->setAddressValidated(true);
             return true;
         } else {
             $errors = array();
             foreach ($result->getMessages() as $message) {
                 $errors[] = $this->__($message->getSummary());
             }
             return $errors;
         }
     }
     return true;
 }