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 validateAddressAction() { $data = $this->getRequest()->getParams(); $address_type = 'shipping'; if (isset($data['billing'])) { $address_type = 'billing'; } $regions = json_decode(Mage::helper("pixcountries/data")->getRegionJson(), true); $us_regions = (array) $regions['US']; $region = $us_regions[$data[$address_type]['region_id']]['code']; require_once './lib/USPS/USPS-php-api-master/USPSAddressVerify.php'; $verify = new USPSAddressVerify('356FRANZ7578'); $address = new USPSAddress(); $address->setFirmName('Apartment'); $address->setApt($data[$address_type]['street'][0]); $address->setAddress($data[$address_type]['street'][1]); $address->setCity($data[$address_type]['city']); $address->setState($region); $address->setZip5($data[$address_type]['postcode']); $address->setZip4(''); $verify->addAddress($address); $verify->verify(); $response = $verify->getArrayResponse(); if ($verify->isSuccess()) { if ($region == "AK" || $region == "HI" || $region == "PR") { echo "Alaska, Hawaii and Puerto Rico are not allowed yet."; } else { $address_resp = $response['AddressValidateResponse']['Address']; // check if address have any corrections if ($address_resp['Address2'] != $data[$address_type]['street'][0] || $address_resp['City'] != $data[$address_type]['city'] || $address_resp['State'] != $region || $address_resp['Zip5'] != $data[$address_type]['postcode']) { if ($address_resp['State'] != $region) { $address_resp['State'] = '<span class="pinktxt" field="' . $address_type . '[region_id]">' . $address_resp['State'] . '</span>'; } if ($address_resp['City'] != $data[$address_type]['city']) { $address_resp['City'] = '<span class="pinktxt" field="' . $address_type . '[city]">' . $address_resp['City'] . '</span>'; } if ($address_resp['Address2'] != $data[$address_type]['street'][0]) { $address_resp['Address2'] = '<span class="pinktxt" field="' . $address_type . '[street][]">' . $address_resp['Address2'] . '</span>'; } if ($address_resp['Zip5'] != $data[$address_type]['postcode']) { $address_resp['Zip5'] = '<span class="pinktxt" field="' . $address_type . '[postcode]">' . $address_resp['Zip5'] . '</span>'; } $html = '<div class="modal fade" id="verify_address" tabindex="-1" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close close-address-validation" data-dismiss="modal" aria-label="Close" id="close-modal"><span aria-hidden="true">×</span></button> <h2>Verify Your Shipping Address</h2> <p>To ensure a sweet delivery, the USPS suggests a slightly modified address. We’ve marked our suggestions in <span class="pinktxt">pink</span> below. Please choose the version you want to use.</p> </div> <div class="modal-body"> <div class="address_options"> <div class="address_option first-opt" onclick="select_address(this)"> <p><strong>Original Address:</strong><br /> ' . $data[$address_type]['firstname'] . ' ' . $data[$address_type]['lastname'] . ', ' . $data[$address_type]['street'][0] . ', ' . (!empty($data[$address_type]['street'][1]) ? $data[$address_type]['street'][1] . ', ' : "") . $data[$address_type]['city'] . ', ' . $region . ' ' . $data[$address_type]['postcode'] . ', United States</p> </div> <div class="address_option correct_opt selected"> <p><strong>Suggested:</strong><br /> ' . $data[$address_type]['firstname'] . ' ' . $data[$address_type]['lastname'] . ', ' . $address_resp['Address2'] . ', ' . (!empty($data[$address_type]['street'][1]) ? $data[$address_type]['street'][1] . ', ' : "") . $address_resp['City'] . ', ' . $address_resp['State'] . ' ' . $address_resp['Zip5'] . ', United States</p> </div> </div> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" id="ship_to_this_address"><span class="blue-bttn">Ship to this address</span></a> <div class="clearfix"></div> </div> </div> </div> </div>'; echo $html; } else { echo "ok"; } die; } } else { $html = '<div class="modal fade" id="verify_address" tabindex="-1" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close close-address-validation" data-dismiss="modal" aria-label="Close" id="close-modal"><span aria-hidden="true">×</span></button> <h2>Error</h2> <p style="margin-left: 42px;">' . $verify->getErrorMessage() . '</p> </div> </div> </div> </div>'; echo $html; die; } }
<?php // Load the class require_once '../USPSAddressVerify.php'; // Initiate and set the username provided from usps $verify = new USPSAddressVerify('xxxx'); // During test mode this seems not to always work as expected //$verify->setTestMode(true); // Create new address object and assign the properties // apartently the order you assign them is important so make sure // to set them as the example below $address = new USPSAddress(); $address->setFirmName('Apartment'); $address->setApt('100'); $address->setAddress('9200 Milliken Ave'); $address->setCity('Rancho Cucomonga'); $address->setState('CA'); $address->setZip5(91730); $address->setZip4(''); // Add the address object to the address verify class $verify->addAddress($address); // Perform the request and return result print_r($verify->verify()); print_r($verify->getArrayResponse()); var_dump($verify->isError()); // See if it was successful if ($verify->isSuccess()) { echo 'Done'; } else { echo 'Error: ' . $verify->getErrorMessage(); }
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; }