diff --git a/README.md b/README.md index 0377fd0..5e4b587 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Laravel-USPS -Laravel-USPS is a composer package that allows you to integrate the USPS Address / Shipping API. This package is ported from @author Vincent Gabriel https://github.com/VinceG/USPS-php-api +Laravel-USPS is a composer package that allows you to integrate the USPS Address / Shipping API / Rates Calculator. This package is ported from @author Vincent Gabriel https://github.com/VinceG/USPS-php-api - Requires a valid USPS API Username - - Tested on Laravel 7 + - Tested on Laravel 8 ## Installation @@ -40,7 +40,7 @@ Add your USPS username config in `config/services.php`. ``` ## Example Controller Usage -The only method completed for Laravel is the `Usps::validate` which is defined in `vendor/johnpaulmedina/laravel-usps/src/Usps/Usps.php`. As this package was developed for internal use I did not bring over all the features but you are more than welcome to contribute the methods you need and I will merge them. I suggest looking at the original PHP-Wrapper by @VinceG [USPS PHP-Api](https://github.com/VinceG/USPS-php-api "USPS PHP-Api by VinceG") as I ported those clases and autoloaded them to use in the `Usps.php` file. +The only method completed for Laravel is the `Usps::validate`, `Usps::rate` which is defined in `vendor/johnpaulmedina/laravel-usps/src/Usps/Usps.php`. As this package was developed for internal use I did not bring over all the features but you are more than welcome to contribute the methods you need and I will merge them. I suggest looking at the original PHP-Wrapper by @VinceG [USPS PHP-Api](https://github.com/VinceG/USPS-php-api "USPS PHP-Api by VinceG") as I ported those clases and autoloaded them to use in the `Usps.php` file. ```php $request->input('Service', 'PRIORITY COMMERCIAL'), + 'FirstClassMailType' => $request->input('FirstClassMailType', ''), + 'ZipOrigination' => $request->input('ZipOrigination', '91601'), + 'ZipDestination' => $request->input('ZipDestination', $zipcode), + 'Pounds' => $request->input('Pounds', $weight), + 'Ounces' => $request->input('Ounces', 0), + 'Container' => $request->input('Container', 'VARIABLE'), + 'Machinable' => $request->input('Machinable', 'True'), + ] + ); + + + $usps_return_rate = Arr::get($usps_rate, 'rate.RateV4Response.Package.Postage.Rate'); + $usps_return_weight = Arr::get($usps_rate, 'rate.RateV4Response.Package.Pounds'); + + + return response()->json([ + 'rate' => $usps_return_rate, + 'weight'=> $usps_return_weight, + 'usps' => $usps_rate + ]); + + } + } ``` @@ -88,6 +117,7 @@ Contributors @pdbreen @bredmor @scs-ben +@daveore090 @VinceG Original README.MD @@ -112,6 +142,7 @@ Requirements - PHP >= 7.2.5 configured with the following extensions: - cURL - USPS API Username +- - Laravel 8 Authors diff --git a/composer.json b/composer.json index 6abe3de..104c6e9 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,11 @@ { "name": "johnpaulmedina/laravel-usps", - "description": "USPS API for Laravel 7", + "description": "USPS API for Laravel 8", "type": "library", "keywords": [ "usps", "laravel", - "laravel7", + "laravel8", "laravel usps" ], "authors": [ @@ -21,8 +21,8 @@ "homepage":"https://github.com/johnpaulmedina/laravel-usps/", "license": "MIT", "require": { - "php": "^7.2.5", - "illuminate/support": "^7.0" + "php": "^7.3|^8.0", + "illuminate/support": "^8.0" }, "autoload": { "psr-4": { diff --git a/src/RatePackage.php b/src/RatePackage.php index da21c41..f5e6777 100755 --- a/src/RatePackage.php +++ b/src/RatePackage.php @@ -158,6 +158,11 @@ public function setSize($value) { return $this->setField('Size', $value); } + + public function setMachinable($value) + { + return $this->setField('Machinable', $value); + } /** * Add an element to the stack diff --git a/src/Usps.php b/src/Usps.php index b3342d8..8e49fc9 100644 --- a/src/Usps.php +++ b/src/Usps.php @@ -81,4 +81,37 @@ public function trackConfirm($ids, $sourceId = null) return $trackConfirm->getArrayResponse(); } + + public function rate($request){ + $rate = new Rate($this->config['username']); + $ratepackage = new RatePackage(); + $ratepackage->setService((array_key_exists('Service', $request) ? $request['Service'] : null )); + $ratepackage->setFirstClassMailType((array_key_exists('FirstClassMailType', $request) ? $request['FirstClassMailType'] : null )); + $ratepackage->setZipOrigination((array_key_exists('ZipOrigination', $request) ? $request['ZipOrigination'] : null )); + $ratepackage->setZipDestination((array_key_exists('ZipDestination', $request) ? $request['ZipDestination'] : null )); + $ratepackage->setPounds((array_key_exists('Pounds', $request) ? $request['Pounds'] : null )); + $ratepackage->setOunces((array_key_exists('Ounces', $request) ? $request['Ounces'] : null )); + $ratepackage->setContainer((array_key_exists('Container', $request) ? $request['Container'] : null )); + $ratepackage->setSize((array_key_exists('Size', $request) ? $request['Size'] : null )); + $ratepackage->setMachinable((array_key_exists('Machinable', $request) ? $request['Machinable'] : null )); + + + // Add the Package object to the Rate Package class + $rate->addPackage($ratepackage); + + // Perform the request and return result + $val1 = $rate->getRate(); + $val2 = $rate->getArrayResponse(); + + // var_dump($verify->isError()); + + // See if it was successful + if ($rate->isSuccess()) { + return ['rate' => $val2]; + } else { + return ['error' => $rate->getErrorMessage()]; + } + + + } }