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..b3342d8 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 : [ $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