Merge pull request #16169 from agh1/5.21.0-releasenotes-final
[civicrm-core.git] / CRM / Utils / Address / USPS.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * Address utilities.
19 */
20 class CRM_Utils_Address_USPS {
21
22 /**
23 * Whether USPS validation should be disabled during import.
24 *
25 * @var bool
26 */
27 protected static $_disabled = FALSE;
28
29 /**
30 * Disable the USPS validation.
31 *
32 * @param bool $disable
33 */
34 public static function disable($disable = TRUE) {
35 self::$_disabled = $disable;
36 }
37
38 /**
39 * Check address against USPS.
40 *
41 * @param array $values
42 *
43 * @return bool
44 */
45 public static function checkAddress(&$values) {
46 if (self::$_disabled) {
47 return FALSE;
48 }
49 if (!isset($values['street_address']) ||
50 (!isset($values['city']) &&
51 !isset($values['state_province']) &&
52 !isset($values['postal_code'])
53 )
54 ) {
55 return FALSE;
56 }
57
58 $userID = Civi::settings()->get('address_standardization_userid');
59 $url = Civi::settings()->get('address_standardization_url');
60
61 if (empty($userID) ||
62 empty($url)
63 ) {
64 return FALSE;
65 }
66
67 $address2 = str_replace(',', '', $values['street_address']);
68
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>';
70
71 require_once 'HTTP/Request.php';
72 $request = new HTTP_Request();
73
74 $request->setURL($url);
75
76 $request->addQueryString('API', 'Verify');
77 $request->addQueryString('XML', $XMLQuery);
78
79 $response = $request->sendRequest();
80
81 $session = CRM_Core_Session::singleton();
82
83 $code = $request->getResponseCode();
84 if ($code != 200) {
85 $session->setStatus(ts('USPS Address Lookup Failed with HTTP status code: %1',
86 [1 => $code]
87 ));
88 return FALSE;
89 }
90
91 $responseBody = $request->getResponseBody();
92
93 $xml = simplexml_load_string($responseBody);
94
95 if (is_null($xml) || is_null($xml->Address)) {
96 $session->setStatus(ts('Your USPS API Lookup has Failed.'));
97 return FALSE;
98 }
99
100 if ($xml->Number == '80040b1a') {
101 $session->setStatus(ts('Your USPS API Authorization has Failed.'));
102 return FALSE;
103 }
104
105 if (array_key_exists('Error', $xml->Address)) {
106 $session->setStatus(ts('Address not found in USPS database.'));
107 return FALSE;
108 }
109
110 $values['street_address'] = (string) $xml->Address->Address2;
111 $values['city'] = (string) $xml->Address->City;
112 $values['state_province'] = (string) $xml->Address->State;
113 $values['postal_code'] = (string) $xml->Address->Zip5;
114 $values['postal_code_suffix'] = (string) $xml->Address->Zip4;
115
116 if (array_key_exists('Address1', $xml->Address)) {
117 $values['supplemental_address_1'] = (string) $xml->Address->Address1;
118 }
119
120 return TRUE;
121 }
122
123 }