|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RahulGodiyal\PhpUpsApiWrapper; |
| 4 | + |
| 5 | +use RahulGodiyal\PhpUpsApiWrapper\Auth; |
| 6 | + |
| 7 | +class AddressValidation extends Auth |
| 8 | +{ |
| 9 | + private static $_address; |
| 10 | + private $_request_option; |
| 11 | + private $_version; |
| 12 | + private $_query; |
| 13 | + |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + parent::__construct(); |
| 17 | + $this->_request_option = "3"; |
| 18 | + $this->_version = "v2"; |
| 19 | + $this->_query = [ |
| 20 | + "regionalrequestindicator" => "string", |
| 21 | + "maximumcandidatelistsize" => "1" |
| 22 | + ]; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Set Address to validate |
| 27 | + * @param array $address |
| 28 | + * @return self |
| 29 | + */ |
| 30 | + public static function setAddress(array $address) |
| 31 | + { |
| 32 | + self::$_address = $address; |
| 33 | + return new self; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Validate the address |
| 38 | + * @param string $client_id |
| 39 | + * @param string $client_secret |
| 40 | + * @return array of validated address |
| 41 | + */ |
| 42 | + public function validate(String $client_id, String $client_secret) |
| 43 | + { |
| 44 | + $auth = $this->authenticate($client_id, $client_secret); |
| 45 | + |
| 46 | + if ($auth['status'] == 'fail') { |
| 47 | + return $auth; |
| 48 | + } |
| 49 | + |
| 50 | + $access_token = $auth['access_token']; |
| 51 | + $curl = curl_init(); |
| 52 | + |
| 53 | + curl_setopt_array($curl, [ |
| 54 | + CURLOPT_HTTPHEADER => [ |
| 55 | + "Authorization: Bearer $access_token", |
| 56 | + "Content-Type: application/json" |
| 57 | + ], |
| 58 | + CURLOPT_POSTFIELDS => json_encode($this->_payload()), |
| 59 | + CURLOPT_URL => $this->_getAPIBaseURL() . "/api/addressvalidation/" . $this->_version . "/" . $this->_request_option . "?" . http_build_query($this->_query), |
| 60 | + CURLOPT_RETURNTRANSFER => true, |
| 61 | + CURLOPT_CUSTOMREQUEST => "POST", |
| 62 | + ]); |
| 63 | + |
| 64 | + $response = curl_exec($curl); |
| 65 | + curl_close($curl); |
| 66 | + $res = json_decode($response); |
| 67 | + |
| 68 | + if (!isset($res->XAVResponse)) { |
| 69 | + $error = $res->response->errors[0]->message; |
| 70 | + return ['status' => 'fail', 'msg' => $error]; |
| 71 | + } |
| 72 | + |
| 73 | + $addresses = $this->_getAddresses($res->XAVResponse->Candidate); |
| 74 | + return ['status' => 'success', 'addresses' => $addresses]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get Payload |
| 79 | + * @return array $payload |
| 80 | + */ |
| 81 | + private function _payload() |
| 82 | + { |
| 83 | + return [ |
| 84 | + "XAVRequest" => [ |
| 85 | + "AddressKeyFormat" => self::$_address |
| 86 | + ] |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get Addresses |
| 92 | + * @param array of objects |
| 93 | + * @return array of addresses |
| 94 | + */ |
| 95 | + private function _getAddresses(Array $candidates) |
| 96 | + { |
| 97 | + $addresses = []; |
| 98 | + foreach ($candidates as $candObj) { |
| 99 | + $addressObj = $candObj->AddressKeyFormat; |
| 100 | + $address = [ |
| 101 | + 'address_line' => $addressObj->AddressLine, |
| 102 | + 'city' => $addressObj->PoliticalDivision2, |
| 103 | + 'state' => $addressObj->PoliticalDivision1, |
| 104 | + 'zipcode' => $addressObj->PostcodePrimaryLow, |
| 105 | + 'region' => $addressObj->Region, |
| 106 | + 'country' => $addressObj->CountryCode |
| 107 | + ]; |
| 108 | + array_push($addresses, $address); |
| 109 | + } |
| 110 | + |
| 111 | + return $addresses; |
| 112 | + } |
| 113 | +} |
0 commit comments