Skip to content

Commit 4f7fba1

Browse files
committed
UPS validation Api wrapper
0 parents  commit 4f7fba1

File tree

7 files changed

+311
-0
lines changed

7 files changed

+311
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Rahul Godiyal
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
# RahulGodiyal\PhpUpsApiWrapper
3+
4+
API Wrapper for UPS web services.
5+
6+
7+
8+
9+
## Installation
10+
11+
Install with composer
12+
13+
```bash
14+
composer install rahul-godiyal/php-ups-api-wrapper
15+
```
16+
17+
## Usage/Examples
18+
19+
UPS Address Validation
20+
```php
21+
<?php
22+
23+
use RahulGodiyal\PhpUpsApiWrapper\AddressValidation;
24+
25+
require_once('./vendor/autoload.php');
26+
27+
$client_id = "xxxxxxxxxxxxxxxx"; // UPS Client ID
28+
$client_secret = "xxxxxxxxxxxxxxx"; // UPS Client Secret
29+
30+
// Address to be validated
31+
$address = [
32+
"AddressLine" => [
33+
"785 GODDARD CT"
34+
],
35+
"PoliticalDivision2" => "ALPHARATTA",
36+
"PoliticalDivision1" => "CA",
37+
"PostcodePrimaryLow" => "30005",
38+
"CountryCode" => "US"
39+
];
40+
41+
echo '<pre>';
42+
print_r(AddressValidation::setAddress($address)->validate($client_id, $client_secret));
43+
echo '</pre>';
44+
die();
45+
46+
```
47+

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "rahul-godiyal/php-ups-api-wrapper",
3+
"description": "API Wrapper for UPS web services",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"RahulGodiyal\\PhpUpsApiWrapper\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Rahul Godiyal",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"minimum-stability": "stable",
18+
"require": {}
19+
}

demo/validate-address.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use RahulGodiyal\PhpUpsApiWrapper\AddressValidation;
4+
5+
require_once('./vendor/autoload.php');
6+
7+
$client_id = "xxxxxxxxxxxxxx";
8+
$client_secret = "xxxxxxxxxxxxxxxx";
9+
10+
$address = [
11+
"AddressLine" => [
12+
"785 GODDARD CT"
13+
],
14+
"PoliticalDivision2" => "ALPHARATTA",
15+
"PoliticalDivision1" => "CA",
16+
"PostcodePrimaryLow" => "30005",
17+
"CountryCode" => "US"
18+
];
19+
20+
echo '<pre>';
21+
print_r(AddressValidation::setAddress($address)->validate($client_id, $client_secret));
22+
echo '</pre>';
23+
die();

src/AddressValidation.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

src/Auth.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace RahulGodiyal\PhpUpsApiWrapper;
4+
5+
class Auth
6+
{
7+
private $_mode;
8+
private $_dev_api_base_url;
9+
private $_prod_api_base_url;
10+
11+
public function __construct()
12+
{
13+
$this->_mode = 'DEV';
14+
$this->_dev_api_base_url = 'https://wwwcie.ups.com';
15+
$this->_prod_api_base_url = 'https://onlinetools.ups.com';
16+
}
17+
18+
/**
19+
* Authenticate User
20+
* @param string $client_id
21+
* @param string $client_secret
22+
* @return string $access_token
23+
*/
24+
public function authenticate(String $client_id, String $client_secret)
25+
{
26+
$curl = curl_init();
27+
28+
curl_setopt_array($curl, [
29+
CURLOPT_HTTPHEADER => [
30+
"Content-Type: application/x-www-form-urlencoded",
31+
"x-merchant-id: string",
32+
"Authorization: Basic " . base64_encode($client_id . ":" . $client_secret)
33+
],
34+
CURLOPT_POSTFIELDS => $this->_getPayload(),
35+
CURLOPT_URL => $this->_getAPIBaseURL() . "/security/v1/oauth/token",
36+
CURLOPT_RETURNTRANSFER => true,
37+
CURLOPT_CUSTOMREQUEST => "POST",
38+
]);
39+
40+
$response = curl_exec($curl);
41+
curl_close($curl);
42+
$res = json_decode($response);
43+
44+
if (!isset($res->access_token)) {
45+
$error = $res->response->errors[0]->message;
46+
return ['status' => 'fail', 'msg' => $error];
47+
}
48+
49+
$access_token = $res->access_token;
50+
return ['status' => 'success', 'access_token' => $access_token];
51+
}
52+
53+
/**
54+
* Get Payload
55+
* @return string
56+
*/
57+
private function _getPayload()
58+
{
59+
return "grant_type=client_credentials";
60+
}
61+
62+
/**
63+
* Set Mode
64+
* @param string DEV|PROD
65+
*/
66+
public function setMode(String $mode)
67+
{
68+
if ($mode === 'PROD') {
69+
$this->_mode = $mode;
70+
}
71+
72+
$this->_mode = 'DEV';
73+
}
74+
75+
/**
76+
* Get Api Base URL
77+
* @return string url
78+
*/
79+
protected function _getAPIBaseURL()
80+
{
81+
if ($this->_mode === 'PROD') {
82+
return $this->_prod_api_base_url;
83+
}
84+
85+
return $this->_dev_api_base_url;
86+
}
87+
}

0 commit comments

Comments
 (0)