diff --git a/src/Fedex/Rate.php b/src/Fedex/Rate.php index bc4ba01..82f8dad 100644 --- a/src/Fedex/Rate.php +++ b/src/Fedex/Rate.php @@ -7,6 +7,7 @@ use pdt256\Shipping\Arr; use pdt256\Shipping\Quote; use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateRequest; +use pdt256\Shipping\Validator; use DOMDocument; use Exception; @@ -15,19 +16,18 @@ class Rate extends RateAdapter private $urlDev = 'https://gatewaybeta.fedex.com/web-services/'; private $urlProd = 'https://gateway.fedex.com/web-services/'; - private $key = 'XXX'; - private $password = 'XXX'; - private $accountNumber = 'XXX'; - private $meterNumber = 'XXX'; - private $dropOffType = 'BUSINESS_SERVICE_CENTER'; - - public $approvedCodes = [ - 'PRIORITY_OVERNIGHT', - 'FEDEX_2_DAY', - 'FEDEX_EXPRESS_SAVER', - 'FEDEX_GROUND', - 'GROUND_HOME_DELIVERY', - ]; + private $key; + private $password; + private $accountNumber; + private $meterNumber; + /** + * Type of Drop off, default value "BUSINESS_SERVICE_CENTER" is defined in __construct if not specified. + */ + private $dropOffType; + /** + * Codes of appropriate shipping types. Default value is specified in __construct. + */ + public $approvedCodes; private $shippingCodes = [ 'EUROPE_FIRST_INTERNATIONAL_PRIORITY' => 'Europe First International Priority', @@ -61,12 +61,37 @@ class Rate extends RateAdapter $this->password = Arr::get($options, 'password'); $this->accountNumber = Arr::get($options, 'accountNumber'); $this->meterNumber = Arr::get($options, 'meterNumber'); - $this->approvedCodes = Arr::get($options, 'approvedCodes'); - $this->dropOffType = Arr::get($options, 'dropOffType'); + $this->approvedCodes = Arr::get($options, 'approvedCodes', [ + 'PRIORITY_OVERNIGHT', + 'FEDEX_2_DAY', + 'FEDEX_EXPRESS_SAVER', + 'FEDEX_GROUND', + 'GROUND_HOME_DELIVERY', + ]); + $this->dropOffType = Arr::get($options, 'dropOffType', 'BUSINESS_SERVICE_CENTER'); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post())); } + protected function validate() + { + foreach ($this->shipment->getPackages() as $package) { + Validator::checkIfNull($package->getWeight(), 'weight'); + Validator::checkIfNull($package->getLength(), 'length'); + Validator::checkIfNull($package->getHeight(), 'height'); + } + Validator::checkIfNull($this->key, 'key'); + Validator::checkIfNull($this->password, 'password'); + Validator::checkIfNull($this->accountNumber, 'accountNumber'); + Validator::checkIfNull($this->meterNumber, 'meterNumber'); + Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode'); + Validator::checkIfNull($this->shipment->getFromCountryCode(), 'fromCountryCode'); + Validator::checkIfNull($this->shipment->getFromIsResidential(), 'fromIsResidential'); + Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode'); + Validator::checkIfNull($this->shipment->getToCountryCode(), 'toCountryCode'); + Validator::checkIfNull($this->shipment->getToIsResidential(), 'toIsResidential'); + return $this; + } protected function prepare() { $date = time(); @@ -103,7 +128,6 @@ class Rate extends RateAdapter '' . ''; } - $this->data = '' . '' . diff --git a/src/RateAdapter.php b/src/RateAdapter.php index 0a334cd..b4b5995 100644 --- a/src/RateAdapter.php +++ b/src/RateAdapter.php @@ -16,6 +16,10 @@ abstract class RateAdapter /** @var @var RateRequest\Adapter */ protected $rateRequest; + /** + * Make sure all necessary fields are set + */ + abstract protected function validate(); /** * Prepare XML */ @@ -47,6 +51,7 @@ abstract class RateAdapter public function getRates() { $this + ->validate() ->prepare() ->execute() ->process() diff --git a/src/Ship.php b/src/Ship.php index dde40f8..0ce4c50 100644 --- a/src/Ship.php +++ b/src/Ship.php @@ -58,7 +58,6 @@ class Ship // Build approvedCodes foreach ($this->shipping_options as $shipping_group => $row) { - foreach ($row as $_carrier => $row2) { if (!isset($approvedCodes[$_carrier])) { $approvedCodes[$_carrier] = []; @@ -89,9 +88,7 @@ class Ship $group_codes = array_keys($row2); if (! empty($rates[$carrier])) { - foreach ($rates[$carrier] as $row3) { - if (in_array($row3->getCode(), $group_codes)) { $row3->setCarrier($carrier); @@ -127,9 +124,7 @@ class Ship $group_codes = array_keys($row2); if (!empty($rates[$carrier])) { - foreach ($rates[$carrier] as $row3) { - if (in_array($row3->getCode(), $group_codes)) { $row3->setCarrier($carrier); $display_rates[$shipping_group][] = $row3; diff --git a/src/UPS/Rate.php b/src/UPS/Rate.php index 0e98561..c8ecf67 100644 --- a/src/UPS/Rate.php +++ b/src/UPS/Rate.php @@ -1,11 +1,11 @@ [ // United States @@ -90,12 +89,34 @@ class Rate extends RateAdapter $this->userId = Arr::get($options, 'userId'); $this->password = Arr::get($options, 'password'); $this->shipperNumber = Arr::get($options, 'shipperNumber'); - $this->approvedCodes = Arr::get($options, 'approvedCodes'); + $this->approvedCodes = Arr::get($options, 'approvedCodes', [ + '03', + '12', + ]); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post())); } + protected function validate() + { + foreach ($this->shipment->getPackages() as $package) { + Validator::checkIfNull($package->getWeight(), 'weight'); + Validator::checkIfNull($package->getLength(), 'length'); + Validator::checkIfNull($package->getHeight(), 'height'); + } + Validator::checkIfNull($this->accessKey, 'accessKey'); + Validator::checkIfNull($this->userId, 'userId'); + Validator::checkIfNull($this->password, 'password'); + Validator::checkIfNull($this->shipperNumber, 'shipperNumber'); + Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode'); + Validator::checkIfNull($this->shipment->getFromCountryCode(), 'fromCountryCode'); + Validator::checkIfNull($this->shipment->getFromIsResidential(), 'fromIsResidential'); + Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode'); + Validator::checkIfNull($this->shipment->getToCountryCode(), 'toCountryCode'); + Validator::checkIfNull($this->shipment->getToIsResidential(), 'toIsResidential'); + return $this; + } protected function prepare() { $service_code = '03'; diff --git a/src/USPS/Rate.php b/src/USPS/Rate.php index f5e4887..2ade0ec 100644 --- a/src/USPS/Rate.php +++ b/src/USPS/Rate.php @@ -6,6 +6,7 @@ use pdt256\Shipping\Arr; use pdt256\Shipping\Quote; use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateRequest; +use pdt256\Shipping\Validator; use DOMDocument; use Exception; @@ -14,13 +15,12 @@ class Rate extends RateAdapter private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll'; private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll'; - private $username = 'XXX'; - private $password = 'XXX'; - - public $approvedCodes = [ - '1', - '4', - ]; + private $username; + private $password; + /** + * Codes of appropriate shipping types. Default value is specified in __construct. + */ + public $approvedCodes; private $shipping_codes = [ 'domestic' => [ @@ -73,10 +73,26 @@ class Rate extends RateAdapter $this->username = Arr::get($options, 'username'); $this->password = Arr::get($options, 'password'); - $this->approvedCodes = Arr::get($options, 'approvedCodes'); + $this->approvedCodes = Arr::get($options, 'approvedCodes', [ + '1', + '4', + ]); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get())); } + protected function validate() + { + foreach ($this->shipment->getPackages() as $package) { + Validator::checkIfNull($package->getWeight(), 'weight'); + Validator::checkIfNull($package->getLength(), 'length'); + Validator::checkIfNull($package->getHeight(), 'height'); + } + Validator::checkIfNull($this->username, 'username'); + Validator::checkIfNull($this->password, 'password'); + Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode'); + Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode'); + return $this; + } protected function prepare() { $packages = ''; diff --git a/src/Validator.php b/src/Validator.php new file mode 100644 index 0000000..70d7acb --- /dev/null +++ b/src/Validator.php @@ -0,0 +1,12 @@ +shipment->setFromStateProvinceCode('CA') ->setFromPostalCode('90401') ->setFromCountryCode('US') + ->setFromIsResidential(true) ->setToPostalCode('78703') ->setToCountryCode('US') ->setToIsResidential(true) @@ -59,12 +60,15 @@ class RateTest extends \PHPUnit_Framework_TestCase { $rateAdapter = new Rate([ 'prod' => false, - 'drop_off_type' => 'BUSINESS_SERVICE_CENTER', + 'key' => 'XXX', + 'password' => 'XXX', + 'accountNumber' => 'XXX', + 'meterNumber' => 'XXX', + 'dropOffType' => 'BUSINESS_SERVICE_CENTER', 'shipment' => $this->shipment, 'approvedCodes' => $this->approvedCodes, 'requestAdapter' => new StubFedex, ]); - $rates = $rateAdapter->getRates(); $ground = new Quote('fedex', 'GROUND_HOME_DELIVERY', 'Ground Home Delivery', 1655); diff --git a/tests/ShipTest.php b/tests/ShipTest.php index 7b8bc10..94175b3 100644 --- a/tests/ShipTest.php +++ b/tests/ShipTest.php @@ -49,6 +49,7 @@ class ShipTest extends \PHPUnit_Framework_TestCase $s->setFromStateProvinceCode('CA') ->setFromPostalCode('90401') ->setFromCountryCode('US') + ->setFromIsResidential(true) ->setToPostalCode('78703') ->setToCountryCode('US') ->setToIsResidential(true); @@ -105,9 +106,9 @@ class ShipTest extends \PHPUnit_Framework_TestCase 'prod' => false, 'key' => 'XXXX', 'password' => 'XXXX', - 'account_number' => 'XXXX', - 'meter_number' => 'XXXX', - 'drop_off_type' => 'BUSINESS_SERVICE_CENTER', + 'accountNumber' => 'XXXX', + 'meterNumber' => 'XXXX', + 'dropOffType' => 'BUSINESS_SERVICE_CENTER', 'shipment' => $this->shipment, 'approvedCodes' => $approvedCodes, 'requestAdapter' => new RateRequest\StubFedex(), diff --git a/tests/UPS/RateTest.php b/tests/UPS/RateTest.php index e16e015..4edf4ad 100644 --- a/tests/UPS/RateTest.php +++ b/tests/UPS/RateTest.php @@ -48,6 +48,7 @@ class RateTest extends \PHPUnit_Framework_TestCase $this->shipment->setFromStateProvinceCode('CA') ->setFromPostalCode('90401') ->setFromCountryCode('US') + ->setFromIsResidential(true) ->setToPostalCode('78703') ->setToCountryCode('US') ->setToIsResidential(true) @@ -57,6 +58,10 @@ class RateTest extends \PHPUnit_Framework_TestCase public function testMockRates() { $rateAdapter = new Rate([ + 'accessKey' => 'XXX', + 'userId' => 'XXX', + 'password' => 'XXX', + 'shipperNumber' => 'XXX', 'prod' => false, 'shipment' => $this->shipment, 'approvedCodes' => $this->approvedCodes,