3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 class CRM_Utils_Address_USPS
{
23 * Whether USPS validation should be disabled during import.
27 protected static $_disabled = FALSE;
30 * Disable the USPS validation.
32 * @param bool $disable
34 public static function disable($disable = TRUE) {
35 self
::$_disabled = $disable;
39 * Check address against USPS.
41 * @param array $values
45 public static function checkAddress(&$values) {
46 if (self
::$_disabled) {
49 if (!isset($values['street_address']) ||
50 (!isset($values['city']) &&
51 !isset($values['state_province']) &&
52 !isset($values['postal_code'])
58 $userID = Civi
::settings()->get('address_standardization_userid');
59 $url = Civi
::settings()->get('address_standardization_url');
67 $address2 = str_replace(',', '', $values['street_address']);
69 $XMLQuery = '<AddressValidateRequest USERID="' . $userID . '"><Address ID="0"><Address1>' . CRM_Utils_Array
::value('supplemental_address_1', $values, '') . '</Address1><Address2>' . $address2 . '</Address2><City>' . $values['city'] . '</City><State>' . $values['state_province'] . '</State><Zip5>' . $values['postal_code'] . '</Zip5><Zip4>' . CRM_Utils_Array
::value('postal_code_suffix', $values, '') . '</Zip4></Address></AddressValidateRequest>';
71 $client = new GuzzleHttp\
Client();
72 $request = $client->request('GET', $url, [
77 'timeout' => \Civi
::settings()->get('http_timeout'),
80 $session = CRM_Core_Session
::singleton();
82 $code = $request->getStatusCode();
84 $session->setStatus(ts('USPS Address Lookup Failed with HTTP status code: %1',
90 $responseBody = $request->getBody();
92 $xml = simplexml_load_string($responseBody);
94 if (is_null($xml) ||
is_null($xml->Address
)) {
95 $session->setStatus(ts('Your USPS API Lookup has Failed.'));
99 if ($xml->Number
== '80040b1a') {
100 $session->setStatus(ts('Your USPS API Authorization has Failed.'));
104 if (array_key_exists('Error', $xml->Address
)) {
105 $session->setStatus(ts('Address not found in USPS database.'));
109 $values['street_address'] = (string) $xml->Address
->Address2
;
110 $values['city'] = (string) $xml->Address
->City
;
111 $values['state_province'] = (string) $xml->Address
->State
;
112 $values['postal_code'] = (string) $xml->Address
->Zip5
;
113 $values['postal_code_suffix'] = (string) $xml->Address
->Zip4
;
115 if (array_key_exists('Address1', $xml->Address
)) {
116 $values['supplemental_address_1'] = (string) $xml->Address
->Address1
;