Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UPS] started a browser for the UPS API
- Loading branch information
1 parent
f8102c1
commit b52cf75
Showing
5 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Buzz\Extension\UPS; | ||
|
||
use Buzz\Browser as BaseBrowser; | ||
use Buzz\Client\ClientInterface; | ||
use Buzz\History\Journal; | ||
|
||
class Browser extends BaseBrowser | ||
{ | ||
protected $accessLicenseNumber; | ||
protected $userId; | ||
protected $password; | ||
|
||
public function __construct($accessLicenseNumber, $userId, $password, ClientInterface $client = null, Journal $journal = null) | ||
{ | ||
$this->accessLicenseNumber = $accessLicenseNumber; | ||
$this->userId = $userId; | ||
$this->password = $password; | ||
|
||
parent::__construct($client, $journal); | ||
} | ||
|
||
public function getAccessLicenseNumber() | ||
{ | ||
return $this->accessLicenseNumber; | ||
} | ||
|
||
public function getUserId() | ||
{ | ||
return $this->userId; | ||
} | ||
|
||
public function getPassword() | ||
{ | ||
return $this->password; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
|
||
namespace Buzz\Extension\UPS\Request; | ||
|
||
use Buzz\Browser; | ||
use Buzz\BrowserAwareInterface; | ||
use Buzz\Extension\UPS\Browser as UpsBrowser; | ||
use Buzz\Extension\UPS\Resource\AddressInterface; | ||
use Buzz\Message\Request; | ||
|
||
class RatingRequest extends Request implements BrowserAwareInterface | ||
{ | ||
const URL_TEST = 'https://wwwcie.ups.com/ups.app/xml/Rate'; | ||
const URL_PROD = 'https://onlinetools.ups.com/ups.app/xml/Rate'; | ||
|
||
protected $accessLicenseNumber; | ||
protected $userId; | ||
protected $password; | ||
protected $shipperNumber; | ||
protected $shipperAddress; | ||
protected $shipToAddress; | ||
protected $shipFromAddress; | ||
protected $packageWeight; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* The URL defaults to the endpoint intended for integration testing. | ||
* | ||
* @param string $url The API endpoint | ||
*/ | ||
public function __construct($url = null) | ||
{ | ||
$this->setMethod('POST'); | ||
$this->fromUrl($url ?: static::URL_TEST); | ||
} | ||
|
||
public function setBrowser(Browser $browser) | ||
{ | ||
if ($browser instanceof UpsBrowser) { | ||
$this->accessLicenseNumber = $browser->getAccessLicenseNumber(); | ||
$this->userId = $browser->getUserId(); | ||
$this->password = $browser->getPassword(); | ||
} | ||
} | ||
|
||
public function setShipperNumber($shipperNumber) | ||
{ | ||
$this->shipperNumber = $shipperNumber; | ||
} | ||
|
||
public function setShipperAddress(AddressInterface $address) | ||
{ | ||
$this->shipperAddress = $address; | ||
} | ||
|
||
public function setShipToAddress(AddressInterface $address) | ||
{ | ||
$this->shipToAddress = $address; | ||
} | ||
|
||
public function setShipFromAddress(AddressInterface $address) | ||
{ | ||
$this->shipFromAddress = $address; | ||
} | ||
|
||
public function setPackageWeight($weight) | ||
{ | ||
$this->packageWeight = $weight; | ||
} | ||
|
||
public function getContent() | ||
{ | ||
return | ||
$this->buildAccessRequestXml()->asXml(). | ||
$this->buildRatingServiceSelectionRequestXml()->asXml(); | ||
} | ||
|
||
protected function buildAccessRequestXml() | ||
{ | ||
$request = new \SimpleXmlElement('<AccessRequest xml:lang="en-US" />'); | ||
$request->addChild('AccessLicenseNumber', $this->accessLicenseNumber); | ||
$request->addChild('UserId', $this->userId); | ||
$request->addChild('Password', $this->password); | ||
|
||
return $request; | ||
} | ||
|
||
protected function buildRatingServiceSelectionRequestXml() | ||
{ | ||
$request = new \SimpleXmlElement(static::REQUEST_XML); | ||
|
||
// shipper | ||
if ($this->shipperNumber || $this->shipperAddress) { | ||
if ($this->shipperNumber) { | ||
$request->Shipment->Shipper->addChild('ShipperNumber', $this->shipperNumber); | ||
} else { | ||
unset($request->Shipment->RateInformation); | ||
} | ||
|
||
if ($this->shipperAddress) { | ||
static::buildAddress($this->shipperAddress, $request->Shipment->Shipper->Address); | ||
} | ||
} else { | ||
unset($request->Shipment->Shipper); | ||
unset($request->Shipment->RateInformation); | ||
} | ||
|
||
// ship to | ||
if ($this->shipToAddress) { | ||
static::buildAddress($this->shipToAddress, $request->Shipment->ShipTo->Address); | ||
} else { | ||
unset($request->Shipment->ShipTo); | ||
} | ||
|
||
// ship from | ||
if ($this->shipFromAddress) { | ||
static::buildAddress($this->shipFromAddress, $request->Shipment->ShipFrom->Address); | ||
} else { | ||
unset($request->Shipment->ShipFrom); | ||
} | ||
|
||
// package weight | ||
if ($this->packageWeight) { | ||
$request->Shipment->Package->PackageWeight->Weight = $this->packageWeight; | ||
} else { | ||
unset($request->Shipment->Package->PackageWeight->Weight); | ||
} | ||
|
||
return $request; | ||
} | ||
|
||
static protected function buildAddress(AddressInterface $address, \SimpleXmlElement $element) | ||
{ | ||
$element->addChild('City', $address->getCity()); | ||
$element->addChild('PostalCode', $address->getPostalCode()); | ||
$element->addChild('CountryCode', $address->getCountryCode()); | ||
$element->addChild('StateProvinceCode', $address->getStateProvinceCode()); | ||
} | ||
|
||
const REQUEST_XML = <<<XML | ||
<?xml version="1.0" ?> | ||
<RatingServiceSelectionRequest> | ||
<Request> | ||
<TransactionReference> | ||
<CustomerContext>Rating and Service</CustomerContext> | ||
<XpciVersion>1.0</XpciVersion> | ||
</TransactionReference> | ||
<RequestAction>Rate</RequestAction> | ||
<RequestOption>Shop</RequestOption> | ||
</Request> | ||
<PickupType> | ||
<Code>01</Code> | ||
<Description>Regular Daily Pickup</Description> | ||
</PickupType> | ||
<Shipment> | ||
<Shipper> | ||
<Address/> | ||
</Shipper> | ||
<ShipTo> | ||
<Address> | ||
<ResidentialAddressIndicator/> | ||
</Address> | ||
</ShipTo> | ||
<ShipFrom> | ||
<Address/> | ||
</ShipFrom> | ||
<Service> | ||
<Code/> | ||
</Service> | ||
<Package> | ||
<PackagingType> | ||
<Code>00</Code> | ||
</PackagingType> | ||
<PackageWeight> | ||
<UnitOfMeasurement> | ||
<Code>LBS</Code> | ||
</UnitOfMeasurement> | ||
<Weight/> | ||
</PackageWeight> | ||
</Package> | ||
<RateInformation> | ||
<NegotiatedRatesIndicator/> | ||
</RateInformation> | ||
</Shipment> | ||
</RatingServiceSelectionRequest> | ||
XML; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Buzz\Extension\UPS\Resource; | ||
|
||
interface AddressInterface | ||
{ | ||
function getCity(); | ||
function getPostalCode(); | ||
function getCountryCode(); | ||
function getStateProvinceCode(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
test/Buzz/Test/Extension/UPS/Request/RatingRequestTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Buzz\Test\Extension\UPS\Request; | ||
|
||
use Buzz\Extension\UPS\Browser; | ||
use Buzz\Extension\UPS\Request\RatingRequest; | ||
|
||
class RatingRequestTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider provideSanityCheckConfigurators | ||
*/ | ||
public function testGetContent($configurator) | ||
{ | ||
// sanity check | ||
$request = new RatingRequest(); | ||
$configurator($request); | ||
$request->getContent(); | ||
} | ||
|
||
public function provideSanityCheckConfigurators() | ||
{ | ||
$address = $this->getMock('Buzz\\Extension\\UPS\\Resource\\AddressInterface'); | ||
|
||
return array( | ||
array(function($request) { $request->setShipperNumber('123'); }), | ||
array(function($request) use($address) { $request->setShipperAddress($address); }), | ||
array(function($request) use($address) { $request->setShipToAddress($address); }), | ||
array(function($request) use($address) { $request->setShipFromAddress($address); }), | ||
); | ||
} | ||
|
||
public function testGetResource() | ||
{ | ||
$request = new RatingRequest(); | ||
$this->assertEquals('/ups.app/xml/Rate', $request->getResource(), '->getResource() defaults to the testing resource'); | ||
} | ||
|
||
public function testGetHost() | ||
{ | ||
$request = new RatingRequest(); | ||
$this->assertEquals('https://wwwcie.ups.com', $request->getHost(), '->getHost() defaults to the testing host'); | ||
} | ||
|
||
public function testSend() | ||
{ | ||
$browser = $this->createBrowser(); | ||
|
||
$address = $this->getMock('Buzz\\Extension\\UPS\\Resource\\AddressInterface'); | ||
$address->expects($this->any())->method('getCity')->will($this->returnValue('New York City')); | ||
$address->expects($this->any())->method('getPostalCode')->will($this->returnValue('10011')); | ||
$address->expects($this->any())->method('getCountryCode')->will($this->returnValue('US')); | ||
$address->expects($this->any())->method('getStateProvinceCode')->will($this->returnValue('NY')); | ||
|
||
$request = new RatingRequest(); | ||
$request->setShipperAddress($address); | ||
$request->setShipToAddress($address); | ||
$request->setPackageWeight(10); | ||
|
||
$response = $browser->send($request); | ||
$xml = new \SimpleXMLElement($response->getContent()); | ||
|
||
$this->assertEquals(1, (integer) $xml->Response->ResponseStatusCode); | ||
$this->assertEquals('Success', (string) $xml->Response->ResponseStatusDescription); | ||
|
||
// // debug | ||
// $content = $request->getContent(); | ||
// print_r(new \SimpleXMLElement(substr($content, strpos($content, '<RatingServiceSelectionRequest>')))); | ||
// print_r(new \SimpleXMLElement($response->getContent())); | ||
} | ||
|
||
protected function createBrowser() | ||
{ | ||
if (!isset($_SERVER['UPS_ACCESS_LICENSE_NUMBER']) | ||
|| !isset($_SERVER['UPS_USER_ID']) | ||
|| !isset($_SERVER['UPS_PASSWORD'])) { | ||
$this->markTestSkipped('No UPS credentials configured'); | ||
} | ||
|
||
return new Browser( | ||
$_SERVER['UPS_ACCESS_LICENSE_NUMBER'], | ||
$_SERVER['UPS_USER_ID'], | ||
$_SERVER['UPS_PASSWORD'] | ||
); | ||
} | ||
} |