From 9d7c8335ff9c49dbde3ad398302a6b4a763b7a62 Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Thu, 27 Nov 2014 00:30:48 -0500 Subject: [PATCH 01/10] Request negotiated rates from UPS --- src/UPS/Rate.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/UPS/Rate.php b/src/UPS/Rate.php index d51f09a..45b1962 100644 --- a/src/UPS/Rate.php +++ b/src/UPS/Rate.php @@ -184,6 +184,9 @@ class Rate extends RateAdapter ' . $pounds . ' + + + '; From a89504e722e688f07d5ebefca27ab7db209a21fa Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Thu, 27 Nov 2014 01:44:07 -0500 Subject: [PATCH 02/10] Expanded to support multiple packages per request, and now more object oriented. USPS still provides the cost of each package back individually, so we sum those up per service requested --- .gitignore | 4 ++ src/Fedex/Rate.php | 64 ++++++++--------- src/Package.php | 123 +++++++++++++++++++++++++++++++ src/RateAdapter.php | 13 +--- src/Shipment.php | 169 +++++++++++++++++++++++++++++++++++++++++++ src/UPS/Rate.php | 71 +++++++++--------- src/USPS/Rate.php | 57 ++++++++------- tests/ShipTest.php | 172 ++++++++++---------------------------------- 8 files changed, 430 insertions(+), 243 deletions(-) create mode 100644 .gitignore create mode 100644 src/Package.php create mode 100644 src/Shipment.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e7c1d61 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +vendor/ +.idea/ +composer.lock + diff --git a/src/Fedex/Rate.php b/src/Fedex/Rate.php index 1275416..5f91875 100644 --- a/src/Fedex/Rate.php +++ b/src/Fedex/Rate.php @@ -88,17 +88,6 @@ class Rate extends RateAdapter protected function prepare() { - $to = Arr::get($this->shipment, 'to'); - $shipper = Arr::get($this->shipment, 'from'); - $dimensions = Arr::get($this->shipment, 'dimensions'); - - $pounds = (int) Arr::get($this->shipment, 'weight'); - $ounces = 0; - - if ($pounds < 1) { - throw new Exception('Weight missing'); - } - $date = time(); $day_name = date('l', $date); @@ -111,6 +100,28 @@ class Rate extends RateAdapter // http://www.fedex.com/templates/components/apps/wpor/secure/downloads/pdf/Aug13/PropDevGuide.pdf // http://www.fedex.com/us/developer/product/WebServices/MyWebHelp_August2010/Content/Proprietary_Developer_Guide/Rate_Services_conditionalized.htm + + $packages = ''; + $sequence_number = 0; + foreach ($this->shipment->getPackages() as $p) { + $sequence_number++; + + $packages .= ' + ' . $sequence_number . ' + 1 + + LB + ' . $p->getWeight() . ' + + + ' . $p->getLength() . ' + ' . $p->getWidth() . ' + ' . $p->getHeight() . ' + IN + + '; + } + $this->data = ' @@ -136,37 +147,24 @@ class Rate extends RateAdapter ' . date('c') . ' ' . $this->drop_off_type . ' - ' . Arr::get($this->shipment, 'packaging_type') . ' + YOUR_PACKAGING
- ' . Arr::get($shipper, 'postal_code') . ' - ' . Arr::get($shipper, 'country_code') . ' - ' . ((Arr::get($shipper, 'is_residential')) ? '1' : '') . ' + ' . $this->shipment->getFromPostalCode() . ' + ' . $this->shipment->getFromCountryCode() . ' + ' . (($this->shipment->isFromResidential()) ? '1' : '') . '
- ' . Arr::get($to, 'postal_code') . ' - ' . Arr::get($to, 'country_code') . ' - ' . ((Arr::get($to, 'is_residential')) ? '1' : '') . ' + ' . $this->shipment->getToPostalCode() . ' + ' . $this->shipment->getToCountryCode() . ' + ' . (($this->shipment->isToResidential()) ? '1' : '') . '
LIST - 1 - - 1 - 1 - - LB - ' . $pounds . ' - - - ' . Arr::get($dimensions, 'length') . ' - ' . Arr::get($dimensions, 'width') . ' - ' . Arr::get($dimensions, 'height') . ' - IN - - + ' . $this->shipment->packageCount() . ' + ' . $packages . '
diff --git a/src/Package.php b/src/Package.php new file mode 100644 index 0000000..07414ce --- /dev/null +++ b/src/Package.php @@ -0,0 +1,123 @@ +size_classification; + } + + /** + * @param mixed $size_classification + * @return $this + */ + public function setSizeClassification($size_classification) + { + $this->size_classification = $size_classification; + return $this; + } + + /** + * @return mixed + */ + public function getPackaging() + { + return $this->packaging; + } + + /** + * @param mixed $packaging + * @return $this + */ + public function setPackaging($packaging) + { + $this->packaging = $packaging; + return $this; + } + + /** + * @return mixed + */ + public function getWeight() + { + return $this->weight; + } + + /** + * @param mixed $weight + * @return $this + */ + public function setWeight($weight) + { + $this->weight = $weight; + return $this; + } + + /** + * @return mixed + */ + public function getWidth() + { + return $this->width; + } + + /** + * @param mixed $width + * @return $this + */ + public function setWidth($width) + { + $this->width = $width; + return $this; + } + + /** + * @return mixed + */ + public function getLength() + { + return $this->length; + } + + /** + * @param mixed $length + * @return $this + */ + public function setLength($length) + { + $this->length = $length; + return $this; + } + + /** + * @return mixed + */ + public function getHeight() + { + return $this->height; + } + + /** + * @param mixed $height + * @return $this + */ + public function setHeight($height) + { + $this->height = $height; + return $this; + } +} diff --git a/src/RateAdapter.php b/src/RateAdapter.php index 430305b..8fd7dbb 100644 --- a/src/RateAdapter.php +++ b/src/RateAdapter.php @@ -7,6 +7,7 @@ abstract class RateAdapter { protected $is_prod = FALSE; + /** @var Shipment */ protected $shipment; protected $data; protected $response; @@ -27,18 +28,6 @@ abstract class RateAdapter if (isset($options['shipment'])) { $this->shipment = $options['shipment']; } - - if (empty($this->shipment['to'])) { - throw new Exception('Shipment "to" missing'); - } - - if (empty($this->shipment['from'])) { - throw new Exception('Shipment "from" missing'); - } - - if (empty($this->shipment['dimensions'])) { - throw new Exception('Shipment "dimensions" missing'); - } } public function set_request_adapter(RateRequest\Adapter $rate_request) diff --git a/src/Shipment.php b/src/Shipment.php new file mode 100644 index 0000000..c7f2a9d --- /dev/null +++ b/src/Shipment.php @@ -0,0 +1,169 @@ +packages[] = $package; + return $this; + } + + /** + * @return Package[] + */ + public function getPackages() + { + return $this->packages; + } + + /** + * @return int + */ + public function packageCount() + { + return count($this->getPackages()); + } + + /** + * @return mixed + */ + public function getFromPostalCode() + { + return $this->from_postal_code; + } + + /** + * @param mixed $from_postal_code + * @return $this + */ + public function setFromPostalCode($from_postal_code) + { + $this->from_postal_code = $from_postal_code; + return $this; + } + + /** + * @return mixed + */ + public function getFromCountryCode() + { + return $this->from_country_code; + } + + /** + * @param mixed $from_country_code + * @return $this + */ + public function setFromCountryCode($from_country_code) + { + $this->from_country_code = $from_country_code; + return $this; + } + + /** + * @return mixed + */ + public function getToPostalCode() + { + return $this->to_postal_code; + } + + /** + * @param mixed $to_postal_code + * @return $this + */ + public function setToPostalCode($to_postal_code) + { + $this->to_postal_code = $to_postal_code; + return $this; + } + + /** + * @return mixed + */ + public function getToCountryCode() + { + return $this->to_country_code; + } + + /** + * @param mixed $to_country_code + * @return $this + */ + public function setToCountryCode($to_country_code) + { + $this->to_country_code = $to_country_code; + return $this; + } + + /** + * @return boolean + */ + public function isToResidential() + { + return $this->to_is_residential; + } + + /** + * @param boolean $to_is_residential + * @return $this + */ + public function setToResidential($to_is_residential) + { + $this->to_is_residential = $to_is_residential; + return $this; + } + + /** + * @return bool + */ + public function isFromResidential() + { + return $this->from_is_residential; + } + + /** + * @param $from_is_residential + * @return $this + */ + public function setFromResidential($from_is_residential) + { + $this->from_is_residential = $from_is_residential; + return $this; + } + + /** + * @return mixed + */ + public function getFromStateProvinceCode() + { + return $this->from_state_province; + } + + /** + * @param $from_state_province + * @return $this + */ + public function setFromStateProvinceCode($from_state_province) + { + $this->from_state_province = $from_state_province; + return $this; + } +} diff --git a/src/UPS/Rate.php b/src/UPS/Rate.php index 45b1962..53a4cbb 100644 --- a/src/UPS/Rate.php +++ b/src/UPS/Rate.php @@ -114,19 +114,31 @@ class Rate extends RateAdapter protected function prepare() { - $to = Arr::get($this->shipment, 'to'); - $shipper = Arr::get($this->shipment, 'from'); - $dimensions = Arr::get($this->shipment, 'dimensions'); - - $pounds = (int) Arr::get($this->shipment, 'weight'); - $ounces = 0; + $service_code = '03'; - if ($pounds < 1) { - throw new Exception('Weight missing'); + $packages = ''; + foreach ($this->shipment->getPackages() as $p) { + $packages .= ' + + 02 + + + + IN + + ' . $p->getLength() . ' + ' . $p->getWidth() . ' + ' . $p->getHeight() . ' + + + + LBS + + ' . $p->getWeight() . ' + + '; } - $service_code = '03'; - $this->data = ' @@ -142,48 +154,31 @@ class Rate extends RateAdapter
- ' . Arr::get($shipper, 'postal_code') . ' - ' . Arr::get($shipper, 'country_code') . ' - ' . ((Arr::get($shipper, 'is_residential')) ? '1' : '') . ' + ' . $this->shipment->getFromPostalCode() . ' + ' . $this->shipment->getFromCountryCode() . ' + ' . (($this->shipment->isFromResidential()) ? '1' : '') . '
' . $this->shipper_number . '
- ' . Arr::get($to, 'postal_code') . ' - ' . Arr::get($to, 'country_code') . ' - ' . ((Arr::get($to, 'is_residential')) ? '1' : '') . ' + ' . $this->shipment->getToPostalCode() . ' + ' . $this->shipment->getToCountryCode() . ' + ' . (($this->shipment->isToResidential()) ? '1' : '') . '
- ' . Arr::get($shipper, 'postal_code') . ' - ' . Arr::get($shipper, 'country_code') . ' - ' . ((Arr::get($shipper, 'is_residential')) ? '1' : '') . ' + ' . $this->shipment->getFromStateProvinceCode() . ' + ' . $this->shipment->getFromPostalCode() . ' + ' . $this->shipment->getFromCountryCode() . ' + ' . (($this->shipment->isFromResidential()) ? '1' : '') . '
' . $service_code . ' - - - 02 - - - - IN - - ' . Arr::get($dimensions, 'length') . ' - ' . Arr::get($dimensions, 'width') . ' - ' . Arr::get($dimensions, 'height') . ' - - - - LBS - - ' . $pounds . ' - - + ' . $packages . ' diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index c54351d..48bcc95 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -95,34 +95,31 @@ class Rate extends RateAdapter protected function prepare() { - $to = Arr::get($this->shipment, 'to'); - $shipper = Arr::get($this->shipment, 'from'); - $dimensions = Arr::get($this->shipment, 'dimensions'); - // https://www.usps.com/business/web-tools-apis/rate-calculators-v1-7a.htm - $pounds = (int) Arr::get($this->shipment, 'weight'); - $ounces = 0; - - if ($pounds < 1) { - throw new Exception('Weight missing'); + $packages = ''; + $sequence_number = 0; + foreach ($this->shipment->getPackages() as $p) { + $sequence_number++; + + $packages .= ' + ALL + ' . $this->shipment->getFromPostalCode() . ' + ' . $this->shipment->getToPostalCode() . ' + ' . $p->getWeight() . ' + 0 + ' . $p->getPackaging() . ' + ' . $p->getSizeClassification() . ' + ' . $p->getWidth() . ' + ' . $p->getLength() . ' + ' . $p->getHeight() . ' + ' . 'False' . ' + '; } $this->data = ' - - ALL - ' . Arr::get($shipper, 'postal_code') . ' - ' . Arr::get($to, 'postal_code') . ' - ' . $pounds . ' - ' . $ounces . ' - ' . Arr::get($this->shipment, 'container') . ' - ' . Arr::get($this->shipment, 'size') . ' - ' . Arr::get($dimensions, 'width') . ' - ' . Arr::get($dimensions, 'length') . ' - ' . Arr::get($dimensions, 'height') . ' - ' . 'False' . ' - + ' . $packages . ' '; return $this; @@ -160,6 +157,8 @@ class Rate extends RateAdapter throw $e; } + $rates = []; + foreach ($postage_list as $postage) { $code = @$postage->getAttribute('CLASSID'); $cost = @$postage->getElementsByTagName('Rate')->item(0)->nodeValue; @@ -170,13 +169,21 @@ class Rate extends RateAdapter continue; } - $this->rates[] = array( + if (array_key_exists($code, $rates)) { + $cost = $rates[$code]['cost'] + ($cost * 100); + } else { + $cost = $cost * 100; + } + + $rates[$code] = [ 'code' => $code, 'name' => $name, - 'cost' => (int) $cost * 100, - ); + 'cost' => (int) $cost, + ]; } + $this->rates = array_values($rates); + return $this; } } diff --git a/tests/ShipTest.php b/tests/ShipTest.php index 9467d7f..34c6e2c 100644 --- a/tests/ShipTest.php +++ b/tests/ShipTest.php @@ -1,5 +1,7 @@ 3, // lbs - 'dimensions' => [ - 'width' => 9, - 'length' => 9, - 'height' => 9, - ], - 'from' => [ - 'postal_code' => '90401', - 'country_code' => 'US', - ], - 'to' => [ - 'postal_code' => '78703', - 'country_code' => 'US', - 'is_residential' => TRUE, - ], - ]; + /** @var Shipment */ + public $shipment; public $shipping_options = [ 'Standard Shipping' => [ @@ -60,6 +47,29 @@ class ShipTest extends PHPUnit_Framework_TestCase ], ]; + public function setUp() + { + $s = new Shipment; + $s->setFromStateProvinceCode('CA') + ->setFromPostalCode('90401') + ->setFromCountryCode('US') + ->setToPostalCode('78703') + ->setToCountryCode('US') + ->setToResidential(true); + + $p = new Package; + $p->setWeight(3) + ->setWidth(9) + ->setLength(9) + ->setHeight(9) + ->setPackaging(Package::USPS_CONTAINER_RECTANGULAR) + ->setSizeClassification(Package::USPS_SIZE_LARGE); + + $s->addPackage($p); + + $this->shipment = $s; + } + private function getUSPSOptions() { $ship = Ship::factory($this->shipping_options); @@ -69,10 +79,7 @@ class ShipTest extends PHPUnit_Framework_TestCase 'prod' => FALSE, 'username' => 'XXXX', 'password' => 'XXXX', - 'shipment' => array_merge($this->shipment, [ - 'size' => 'LARGE', - 'container' => 'RECTANGULAR', - ]), + 'shipment' => $this->shipment, 'approved_codes' => $approved_codes, 'request_adapter' => new RateRequest\StubUSPS(), ]; @@ -100,6 +107,11 @@ class ShipTest extends PHPUnit_Framework_TestCase $ship = Ship::factory($this->shipping_options); $approved_codes = $ship->get_approved_codes('fedex'); + $ps = $this->shipment->getPackages(); + foreach ($ps as $p) { + $p->setPackaging(Package::FEDEX_YOUR_PACKAGING); + } + return [ 'prod' => FALSE, 'key' => 'XXXX', @@ -107,9 +119,7 @@ class ShipTest extends PHPUnit_Framework_TestCase 'account_number' => 'XXXX', 'meter_number' => 'XXXX', 'drop_off_type' => 'BUSINESS_SERVICE_CENTER', - 'shipment' => array_merge($this->shipment, [ - 'packaging_type' => 'YOUR_PACKAGING', - ]), + 'shipment' => $this->shipment, 'approved_codes' => $approved_codes, 'request_adapter' => new RateRequest\StubFedex(), ]; @@ -124,12 +134,12 @@ class ShipTest extends PHPUnit_Framework_TestCase 1 => [ 'code' => '4', 'name' => 'Parcel Post', - 'cost' => 1000, + 'cost' => 1001, ], 0 => [ 'code' => '1', 'name' => 'Priority Mail', - 'cost' => 1200, + 'cost' => 1220, ], ]), json_encode($usps_rates)); } @@ -221,7 +231,7 @@ class ShipTest extends PHPUnit_Framework_TestCase 0 => [ 'code' => '4', 'name' => 'Parcel Post', - 'cost' => 1000, + 'cost' => 1001, 'carrier' => 'usps', ], ], @@ -248,114 +258,6 @@ class ShipTest extends PHPUnit_Framework_TestCase ]), json_encode($display_rates)); } - /** - * @expectedException Exception - */ - public function testUSPSRateMissingTo() - { - $usps_options = $this->getUSPSOptions(); - unset($usps_options['shipment']['to']); - - $usps = new USPS\Rate($usps_options); - $usps_rates = $usps->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testUSPSRateMissingFrom() - { - $usps_options = $this->getUSPSOptions(); - unset($usps_options['shipment']['from']); - - $usps = new USPS\Rate($usps_options); - $usps_rates = $usps->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testUSPSRateMissingDimensions() - { - $usps_options = $this->getUSPSOptions(); - unset($usps_options['shipment']['dimensions']); - - $usps = new USPS\Rate($usps_options); - $usps_rates = $usps->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testUPSRateMissingTo() - { - $ups_options = $this->getUPSOptions(); - unset($ups_options['shipment']['to']); - - $ups = new UPS\Rate($ups_options); - $ups_rates = $ups->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testUPSRateMissingFrom() - { - $ups_options = $this->getUPSOptions(); - unset($ups_options['shipment']['from']); - - $ups = new UPS\Rate($ups_options); - $ups_rates = $ups->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testUPSRateMissingDimensions() - { - $ups_options = $this->getUPSOptions(); - unset($ups_options['shipment']['dimensions']); - - $ups = new UPS\Rate($ups_options); - $ups_rates = $ups->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testFedexRateMissingTo() - { - $fedex_options = $this->getFedexOptions(); - unset($fedex_options['shipment']['to']); - - $fedex = new Fedex\Rate($fedex_options); - $fedex_rates = $fedex->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testFedexRateMissingFrom() - { - $fedex_options = $this->getFedexOptions(); - unset($fedex_options['shipment']['from']); - - $fedex = new Fedex\Rate($fedex_options); - $fedex_rates = $fedex->get_rates(); - } - - /** - * @expectedException Exception - */ - public function testFedexRateMissingDimensions() - { - $fedex_options = $this->getFedexOptions(); - unset($fedex_options['shipment']['dimensions']); - - $fedex = new Fedex\Rate($fedex_options); - $fedex_rates = $fedex->get_rates(); - } - // // Readme Examples: // public function testUSPSReadmeExample() // { From ad3c950d6d75a2be4da5bfde58f8989c076a244b Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Thu, 27 Nov 2014 12:18:41 -0500 Subject: [PATCH 03/10] Update read me --- README.md | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index cd96df5..7fbb5a5 100644 --- a/README.md +++ b/README.md @@ -21,23 +21,18 @@ Add the following lines to your ``composer.json`` file. Create a shipment object: ```php -$shipment = [ - 'weight' => 3, // lbs - 'dimensions' => [ - 'width' => 9, // inches - 'length' => 9, - 'height' => 9, - ], - 'from' => [ - 'postal_code' => '90401', - 'country_code' => 'US', - ], - 'to' => [ - 'postal_code' => '78703', - 'country_code' => 'US', - 'is_residential' => TRUE, - ], -]; +$shipment = new Shipment; +$shipment->setFromStateProvinceCode('IN')->setFromPostalCode('46205') + ->setFromCountryCode('US') + ->setToPostalCode('20101') + ->setToCountryCode('US') + ->setToResidential(true); + +$package = new Package; +$package->setLength(12)->setWidth(4)->setHeight(3)->setWeight(3); +$package->setPackaging(Package::USPS_CONTAINER_RECTANGULAR); +$package->setSizeClassification(Package::USPS_SIZE_LARGE); +$shipment->addPackage($package); ``` ## UPS (Stub) Example @@ -116,10 +111,7 @@ $usps = new USPS\Rate([ 'prod' => FALSE, 'username' => 'XXXX', 'password' => 'XXXX', - 'shipment' => array_merge($shipment, [ - 'size' => 'LARGE', - 'container' => 'RECTANGULAR', - ]), + 'shipment' => $shipment, 'approved_codes' => [ '1', // 1-3 business days '4', // 2-8 business days @@ -162,9 +154,7 @@ $fedex = new Fedex\Rate([ 'account_number' => 'XXXX', 'meter_number' => 'XXXX', 'drop_off_type' => 'BUSINESS_SERVICE_CENTER', - 'shipment' => array_merge($shipment, [ - 'packaging_type' => 'YOUR_PACKAGING', - ]), + 'shipment' => $shipment, 'approved_codes' => [ 'FEDEX_EXPRESS_SAVER', // 1-3 business days 'FEDEX_GROUND', // 1-5 business days From 14c35aa03f543c542a3d5fcd69aa185feade200b Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Thu, 27 Nov 2014 12:37:33 -0500 Subject: [PATCH 04/10] Streamlined the packaging stuff with some reasonable assumed defaults for now --- src/Package.php | 42 ------------------------------------------ src/USPS/Rate.php | 25 +++++++++++++++++++++++-- tests/ShipTest.php | 9 +-------- 3 files changed, 24 insertions(+), 52 deletions(-) diff --git a/src/Package.php b/src/Package.php index 07414ce..b873fb1 100644 --- a/src/Package.php +++ b/src/Package.php @@ -2,52 +2,10 @@ class Package { - const FEDEX_YOUR_PACKAGING = 'YOUR_PACKAGING'; - const USPS_CONTAINER_RECTANGULAR = 'RECTANGULAR'; - const USPS_SIZE_LARGE = 'LARGE'; - protected $weight; protected $width; protected $length; protected $height; - protected $packaging; - protected $size_classification; - - /** - * @return mixed - */ - public function getSizeClassification() - { - return $this->size_classification; - } - - /** - * @param mixed $size_classification - * @return $this - */ - public function setSizeClassification($size_classification) - { - $this->size_classification = $size_classification; - return $this; - } - - /** - * @return mixed - */ - public function getPackaging() - { - return $this->packaging; - } - - /** - * @param mixed $packaging - * @return $this - */ - public function setPackaging($packaging) - { - $this->packaging = $packaging; - return $this; - } /** * @return mixed diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index 48bcc95..48647b5 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -101,14 +101,35 @@ class Rate extends RateAdapter foreach ($this->shipment->getPackages() as $p) { $sequence_number++; + /** + * RateV4Request / Package / Size + required once + Defined as follows: + + REGULAR: Package dimensions are 12’’ or less; + LARGE: Any package dimension is larger than 12’’. + + For example: REGULAR + string + whiteSpace=collapse + enumeration=LARGE + enumeration=REGULAR + + */ + if ($p->getWidth() > 12 or $p->getLength() > 12 or $p->getHeight() > 12) { + $size = 'LARGE'; + } else { + $size = 'REGULAR'; + } + $packages .= ' ALL ' . $this->shipment->getFromPostalCode() . ' ' . $this->shipment->getToPostalCode() . ' ' . $p->getWeight() . ' 0 - ' . $p->getPackaging() . ' - ' . $p->getSizeClassification() . ' + RECTANGULAR + ' . $size . ' ' . $p->getWidth() . ' ' . $p->getLength() . ' ' . $p->getHeight() . ' diff --git a/tests/ShipTest.php b/tests/ShipTest.php index 34c6e2c..ce42ffe 100644 --- a/tests/ShipTest.php +++ b/tests/ShipTest.php @@ -61,9 +61,7 @@ class ShipTest extends PHPUnit_Framework_TestCase $p->setWeight(3) ->setWidth(9) ->setLength(9) - ->setHeight(9) - ->setPackaging(Package::USPS_CONTAINER_RECTANGULAR) - ->setSizeClassification(Package::USPS_SIZE_LARGE); + ->setHeight(9); $s->addPackage($p); @@ -107,11 +105,6 @@ class ShipTest extends PHPUnit_Framework_TestCase $ship = Ship::factory($this->shipping_options); $approved_codes = $ship->get_approved_codes('fedex'); - $ps = $this->shipment->getPackages(); - foreach ($ps as $p) { - $p->setPackaging(Package::FEDEX_YOUR_PACKAGING); - } - return [ 'prod' => FALSE, 'key' => 'XXXX', From 6d42cd12730b852c0181f31bf6af078838672fcc Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Thu, 27 Nov 2014 12:38:22 -0500 Subject: [PATCH 05/10] Readme update --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7fbb5a5..e227648 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,9 @@ Create a shipment object: ```php $shipment = new Shipment; -$shipment->setFromStateProvinceCode('IN')->setFromPostalCode('46205') +$shipment + ->setFromStateProvinceCode('IN') + ->setFromPostalCode('46205') ->setFromCountryCode('US') ->setToPostalCode('20101') ->setToCountryCode('US') @@ -30,8 +32,7 @@ $shipment->setFromStateProvinceCode('IN')->setFromPostalCode('46205') $package = new Package; $package->setLength(12)->setWidth(4)->setHeight(3)->setWeight(3); -$package->setPackaging(Package::USPS_CONTAINER_RECTANGULAR); -$package->setSizeClassification(Package::USPS_SIZE_LARGE); + $shipment->addPackage($package); ``` From 1f98232ac0f70536ddd4f49cbabb528c3373fb36 Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Thu, 27 Nov 2014 13:27:56 -0500 Subject: [PATCH 06/10] Provide rate quotes back as objects --- composer.json | 4 +- src/Fedex/Rate.php | 21 ++-- src/Quote.php | 123 ++++++++++++++++++++ src/RateAdapter.php | 4 +- src/Ship.php | 12 +- src/UPS/Rate.php | 13 ++- src/USPS/Rate.php | 17 ++- tests/ShipTest.php | 268 +++++++++++++++++++------------------------- 8 files changed, 280 insertions(+), 182 deletions(-) create mode 100644 src/Quote.php diff --git a/composer.json b/composer.json index 58cef70..3c7d92d 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,9 @@ "email": "pdt256@gmail.com" } ], - "require": {}, + "require": { + "nesbot/carbon": "1.*" + }, "require-dev": { "phpunit/phpunit": "4.0.*" }, diff --git a/src/Fedex/Rate.php b/src/Fedex/Rate.php index 5f91875..328bbac 100644 --- a/src/Fedex/Rate.php +++ b/src/Fedex/Rate.php @@ -1,8 +1,10 @@ getElementsByTagName('TotalNetCharge')->item(0) ->getElementsByTagName('Amount')->item(0)->nodeValue; - $this->rates[] = array( - 'code' => $code, - 'name' => $name, - 'cost' => (int) $cost * 100, - 'delivery_ts' => $delivery_ts, - 'transit_time' => $transit_time, - ); + $quote = new Quote; + $quote + ->setCarrier('fedex') + ->setCode($code) + ->setName($name) + ->setCost((int) $cost * 100) + ->setTransitTime($transit_time); + if ($delivery_ts) { + $quote->setDeliveryEstimate(new Carbon($delivery_ts)); + } + + $this->rates[] = $quote; } return $this; diff --git a/src/Quote.php b/src/Quote.php new file mode 100644 index 0000000..e37cb10 --- /dev/null +++ b/src/Quote.php @@ -0,0 +1,123 @@ +carrier; + } + + /** + * @param mixed $carrier + * @return $this + */ + public function setCarrier($carrier) + { + $this->carrier = $carrier; + return $this; + } + + /** + * @return mixed + */ + public function getCode() + { + return $this->code; + } + + /** + * @param string $code + * @return $this + */ + public function setCode($code) + { + $this->code = $code; + return $this; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * @return mixed + */ + public function getCost() + { + return $this->cost; + } + + /** + * Quoted cost of this service, in pennies + * + * @param int $cost + * @return $this + */ + public function setCost($cost) + { + $this->cost = $cost; + return $this; + } + + /** + * @return mixed + */ + public function getTransitTime() + { + return $this->transit_time; + } + + /** + * @param mixed $transit_time + * @return $this + */ + public function setTransitTime($transit_time) + { + $this->transit_time = $transit_time; + return $this; + } + + /** + * @return mixed + */ + public function getDeliveryEstimate() + { + return $this->delivery_ts; + } + + /** + * @param Carbon $estimate + * @return $this + */ + public function setDeliveryEstimate(Carbon $estimate) + { + $this->delivery_ts = $estimate; + return $this; + } +} diff --git a/src/RateAdapter.php b/src/RateAdapter.php index 8fd7dbb..de2d17a 100644 --- a/src/RateAdapter.php +++ b/src/RateAdapter.php @@ -43,11 +43,11 @@ abstract class RateAdapter ->process() ->sort_by_cost(); - return $this->rates; + return array_values($this->rates); } protected function sort_by_cost() { - uasort($this->rates, create_function('$a, $b', 'return ($a["cost"] > $b["cost"]);')); + uasort($this->rates, create_function('$a, $b', 'return ($a->getCost() > $b->getCost());')); } } diff --git a/src/Ship.php b/src/Ship.php index d176c0c..06e5363 100644 --- a/src/Ship.php +++ b/src/Ship.php @@ -88,13 +88,13 @@ class Ship foreach ($rates[$carrier] as $row3) { - if (in_array($row3['code'], $group_codes)) { - $row3['carrier'] = $carrier; + if (in_array($row3->getCode(), $group_codes)) { + $row3->setCarrier($carrier); if ($cheapest_row === NULL) { $cheapest_row = $row3; } else { - if ($row3['cost'] < $cheapest_row['cost']) { + if ($row3->getCost() < $cheapest_row->getCost()) { $cheapest_row = $row3; } } @@ -126,8 +126,8 @@ class Ship foreach ($rates[$carrier] as $row3) { - if (in_array($row3['code'], $group_codes)) { - $row3['carrier'] = $carrier; + if (in_array($row3->getCode(), $group_codes)) { + $row3->setCarrier($carrier); $display_rates[$shipping_group][] = $row3; } } @@ -142,6 +142,6 @@ class Ship protected function sort_by_cost( & $rates) { - uasort($rates, create_function('$a, $b', 'return ($a["cost"] > $b["cost"]);')); + uasort($rates, create_function('$a, $b', 'return ($a->getCost() > $b->getCost());')); } } diff --git a/src/UPS/Rate.php b/src/UPS/Rate.php index 53a4cbb..bb33c50 100644 --- a/src/UPS/Rate.php +++ b/src/UPS/Rate.php @@ -3,6 +3,7 @@ namespace pdt256\Shipping\UPS; use pdt256\Ship; use pdt256\Shipping\Arr; +use pdt256\Shipping\Quote; use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateRequest; use DOMDocument; @@ -233,11 +234,13 @@ class Rate extends RateAdapter continue; } - $this->rates[] = array( - 'code' => $code, - 'name' => $name, - 'cost' => (int) $cost * 100, - ); + $quote = new Quote; + $quote + ->setCarrier('ups') + ->setCode($code) + ->setName($name) + ->setCost((int) $cost * 100); + $this->rates[] = $quote; } return $this; diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index 48647b5..c0ebf1f 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -3,6 +3,7 @@ namespace pdt256\Shipping\USPS; use pdt256\Shipping; use pdt256\Shipping\Arr; +use pdt256\Shipping\Quote; use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateRequest; use DOMDocument; @@ -178,6 +179,7 @@ class Rate extends RateAdapter throw $e; } + /** @var Quote[] $rates */ $rates = []; foreach ($postage_list as $postage) { @@ -191,16 +193,19 @@ class Rate extends RateAdapter } if (array_key_exists($code, $rates)) { - $cost = $rates[$code]['cost'] + ($cost * 100); + $cost = $rates[$code]->getCost() + ($cost * 100); } else { $cost = $cost * 100; } - $rates[$code] = [ - 'code' => $code, - 'name' => $name, - 'cost' => (int) $cost, - ]; + $quote = new Quote; + $quote + ->setCarrier('usps') + ->setCode($code) + ->setName($name) + ->setCost((int) $cost); + + $rates[$quote->getCode()] = $quote; } $this->rates = array_values($rates); diff --git a/tests/ShipTest.php b/tests/ShipTest.php index ce42ffe..4fa34df 100644 --- a/tests/ShipTest.php +++ b/tests/ShipTest.php @@ -1,5 +1,7 @@ getUSPSOptions()); $usps_rates = $usps->get_rates(); - $this->assertEquals(json_encode([ - 1 => [ - 'code' => '4', - 'name' => 'Parcel Post', - 'cost' => 1001, - ], - 0 => [ - 'code' => '1', - 'name' => 'Priority Mail', - 'cost' => 1220, - ], - ]), json_encode($usps_rates)); + $post = new Quote; + $post + ->setCarrier('usps') + ->setCode(4) + ->setName('Parcel Post') + ->setCost(1001); + + $priority = new Quote; + $priority + ->setCarrier('usps') + ->setCode(1) + ->setName('Priority Mail') + ->setCost(1220); + + $expected_return = [$post, $priority]; + + $this->assertEquals($expected_return, $usps_rates); } public function testUPSRate() @@ -142,28 +149,37 @@ class ShipTest extends PHPUnit_Framework_TestCase $ups = new UPS\Rate($this->getUPSOptions()); $ups_rates = $ups->get_rates(); - $this->assertEquals(json_encode([ - 0 => [ - 'code' => '03', - 'name' => 'UPS Ground', - 'cost' => 1900, - ], - 1 => [ - 'code' => '02', - 'name' => 'UPS 2nd Day Air', - 'cost' => 4900, - ], - 2 => [ - 'code' => '13', - 'name' => 'UPS Next Day Air Saver', - 'cost' => 8900, - ], - 3 => [ - 'code' => '01', - 'name' => 'UPS Next Day Air', - 'cost' => 9300, - ], - ]), json_encode($ups_rates)); + $ground = new Quote; + $ground + ->setCarrier('ups') + ->setCode('03') + ->setName('UPS Ground') + ->setCost(1900); + + $twodayair = new Quote; + $twodayair + ->setCarrier('ups') + ->setCode('02') + ->setName('UPS 2nd Day Air') + ->setCost(4900); + + $nextdaysaver = new Quote; + $nextdaysaver + ->setCarrier('ups') + ->setCode('13') + ->setName('UPS Next Day Air Saver') + ->setCost(8900); + + $nextdayair = new Quote; + $nextdayair + ->setCarrier('ups') + ->setCode('01') + ->setName('UPS Next Day Air') + ->setCost(9300); + + $expected_return = [$ground, $twodayair, $nextdaysaver, $nextdayair]; + + $this->assertEquals($expected_return, $ups_rates); } public function testFedexRate() @@ -171,36 +187,44 @@ class ShipTest extends PHPUnit_Framework_TestCase $fedex = new Fedex\Rate($this->getFedexOptions()); $fedex_rates = $fedex->get_rates(); - $this->assertEquals(json_encode([ - 3 => [ - 'code' => 'GROUND_HOME_DELIVERY', - 'name' => 'Ground Home Delivery', - 'cost' => 1600, - 'delivery_ts' => NULL, - 'transit_time' => 'THREE_DAYS', - ], - 2 => [ - 'code' => 'FEDEX_EXPRESS_SAVER', - 'name' => 'Fedex Express Saver', - 'cost' => 2900, - 'delivery_ts' => '2014-09-30T20:00:00', - 'transit_time' => NULL, - ], - 1 => [ - 'code' => 'FEDEX_2_DAY', - 'name' => 'Fedex 2 Day', - 'cost' => 4000, - 'delivery_ts' => '2014-09-29T20:00:00', - 'transit_time' => NULL, - ], - 0 => [ - 'code' => 'STANDARD_OVERNIGHT', - 'name' => 'Standard Overnight', - 'cost' => 7800, - 'delivery_ts' => '2014-09-26T20:00:00', - 'transit_time' => NULL, - ], - ]), json_encode($fedex_rates)); + $ground = new Quote; + $ground + ->setCarrier('fedex') + ->setCode('GROUND_HOME_DELIVERY') + ->setName('Ground Home Delivery') + ->setCost(1600) + ->setTransitTime('THREE_DAYS'); + + $express = new Quote; + $express + ->setCarrier('fedex') + ->setCode('FEDEX_EXPRESS_SAVER') + ->setName('Fedex Express Saver') + ->setCost(2900) + ->setDeliveryEstimate(new Carbon('2014-09-30T20:00:00')) + ->setTransitTime(null); + + $secondday = new Quote; + $secondday + ->setCarrier('fedex') + ->setCode('FEDEX_2_DAY') + ->setName('Fedex 2 Day') + ->setCost(4000) + ->setDeliveryEstimate(new Carbon('2014-09-29T20:00:00')) + ->setTransitTime(null); + + $overnight = new Quote; + $overnight + ->setCarrier('fedex') + ->setCode('STANDARD_OVERNIGHT') + ->setName('Standard Overnight') + ->setCost(7800) + ->setDeliveryEstimate(new Carbon('2014-09-26T20:00:00')) + ->setTransitTime(null); + + $expected_result = [$ground, $express, $secondday, $overnight]; + + $this->assertEquals($expected_result, $fedex_rates); } public function testDisplayOptions() @@ -219,102 +243,36 @@ class ShipTest extends PHPUnit_Framework_TestCase $ship = Ship::factory($this->shipping_options); $display_rates = $ship->get_display_rates($rates); - $this->assertEquals(json_encode([ + $post = new Quote; + $post->setCode(4) + ->setName('Parcel Post') + ->setCost(1001) + ->setCarrier('usps'); + + $fedex_two_day = new Quote; + $fedex_two_day->setCode('FEDEX_2_DAY') + ->setName('Fedex 2 Day') + ->setCost(4000) + ->setDeliveryEstimate(new Carbon('2014-09-29T20:00:00')) + ->setCarrier('fedex'); + + $overnight = new Quote; + $overnight->setCode('STANDARD_OVERNIGHT') + ->setName('Standard Overnight') + ->setCost(7800) + ->setDeliveryEstimate(new Carbon('2014-09-26T20:00:00')) + ->setCarrier('fedex'); + + $this->assertEquals([ 'Standard Shipping' => [ - 0 => [ - 'code' => '4', - 'name' => 'Parcel Post', - 'cost' => 1001, - 'carrier' => 'usps', - ], + $post, ], 'Two-Day Shipping' => [ - 0 => [ - 'code' => 'FEDEX_2_DAY', - 'name' => 'Fedex 2 Day', - 'cost' => 4000, - 'delivery_ts' => '2014-09-29T20:00:00', - 'transit_time' => NULL, - 'carrier' => 'fedex', - ], + $fedex_two_day, ], 'One-Day Shipping' => [ - 0 => [ - 'code' => 'STANDARD_OVERNIGHT', - 'name' => 'Standard Overnight', - 'cost' => 7800, - 'delivery_ts' => '2014-09-26T20:00:00', - 'transit_time' => NULL, - 'carrier' => 'fedex', - ], + $overnight, ], - ]), json_encode($display_rates)); + ], $display_rates); } - - // // Readme Examples: - // public function testUSPSReadmeExample() - // { - // $usps = new USPS\Rate([ - // 'prod' => FALSE, - // 'username' => 'XXXX', - // 'password' => 'XXXX', - // 'shipment' => array_merge($this->shipment, [ - // 'size' => 'LARGE', - // 'container' => 'RECTANGULAR', - // ]), - // 'approved_codes' => [ - // '1', // 1-3 business days - // '4', // 2-8 business days - // ], - // 'request_adapter' => new RateRequest\StubUSPS(), - // ]); - // - // $usps_rates = $usps->get_rates(); - // var_export($usps_rates); - // } - // - // public function testUPSReadmeExample() - // { - // $ups = new UPS\Rate([ - // 'prod' => FALSE, - // 'shipment' => $this->shipment, - // 'approved_codes' => [ - // '03', // 1-5 business days - // '02', // 2 business days - // '01', // next business day 10:30am - // '13', // next business day by 3pm - // '14', // next business day by 8am - // ], - // 'request_adapter' => new RateRequest\StubUPS(), - // ]); - // - // $ups_rates = $ups->get_rates(); - // var_export($ups_rates); - // } - // - // public function testFedexReadmeExample() - // { - // $fedex = new Fedex\Rate([ - // 'prod' => FALSE, - // 'key' => 'XXXX', - // 'password' => 'XXXX', - // 'account_number' => 'XXXX', - // 'meter_number' => 'XXXX', - // 'drop_off_type' => 'BUSINESS_SERVICE_CENTER', - // 'shipment' => array_merge($this->shipment, [ - // 'packaging_type' => 'YOUR_PACKAGING', - // ]), - // 'approved_codes' => [ - // 'FEDEX_EXPRESS_SAVER', // 1-3 business days - // 'FEDEX_GROUND', // 1-5 business days - // 'GROUND_HOME_DELIVERY', // 1-5 business days - // 'FEDEX_2_DAY', // 2 business days - // 'STANDARD_OVERNIGHT', // overnight - // ], - // 'request_adapter' => new RateRequest\StubFedex(), - // ]); - // - // $fedex_rates = $fedex->get_rates(); - // var_export($fedex_rates); - // } } From 64dbeee35dbf7651bcafbb72dd7396f821c87b80 Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Thu, 27 Nov 2014 13:33:40 -0500 Subject: [PATCH 07/10] Updates to the documentation example return values --- README.md | 251 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 177 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index e227648..6a6928d 100644 --- a/README.md +++ b/README.md @@ -74,32 +74,68 @@ $ups_rates = $ups->get_rates(); Output array sorted by cost: (in cents) ```php -array ( - 0 => - array ( - 'code' => '03', - 'name' => 'UPS Ground', - 'cost' => 1900, - ), - 1 => - array ( - 'code' => '02', - 'name' => 'UPS 2nd Day Air', - 'cost' => 4900, - ), - 2 => - array ( - 'code' => '13', - 'name' => 'UPS Next Day Air Saver', - 'cost' => 8900, - ), - 3 => - array ( - 'code' => '01', - 'name' => 'UPS Next Day Air', - 'cost' => 9300, - ), -) +array(4) { + [0] => + class pdt256\Shipping\Quote#56 (6) { + protected $code => + string(2) "03" + protected $name => + string(10) "UPS Ground" + protected $cost => + int(1900) + protected $transit_time => + NULL + protected $delivery_ts => + NULL + protected $carrier => + string(3) "ups" + } + [1] => + class pdt256\Shipping\Quote#58 (6) { + protected $code => + string(2) "02" + protected $name => + string(15) "UPS 2nd Day Air" + protected $cost => + int(4900) + protected $transit_time => + NULL + protected $delivery_ts => + NULL + protected $carrier => + string(3) "ups" + } + [2] => + class pdt256\Shipping\Quote#57 (6) { + protected $code => + string(2) "13" + protected $name => + string(22) "UPS Next Day Air Saver" + protected $cost => + int(8900) + protected $transit_time => + NULL + protected $delivery_ts => + NULL + protected $carrier => + string(3) "ups" + } + [3] => + class pdt256\Shipping\Quote#55 (6) { + protected $code => + string(2) "01" + protected $name => + string(16) "UPS Next Day Air" + protected $cost => + int(9300) + protected $transit_time => + NULL + protected $delivery_ts => + NULL + protected $carrier => + string(3) "ups" + } +} ``` ## USPS (Stub) Example @@ -126,20 +162,38 @@ $usps_rates = $usps->get_rates(); Output array sorted by cost: (in cents) ```php -array ( - 1 => - array ( - 'code' => '4', - 'name' => 'Parcel Post', - 'cost' => 1000, - ), - 0 => - array ( - 'code' => '1', - 'name' => 'Priority Mail', - 'cost' => 1200, - ), -) +array(2) { + [0] => + class pdt256\Shipping\Quote#30 (6) { + protected $code => + string(1) "4" + protected $name => + string(11) "Parcel Post" + protected $cost => + int(1001) + protected $transit_time => + NULL + protected $delivery_ts => + NULL + protected $carrier => + string(4) "usps" + } + [1] => + class pdt256\Shipping\Quote#26 (6) { + protected $code => + string(1) "1" + protected $name => + string(13) "Priority Mail" + protected $cost => + int(1220) + protected $transit_time => + NULL + protected $delivery_ts => + NULL + protected $carrier => + string(4) "usps" + } +} ``` ## Fedex (Stub) Example @@ -172,40 +226,89 @@ $fedex_rates = $fedex->get_rates(); Output array sorted by cost: (in cents) ```php -array ( - 3 => - array ( - 'code' => 'GROUND_HOME_DELIVERY', - 'name' => 'Ground Home Delivery', - 'cost' => 1600, - 'delivery_ts' => NULL, - 'transit_time' => 'THREE_DAYS', - ), - 2 => - array ( - 'code' => 'FEDEX_EXPRESS_SAVER', - 'name' => 'Fedex Express Saver', - 'cost' => 2900, - 'delivery_ts' => '2014-09-30T20:00:00', - 'transit_time' => NULL, - ), - 1 => - array ( - 'code' => 'FEDEX_2_DAY', - 'name' => 'Fedex 2 Day', - 'cost' => 4000, - 'delivery_ts' => '2014-09-29T20:00:00', - 'transit_time' => NULL, - ), - 0 => - array ( - 'code' => 'STANDARD_OVERNIGHT', - 'name' => 'Standard Overnight', - 'cost' => 7800, - 'delivery_ts' => '2014-09-26T20:00:00', - 'transit_time' => NULL, - ), -) +array(4) { + [0] => + class pdt256\Shipping\Quote#65 (6) { + protected $code => + string(20) "GROUND_HOME_DELIVERY" + protected $name => + string(20) "Ground Home Delivery" + protected $cost => + int(1600) + protected $transit_time => + string(10) "THREE_DAYS" + protected $delivery_ts => + NULL + protected $carrier => + string(5) "fedex" + } + [1] => + class pdt256\Shipping\Quote#63 (6) { + protected $code => + string(19) "FEDEX_EXPRESS_SAVER" + protected $name => + string(19) "Fedex Express Saver" + protected $cost => + int(2900) + protected $transit_time => + NULL + protected $delivery_ts => + class Carbon\Carbon#23 (3) { + public $date => + string(26) "2014-09-30 20:00:00.000000" + public $timezone_type => + int(3) + public $timezone => + string(16) "America/New_York" + } + protected $carrier => + string(5) "fedex" + } + [2] => + class pdt256\Shipping\Quote#61 (6) { + protected $code => + string(11) "FEDEX_2_DAY" + protected $name => + string(11) "Fedex 2 Day" + protected $cost => + int(4000) + protected $transit_time => + NULL + protected $delivery_ts => + class Carbon\Carbon#26 (3) { + public $date => + string(26) "2014-09-29 20:00:00.000000" + public $timezone_type => + int(3) + public $timezone => + string(16) "America/New_York" + } + protected $carrier => + string(5) "fedex" + } + [3] => + class pdt256\Shipping\Quote#60 (6) { + protected $code => + string(18) "STANDARD_OVERNIGHT" + protected $name => + string(18) "Standard Overnight" + protected $cost => + int(7800) + protected $transit_time => + NULL + protected $delivery_ts => + class Carbon\Carbon#58 (3) { + public $date => + string(26) "2014-09-26 20:00:00.000000" + public $timezone_type => + int(3) + public $timezone => + string(16) "America/New_York" + } + protected $carrier => + string(5) "fedex" + } +} ``` ### License From c05c14bcc1cea63a1c5f8c36087fd4f8a8f68ce4 Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Fri, 28 Nov 2014 12:08:15 -0500 Subject: [PATCH 08/10] Change to the carbon dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3c7d92d..41fa062 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "nesbot/carbon": "1.*" + "nesbot/carbon": "~1.0" }, "require-dev": { "phpunit/phpunit": "4.0.*" From 3dbfc3e1356be079c0a4d1938e9bc9d9268d0c7c Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Fri, 28 Nov 2014 22:19:02 -0500 Subject: [PATCH 09/10] Specify the container based on the package size --- src/USPS/Rate.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index c0ebf1f..fe39462 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -119,8 +119,10 @@ class Rate extends RateAdapter */ if ($p->getWidth() > 12 or $p->getLength() > 12 or $p->getHeight() > 12) { $size = 'LARGE'; + $container = 'RECTANGULAR'; } else { $size = 'REGULAR'; + $container = 'VARIABLE'; } $packages .= ' @@ -129,7 +131,7 @@ class Rate extends RateAdapter ' . $this->shipment->getToPostalCode() . ' ' . $p->getWeight() . ' 0 - RECTANGULAR + ' . $container . ' ' . $size . ' ' . $p->getWidth() . ' ' . $p->getLength() . ' From 66d7f46d619563b1d8555cfb80d5aea555232532 Mon Sep 17 00:00:00 2001 From: Troy Davisson Date: Sun, 30 Nov 2014 21:51:16 -0500 Subject: [PATCH 10/10] Drop Carbon requirement --- composer.json | 3 --- src/Fedex/Rate.php | 4 ++-- src/Quote.php | 6 +++--- tests/ShipTest.php | 11 +++++------ 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 41fa062..f7694c4 100644 --- a/composer.json +++ b/composer.json @@ -9,9 +9,6 @@ "email": "pdt256@gmail.com" } ], - "require": { - "nesbot/carbon": "~1.0" - }, "require-dev": { "phpunit/phpunit": "4.0.*" }, diff --git a/src/Fedex/Rate.php b/src/Fedex/Rate.php index 328bbac..26022cc 100644 --- a/src/Fedex/Rate.php +++ b/src/Fedex/Rate.php @@ -1,7 +1,7 @@ setCost((int) $cost * 100) ->setTransitTime($transit_time); if ($delivery_ts) { - $quote->setDeliveryEstimate(new Carbon($delivery_ts)); + $quote->setDeliveryEstimate(new DateTime($delivery_ts)); } $this->rates[] = $quote; diff --git a/src/Quote.php b/src/Quote.php index e37cb10..0daf8d4 100644 --- a/src/Quote.php +++ b/src/Quote.php @@ -1,6 +1,6 @@ delivery_ts = $estimate; return $this; diff --git a/tests/ShipTest.php b/tests/ShipTest.php index 4fa34df..f6cb5ee 100644 --- a/tests/ShipTest.php +++ b/tests/ShipTest.php @@ -1,5 +1,4 @@ setCode('FEDEX_EXPRESS_SAVER') ->setName('Fedex Express Saver') ->setCost(2900) - ->setDeliveryEstimate(new Carbon('2014-09-30T20:00:00')) + ->setDeliveryEstimate(new DateTime('2014-09-30T20:00:00')) ->setTransitTime(null); $secondday = new Quote; @@ -210,7 +209,7 @@ class ShipTest extends PHPUnit_Framework_TestCase ->setCode('FEDEX_2_DAY') ->setName('Fedex 2 Day') ->setCost(4000) - ->setDeliveryEstimate(new Carbon('2014-09-29T20:00:00')) + ->setDeliveryEstimate(new DateTime('2014-09-29T20:00:00')) ->setTransitTime(null); $overnight = new Quote; @@ -219,7 +218,7 @@ class ShipTest extends PHPUnit_Framework_TestCase ->setCode('STANDARD_OVERNIGHT') ->setName('Standard Overnight') ->setCost(7800) - ->setDeliveryEstimate(new Carbon('2014-09-26T20:00:00')) + ->setDeliveryEstimate(new DateTime('2014-09-26T20:00:00')) ->setTransitTime(null); $expected_result = [$ground, $express, $secondday, $overnight]; @@ -253,14 +252,14 @@ class ShipTest extends PHPUnit_Framework_TestCase $fedex_two_day->setCode('FEDEX_2_DAY') ->setName('Fedex 2 Day') ->setCost(4000) - ->setDeliveryEstimate(new Carbon('2014-09-29T20:00:00')) + ->setDeliveryEstimate(new DateTime('2014-09-29T20:00:00')) ->setCarrier('fedex'); $overnight = new Quote; $overnight->setCode('STANDARD_OVERNIGHT') ->setName('Standard Overnight') ->setCost(7800) - ->setDeliveryEstimate(new Carbon('2014-09-26T20:00:00')) + ->setDeliveryEstimate(new DateTime('2014-09-26T20:00:00')) ->setCarrier('fedex'); $this->assertEquals([