<?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 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; } }