diff --git a/src/Fedex/Rate.php b/src/Fedex/Rate.php index bc4ba01..33fda6f 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,10 +16,10 @@ 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 $key; + private $password; + private $accountNumber; + private $meterNumber; private $dropOffType = 'BUSINESS_SERVICE_CENTER'; public $approvedCodes = [ @@ -66,7 +67,26 @@ class Rate extends RateAdapter $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 +123,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/UPS/Rate.php b/src/UPS/Rate.php index b3e68cd..a366688 100644 --- a/src/UPS/Rate.php +++ b/src/UPS/Rate.php @@ -5,6 +5,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; @@ -13,10 +14,10 @@ class Rate extends RateAdapter private $urlDev = 'https://wwwcie.ups.com/ups.app/xml/Rate'; private $urlProd = 'https://www.ups.com/ups.app/xml/Rate'; - private $accessKey = 'XXX'; - private $userId = 'XXX'; - private $password = 'XXX'; - private $shipperNumber = 'XXX'; + private $accessKey; + private $userId; + private $password; + private $shipperNumber; public $approvedCodes = [ '03', @@ -94,7 +95,26 @@ class Rate extends RateAdapter $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..91e44bf 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,8 +15,8 @@ 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'; + private $username; + private $password; public $approvedCodes = [ '1', @@ -76,7 +77,20 @@ class Rate extends RateAdapter $this->approvedCodes = Arr::get($options, 'approvedCodes'); $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..ed697f6 --- /dev/null +++ b/src/Validator.php @@ -0,0 +1,10 @@ +