public function usps_validate_street_address($type = 'Billing', $data = false) { $error = false; if (!in_array($type, array('Billing', 'Shipping')) && !$data) { return false; } if (!$data) { $address = $this->getQuote()->{"get{$type}Address"}(); $data = $address->getData(); } if (isset($data['country_id']) && !empty($data['country_id']) && $data['country_id'] == 'US') { // skip regions $state_no_ups = array('virgin islands', 'puerto rico', 'guam'); $regionName = ''; if (!empty($data['region'])) { $regionName = $data['region']; } else { if (isset($data['region_id']) && !empty($data['region_id'])) { $regionModel = Mage::getModel('directory/region')->load($data['region_id']); $regionName = $regionModel->getName(); } } if (!empty($regionName)) { $reg = strtolower($regionName); if (in_array($reg, $state_no_ups)) { return false; } } if (!empty($data['street']) && !empty($data['city']) && !empty($data['postcode']) && !empty($data['region_id'])) { $regionModel = Mage::getModel('directory/region')->load($data['region_id']); $regionId = $regionModel->getCode(); if (empty($regionId)) { return false; } $test_mode = (bool) Mage::getStoreConfig('addressverification/usps_address_verification/test_mode'); $key = Mage::getStoreConfig('addressverification/usps_address_verification/usps_access_key'); if (empty($key)) { return false; } $data['street'] = strip_tags($data['street']); $data['street'] = str_replace("\r\n", ", ", $data['street']); $data['street'] = str_replace("\n\r", ", ", $data['street']); $data['street'] = str_replace("\r", ", ", $data['street']); $data['street'] = str_replace("\n", ", ", $data['street']); $data['street'] = str_replace(",", "", $data['street']); $check_address = array('street' => $data['street'], 'city' => $data['city'], 'state' => $regionId, 'zip_code' => $data['postcode'], 'country' => 'US'); // end address include_once $this->lib_path . 'XMLParser.php'; include_once $this->lib_path . 'usps/USPSAddressVerify.php'; $verify = new USPSAddressVerify($key); if ($test_mode) { $verify->setTestMode(true); } else { $verify->setTestMode(false); } $usps_address = new USPSAddress(); if (isset($data['company']) && !empty($data['company'])) { $usps_address->setFirmName($data['company']); } $street_info = $address->getStreet(); $street1 = ''; $street2 = ''; if (is_array($street_info)) { $street1 = $street_info[0]; if (isset($street_info[1])) { $street2 = $street_info[1]; } } else { $street1 = $data['street']; } $usps_address->setApt($street2); $usps_address->setAddress($street1); $usps_address->setCity($data['city']); $usps_address->setState($regionId); $zip = trim($data['postcode']); $zip = str_replace(' ', '-', $zip); $z_p = explode('-', $zip); $zip4 = ''; $zip5 = $z_p[0]; if (isset($z_p[1]) && !empty($z_p[1])) { $zip4 = $z_p[1]; } $usps_address->setZip5($zip5); $usps_address->setZip4($zip4); $verify->addAddress($usps_address); // Perform the request and return result $verify->verify(); $response = $verify->getArrayResponse(); if ($verify->isSuccess()) { // get lis of addresses $candidates = $this->get_usps_candidates($response); // check if candidate address is differ from entered if (empty($candidates)) { return array('error' => 'NO', 'candidates' => array(), 'original_address' => $check_address); } // check if any address match for 100% $match = false; foreach ($candidates as $cand) { // compare state if (strtolower($cand['region_abbr']) != strtolower($check_address['state'])) { continue; } // compare zip if ($cand['postcode'] != $check_address['zip_code']) { $zip_parts1 = explode('-', $cand['postcode']); $zip_form = str_replace(' ', '-', $check_address['zip_code']); $zip_parts2 = explode('-', $zip_form); if ($zip_parts1[0] != $zip_parts2[0]) { continue; } } // from USPS $addr1 = strtolower($cand['street']); $city1 = strtolower($cand['city']); // from form $addr2 = strtolower($street1); $city2 = strtolower($check_address['city']); // compare street $p1 = strpos($addr1, $addr2); if ($p1 === false) { $p1 = strpos($addr2, $addr1); } if ($p1 === false) { continue; } // compare city $p2 = strpos($city1, $city2); if ($p2 === false) { $p2 = strpos($city2, $city1); } if ($p2 === false) { continue; } $match = true; break; } if (!$match) { $error = 'YES'; } return array('error' => $error, 'candidates' => $candidates, 'original_address' => $check_address); } else { $er_code = $verify->getErrorCode(); $er_code = strtolower($er_code); if ($er_code == '-2147219401') { return array('error' => 'NO', 'candidates' => array(), 'original_address' => $check_address); } elseif ($er_code == '80040b1a') { return array('error' => 'API Authorization failure. User is not authorized to use API Verify.', 'candidates' => array(), 'original_address' => $check_address); } elseif ($er_code == '-2147219040') { return array('error' => 'This Information has not been included in this Test Server.', 'candidates' => array(), 'original_address' => $check_address); } } } } else { return false; } return $error; }
public function usps_validate_street_address($type = 'Billing', $data = false) { $error = false; if (!in_array($type, array('Billing', 'Shipping')) && !$data) { return false; } if (!$data) { $address = $this->getQuote()->{"get{$type}Address"}(); $data = $address->getData(); } if (isset($data['country_id']) && !empty($data['country_id']) && $data['country_id'] == 'US') { // skip regions $state_no_ups = array('hawaii', 'virgin islands', 'puerto rico', 'guam'); if (!empty($data['region'])) { $reg = strtolower($data['region']); if (in_array($reg, $state_no_ups)) { return false; } } if (!empty($data['street']) && !empty($data['city']) && !empty($data['postcode']) && !empty($data['region_id'])) { $regionModel = Mage::getModel('directory/region')->load($data['region_id']); $regionId = $regionModel->getCode(); if (empty($regionId)) { return false; } $test_mode = (bool) Mage::getStoreConfig('onepagecheckout/usps_address_verification/test_mode'); $key = Mage::getStoreConfig('onepagecheckout/usps_address_verification/usps_access_key'); if (empty($key)) { return false; } $check_address = array('street' => $data['street'], 'city' => $data['city'], 'state' => $regionId, 'zip_code' => $data['postcode'], 'country' => 'US'); // end address include_once 'iwd/opcvalidation/usps/USPSAddressVerify.php'; $verify = new USPSAddressVerify($key); if ($test_mode) { $verify->setTestMode(true); } else { $verify->setTestMode(false); } $usps_address = new USPSAddress(); if (isset($data['company']) && !empty($data['company'])) { $usps_address->setFirmName($data['company']); } $street_info = $address->getStreet(); $street1 = ''; $street2 = ''; if (is_array($street_info)) { $street1 = $street_info[0]; if (isset($street_info[1])) { $street2 = $street_info[1]; } } else { $street1 = $data['street']; } $usps_address->setApt($street2); $usps_address->setAddress($street1); $usps_address->setCity($data['city']); $usps_address->setState($regionId); $zip = trim($data['postcode']); $zip = str_replace(' ', '-', $zip); $z_p = explode('-', $zip); $zip4 = ''; $zip5 = $z_p[0]; if (isset($z_p[1]) && !empty($z_p[1])) { $zip4 = $z_p[1]; } $usps_address->setZip5($zip5); $usps_address->setZip4($zip4); $verify->addAddress($usps_address); // Perform the request and return result $verify->verify(); $response = $verify->getArrayResponse(); if ($verify->isSuccess()) { // get lis of addresses $candidates = $this->get_usps_candidates($response); // check if candidate address is differ from entered if (empty($candidates)) { return array('error' => 'NO', 'candidates' => array(), 'original_address' => $check_address); } if (strtolower($candidates[0]['street']) != strtolower($street1) || strtolower($candidates[0]['city']) != strtolower($check_address['city']) || strtolower($candidates[0]['region_abbr']) != strtolower($check_address['state']) || strtolower($candidates[0]['postcode']) != strtolower($check_address['zip_code'])) { $error = 'YES'; } return array('error' => $error, 'candidates' => $candidates, 'original_address' => $check_address); } else { $er_code = $verify->getErrorCode(); if ($er_code == '-2147219401') { return array('error' => 'NO', 'candidates' => array(), 'original_address' => $check_address); } elseif ($er_code == '80040b1a') { return array('error' => 'API Authorization failure. User is not authorized to use API Verify.', 'candidates' => array(), 'original_address' => $check_address); // } elseif ($er_code == '-2147219040') { return array('error' => 'This Information has not been included in this Test Server.', 'candidates' => array(), 'original_address' => $check_address); } } } } else { return false; } return $error; }