_serviceLocation = ($this->getConfig()->testMode)
? $this->getConfig()->locationTest : $this->getConfig()->location;
return $this;
}
public function getRate()
{
return $this->_getServiceResponse();
}
protected function _getServiceResponse()
{
if (null == $this->_response->getData()) {
if (true === ($errors = $this->_validateRequiredRateFields())) {
try {
$this->_prepareXml();
if (!($serviceResponse = $this->_callService()) instanceof Qs_Exception) {
$this->_response->prepareServiceResponse($serviceResponse);
} else {
$this->_response->setIsSuccess(false)->setData(array(
'errorCode' => self::ERROR_SERVICE_CONNECTION,
'errorText' => sprintf(
$this->_errorMap[self::ERROR_SERVICE_CONNECTION],
$this->getServiceName()
)
));
}
} catch (App_Service_Shipping_Ups_Exception $e) {
$this->_response->setIsSuccess(false)->setData(array(
'errorCode' => $e->getCode(),
'errorText' => $e->getMessage()
));
}
} else {
$this->_response->setIsSuccess(false)->setData(array(
'errorCode' => self::ERROR_VALIDATE_REQUIRED_PARAMS,
'errorText' => implode('
', $errors)
));
}
}
return $this->_response;
}
/**
* Documentation search there http://www.ups.com/content/us/en/resources/techsupport/integrating_apis.html
*
* @return App_Service_Shipping_Ups
*/
protected function _prepareXml()
{
$authWriter = new SimpleXMLElement('');
$authWriter->addChild('AccessLicenseNumber', $this->getConfig()->accessKey);
$authWriter->addChild('UserId', $this->getConfig()->userName);
$authWriter->addChild('Password', $this->getConfig()->password);
$mainWriter = new SimpleXMLElement('');
$requestWriter = $mainWriter->addChild('Request');
$transactionReferenceWriter = $requestWriter->addChild('TransactionReference');
$transactionReferenceWriter->addChild('CustomerContext', 'Rate Request');
$transactionReferenceWriter->addChild('XpciVersion', $this->getConfig()->version);
$requestWriter->addChild('RequestAction', $this->getConfig()->requestAction);
$requestWriter->addChild('RequestOption', $this->getConfig()->requestOption);
$pickupTypeWriter = $mainWriter->addChild('PickupType');
$pickupTypeWriter->addChild('Code', $this->getConfig()->pickupType);
if ($this->getOption('shipperCountry') == self::COUNTRY_CODE_US) {
$customerClassificationWriter = $mainWriter->addChild('CustomerClassification');
$customerClassificationWriter->addChild('Code', $this->getConfig()->classification);
}
$shipmentWriter = $mainWriter->addChild('Shipment');
$shipperWriter = $shipmentWriter->addChild('Shipper');
$shipperAddressWriter = $shipperWriter->addChild('Address');
if ($this->getOption('shipperCity')) {
$shipperAddressWriter->addChild('City', $this->getOption('shipperCity'));
}
$shipperAddressWriter->addChild('StateProvinceCode', $this->getOption('shipperState'));
$shipperAddressWriter->addChild('CountryCode', $this->getOption('shipperCountry'));
$shipperAddressWriter->addChild('PostalCode', $this->getOption('shipperPostalCode'));
$shipToWriter = $shipmentWriter->addChild('ShipTo');
$shipToAddressWriter = $shipToWriter->addChild('Address');
if ($this->getOption('shipToCity')) {
$shipToAddressWriter->addChild('City', $this->getOption('shipToCity'));
}
$shipToAddressWriter->addChild('StateProvinceCode', $this->getOption('shipToState'));
$shipToAddressWriter->addChild('CountryCode', $this->getOption('shipToCountry'));
$shipToAddressWriter->addChild('PostalCode', $this->getOption('shipToPostalCode'));
if ($this->getOption('addressType') == self::ADDRESS_TYPE_RESIDENTIAL) {
$shipToAddressWriter->addChild('ResidentialAddressIndicator');
}
$shipFromWriter = $shipmentWriter->addChild('ShipFrom');
$shipFromAddressWriter = $shipFromWriter->addChild('Address');
if ($this->getOption('shipperCity')) {
$shipFromAddressWriter->addChild('City', $this->getOption('shipperCity'));
}
$shipFromAddressWriter->addChild('StateProvinceCode', $this->getOption('shipperState'));
$shipFromAddressWriter->addChild('CountryCode', $this->getOption('shipperCountry'));
$shipFromAddressWriter->addChild('PostalCode', $this->getOption('shipperPostalCode'));
$service = $shipmentWriter->addChild('Service');
$service->addChild('Code', $this->getOption('serviceCode'));
foreach ($this->_getPackagesInformation() as $packageInformation) {
$packageWriter = $shipmentWriter->addChild('Package');
$packagingTypeWriter = $packageWriter->addChild('PackagingType');
$packagingTypeWriter->addChild('Code', $this->getConfig()->packagingType);
/* Required if Packaging Type in not Letter, Express Tube,
* or Express Box; Required for GB to GB and Poland to Poland shipments
*
*
* $dimensionsWriter = $packageWriter->addChild('Dimensions');
* $unitOfMeasurementWriter = $dimensionsWriter->addChild('UnitOfMeasurement');
*
* $unitOfMeasurementWriter->addChild('Code', $this->getConfig()->unitOfMeasurementCode);
*
* $dimensionsWriter->addChild('Description', $this->getOption('dimensionsDescription'));
*
* //Length of the package used to determine dimensional weight
* $dimensionsWriter->addChild('Length', $this->getOption('dimensionsLength'));
*
* //Width of the package used to determine dimensional weight
* $dimensionsWriter->addChild('Width', $this->getOption('dimensionsWidth'));
*
* //Height of the package used to determine dimensional weight
* $dimensionsWriter->addChild('Height', $this->getOption('dimensionsHeight'));
*
*/
$packageWeightWriter = $packageWriter->addChild('PackageWeight');
$unitOfMeasurementWriter = $packageWeightWriter->addChild('UnitOfMeasurement');
$unitOfMeasurementWriter->addChild('Code', $this->getConfig()->unitOfMeasurementCode);
$packageWeightWriter->addChild('Weight', $this->_prepareWeight($packageInformation['weight']));
if ($this->getConfig()->insurance) {
$packageServiceOptionsWriter = $packageWriter->addChild('PackageServiceOptions');
$insuredValueWriter = $packageServiceOptionsWriter->addChild('InsuredValue');
$insuredValueWriter->addChild('CurrencyCode', $this->getOption('currencyCode'));
$insuredValueWriter->addChild('MonetaryValue', $this->getOption('cartTotal'));
}
}
$this->_xml = $authWriter->asXML() . $mainWriter->asXML();
return $this;
}
protected function _callService()
{
try {
$ch = curl_init($this->_serviceLocation);
curl_setopt($ch, CURLOPT_HEADER, $this->getConfig()->CURLOPT_HEADER);
curl_setopt($ch, CURLOPT_POST, $this->getConfig()->CURLOPT_POST);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->getConfig()->CURLOPT_TIMEOUT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $this->getConfig()->CURLOPT_RETURNTRANSFER);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfig()->CURLOPT_SSL_VERIFYPEER);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_xml);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != self::HTTP_CODE_OK) {
$response = new App_Service_Shipping_Ups_Exception();
}
curl_close($ch);
} catch (Qs_Exception $e) {
return $e;
}
return $response;
}
}