public function ups_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/ups_address_verification/test_mode'); $login = Mage::getStoreConfig('addressverification/ups_address_verification/ups_login'); $pass = Mage::getStoreConfig('addressverification/ups_address_verification/ups_pass'); $key = Mage::getStoreConfig('addressverification/ups_address_verification/ups_access_key'); /// setup config $GLOBALS['ups_api']['access_key'] = $key; $GLOBALS['ups_api']['developer_key'] = ''; if ($test_mode) { $GLOBALS['ups_api']['server'] = 'https://wwwcie.ups.com'; $GLOBALS['ups_street_level_api']['server'] = 'https://wwwcie.ups.com'; // in other DOCS test server should be https://wwwcie.ups.com/webservices/XAV } else { $GLOBALS['ups_api']['server'] = 'https://www.ups.com'; $GLOBALS['ups_street_level_api']['server'] = 'https://onlinetools.ups.com'; // in other DOCS live server should be https://onlinetools.ups.com/webservices/XAV } /** set the username and password used to connect to UPS **/ $GLOBALS['ups_api']['username'] = $login; $GLOBALS['ups_api']['password'] = $pass; /////////// include_once $this->lib_path . 'XMLParser.php'; include_once $this->lib_path . 'ups/UpsAPI.php'; include_once $this->lib_path . 'ups/UpsAPI/USStreetLevelValidation.php'; $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 $customer_data = ''; $validation = new UpsAPI_USStreetLevelValidation($check_address); $xml = $validation->buildRequest($customer_data); // returns an array $response = $validation->sendRequest($xml); if (isset($response['Response'])) { // check for errors if (!isset($response['AddressKeyFormat'])) { $res_errors = $validation->getResultsErrors(); if (!empty($res_errors)) { $err = implode('; ', $res_errors); return array('error' => $err, 'candidates' => array(), 'original_address' => $check_address); } } // $match_type = $validation->getMatchType(); // get lis of addresses $candidates = $this->get_ups_candidates($response); /* if($match_type == 'Unknown') { $error = 'NO'; // check if any address match for 100% and get it classification foreach($candidates as $cand) { // compare state if($cand['region'] != $data['region_id']) continue; // compare zip if($cand['postcode'] != $data['postcode']) continue; // from UPS $addr1 = strtolower($cand['street']); $city1 = strtolower($cand['city']); // from form $addr2 = strtolower($data['street']); $city2 = strtolower($data['city']); // compare street if($addr1 != $addr2) continue; // compare city if($city1 != $city2) continue; $error = false; $match_type = $cand['class_description']; break; } } */ if ($error != 'NO') { // check if any address match for 100% $match = false; foreach ($candidates as $cand) { // compare state if ($cand['region'] != $data['region_id']) { continue; } // compare zip if ($cand['postcode'] != $data['postcode']) { $zip_parts1 = explode('-', $cand['postcode']); $zip_form = str_replace(' ', '-', $data['postcode']); $zip_parts2 = explode('-', $zip_form); if ($zip_parts1[0] != $zip_parts2[0]) { continue; } } // from UPS $addr1 = strtolower($cand['street']); $city1 = strtolower($cand['city']); // from form $addr2 = strtolower($data['street']); $city2 = strtolower($data['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 { return false; } return $error; }
public function ups_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/ups_address_verification/test_mode'); $login = Mage::getStoreConfig('onepagecheckout/ups_address_verification/ups_login'); $pass = Mage::getStoreConfig('onepagecheckout/ups_address_verification/ups_pass'); $key = Mage::getStoreConfig('onepagecheckout/ups_address_verification/ups_access_key'); /// setup config $GLOBALS['ups_api']['access_key'] = $key; $GLOBALS['ups_api']['developer_key'] = ''; if ($test_mode) { $GLOBALS['ups_api']['server'] = 'https://wwwcie.ups.com'; $GLOBALS['ups_street_level_api']['server'] = 'https://wwwcie.ups.com'; // in other DOCS test server should be https://wwwcie.ups.com/webservices/XAV } else { $GLOBALS['ups_api']['server'] = 'https://www.ups.com'; $GLOBALS['ups_street_level_api']['server'] = 'https://onlinetools.ups.com'; // in other DOCS live server should be https://onlinetools.ups.com/webservices/XAV } /** set the username and password used to connect to UPS **/ $GLOBALS['ups_api']['username'] = $login; $GLOBALS['ups_api']['password'] = $pass; /////////// include_once 'iwd/opcvalidation/ups/UpsAPI.php'; include_once 'iwd/opcvalidation/ups/UpsAPI/USStreetLevelValidation.php'; $check_address = array('street' => $data['street'], 'city' => $data['city'], 'state' => $regionId, 'zip_code' => $data['postcode'], 'country' => 'US'); // end address $customer_data = ''; $validation = new UpsAPI_USStreetLevelValidation($check_address); $xml = $validation->buildRequest($customer_data); // returns an array $response = $validation->sendRequest($xml, false); if (isset($response['Response'])) { $match_type = $validation->getMatchType(); if ($match_type == 'Unknown') { $error = 'NO'; } // get lis of addresses $candidates = $this->get_ups_candidates($response); return array('error' => $error, 'candidates' => $candidates, 'original_address' => $check_address); } } } else { return false; } return $error; }