Skip to content

Commit

Permalink
Merge pull request #4 from pdbreen/master
Browse files Browse the repository at this point in the history
trackConfirm support + a bit more
  • Loading branch information
John Paul Medina authored Mar 10, 2019
2 parents edeffa8 + 73f0624 commit 26d422b
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 32 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;`

Expand All @@ -32,8 +34,9 @@ Add your USPS username config in `config/services.php`.

```php
'usps' => [
'username' => "XXXXXXXXXXXX"
]
'username' => "XXXXXXXXXXXX",
'testmode' => false,
],
```

## Example Controller Usage
Expand All @@ -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'
)
);
}
}
```

Expand Down
16 changes: 13 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class Address
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/AddressVerify.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class AddressVerify extends USPSBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/CityStateLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class CityStateLookup extends USPSBase
{
Expand Down
19 changes: 19 additions & 0 deletions src/Usps/Exceptions/UspsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: Patrick Breen
* Date: 2/14/2018
* Time: 2:54 PM
*/

namespace Johnpaulmedina\Usps\Exceptions;

use Throwable;

class UspsException extends \Exception
{
public function __construct(string $message, int $code)
{
parent::__construct($message, $code, null);
}
}
19 changes: 19 additions & 0 deletions src/Usps/Exceptions/UspsTrackConfirmException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: Patrick Breen
* Date: 2/14/2018
* Time: 2:54 PM
*/

namespace Johnpaulmedina\Usps\Exceptions;

use Throwable;

class UspsTrackConfirmException extends UspsException
{
public function __construct(string $message, int $code)
{
parent::__construct($message, $code);
}
}
2 changes: 1 addition & 1 deletion src/Usps/Facades/Usps.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Usps\Facades;
namespace Johnpaulmedina\Usps\Facades;

use Illuminate\Support\Facades\Facade;

Expand Down
2 changes: 1 addition & 1 deletion src/Usps/FirstClassServiceStandards.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class FirstClassServiceStandards extends USPSBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/InternationalLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class InternationalLabel extends USPSBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/OpenDistributeLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class OpenDistributeLabel extends USPSBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/PriorityLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class PriorityLabel extends USPSBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/PriorityMailServiceStandards.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class PriorityMailServiceStandards extends USPSBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class Rate extends USPSBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/RatePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class RatePackage extends Rate
{
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/ServiceDeliveryCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class ServiceDeliveryCalculator extends USPSBase
{
Expand Down
23 changes: 21 additions & 2 deletions src/Usps/TrackConfirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

class TrackConfirm extends USPSBase
{
/**
* @var string - the api version used for this type of call
*/
protected $apiVersion = 'TrackV2';
/**
* @var array - additional request parameters for Revision 1
*/
protected $requestData = [];
/**
* @var array - list of all packages added so far
*/
Expand Down Expand Up @@ -43,7 +47,7 @@ public function getTracking()
*/
public function getPostFields()
{
return $this->packages;
return array_merge($this->requestData, $this->packages);
}

/**
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Usps/USPSBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

abstract class USPSBase
{
Expand Down
35 changes: 31 additions & 4 deletions src/Usps/Usps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/Usps/UspsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -49,4 +49,4 @@ public function provides()
return array('usps');
}

}
}
2 changes: 1 addition & 1 deletion src/Usps/XML2Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/


namespace Usps;
namespace Johnpaulmedina\Usps;

/**
* XML2Array: A class to convert XML to array in PHP
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/XMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

/**
* Array2XML: A class to convert array in PHP to XML
Expand Down
2 changes: 1 addition & 1 deletion src/Usps/ZipCodeLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Vincent Gabriel
*/

namespace Usps;
namespace Johnpaulmedina\Usps;

/**
* USPS Zip code lookup by city/state
Expand Down

0 comments on commit 26d422b

Please sign in to comment.