/** * Check fields * * @return array|null */ protected function _checkFields() { /** @var Mage_Checkout_Model_Session $session */ $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; } } } return null; }
/** * Render address * * @param Mage_Customer_Model_Address_Abstract $address * @return string */ public function render(Mage_Customer_Model_Address_Abstract $address, $format = null) { $address->getRegion(); $address->getCountry(); $address->explodeStreetAddress(); $formater = new Varien_Filter_Template(); $data = $address->getData(); if ($this->getType()->getHtmlEscape()) { foreach ($data as $key => $value) { if (is_object($value)) { unset($data[$key]); } else { $data[$key] = $this->htmlEscape($value); } } } /** * Remove data that mustn't show */ if (!$this->helper('customer/address')->canShowConfig('prefix_show')) { unset($data['prefix']); } if (!$this->helper('customer/address')->canShowConfig('middlename_show')) { unset($data['middlename']); } if (!$this->helper('customer/address')->canShowConfig('suffix_show')) { unset($data['suffix']); } $formater->setVariables(array_merge($data, array('country' => $address->getCountryModel()->getName()))); $format = !is_null($format) ? $format : $this->getFormat($address); return $formater->filter($format); }
/** * Store date and score to customerAddress. * If the quoteAddress is not a saved customerAddress we do nothing * If it gets saved to the addressBook at end of checkout Magento´ convert functionality saves the data for us * * @param Mage_Customer_Model_Address_Abstract $address * @return bool */ public function saveCustomerAddress(Mage_Customer_Model_Address_Abstract $address) { $customerAddressId = $address->getCustomerAddressId(); if (empty($customerAddressId)) { return false; } $customerAddress = $this->getFactory()->getModelCustomerAddress(); $customerAddress->load($customerAddressId); if (!$customerAddress->hasData()) { return false; } $customerAddress->setData($this->prefix . '_score', $address->getData($this->prefix . '_score')); $customerAddress->setData($this->prefix . '_date', $address->getData($this->prefix . '_date')); $customerAddress->setData($this->prefix . '_hash', $address->getData($this->prefix . '_hash')); $customerAddress->setCity($address->getCity()); $customerAddress->setStreetFull($address->getStreetFull()); $customerAddress->setZip($address->getZip()); $customerAddress->save(); return true; }
/** * Render address * * @param Mage_Customer_Model_Address_Abstract $address * @return string */ public function render(Mage_Customer_Model_Address_Abstract $address) { $format = $this->getType()->getDefaultFormat(); $countryFormat = $address->getCountryModel()->getFormat($this->getType()->getCode()); $address->getRegion(); $address->getCountry(); $address->explodeStreetAddress(); if ($countryFormat) { $format = $countryFormat->getFormat(); } $formater = new Varien_Filter_Template(); $formater->setVariables(array_merge($address->getData(), array('country' => $address->getCountryModel()->getName()))); return $formater->filter($format); }
/** * Render address * * @param Mage_Customer_Model_Address_Abstract $address * @return string */ public function render(Mage_Customer_Model_Address_Abstract $address, $format = null) { $address->getRegion(); $address->getCountry(); $address->explodeStreetAddress(); $formater = new Varien_Filter_Template(); $data = $address->getData(); if ($this->getType()->getHtmlEscape()) { foreach ($data as $key => $value) { $data[$key] = $this->htmlEscape($value); } } $formater->setVariables(array_merge($data, array('country' => $address->getCountryModel()->getName()))); $format = !is_null($format) ? $format : $this->getFormat($address); return $formater->filter($format); }
protected function checkAddress(Mage_Customer_Model_Address_Abstract $address, $validityInSeconds) { if (!$address->hasData()) { return false; } $savedDate = $address->getData($this->prefix . '_date'); $savedScore = $address->getData($this->prefix . '_score'); if (empty($savedDate) or empty($savedScore)) { return false; } if ($this->addressHasChanged($address)) { return false; } // Verify the validity period is not expired: if (!$this->helper()->isDateStillValid($savedDate, $validityInSeconds)) { return false; } $address->setData($this->prefix . '_score', $savedScore); return $savedScore; }
/** * 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; }
/** * Extract only the data from the addres that gets validated. * The extracted data can be compared to the data in an existing * @param Mage_Customer_Model_Address_Abstract $address * @return Mage_Customer_Model_Address_Abstract - an address object containing only the data that gets validated */ protected function _extractValidatedAddressData(Mage_Customer_Model_Address_Abstract $address) { $validatedAddress = Mage::getModel('customer/address')->setData(['street' => $address->getData('street'), 'city' => $address->getCity(), 'region_id' => $address->getRegionId(), 'country_id' => $address->getCountryId(), 'postcode' => $address->getPostcode(), 'address_type' => $address->getAddressType()]); return $validatedAddress; }
/** * Set the billing address for the card. */ public function setAddress(Mage_Customer_Model_Address_Abstract $address) { return parent::setAddress(serialize($address->getData())); }