Skip to content

Commit 55c3441

Browse files
authored
Merge pull request #2 from ctwillie/development
Development
2 parents 0196dbd + 65c3188 commit 55c3441

File tree

4 files changed

+127
-50
lines changed

4 files changed

+127
-50
lines changed

README.md

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Laravel-Usps
22

3-
This package provides a very simple wrapper for the United States Postal Service API. Currently, this package only provides address verification features, but will soon comprise all features offered by the USPS API. In the meantime, consider using [johnpaulmedina/laravel-usps](https://github.com/johnpaulmedina/laravel-usps), which is a great package.
3+
This package provides a very simple wrapper for the United States Postal Service API. Currently, this package only provides address validation features, but will soon comprise all features offered by the USPS API. In the meantime, consider using [johnpaulmedina/laravel-usps](https://github.com/johnpaulmedina/laravel-usps), which is a great package.
44

55
### Prerequisites
66

@@ -13,54 +13,80 @@ from the United States Postal Service. This user ID is required to use this pack
1313
composer require ctwillie/laravel-usps
1414
```
1515

16-
## Setup
17-
18-
Starting at Laravel 5.5, this package will be automatically discovered and registered as a service provider.
19-
For earlier versions of Laravel, you will need to manually register this packages' service provider in `config/app.php`
20-
by adding this class to the providers array.
21-
22-
```php
23-
'providers' => [
24-
...
25-
ctwillie\Usps\UspsServiceProvider::class
26-
];
27-
```
28-
Then add an alias for the class also in `config/app.php` inside the aliases array.
29-
30-
```php
31-
'aliases' => [
32-
...
33-
'Usps' => ctwillie\Usps\UspsServiceProvider::class
34-
];
35-
```
36-
3716
## Configuration
3817

39-
There are two important configurations.
18+
There are three important configurations.
4019
1. Your USPS user ID:
4120
- If you have not received your USPS user ID, follow the link in the [prerequisites](#Prerequisites) section to register with the
4221
United States Postal Service. It is required to use this package.
4322

4423
2. Whether you want SSL verification enabled for API requests:
45-
- This setting is set to `true` by default for security reasons. You can override this behavior by setting the `verrifyssl` config setting to `false`. Do this at your own risk.
24+
- This setting is set to `true` by default for security reasons. You can override this behavior by setting the `verrifyssl` config setting to `false`. Do this at your own risk.
25+
26+
3. Which environment you are working in:
27+
- The options are `'local' and 'production'` which tell the package which API url to use, testing or production respectively. If no configuration is found for `env`, it will default to the environment recognized by laravel. This setting takes precedence over `APP_ENV` from your `.env` file.
4628

4729
We recommend placing all configuration settings in your `.env` file and use Laravel's `env()` helper function to access these values.
4830

49-
In `config/services.php` add these two settings.
31+
In `config/services.php` add these three settings.
5032

5133
```php
5234
'usps' => [
5335

54-
'userid' => env('USPS_USER_ID'), // ********
55-
'verifyssl' => env('USPS_VERIFY_SSL') // true|false
56-
36+
'userid' => env('USPS_USER_ID'), // string
37+
'verifyssl' => env('USPS_VERIFY_SSL'), // bool
38+
'env' => env('USPS_ENV') //string
5739
];
5840
```
5941

6042
## Usage
6143

62-
The current features offered by this package are listed below.
63-
- [Address Verification](#Address)
44+
The current features offered by this package are:
45+
- [Address Validation](#Address-Validation)
46+
47+
48+
## Address Validation
49+
50+
The `Address` class handles creating and formatting address data. Pass the constructor an associative array of address details. Array keys are case-sensitive.
51+
Below is an example of creating an address and making an api request for validation.
52+
53+
```php
54+
use ctwillie\Usps\Address;
55+
56+
$address = new Address([
57+
'Address2' => '6406 Ivy Lane',
58+
'City' => 'Greenbelt',
59+
'State' => 'MD',
60+
'Zip5' => 20770
61+
]);
62+
63+
$response = $address->validate();
64+
```
65+
The USPS api supports up to 5 address validations per request. If you need to validate more than one address at a time, pass a multi dim array to the `Address` constructor.
66+
67+
```php
68+
use ctwillie\Usps\Address;
69+
70+
$address1 = [
71+
'Address2' => '6406 Ivy Lane',
72+
'City' => 'Greenbelt',
73+
'State' => 'MD',
74+
'Zip5' => 20770
75+
];
76+
77+
$address2 = [
78+
'Address2' => '6406 Ivy Lane',
79+
'City' => 'Greenbelt',
80+
'State' => 'MD',
81+
'Zip5' => 20770
82+
];
83+
84+
$addresses = new Address([$address1, $address2])
85+
86+
$response = $addresses->validate();
87+
```
88+
89+
The response will contain the [corrected address](https://www.usps.com/business/web-tools-apis/address-information-api.pdf), or an error if not enough information was given or the address does not exists.
6490

6591
## Contributing
6692

dev-notes.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Using a local repository
2+
"repositories": [
3+
{
4+
"type": "path",
5+
"url": "~/code/packages/ctwillie/laravel-usps",
6+
"options": {
7+
"symlink": true
8+
}
9+
}
10+
]
11+
12+
# Then run
13+
composer require package-name @dev

src/Usps/API.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,75 @@
77
use Spatie\ArrayToXml\ArrayToXml;
88
use GuzzleHttp\Client;
99

10+
/**
11+
* Class used by each entity to interact with the API and get configurations
12+
*
13+
* Each entity will be responsible for managing its own data used in the api
14+
* request. This class handles the common functions, such as, data formatting,
15+
* creating http client, making request, returning response etc.
16+
*
17+
*/
1018
abstract class API {
1119

1220
const PROD_URL = 'https://secure.shippingapis.com/ShippingAPI.dll';
1321
const LOCAL_URL = 'http://production.shippingapis.com/ShippingAPITest.dll';
1422
protected $env;
1523
protected $apiUrl;
1624
protected $userId;
25+
protected $rootElements = [
26+
'Verify' => 'AddressValidateRequest'
27+
];
1728

29+
/** Determines env to set api url, http client settings etc. */
1830
public function __construct() {
1931
$this->setEnvironment();
2032
$this->setUrl();
2133
$this->userId = config('services.usps.userid');
2234
}
2335

24-
// Determines the current application environment
36+
/** Determines the current application env */
2537
public function setEnvironment()
2638
{
2739
if (Config::has('services.usps.env')) {
40+
2841
$this->env = strtolower(config('services.usps.env'));
42+
2943
} elseif (App::environment(['local', 'development'])) {
44+
3045
$this->env = 'local';
31-
}
46+
}
47+
3248
$this->env = 'production';
3349
}
3450

51+
/** Determines the api url to use based on running env */
3552
public function setUrl()
3653
{
3754
$this->apiUrl = $this->env === 'local' ? self::LOCAL_URL : self::PROD_URL;
3855
}
3956

57+
/**
58+
* Makes sure a userid has been provided
59+
*
60+
* @throws Exception If no userid is found in config/services.php
61+
*/
4062
public function checkForUserId()
4163
{
4264
if (is_null($this->userId)) {
4365
throw new \Exception('USPS: A user ID is required. None found in config/services.php');
4466
}
4567
}
4668

47-
// create a map to determine the rootElementName foreach api type
69+
/**
70+
* Converts entity data into xml format and url encodes
71+
*
72+
* @return string
73+
*/
4874
protected function convertToXML()
4975
{
5076
$xml = ArrayToXml::convert( $this->getRequestData(),
5177
[
52-
'rootElementName' => $this->rootElementName,
78+
'rootElementName' => $this->rootElements[$this->apiType],
5379

5480
'_attributes' => ['USERID' => $this->userId]
5581

@@ -58,6 +84,14 @@ protected function convertToXML()
5884
return urlencode($xml);
5985
}
6086

87+
/**
88+
* Handles making the api request
89+
*
90+
* This method handles creating the http client and gathering
91+
* all necessary data for making the api request.
92+
*
93+
* @return StreamInterface of the response content body
94+
*/
6195
public function makeRequest()
6296
{
6397
$this->checkForUserId();
@@ -71,8 +105,12 @@ public function makeRequest()
71105
return $response->getBody();
72106
}
73107

74-
// Abstract to be defined by each entity
75-
abstract public function getRequestData() : array;
108+
/**
109+
* Each entity decides the array structure for the request data
110+
*
111+
* @return array
112+
*/
113+
abstract public function getRequestData();
76114

77115
public function validate()
78116
{

src/Usps/Address.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace ctwillie\Usps;
44

5+
/**
6+
* This class gathers data for the 'Verify' api route
7+
*
8+
* It is responsible for formatting address request data that
9+
* will be handled by the API class
10+
*/
511
class Address extends API
612
{
7-
// use map to determine root element
8-
// key will be api type ('verify' => 'AddressValidateRequest')
9-
protected $rootElementName = 'AddressValidateRequest';
1013
protected $apiType = 'Verify';
1114
protected $addresses = [];
1215
protected $allowedProperties = ['Address1','Address2','City','State','Zip4','Zip5'];
@@ -16,8 +19,6 @@ class Address extends API
1619
* Handles single or multiple addresses passes to constructor
1720
*
1821
* @param array $addresses
19-
* @return void
20-
*
2122
*/
2223
public function __construct(array $addresses = [])
2324
{
@@ -26,11 +27,9 @@ public function __construct(array $addresses = [])
2627
}
2728

2829
/**
29-
* Handles multiple addresses
30+
* Handles adding multiple addresses
3031
*
3132
* @param array $addresses
32-
* @return void
33-
*
3433
*/
3534
public function handleMultipleAddresses(array $addresses)
3635
{
@@ -55,13 +54,9 @@ public function handleMultipleAddresses(array $addresses)
5554
}
5655

5756
/**
58-
* Handles single address
57+
* Handles adding a single address
5958
*
6059
* @param array $address
61-
* @return void
62-
*
63-
* Add addAddress method which calls this method and checks five address limit
64-
*
6560
*/
6661
public function handleSingleAddress(array $address)
6762
{
@@ -80,7 +75,12 @@ public function handleSingleAddress(array $address)
8075
}
8176
}
8277

83-
public function getRequestData() : array
78+
/**
79+
* Formats the request data.
80+
*
81+
* @return array
82+
*/
83+
public function getRequestData()
8484
{
8585
return ['Address' => $this->addresses];
8686
}

0 commit comments

Comments
 (0)