Skip to content

Commit

Permalink
Merge pull request #19 from daveore090/dev-laravel8
Browse files Browse the repository at this point in the history
L8
  • Loading branch information
johnpaulmedina authored May 14, 2022
2 parents 207d3d6 + 634e15a commit 560809e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
<?php

Expand Down Expand Up @@ -80,6 +80,35 @@ class USPSController extends Controller
)
);
}

public function rate(Request $request) {

$usps_rate = Usps::rate(
[
'Service' => $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
]);

}

}
```

Expand All @@ -88,6 +117,7 @@ Contributors
@pdbreen
@bredmor
@scs-ben
@daveore090

@VinceG Original README.MD

Expand All @@ -112,6 +142,7 @@ Requirements
- PHP >= 7.2.5 configured with the following extensions:
- cURL
- USPS API Username
- - Laravel 8


Authors
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions src/RatePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/Usps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
}


}
}

0 comments on commit 560809e

Please sign in to comment.