From 9b6f524c04d6e47f68d1c50fac935eab73bf091d Mon Sep 17 00:00:00 2001 From: Patrick Breen Date: Wed, 14 Feb 2018 15:33:57 -0500 Subject: [PATCH 1/4] Push Usps namespace into Johnpualmedina scope; add trackConfirm support; add testmode support --- README.md | 26 +++++++++++--- composer.json | 2 +- src/Usps/Address.php | 2 +- src/Usps/AddressVerify.php | 2 +- src/Usps/CityStateLookup.php | 2 +- src/Usps/Exceptions/UspsException.php | 19 ++++++++++ .../Exceptions/UspsTrackConfirmException.php | 19 ++++++++++ src/Usps/Facades/Usps.php | 2 +- src/Usps/FirstClassServiceStandards.php | 2 +- src/Usps/InternationalLabel.php | 2 +- src/Usps/OpenDistributeLabel.php | 2 +- src/Usps/PriorityLabel.php | 2 +- src/Usps/PriorityMailServiceStandards.php | 2 +- src/Usps/Rate.php | 2 +- src/Usps/RatePackage.php | 2 +- src/Usps/ServiceDeliveryCalculator.php | 2 +- src/Usps/TrackConfirm.php | 23 ++++++++++-- src/Usps/USPSBase.php | 2 +- src/Usps/Usps.php | 35 ++++++++++++++++--- src/Usps/UspsServiceProvider.php | 4 +-- src/Usps/XML2Array.php | 2 +- src/Usps/XMLParser.php | 2 +- src/Usps/ZipCodeLookup.php | 2 +- 23 files changed, 131 insertions(+), 29 deletions(-) create mode 100644 src/Usps/Exceptions/UspsException.php create mode 100644 src/Usps/Exceptions/UspsTrackConfirmException.php diff --git a/README.md b/README.md index cb5dd73..80876eb 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ To wire this up in your Laravel project you need to add the service provider. Open `config/app.php`, and add a new item to the providers array. ```php -'Usps\UspsServiceProvider', +Johnpaulmedina\Usps\UspsServiceProvider::class, ``` Then you must also specify the alias in `config/app.php`. Add a new item to the Aliases array. ```php -'Usps' => 'Usps\Facades\Usps', +'Usps' => Johnpaulmedina\Usps\Facades\Usps::class, ``` This will allow integration by adding the Facade `Use Usps;` @@ -32,8 +32,9 @@ Add your USPS username config in `config/services.php`. ```php 'usps' => [ - 'username' => "XXXXXXXXXXXX" - ] + 'username' => "XXXXXXXXXXXX", + 'testmode' => false, + ], ``` ## Example Controller Usage @@ -60,6 +61,23 @@ class USPSController extends Controller ) ); } + + public function trackConfirm() { + return response()->json( + Usps::trackConfirm( + Request::input('id') + ) + ); + } + + public function trackConfirmRevision1() { + return response()->json( + Usps::trackConfirm( + Request::input('id'), + 'Acme, Inc' + ) + ); + } } ``` diff --git a/composer.json b/composer.json index 36ffc67..ab0b0b2 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "autoload": { "psr-0": { - "Usps": "src/" + "Johnpaulmedina\Usps": "src/" } }, "minimum-stability": "dev" diff --git a/src/Usps/Address.php b/src/Usps/Address.php index 586c21a..28a1e36 100755 --- a/src/Usps/Address.php +++ b/src/Usps/Address.php @@ -11,7 +11,7 @@ */ -namespace Usps; +namespace Johnpaulmedina\Usps; class Address { diff --git a/src/Usps/AddressVerify.php b/src/Usps/AddressVerify.php index 6fdf321..21c1165 100755 --- a/src/Usps/AddressVerify.php +++ b/src/Usps/AddressVerify.php @@ -9,7 +9,7 @@ */ -namespace Usps; +namespace Johnpaulmedina\Usps; class AddressVerify extends USPSBase { diff --git a/src/Usps/CityStateLookup.php b/src/Usps/CityStateLookup.php index 17ebd2f..df1d117 100755 --- a/src/Usps/CityStateLookup.php +++ b/src/Usps/CityStateLookup.php @@ -11,7 +11,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; class CityStateLookup extends USPSBase { diff --git a/src/Usps/Exceptions/UspsException.php b/src/Usps/Exceptions/UspsException.php new file mode 100644 index 0000000..7a62886 --- /dev/null +++ b/src/Usps/Exceptions/UspsException.php @@ -0,0 +1,19 @@ +packages; + return array_merge($this->requestData, $this->packages); } /** @@ -56,4 +60,19 @@ public function addPackage($id) { $this->packages['TrackID'][] = ['@attributes' => ['ID' => $id]]; } + + /** + * Set revision ID and additional required fields + * + * @param string $clientIp + * @param string $sourceId + * @param int $revisionId + * @return void + */ + public function setRevision($clientIp, $sourceId, $revisionId = 1) + { + $this->requestData['Revision'] = $revisionId; + $this->requestData['ClientIp'] = $clientIp; + $this->requestData['SourceId'] = $sourceId; + } } diff --git a/src/Usps/USPSBase.php b/src/Usps/USPSBase.php index 1b7e48d..066f8ca 100755 --- a/src/Usps/USPSBase.php +++ b/src/Usps/USPSBase.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; abstract class USPSBase { diff --git a/src/Usps/Usps.php b/src/Usps/Usps.php index 3e462d4..5192a10 100644 --- a/src/Usps/Usps.php +++ b/src/Usps/Usps.php @@ -10,11 +10,9 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; -function __autoload($class_name) { - include $class_name . '.php'; -} +use Johnpaulmedina\Usps\Exceptions\UspsTrackConfirmException; class Usps { @@ -54,4 +52,33 @@ public function validate($request) { } + + /** + * @param $ids array|string + * @param $sourceId null|string + * + * @return array + * @throws UspsTrackConfirmException + */ + public function trackConfirm($ids, $sourceId = null) + { + $trackConfirm = new TrackConfirm($this->config['username']); + $trackConfirm->setTestMode(empty($this->config['testmode']) ? false : true); + + if ($sourceId) { + // Assume revision 1 tracking is desired when sourceId supplied + $trackConfirm->setRevision(request()->getClientIp(), $sourceId); + } + + collect(is_array($ids)?: [ $ids ])->each(function ($id) use ($trackConfirm) { + $trackConfirm->addPackage($id); + }); + + $trackConfirm->getTracking(); + if ($trackConfirm->isError()) { + throw new UspsTrackConfirmException($trackConfirm->getErrorMessage(), $trackConfirm->getErrorCode()); + } + + return $trackConfirm->getArrayResponse(); + } } diff --git a/src/Usps/UspsServiceProvider.php b/src/Usps/UspsServiceProvider.php index 93c5712..2ea85c5 100644 --- a/src/Usps/UspsServiceProvider.php +++ b/src/Usps/UspsServiceProvider.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; use Illuminate\Support\ServiceProvider; @@ -49,4 +49,4 @@ public function provides() return array('usps'); } -} \ No newline at end of file +} diff --git a/src/Usps/XML2Array.php b/src/Usps/XML2Array.php index 9d0479a..8937d9f 100755 --- a/src/Usps/XML2Array.php +++ b/src/Usps/XML2Array.php @@ -10,7 +10,7 @@ */ -namespace Usps; +namespace Johnpaulmedina\Usps; /** * XML2Array: A class to convert XML to array in PHP diff --git a/src/Usps/XMLParser.php b/src/Usps/XMLParser.php index 388c293..7e35070 100755 --- a/src/Usps/XMLParser.php +++ b/src/Usps/XMLParser.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; /** * Array2XML: A class to convert array in PHP to XML diff --git a/src/Usps/ZipCodeLookup.php b/src/Usps/ZipCodeLookup.php index 9c05cd3..2ec961a 100755 --- a/src/Usps/ZipCodeLookup.php +++ b/src/Usps/ZipCodeLookup.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; /** * USPS Zip code lookup by city/state From cbef857a68ebe31ac2e913eae82f8150fa726235 Mon Sep 17 00:00:00 2001 From: Patrick Breen Date: Wed, 14 Feb 2018 15:38:00 -0500 Subject: [PATCH 2/4] Add laravel auto discovery; fix autoload --- composer.json | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index ab0b0b2..4fb66b9 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,21 @@ "license": "MIT", "require": { "php": ">=7.1.0", - "illuminate/support": "~5.4" + "illuminate/support": "~5.4|~5.5|~5.6" }, "autoload": { - "psr-0": { - "Johnpaulmedina\Usps": "src/" + "psr-4": { + "Johnpaulmedina\\Usps\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Johnpaulmedina\\Usps\\UspsServiceProvider" + ], + "aliases": { + "Usps": "Johnpaulmedina\\Usps\\Facades\\Usps" + } } }, "minimum-stability": "dev" From c077ff7149b8fd46daa4b1a8d0233446695a63f4 Mon Sep 17 00:00:00 2001 From: Patrick Breen Date: Wed, 14 Feb 2018 15:43:28 -0500 Subject: [PATCH 3/4] Push Usps namespace into Johnpualmedina scope; add trackConfirm support; add testmode support; add Laravel 5.5+ auto discovery; update readme --- README.md | 30 +++++++++++++--- composer.json | 16 +++++++-- src/Usps/Address.php | 2 +- src/Usps/AddressVerify.php | 2 +- src/Usps/CityStateLookup.php | 2 +- src/Usps/Exceptions/UspsException.php | 19 ++++++++++ .../Exceptions/UspsTrackConfirmException.php | 19 ++++++++++ src/Usps/Facades/Usps.php | 2 +- src/Usps/FirstClassServiceStandards.php | 2 +- src/Usps/InternationalLabel.php | 2 +- src/Usps/OpenDistributeLabel.php | 2 +- src/Usps/PriorityLabel.php | 2 +- src/Usps/PriorityMailServiceStandards.php | 2 +- src/Usps/Rate.php | 2 +- src/Usps/RatePackage.php | 2 +- src/Usps/ServiceDeliveryCalculator.php | 2 +- src/Usps/TrackConfirm.php | 23 ++++++++++-- src/Usps/USPSBase.php | 2 +- src/Usps/Usps.php | 35 ++++++++++++++++--- src/Usps/UspsServiceProvider.php | 4 +-- src/Usps/XML2Array.php | 2 +- src/Usps/XMLParser.php | 2 +- src/Usps/ZipCodeLookup.php | 2 +- 23 files changed, 146 insertions(+), 32 deletions(-) create mode 100644 src/Usps/Exceptions/UspsException.php create mode 100644 src/Usps/Exceptions/UspsTrackConfirmException.php diff --git a/README.md b/README.md index cb5dd73..8ea1d57 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,18 @@ composer require johnpaulmedina/laravel-usps:dev-master ``` ## Laravel integration -To wire this up in your Laravel project you need to add the service provider. +For Laravel 5.5 and later, this package will be auto discovered and registered. + +To wire this up in your Laravel 5.4 project you need to add the service provider. Open `config/app.php`, and add a new item to the providers array. ```php -'Usps\UspsServiceProvider', +Johnpaulmedina\Usps\UspsServiceProvider::class, ``` Then you must also specify the alias in `config/app.php`. Add a new item to the Aliases array. ```php -'Usps' => 'Usps\Facades\Usps', +'Usps' => Johnpaulmedina\Usps\Facades\Usps::class, ``` This will allow integration by adding the Facade `Use Usps;` @@ -32,8 +34,9 @@ Add your USPS username config in `config/services.php`. ```php 'usps' => [ - 'username' => "XXXXXXXXXXXX" - ] + 'username' => "XXXXXXXXXXXX", + 'testmode' => false, + ], ``` ## Example Controller Usage @@ -60,6 +63,23 @@ class USPSController extends Controller ) ); } + + public function trackConfirm() { + return response()->json( + Usps::trackConfirm( + Request::input('id') + ) + ); + } + + public function trackConfirmRevision1() { + return response()->json( + Usps::trackConfirm( + Request::input('id'), + 'Acme, Inc' + ) + ); + } } ``` diff --git a/composer.json b/composer.json index 36ffc67..4fb66b9 100644 --- a/composer.json +++ b/composer.json @@ -17,11 +17,21 @@ "license": "MIT", "require": { "php": ">=7.1.0", - "illuminate/support": "~5.4" + "illuminate/support": "~5.4|~5.5|~5.6" }, "autoload": { - "psr-0": { - "Usps": "src/" + "psr-4": { + "Johnpaulmedina\\Usps\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Johnpaulmedina\\Usps\\UspsServiceProvider" + ], + "aliases": { + "Usps": "Johnpaulmedina\\Usps\\Facades\\Usps" + } } }, "minimum-stability": "dev" diff --git a/src/Usps/Address.php b/src/Usps/Address.php index 586c21a..28a1e36 100755 --- a/src/Usps/Address.php +++ b/src/Usps/Address.php @@ -11,7 +11,7 @@ */ -namespace Usps; +namespace Johnpaulmedina\Usps; class Address { diff --git a/src/Usps/AddressVerify.php b/src/Usps/AddressVerify.php index 6fdf321..21c1165 100755 --- a/src/Usps/AddressVerify.php +++ b/src/Usps/AddressVerify.php @@ -9,7 +9,7 @@ */ -namespace Usps; +namespace Johnpaulmedina\Usps; class AddressVerify extends USPSBase { diff --git a/src/Usps/CityStateLookup.php b/src/Usps/CityStateLookup.php index 17ebd2f..df1d117 100755 --- a/src/Usps/CityStateLookup.php +++ b/src/Usps/CityStateLookup.php @@ -11,7 +11,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; class CityStateLookup extends USPSBase { diff --git a/src/Usps/Exceptions/UspsException.php b/src/Usps/Exceptions/UspsException.php new file mode 100644 index 0000000..7a62886 --- /dev/null +++ b/src/Usps/Exceptions/UspsException.php @@ -0,0 +1,19 @@ +packages; + return array_merge($this->requestData, $this->packages); } /** @@ -56,4 +60,19 @@ public function addPackage($id) { $this->packages['TrackID'][] = ['@attributes' => ['ID' => $id]]; } + + /** + * Set revision ID and additional required fields + * + * @param string $clientIp + * @param string $sourceId + * @param int $revisionId + * @return void + */ + public function setRevision($clientIp, $sourceId, $revisionId = 1) + { + $this->requestData['Revision'] = $revisionId; + $this->requestData['ClientIp'] = $clientIp; + $this->requestData['SourceId'] = $sourceId; + } } diff --git a/src/Usps/USPSBase.php b/src/Usps/USPSBase.php index 1b7e48d..066f8ca 100755 --- a/src/Usps/USPSBase.php +++ b/src/Usps/USPSBase.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; abstract class USPSBase { diff --git a/src/Usps/Usps.php b/src/Usps/Usps.php index 3e462d4..5192a10 100644 --- a/src/Usps/Usps.php +++ b/src/Usps/Usps.php @@ -10,11 +10,9 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; -function __autoload($class_name) { - include $class_name . '.php'; -} +use Johnpaulmedina\Usps\Exceptions\UspsTrackConfirmException; class Usps { @@ -54,4 +52,33 @@ public function validate($request) { } + + /** + * @param $ids array|string + * @param $sourceId null|string + * + * @return array + * @throws UspsTrackConfirmException + */ + public function trackConfirm($ids, $sourceId = null) + { + $trackConfirm = new TrackConfirm($this->config['username']); + $trackConfirm->setTestMode(empty($this->config['testmode']) ? false : true); + + if ($sourceId) { + // Assume revision 1 tracking is desired when sourceId supplied + $trackConfirm->setRevision(request()->getClientIp(), $sourceId); + } + + collect(is_array($ids)?: [ $ids ])->each(function ($id) use ($trackConfirm) { + $trackConfirm->addPackage($id); + }); + + $trackConfirm->getTracking(); + if ($trackConfirm->isError()) { + throw new UspsTrackConfirmException($trackConfirm->getErrorMessage(), $trackConfirm->getErrorCode()); + } + + return $trackConfirm->getArrayResponse(); + } } diff --git a/src/Usps/UspsServiceProvider.php b/src/Usps/UspsServiceProvider.php index 93c5712..2ea85c5 100644 --- a/src/Usps/UspsServiceProvider.php +++ b/src/Usps/UspsServiceProvider.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; use Illuminate\Support\ServiceProvider; @@ -49,4 +49,4 @@ public function provides() return array('usps'); } -} \ No newline at end of file +} diff --git a/src/Usps/XML2Array.php b/src/Usps/XML2Array.php index 9d0479a..8937d9f 100755 --- a/src/Usps/XML2Array.php +++ b/src/Usps/XML2Array.php @@ -10,7 +10,7 @@ */ -namespace Usps; +namespace Johnpaulmedina\Usps; /** * XML2Array: A class to convert XML to array in PHP diff --git a/src/Usps/XMLParser.php b/src/Usps/XMLParser.php index 388c293..7e35070 100755 --- a/src/Usps/XMLParser.php +++ b/src/Usps/XMLParser.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; /** * Array2XML: A class to convert array in PHP to XML diff --git a/src/Usps/ZipCodeLookup.php b/src/Usps/ZipCodeLookup.php index 9c05cd3..2ec961a 100755 --- a/src/Usps/ZipCodeLookup.php +++ b/src/Usps/ZipCodeLookup.php @@ -8,7 +8,7 @@ * @author Vincent Gabriel */ -namespace Usps; +namespace Johnpaulmedina\Usps; /** * USPS Zip code lookup by city/state From 78e6af38ce82a57460ece0a387ead02dc504e6c1 Mon Sep 17 00:00:00 2001 From: Patrick Breen Date: Wed, 14 Feb 2018 16:02:45 -0500 Subject: [PATCH 4/4] Fix handling of id array --- src/Usps/Usps.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Usps/Usps.php b/src/Usps/Usps.php index 5192a10..b3342d8 100644 --- a/src/Usps/Usps.php +++ b/src/Usps/Usps.php @@ -70,7 +70,7 @@ public function trackConfirm($ids, $sourceId = null) $trackConfirm->setRevision(request()->getClientIp(), $sourceId); } - collect(is_array($ids)?: [ $ids ])->each(function ($id) use ($trackConfirm) { + collect(is_array($ids)? $ids : [ $ids ])->each(function ($id) use ($trackConfirm) { $trackConfirm->addPackage($id); });