示例#1
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;
 }
示例#2
0
 /**
  * Validate address
  *
  * @return ValidateResult
  */
 protected function _sendAddressValidationRequest()
 {
     /** @var OnePica_AvaTax_Model_Config $config */
     $config = Mage::getSingleton('avatax/config')->init($this->_storeId);
     $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());
     return $result;
 }
 function testLatLongValidation()
 {
     $address = new Address();
     $address->setLine1("900 Winslow Way");
     $address->setLine2("Ste 130");
     $address->setCity("Bainbridge Island");
     $address->setRegion("WA");
     $address->setPostalCode("98110");
     $address->setLongitude("-122.510359");
     $address->setLatitude("47.624972");
     $validateRequest = new ValidateRequest();
     $validateRequest->setAddress($address);
     $validateRequest->setTextCase(TextCase::$Upper);
     //added for 4.13 changes
     $validateRequest->setCoordinates(true);
     //Sets Profile name from Configuration File to "Jaas"
     //this will force it to Jaas (PostLocate)
     $this->client = new AddressServiceSoap("JaasDevelopement");
     //validate the Request
     $result = $this->client->validate($validateRequest);
     //Re-Assigning to the original Profile
     $this->client = new AddressServiceSoap("Development");
     $this->assertEqual($result->getResultCode(), SeverityLevel::$Success);
     $validAddresses = $result->getValidAddresses();
     $this->assertEqual(1, count($validAddresses));
     $validAddresses = $result->getValidAddresses();
     if (count($validAddresses) != 1) {
         echo "Unexpected number of addresses returned.  Expected one address.";
     } else {
         $validAddress = $validAddresses[0];
         $this->assertEqual(strtoupper($address->getLine1()) . " E " . strtoupper($address->getLine2()), $validAddress->getLine1());
         $this->assertEqual("", $validAddress->getLine2());
         $this->assertEqual(strtoupper($address->getCity()), $validAddress->getCity());
         $this->assertEqual(strtoupper($address->getRegion()), $validAddress->getRegion());
         $this->assertEqual($address->getPostalCode() . "-2450", $validAddress->getPostalCode());
         $this->assertEqual("H", $validAddress->getAddressType());
         $this->assertEqual("C051", $validAddress->getCarrierRoute());
         //Ticket 21203: Modified Fips Code value for jaas enabled account.
         //$this->assertEqual("5303500000", $validAddress->getFipsCode());
         $this->assertEqual("5303503736", $validAddress->getFipsCode());
         $this->assertEqual("KITSAP", $validAddress->getCounty());
         $this->assertEqual(strtoupper($address->getCity()) . " " . strtoupper($address->getRegion()) . " " . $address->getPostalCode() . "-2450", $validAddress->getLine4());
         $this->assertEqual("981102450307", $validAddress->getPostNet());
         // Added 4.13 changes for the Lat Long
         // Update to check for ZIP+4 precision
         // Zip+4 precision coordinates
         if (strlen($validAddress->getLatitude()) > 7) {
             echo "ZIP+4 precision coordinates received";
             $this->assertEqual($address->getLatitude(), $validAddress->getLatitude());
             $this->assertEqual($address->getLongitude(), $validAddress->getLongitude());
         } else {
             echo "ZIP5 precision coordinates received";
             $this->assertEqual(substr($validAddress->getLatitude(), 0, 4), substr($address->getLatitude(), 0, 4), "Expected Latitude to start with '47.64'");
             $this->assertEqual(substr($validAddress->getLongitude(), 0, 6), substr($address->getLongitude(), 0, 6), "Expected Longitude to start with '-122.53'");
         }
     }
 }