PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Usa/Model/Shipping/Carrier/Ups.php

https://bitbucket.org/jit_bec/shopifine
PHP | 1809 lines | 1345 code | 181 blank | 283 comment | 180 complexity | d250bb3d610ebb5f15f7ea70a3e9baf6 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Usa
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * UPS shipping implementation
  28. *
  29. * @category Mage
  30. * @package Mage_Usa
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Usa_Model_Shipping_Carrier_Ups
  34. extends Mage_Usa_Model_Shipping_Carrier_Abstract
  35. implements Mage_Shipping_Model_Carrier_Interface
  36. {
  37. /**
  38. * Code of the carrier
  39. *
  40. * @var string
  41. */
  42. const CODE = 'ups';
  43. /**
  44. * Delivery Confirmation level based on origin/destination
  45. *
  46. * @var int
  47. */
  48. const DELIVERY_CONFIRMATION_SHIPMENT = 1;
  49. const DELIVERY_CONFIRMATION_PACKAGE = 2;
  50. /**
  51. * Code of the carrier
  52. *
  53. * @var string
  54. */
  55. protected $_code = self::CODE;
  56. /**
  57. * Rate request data
  58. *
  59. * @var Mage_Shipping_Model_Rate_Request|null
  60. */
  61. protected $_request = null;
  62. /**
  63. * Raw rate request data
  64. *
  65. * @var Varien_Object|null
  66. */
  67. protected $_rawRequest = null;
  68. /**
  69. * Rate result data
  70. *
  71. * @var Mage_Shipping_Model_Rate_Result|null
  72. */
  73. protected $_result = null;
  74. /**
  75. * Base currency rate
  76. *
  77. * @var double
  78. */
  79. protected $_baseCurrencyRate;
  80. /**
  81. * Xml access request
  82. *
  83. * @var string
  84. */
  85. protected $_xmlAccessRequest = null;
  86. /**
  87. * Default cgi gateway url
  88. *
  89. * @var string
  90. */
  91. protected $_defaultCgiGatewayUrl = 'http://www.ups.com:80/using/services/rave/qcostcgi.cgi';
  92. /**
  93. * Default urls for shipment
  94. *
  95. * @var array
  96. */
  97. protected $_defaultUrls = array(
  98. 'ShipConfirm' => 'https://wwwcie.ups.com/ups.app/xml/ShipConfirm',
  99. 'ShipAccept' => 'https://wwwcie.ups.com/ups.app/xml/ShipAccept',
  100. );
  101. /**
  102. * Container types that could be customized for UPS carrier
  103. *
  104. * @var array
  105. */
  106. protected $_customizableContainerTypes = array('CP', 'CSP');
  107. /**
  108. * Collect and get rates
  109. *
  110. * @param Mage_Shipping_Model_Rate_Request $request
  111. * @return Mage_Shipping_Model_Rate_Result|bool|null
  112. */
  113. public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  114. {
  115. if (!$this->getConfigFlag($this->_activeFlag)) {
  116. return false;
  117. }
  118. $this->setRequest($request);
  119. $this->_result = $this->_getQuotes();
  120. $this->_updateFreeMethodQuote($request);
  121. return $this->getResult();
  122. }
  123. /**
  124. * Prepare and set request to this instance
  125. *
  126. * @param Mage_Shipping_Model_Rate_Request $request
  127. * @return Mage_Usa_Model_Shipping_Carrier_Ups
  128. */
  129. public function setRequest(Mage_Shipping_Model_Rate_Request $request)
  130. {
  131. $this->_request = $request;
  132. $r = new Varien_Object();
  133. if ($request->getLimitMethod()) {
  134. $r->setAction($this->getCode('action', 'single'));
  135. $r->setProduct($request->getLimitMethod());
  136. } else {
  137. $r->setAction($this->getCode('action', 'all'));
  138. $r->setProduct('GND'.$this->getConfigData('dest_type'));
  139. }
  140. if ($request->getUpsPickup()) {
  141. $pickup = $request->getUpsPickup();
  142. } else {
  143. $pickup = $this->getConfigData('pickup');
  144. }
  145. $r->setPickup($this->getCode('pickup', $pickup));
  146. if ($request->getUpsContainer()) {
  147. $container = $request->getUpsContainer();
  148. } else {
  149. $container = $this->getConfigData('container');
  150. }
  151. $r->setContainer($this->getCode('container', $container));
  152. if ($request->getUpsDestType()) {
  153. $destType = $request->getUpsDestType();
  154. } else {
  155. $destType = $this->getConfigData('dest_type');
  156. }
  157. $r->setDestType($this->getCode('dest_type', $destType));
  158. if ($request->getOrigCountry()) {
  159. $origCountry = $request->getOrigCountry();
  160. } else {
  161. $origCountry = Mage::getStoreConfig(
  162. Mage_Shipping_Model_Shipping::XML_PATH_STORE_COUNTRY_ID,
  163. $request->getStoreId()
  164. );
  165. }
  166. $r->setOrigCountry(Mage::getModel('directory/country')->load($origCountry)->getIso2Code());
  167. if ($request->getOrigRegionCode()) {
  168. $origRegionCode = $request->getOrigRegionCode();
  169. } else {
  170. $origRegionCode = Mage::getStoreConfig(
  171. Mage_Shipping_Model_Shipping::XML_PATH_STORE_REGION_ID,
  172. $request->getStoreId()
  173. );
  174. }
  175. if (is_numeric($origRegionCode)) {
  176. $origRegionCode = Mage::getModel('directory/region')->load($origRegionCode)->getCode();
  177. }
  178. $r->setOrigRegionCode($origRegionCode);
  179. if ($request->getOrigPostcode()) {
  180. $r->setOrigPostal($request->getOrigPostcode());
  181. } else {
  182. $r->setOrigPostal(Mage::getStoreConfig(
  183. Mage_Shipping_Model_Shipping::XML_PATH_STORE_ZIP,
  184. $request->getStoreId()
  185. ));
  186. }
  187. if ($request->getOrigCity()) {
  188. $r->setOrigCity($request->getOrigCity());
  189. } else {
  190. $r->setOrigCity(Mage::getStoreConfig(
  191. Mage_Shipping_Model_Shipping::XML_PATH_STORE_CITY,
  192. $request->getStoreId()
  193. ));
  194. }
  195. if ($request->getDestCountryId()) {
  196. $destCountry = $request->getDestCountryId();
  197. } else {
  198. $destCountry = self::USA_COUNTRY_ID;
  199. }
  200. //for UPS, puero rico state for US will assume as puerto rico country
  201. if ($destCountry == self::USA_COUNTRY_ID
  202. && ($request->getDestPostcode()=='00912' || $request->getDestRegionCode()==self::PUERTORICO_COUNTRY_ID)
  203. ) {
  204. $destCountry = self::PUERTORICO_COUNTRY_ID;
  205. }
  206. // For UPS, Guam state of the USA will be represented by Guam country
  207. if ($destCountry == self::USA_COUNTRY_ID && $request->getDestRegionCode() == self::GUAM_REGION_CODE) {
  208. $destCountry = self::GUAM_COUNTRY_ID;
  209. }
  210. $r->setDestCountry(Mage::getModel('directory/country')->load($destCountry)->getIso2Code());
  211. $r->setDestRegionCode($request->getDestRegionCode());
  212. if ($request->getDestPostcode()) {
  213. $r->setDestPostal($request->getDestPostcode());
  214. } else {
  215. }
  216. $weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
  217. $weight = $this->_getCorrectWeight($weight);
  218. $r->setWeight($weight);
  219. if ($request->getFreeMethodWeight()!=$request->getPackageWeight()) {
  220. $r->setFreeMethodWeight($request->getFreeMethodWeight());
  221. }
  222. $r->setValue($request->getPackageValue());
  223. $r->setValueWithDiscount($request->getPackageValueWithDiscount());
  224. if ($request->getUpsUnitMeasure()) {
  225. $unit = $request->getUpsUnitMeasure();
  226. } else {
  227. $unit = $this->getConfigData('unit_of_measure');
  228. }
  229. $r->setUnitMeasure($unit);
  230. $r->setIsReturn($request->getIsReturn());
  231. $r->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax());
  232. $this->_rawRequest = $r;
  233. return $this;
  234. }
  235. /**
  236. * Get correct weigt.
  237. *
  238. * Namely:
  239. * Checks the current weight to comply with the minimum weight standards set by the carrier.
  240. * Then strictly rounds the weight up until the first significant digit after the decimal point.
  241. *
  242. * @param float|integer|double $weight
  243. * @return float
  244. */
  245. protected function _getCorrectWeight($weight)
  246. {
  247. $minWeight = $this->getConfigData('min_package_weight');
  248. if($weight < $minWeight){
  249. $weight = $minWeight;
  250. }
  251. //rounds a number to one significant figure
  252. $weight = ceil($weight*10) / 10;
  253. return $weight;
  254. }
  255. /**
  256. * Get result of request
  257. *
  258. * @return mixed
  259. */
  260. public function getResult()
  261. {
  262. return $this->_result;
  263. }
  264. /**
  265. * Do remote request for and handle errors
  266. *
  267. * @return Mage_Shipping_Model_Rate_Result
  268. */
  269. protected function _getQuotes()
  270. {
  271. switch ($this->getConfigData('type')) {
  272. case 'UPS':
  273. return $this->_getCgiQuotes();
  274. case 'UPS_XML':
  275. return $this->_getXmlQuotes();
  276. }
  277. return null;
  278. }
  279. /**
  280. * Set free method request
  281. *
  282. * @param string $freeMethod
  283. * @return null
  284. */
  285. protected function _setFreeMethodRequest($freeMethod)
  286. {
  287. $r = $this->_rawRequest;
  288. $weight = $this->getTotalNumOfBoxes($r->getFreeMethodWeight());
  289. $weight = $this->_getCorrectWeight($weight);
  290. $r->setWeight($weight);
  291. $r->setAction($this->getCode('action', 'single'));
  292. $r->setProduct($freeMethod);
  293. }
  294. /**
  295. * Get cgi rates
  296. *
  297. * @return Mage_Shipping_Model_Rate_Result
  298. */
  299. protected function _getCgiQuotes()
  300. {
  301. $r = $this->_rawRequest;
  302. $params = array(
  303. 'accept_UPS_license_agreement' => 'yes',
  304. '10_action' => $r->getAction(),
  305. '13_product' => $r->getProduct(),
  306. '14_origCountry' => $r->getOrigCountry(),
  307. '15_origPostal' => $r->getOrigPostal(),
  308. 'origCity' => $r->getOrigCity(),
  309. '19_destPostal' => Mage_Usa_Model_Shipping_Carrier_Abstract::USA_COUNTRY_ID == $r->getDestCountry() ?
  310. substr($r->getDestPostal(), 0, 5) :
  311. $r->getDestPostal(),
  312. '22_destCountry' => $r->getDestCountry(),
  313. '23_weight' => $r->getWeight(),
  314. '47_rate_chart' => $r->getPickup(),
  315. '48_container' => $r->getContainer(),
  316. '49_residential' => $r->getDestType(),
  317. 'weight_std' => strtolower($r->getUnitMeasure()),
  318. );
  319. $params['47_rate_chart'] = $params['47_rate_chart']['label'];
  320. $responseBody = $this->_getCachedQuotes($params);
  321. if ($responseBody === null) {
  322. $debugData = array('request' => $params);
  323. try {
  324. $url = $this->getConfigData('gateway_url');
  325. if (!$url) {
  326. $url = $this->_defaultCgiGatewayUrl;
  327. }
  328. $client = new Zend_Http_Client();
  329. $client->setUri($url);
  330. $client->setConfig(array('maxredirects'=>0, 'timeout'=>30));
  331. $client->setParameterGet($params);
  332. $response = $client->request();
  333. $responseBody = $response->getBody();
  334. $debugData['result'] = $responseBody;
  335. $this->_setCachedQuotes($params, $responseBody);
  336. }
  337. catch (Exception $e) {
  338. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  339. $responseBody = '';
  340. }
  341. $this->_debug($debugData);
  342. }
  343. return $this->_parseCgiResponse($responseBody);
  344. }
  345. /**
  346. * Get shipment by code
  347. *
  348. * @param string $code
  349. * @param string $origin
  350. * @return array|bool
  351. */
  352. public function getShipmentByCode($code, $origin = null){
  353. if($origin===null){
  354. $origin = $this->getConfigData('origin_shipment');
  355. }
  356. $arr = $this->getCode('originShipment',$origin);
  357. if(isset($arr[$code]))
  358. return $arr[$code];
  359. else
  360. return false;
  361. }
  362. /**
  363. * Prepare shipping rate result based on response
  364. *
  365. * @param mixed $response
  366. * @return Mage_Shipping_Model_Rate_Result
  367. */
  368. protected function _parseCgiResponse($response)
  369. {
  370. $costArr = array();
  371. $priceArr = array();
  372. $errorTitle = Mage::helper('usa')->__('Unknown error');
  373. if (strlen(trim($response))>0) {
  374. $rRows = explode("\n", $response);
  375. $allowedMethods = explode(",", $this->getConfigData('allowed_methods'));
  376. foreach ($rRows as $rRow) {
  377. $r = explode('%', $rRow);
  378. switch (substr($r[0],-1)) {
  379. case 3: case 4:
  380. if (in_array($r[1], $allowedMethods)) {
  381. $responsePrice = Mage::app()->getLocale()->getNumber($r[8]);
  382. $costArr[$r[1]] = $responsePrice;
  383. $priceArr[$r[1]] = $this->getMethodPrice($responsePrice, $r[1]);
  384. }
  385. break;
  386. case 5:
  387. $errorTitle = $r[1];
  388. break;
  389. case 6:
  390. if (in_array($r[3], $allowedMethods)) {
  391. $responsePrice = Mage::app()->getLocale()->getNumber($r[10]);
  392. $costArr[$r[3]] = $responsePrice;
  393. $priceArr[$r[3]] = $this->getMethodPrice($responsePrice, $r[3]);
  394. }
  395. break;
  396. }
  397. }
  398. asort($priceArr);
  399. }
  400. $result = Mage::getModel('shipping/rate_result');
  401. $defaults = $this->getDefaults();
  402. if (empty($priceArr)) {
  403. $error = Mage::getModel('shipping/rate_result_error');
  404. $error->setCarrier('ups');
  405. $error->setCarrierTitle($this->getConfigData('title'));
  406. $error->setErrorMessage($this->getConfigData('specificerrmsg'));
  407. $result->append($error);
  408. } else {
  409. foreach ($priceArr as $method=>$price) {
  410. $rate = Mage::getModel('shipping/rate_result_method');
  411. $rate->setCarrier('ups');
  412. $rate->setCarrierTitle($this->getConfigData('title'));
  413. $rate->setMethod($method);
  414. $method_arr = $this->getCode('method', $method);
  415. $rate->setMethodTitle(Mage::helper('usa')->__($method_arr));
  416. $rate->setCost($costArr[$method]);
  417. $rate->setPrice($price);
  418. $result->append($rate);
  419. }
  420. }
  421. return $result;
  422. }
  423. /**
  424. * Get configuration data of carrier
  425. *
  426. * @param string $type
  427. * @param string $code
  428. * @return array|bool
  429. */
  430. public function getCode($type, $code='')
  431. {
  432. $codes = array(
  433. 'action'=>array(
  434. 'single'=>'3',
  435. 'all'=>'4',
  436. ),
  437. 'originShipment'=>array(
  438. // United States Domestic Shipments
  439. 'United States Domestic Shipments' => array(
  440. '01' => Mage::helper('usa')->__('UPS Next Day Air'),
  441. '02' => Mage::helper('usa')->__('UPS Second Day Air'),
  442. '03' => Mage::helper('usa')->__('UPS Ground'),
  443. '07' => Mage::helper('usa')->__('UPS Worldwide Express'),
  444. '08' => Mage::helper('usa')->__('UPS Worldwide Expedited'),
  445. '11' => Mage::helper('usa')->__('UPS Standard'),
  446. '12' => Mage::helper('usa')->__('UPS Three-Day Select'),
  447. '13' => Mage::helper('usa')->__('UPS Next Day Air Saver'),
  448. '14' => Mage::helper('usa')->__('UPS Next Day Air Early A.M.'),
  449. '54' => Mage::helper('usa')->__('UPS Worldwide Express Plus'),
  450. '59' => Mage::helper('usa')->__('UPS Second Day Air A.M.'),
  451. '65' => Mage::helper('usa')->__('UPS Saver'),
  452. ),
  453. // Shipments Originating in United States
  454. 'Shipments Originating in United States' => array(
  455. '01' => Mage::helper('usa')->__('UPS Next Day Air'),
  456. '02' => Mage::helper('usa')->__('UPS Second Day Air'),
  457. '03' => Mage::helper('usa')->__('UPS Ground'),
  458. '07' => Mage::helper('usa')->__('UPS Worldwide Express'),
  459. '08' => Mage::helper('usa')->__('UPS Worldwide Expedited'),
  460. '11' => Mage::helper('usa')->__('UPS Standard'),
  461. '12' => Mage::helper('usa')->__('UPS Three-Day Select'),
  462. '14' => Mage::helper('usa')->__('UPS Next Day Air Early A.M.'),
  463. '54' => Mage::helper('usa')->__('UPS Worldwide Express Plus'),
  464. '59' => Mage::helper('usa')->__('UPS Second Day Air A.M.'),
  465. '65' => Mage::helper('usa')->__('UPS Worldwide Saver'),
  466. ),
  467. // Shipments Originating in Canada
  468. 'Shipments Originating in Canada' => array(
  469. '01' => Mage::helper('usa')->__('UPS Express'),
  470. '02' => Mage::helper('usa')->__('UPS Expedited'),
  471. '07' => Mage::helper('usa')->__('UPS Worldwide Express'),
  472. '08' => Mage::helper('usa')->__('UPS Worldwide Expedited'),
  473. '11' => Mage::helper('usa')->__('UPS Standard'),
  474. '12' => Mage::helper('usa')->__('UPS Three-Day Select'),
  475. '14' => Mage::helper('usa')->__('UPS Express Early A.M.'),
  476. '65' => Mage::helper('usa')->__('UPS Saver'),
  477. ),
  478. // Shipments Originating in the European Union
  479. 'Shipments Originating in the European Union' => array(
  480. '07' => Mage::helper('usa')->__('UPS Express'),
  481. '08' => Mage::helper('usa')->__('UPS Expedited'),
  482. '11' => Mage::helper('usa')->__('UPS Standard'),
  483. '54' => Mage::helper('usa')->__('UPS Worldwide Express PlusSM'),
  484. '65' => Mage::helper('usa')->__('UPS Saver'),
  485. ),
  486. // Polish Domestic Shipments
  487. 'Polish Domestic Shipments' => array(
  488. '07' => Mage::helper('usa')->__('UPS Express'),
  489. '08' => Mage::helper('usa')->__('UPS Expedited'),
  490. '11' => Mage::helper('usa')->__('UPS Standard'),
  491. '54' => Mage::helper('usa')->__('UPS Worldwide Express Plus'),
  492. '65' => Mage::helper('usa')->__('UPS Saver'),
  493. '82' => Mage::helper('usa')->__('UPS Today Standard'),
  494. '83' => Mage::helper('usa')->__('UPS Today Dedicated Courrier'),
  495. '84' => Mage::helper('usa')->__('UPS Today Intercity'),
  496. '85' => Mage::helper('usa')->__('UPS Today Express'),
  497. '86' => Mage::helper('usa')->__('UPS Today Express Saver'),
  498. ),
  499. // Puerto Rico Origin
  500. 'Puerto Rico Origin' => array(
  501. '01' => Mage::helper('usa')->__('UPS Next Day Air'),
  502. '02' => Mage::helper('usa')->__('UPS Second Day Air'),
  503. '03' => Mage::helper('usa')->__('UPS Ground'),
  504. '07' => Mage::helper('usa')->__('UPS Worldwide Express'),
  505. '08' => Mage::helper('usa')->__('UPS Worldwide Expedited'),
  506. '14' => Mage::helper('usa')->__('UPS Next Day Air Early A.M.'),
  507. '54' => Mage::helper('usa')->__('UPS Worldwide Express Plus'),
  508. '65' => Mage::helper('usa')->__('UPS Saver'),
  509. ),
  510. // Shipments Originating in Mexico
  511. 'Shipments Originating in Mexico' => array(
  512. '07' => Mage::helper('usa')->__('UPS Express'),
  513. '08' => Mage::helper('usa')->__('UPS Expedited'),
  514. '54' => Mage::helper('usa')->__('UPS Express Plus'),
  515. '65' => Mage::helper('usa')->__('UPS Saver'),
  516. ),
  517. // Shipments Originating in Other Countries
  518. 'Shipments Originating in Other Countries' => array(
  519. '07' => Mage::helper('usa')->__('UPS Express'),
  520. '08' => Mage::helper('usa')->__('UPS Worldwide Expedited'),
  521. '11' => Mage::helper('usa')->__('UPS Standard'),
  522. '54' => Mage::helper('usa')->__('UPS Worldwide Express Plus'),
  523. '65' => Mage::helper('usa')->__('UPS Saver')
  524. )
  525. ),
  526. 'method'=>array(
  527. '1DM' => Mage::helper('usa')->__('Next Day Air Early AM'),
  528. '1DML' => Mage::helper('usa')->__('Next Day Air Early AM Letter'),
  529. '1DA' => Mage::helper('usa')->__('Next Day Air'),
  530. '1DAL' => Mage::helper('usa')->__('Next Day Air Letter'),
  531. '1DAPI' => Mage::helper('usa')->__('Next Day Air Intra (Puerto Rico)'),
  532. '1DP' => Mage::helper('usa')->__('Next Day Air Saver'),
  533. '1DPL' => Mage::helper('usa')->__('Next Day Air Saver Letter'),
  534. '2DM' => Mage::helper('usa')->__('2nd Day Air AM'),
  535. '2DML' => Mage::helper('usa')->__('2nd Day Air AM Letter'),
  536. '2DA' => Mage::helper('usa')->__('2nd Day Air'),
  537. '2DAL' => Mage::helper('usa')->__('2nd Day Air Letter'),
  538. '3DS' => Mage::helper('usa')->__('3 Day Select'),
  539. 'GND' => Mage::helper('usa')->__('Ground'),
  540. 'GNDCOM' => Mage::helper('usa')->__('Ground Commercial'),
  541. 'GNDRES' => Mage::helper('usa')->__('Ground Residential'),
  542. 'STD' => Mage::helper('usa')->__('Canada Standard'),
  543. 'XPR' => Mage::helper('usa')->__('Worldwide Express'),
  544. 'WXS' => Mage::helper('usa')->__('Worldwide Express Saver'),
  545. 'XPRL' => Mage::helper('usa')->__('Worldwide Express Letter'),
  546. 'XDM' => Mage::helper('usa')->__('Worldwide Express Plus'),
  547. 'XDML' => Mage::helper('usa')->__('Worldwide Express Plus Letter'),
  548. 'XPD' => Mage::helper('usa')->__('Worldwide Expedited'),
  549. ),
  550. 'pickup'=>array(
  551. 'RDP' => array("label"=>'Regular Daily Pickup',"code"=>"01"),
  552. 'OCA' => array("label"=>'On Call Air',"code"=>"07"),
  553. 'OTP' => array("label"=>'One Time Pickup',"code"=>"06"),
  554. 'LC' => array("label"=>'Letter Center',"code"=>"19"),
  555. 'CC' => array("label"=>'Customer Counter',"code"=>"03"),
  556. ),
  557. 'container'=>array(
  558. 'CP' => '00', // Customer Packaging
  559. 'ULE' => '01', // UPS Letter Envelope
  560. 'CSP' => '02', // Customer Supplied Package
  561. 'UT' => '03', // UPS Tube
  562. 'PAK' => '04', // PAK
  563. 'UEB' => '21', // UPS Express Box
  564. 'UW25' => '24', // UPS Worldwide 25 kilo
  565. 'UW10' => '25', // UPS Worldwide 10 kilo
  566. 'PLT' => '30', // Pallet
  567. 'SEB' => '2a', // Small Express Box
  568. 'MEB' => '2b', // Medium Express Box
  569. 'LEB' => '2c', // Large Express Box
  570. ),
  571. 'container_description'=>array(
  572. 'CP' => Mage::helper('usa')->__('Customer Packaging'),
  573. 'ULE' => Mage::helper('usa')->__('UPS Letter Envelope'),
  574. 'CSP' => Mage::helper('usa')->__('Customer Supplied Package'),
  575. 'UT' => Mage::helper('usa')->__('UPS Tube'),
  576. 'PAK' => Mage::helper('usa')->__('PAK'),
  577. 'UEB' => Mage::helper('usa')->__('UPS Express Box'),
  578. 'UW25' => Mage::helper('usa')->__('UPS Worldwide 25 kilo'),
  579. 'UW10' => Mage::helper('usa')->__('UPS Worldwide 10 kilo'),
  580. 'PLT' => Mage::helper('usa')->__('Pallet'),
  581. 'SEB' => Mage::helper('usa')->__('Small Express Box'),
  582. 'MEB' => Mage::helper('usa')->__('Medium Express Box'),
  583. 'LEB' => Mage::helper('usa')->__('Large Express Box'),
  584. ),
  585. 'dest_type'=>array(
  586. 'RES' => '01', // Residential
  587. 'COM' => '02', // Commercial
  588. ),
  589. 'dest_type_description'=>array(
  590. 'RES' => Mage::helper('usa')->__('Residential'),
  591. 'COM' => Mage::helper('usa')->__('Commercial'),
  592. ),
  593. 'unit_of_measure'=>array(
  594. 'LBS' => Mage::helper('usa')->__('Pounds'),
  595. 'KGS' => Mage::helper('usa')->__('Kilograms'),
  596. ),
  597. 'containers_filter' => array(
  598. array(
  599. 'containers' => array('00'), // Customer Packaging
  600. 'filters' => array(
  601. 'within_us' => array(
  602. 'method' => array(
  603. '01', // Next Day Air
  604. '13', // Next Day Air Saver
  605. '12', // 3 Day Select
  606. '59', // 2nd Day Air AM
  607. '03', // Ground
  608. '14', // Next Day Air Early AM
  609. '02', // 2nd Day Air
  610. )
  611. ),
  612. 'from_us' => array(
  613. 'method' => array(
  614. '07', // Worldwide Express
  615. '54', // Worldwide Express Plus
  616. '08', // Worldwide Expedited
  617. '65', // Worldwide Saver
  618. '11', // Standard
  619. )
  620. )
  621. )
  622. ),
  623. array(
  624. // Small Express Box, Medium Express Box, Large Express Box, UPS Tube
  625. 'containers' => array('2a', '2b', '2c', '03'),
  626. 'filters' => array(
  627. 'within_us' => array(
  628. 'method' => array(
  629. '01', // Next Day Air
  630. '13', // Next Day Air Saver
  631. '14', // Next Day Air Early AM
  632. '02', // 2nd Day Air
  633. '59', // 2nd Day Air AM
  634. '13', // Next Day Air Saver
  635. )
  636. ),
  637. 'from_us' => array(
  638. 'method' => array(
  639. '07', // Worldwide Express
  640. '54', // Worldwide Express Plus
  641. '08', // Worldwide Expedited
  642. '65', // Worldwide Saver
  643. )
  644. )
  645. )
  646. ),
  647. array(
  648. 'containers' => array('24', '25'), // UPS Worldwide 25 kilo, UPS Worldwide 10 kilo
  649. 'filters' => array(
  650. 'within_us' => array(
  651. 'method' => array()
  652. ),
  653. 'from_us' => array(
  654. 'method' => array(
  655. '07', // Worldwide Express
  656. '54', // Worldwide Express Plus
  657. '65', // Worldwide Saver
  658. )
  659. )
  660. )
  661. ),
  662. array(
  663. 'containers' => array('01', '04'), // UPS Letter, UPS PAK
  664. 'filters' => array(
  665. 'within_us' => array(
  666. 'method' => array(
  667. '01', // Next Day Air
  668. '14', // Next Day Air Early AM
  669. '02', // 2nd Day Air
  670. '59', // 2nd Day Air AM
  671. '13', // Next Day Air Saver
  672. )
  673. ),
  674. 'from_us' => array(
  675. 'method' => array(
  676. '07', // Worldwide Express
  677. '54', // Worldwide Express Plus
  678. '65', // Worldwide Saver
  679. )
  680. )
  681. )
  682. ),
  683. array(
  684. 'containers' => array('04'), // UPS PAK
  685. 'filters' => array(
  686. 'within_us' => array(
  687. 'method' => array()
  688. ),
  689. 'from_us' => array(
  690. 'method' => array(
  691. '08', // Worldwide Expedited
  692. )
  693. )
  694. )
  695. ),
  696. )
  697. );
  698. if (!isset($codes[$type])) {
  699. return false;
  700. } elseif (''===$code) {
  701. return $codes[$type];
  702. }
  703. if (!isset($codes[$type][$code])) {
  704. return false;
  705. } else {
  706. return $codes[$type][$code];
  707. }
  708. }
  709. /**
  710. * Get xml rates
  711. *
  712. * @return Mage_Shipping_Model_Rate_Result
  713. */
  714. protected function _getXmlQuotes()
  715. {
  716. $url = $this->getConfigData('gateway_xml_url');
  717. $this->setXMLAccessRequest();
  718. $xmlRequest=$this->_xmlAccessRequest;
  719. $r = $this->_rawRequest;
  720. $params = array(
  721. 'accept_UPS_license_agreement' => 'yes',
  722. '10_action' => $r->getAction(),
  723. '13_product' => $r->getProduct(),
  724. '14_origCountry' => $r->getOrigCountry(),
  725. '15_origPostal' => $r->getOrigPostal(),
  726. 'origCity' => $r->getOrigCity(),
  727. 'origRegionCode' => $r->getOrigRegionCode(),
  728. '19_destPostal' => Mage_Usa_Model_Shipping_Carrier_Abstract::USA_COUNTRY_ID == $r->getDestCountry() ?
  729. substr($r->getDestPostal(), 0, 5) :
  730. $r->getDestPostal(),
  731. '22_destCountry' => $r->getDestCountry(),
  732. 'destRegionCode' => $r->getDestRegionCode(),
  733. '23_weight' => $r->getWeight(),
  734. '47_rate_chart' => $r->getPickup(),
  735. '48_container' => $r->getContainer(),
  736. '49_residential' => $r->getDestType(),
  737. );
  738. if ($params['10_action'] == '4') {
  739. $params['10_action'] = 'Shop';
  740. $serviceCode = null; // Service code is not relevant when we're asking ALL possible services' rates
  741. } else {
  742. $params['10_action'] = 'Rate';
  743. $serviceCode = $r->getProduct() ? $r->getProduct() : '';
  744. }
  745. $serviceDescription = $serviceCode ? $this->getShipmentByCode($serviceCode) : '';
  746. $xmlRequest .= <<< XMLRequest
  747. <?xml version="1.0"?>
  748. <RatingServiceSelectionRequest xml:lang="en-US">
  749. <Request>
  750. <TransactionReference>
  751. <CustomerContext>Rating and Service</CustomerContext>
  752. <XpciVersion>1.0</XpciVersion>
  753. </TransactionReference>
  754. <RequestAction>Rate</RequestAction>
  755. <RequestOption>{$params['10_action']}</RequestOption>
  756. </Request>
  757. <PickupType>
  758. <Code>{$params['47_rate_chart']['code']}</Code>
  759. <Description>{$params['47_rate_chart']['label']}</Description>
  760. </PickupType>
  761. <Shipment>
  762. XMLRequest;
  763. if ($serviceCode !== null) {
  764. $xmlRequest .= "<Service>" .
  765. "<Code>{$serviceCode}</Code>" .
  766. "<Description>{$serviceDescription}</Description>" .
  767. "</Service>";
  768. }
  769. $xmlRequest .= <<< XMLRequest
  770. <Shipper>
  771. XMLRequest;
  772. if ($this->getConfigFlag('negotiated_active') && ($shipper = $this->getConfigData('shipper_number')) ) {
  773. $xmlRequest .= "<ShipperNumber>{$shipper}</ShipperNumber>";
  774. }
  775. if ($r->getIsReturn()) {
  776. $shipperCity = '';
  777. $shipperPostalCode = $params['19_destPostal'];
  778. $shipperCountryCode = $params['22_destCountry'];
  779. $shipperStateProvince = $params['destRegionCode'];
  780. } else {
  781. $shipperCity = $params['origCity'];
  782. $shipperPostalCode = $params['15_origPostal'];
  783. $shipperCountryCode = $params['14_origCountry'];
  784. $shipperStateProvince = $params['origRegionCode'];
  785. }
  786. $xmlRequest .= <<< XMLRequest
  787. <Address>
  788. <City>{$shipperCity}</City>
  789. <PostalCode>{$shipperPostalCode}</PostalCode>
  790. <CountryCode>{$shipperCountryCode}</CountryCode>
  791. <StateProvinceCode>{$shipperStateProvince}</StateProvinceCode>
  792. </Address>
  793. </Shipper>
  794. <ShipTo>
  795. <Address>
  796. <PostalCode>{$params['19_destPostal']}</PostalCode>
  797. <CountryCode>{$params['22_destCountry']}</CountryCode>
  798. <ResidentialAddress>{$params['49_residential']}</ResidentialAddress>
  799. <StateProvinceCode>{$params['destRegionCode']}</StateProvinceCode>
  800. XMLRequest;
  801. $xmlRequest .= ($params['49_residential']==='01'
  802. ? "<ResidentialAddressIndicator>{$params['49_residential']}</ResidentialAddressIndicator>"
  803. : ''
  804. );
  805. $xmlRequest .= <<< XMLRequest
  806. </Address>
  807. </ShipTo>
  808. <ShipFrom>
  809. <Address>
  810. <PostalCode>{$params['15_origPostal']}</PostalCode>
  811. <CountryCode>{$params['14_origCountry']}</CountryCode>
  812. <StateProvinceCode>{$params['origRegionCode']}</StateProvinceCode>
  813. </Address>
  814. </ShipFrom>
  815. <Package>
  816. <PackagingType><Code>{$params['48_container']}</Code></PackagingType>
  817. <PackageWeight>
  818. <UnitOfMeasurement><Code>{$r->getUnitMeasure()}</Code></UnitOfMeasurement>
  819. <Weight>{$params['23_weight']}</Weight>
  820. </PackageWeight>
  821. </Package>
  822. XMLRequest;
  823. if ($this->getConfigFlag('negotiated_active')) {
  824. $xmlRequest .= "<RateInformation><NegotiatedRatesIndicator/></RateInformation>";
  825. }
  826. $xmlRequest .= <<< XMLRequest
  827. </Shipment>
  828. </RatingServiceSelectionRequest>
  829. XMLRequest;
  830. $xmlResponse = $this->_getCachedQuotes($xmlRequest);
  831. if ($xmlResponse === null) {
  832. $debugData = array('request' => $xmlRequest);
  833. try {
  834. $ch = curl_init();
  835. curl_setopt($ch, CURLOPT_URL, $url);
  836. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  837. curl_setopt($ch, CURLOPT_HEADER, 0);
  838. curl_setopt($ch, CURLOPT_POST, 1);
  839. curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
  840. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  841. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (boolean)$this->getConfigFlag('mode_xml'));
  842. $xmlResponse = curl_exec ($ch);
  843. $debugData['result'] = $xmlResponse;
  844. $this->_setCachedQuotes($xmlRequest, $xmlResponse);
  845. }
  846. catch (Exception $e) {
  847. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  848. $xmlResponse = '';
  849. }
  850. $this->_debug($debugData);
  851. }
  852. return $this->_parseXmlResponse($xmlResponse);
  853. }
  854. /**
  855. * Get base currency rate
  856. *
  857. * @param string $code
  858. * @return double
  859. */
  860. protected function _getBaseCurrencyRate($code)
  861. {
  862. if (!$this->_baseCurrencyRate) {
  863. $this->_baseCurrencyRate = Mage::getModel('directory/currency')
  864. ->load($code)
  865. ->getAnyRate($this->_request->getBaseCurrency()->getCode());
  866. }
  867. return $this->_baseCurrencyRate;
  868. }
  869. /**
  870. * Prepare shipping rate result based on response
  871. *
  872. * @param mixed $response
  873. * @return Mage_Shipping_Model_Rate_Result
  874. */
  875. protected function _parseXmlResponse($xmlResponse)
  876. {
  877. $costArr = array();
  878. $priceArr = array();
  879. if (strlen(trim($xmlResponse))>0) {
  880. $xml = new Varien_Simplexml_Config();
  881. $xml->loadString($xmlResponse);
  882. $arr = $xml->getXpath("//RatingServiceSelectionResponse/Response/ResponseStatusCode/text()");
  883. $success = (int)$arr[0];
  884. if ($success===1) {
  885. $arr = $xml->getXpath("//RatingServiceSelectionResponse/RatedShipment");
  886. $allowedMethods = explode(",", $this->getConfigData('allowed_methods'));
  887. // Negotiated rates
  888. $negotiatedArr = $xml->getXpath("//RatingServiceSelectionResponse/RatedShipment/NegotiatedRates");
  889. $negotiatedActive = $this->getConfigFlag('negotiated_active')
  890. && $this->getConfigData('shipper_number')
  891. && !empty($negotiatedArr);
  892. $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
  893. foreach ($arr as $shipElement){
  894. $code = (string)$shipElement->Service->Code;
  895. if (in_array($code, $allowedMethods)) {
  896. if ($negotiatedActive) {
  897. $cost = $shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue;
  898. } else {
  899. $cost = $shipElement->TotalCharges->MonetaryValue;
  900. }
  901. //convert price with Origin country currency code to base currency code
  902. $successConversion = true;
  903. $responseCurrencyCode = (string) $shipElement->TotalCharges->CurrencyCode;
  904. if ($responseCurrencyCode) {
  905. if (in_array($responseCurrencyCode, $allowedCurrencies)) {
  906. $cost = (float) $cost * $this->_getBaseCurrencyRate($responseCurrencyCode);
  907. } else {
  908. $errorTitle = Mage::helper('directory')->__('Can\'t convert rate from "%s-%s".', $responseCurrencyCode, $this->_request->getPackageCurrency()->getCode());
  909. $error = Mage::getModel('shipping/rate_result_error');
  910. $error->setCarrier('ups');
  911. $error->setCarrierTitle($this->getConfigData('title'));
  912. $error->setErrorMessage($errorTitle);
  913. $successConversion = false;
  914. }
  915. }
  916. if ($successConversion) {
  917. $costArr[$code] = $cost;
  918. $priceArr[$code] = $this->getMethodPrice(floatval($cost),$code);
  919. }
  920. }
  921. }
  922. } else {
  923. $arr = $xml->getXpath("//RatingServiceSelectionResponse/Response/Error/ErrorDescription/text()");
  924. $errorTitle = (string)$arr[0][0];
  925. $error = Mage::getModel('shipping/rate_result_error');
  926. $error->setCarrier('ups');
  927. $error->setCarrierTitle($this->getConfigData('title'));
  928. $error->setErrorMessage($this->getConfigData('specificerrmsg'));
  929. }
  930. }
  931. $result = Mage::getModel('shipping/rate_result');
  932. $defaults = $this->getDefaults();
  933. if (empty($priceArr)) {
  934. $error = Mage::getModel('shipping/rate_result_error');
  935. $error->setCarrier('ups');
  936. $error->setCarrierTitle($this->getConfigData('title'));
  937. if(!isset($errorTitle)){
  938. $errorTitle = Mage::helper('usa')->__('Cannot retrieve shipping rates');
  939. }
  940. $error->setErrorMessage($this->getConfigData('specificerrmsg'));
  941. $result->append($error);
  942. } else {
  943. foreach ($priceArr as $method=>$price) {
  944. $rate = Mage::getModel('shipping/rate_result_method');
  945. $rate->setCarrier('ups');
  946. $rate->setCarrierTitle($this->getConfigData('title'));
  947. $rate->setMethod($method);
  948. $method_arr = $this->getShipmentByCode($method);
  949. $rate->setMethodTitle($method_arr);
  950. $rate->setCost($costArr[$method]);
  951. $rate->setPrice($price);
  952. $result->append($rate);
  953. }
  954. }
  955. return $result;
  956. }
  957. /**
  958. * Get tracking
  959. *
  960. * @param mixed $trackings
  961. * @return mixed
  962. */
  963. public function getTracking($trackings)
  964. {
  965. $return = array();
  966. if (!is_array($trackings)) {
  967. $trackings = array($trackings);
  968. }
  969. if ($this->getConfigData('type')=='UPS') {
  970. $this->_getCgiTracking($trackings);
  971. } elseif ($this->getConfigData('type')=='UPS_XML'){
  972. $this->setXMLAccessRequest();
  973. $this->_getXmlTracking($trackings);
  974. }
  975. return $this->_result;
  976. }
  977. /**
  978. * Set xml access request
  979. *
  980. * @return null
  981. */
  982. protected function setXMLAccessRequest()
  983. {
  984. $userid = $this->getConfigData('username');
  985. $userid_pass = $this->getConfigData('password');
  986. $access_key = $this->getConfigData('access_license_number');
  987. $this->_xmlAccessRequest = <<<XMLAuth
  988. <?xml version="1.0"?>
  989. <AccessRequest xml:lang="en-US">
  990. <AccessLicenseNumber>$access_key</AccessLicenseNumber>
  991. <UserId>$userid</UserId>
  992. <Password>$userid_pass</Password>
  993. </AccessRequest>
  994. XMLAuth;
  995. }
  996. /**
  997. * Get cgi tracking
  998. *
  999. * @param mixed $trackings
  1000. * @return mixed
  1001. */
  1002. protected function _getCgiTracking($trackings)
  1003. {
  1004. //ups no longer support tracking for data streaming version
  1005. //so we can only reply the popup window to ups.
  1006. $result = Mage::getModel('shipping/tracking_result');
  1007. $defaults = $this->getDefaults();
  1008. foreach($trackings as $tracking){
  1009. $status = Mage::getModel('shipping/tracking_result_status');
  1010. $status->setCarrier('ups');
  1011. $status->setCarrierTitle($this->getConfigData('title'));
  1012. $status->setTracking($tracking);
  1013. $status->setPopup(1);
  1014. $status->setUrl("http://wwwapps.ups.com/WebTracking/processInputRequest?HTMLVersion=5.0&error_carried=true"
  1015. . "&tracknums_displayed=5&TypeOfInquiryNumber=T&loc=en_US&InquiryNumber1=$tracking"
  1016. . "&AgreeToTermsAndConditions=yes"
  1017. );
  1018. $result->append($status);
  1019. }
  1020. $this->_result = $result;
  1021. return $result;
  1022. }
  1023. /**
  1024. * Get xml tracking
  1025. *
  1026. * @param mixed $trackings
  1027. * @return mixed
  1028. */
  1029. protected function _getXmlTracking($trackings)
  1030. {
  1031. $url = $this->getConfigData('tracking_xml_url');
  1032. foreach($trackings as $tracking){
  1033. $xmlRequest=$this->_xmlAccessRequest;
  1034. /*
  1035. * RequestOption==>'activity' or '1' to request all activities
  1036. */
  1037. $xmlRequest .= <<<XMLAuth
  1038. <?xml version="1.0" ?>
  1039. <TrackRequest xml:lang="en-US">
  1040. <Request>
  1041. <RequestAction>Track</RequestAction>
  1042. <RequestOption>activity</RequestOption>
  1043. </Request>
  1044. <TrackingNumber>$tracking</TrackingNumber>
  1045. <IncludeFreight>01</IncludeFreight>
  1046. </TrackRequest>
  1047. XMLAuth;
  1048. $debugData = array('request' => $xmlRequest);
  1049. try {
  1050. $ch = curl_init();
  1051. curl_setopt($ch, CURLOPT_URL, $url);
  1052. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1053. curl_setopt($ch, CURLOPT_HEADER, 0);
  1054. curl_setopt($ch, CURLOPT_POST, 1);
  1055. curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
  1056. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  1057. $xmlResponse = curl_exec ($ch);
  1058. $debugData['result'] = $xmlResponse;
  1059. curl_close ($ch);
  1060. }
  1061. catch (Exception $e) {
  1062. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  1063. $xmlResponse = '';
  1064. }
  1065. $this->_debug($debugData);
  1066. $this->_parseXmlTrackingResponse($tracking, $xmlResponse);
  1067. }
  1068. return $this->_result;
  1069. }
  1070. /**
  1071. * Parse xml tracking response
  1072. *
  1073. * @param string $trackingvalue
  1074. * @param string $response
  1075. * @return null
  1076. */
  1077. protected function _parseXmlTrackingResponse($trackingvalue, $xmlResponse)
  1078. {
  1079. $errorTitle = 'Unable to retrieve tracking';
  1080. $resultArr = array();
  1081. $packageProgress = array();
  1082. if ($xmlResponse) {
  1083. $xml = new Varien_Simplexml_Config();
  1084. $xml->loadString($xmlResponse);
  1085. $arr = $xml->getXpath("//TrackResponse/Response/ResponseStatusCode/text()");
  1086. $success = (int)$arr[0][0];
  1087. if($success===1){
  1088. $arr = $xml->getXpath("//TrackResponse/Shipment/Service/Description/text()");
  1089. $resultArr['service'] = (string)$arr[0];
  1090. $arr = $xml->getXpath("//TrackResponse/Shipment/PickupDate/text()");
  1091. $resultArr['shippeddate'] = (string)$arr[0];
  1092. $arr = $xml->getXpath("//TrackResponse/Shipment/Package/PackageWeight/Weight/text()");
  1093. $weight = (string)$arr[0];
  1094. $arr = $xml->getXpath("//TrackResponse/Shipment/Package/PackageWeight/UnitOfMeasurement/Code/text()");
  1095. $unit = (string)$arr[0];
  1096. $resultArr['weight'] = "{$weight} {$unit}";
  1097. $activityTags = $xml->getXpath("//TrackResponse/Shipment/Package/Activity");
  1098. if ($activityTags) {
  1099. $i=1;
  1100. foreach ($activityTags as $activityTag) {
  1101. $addArr=array();
  1102. if (isset($activityTag->ActivityLocation->Address->City)) {
  1103. $addArr[] = (string)$activityTag->ActivityLocation->Address->City;
  1104. }
  1105. if (isset($activityTag->ActivityLocation->Address->StateProvinceCode)) {
  1106. $addArr[] = (string)$activityTag->ActivityLocation->Address->StateProvinceCode;
  1107. }
  1108. if (isset($activityTag->ActivityLocation->Address->CountryCode)) {
  1109. $addArr[] = (string)$activityTag->ActivityLocation->Address->CountryCode;
  1110. }
  1111. $dateArr = array();
  1112. $date = (string)$activityTag->Date;//YYYYMMDD
  1113. $dateArr[] = substr($date,0,4);
  1114. $dateArr[] = substr($date,4,2);
  1115. $dateArr[] = substr($date,-2,2);
  1116. $timeArr = array();
  1117. $time = (string)$activityTag->Time;//HHMMSS
  1118. $timeArr[] = substr($time,0,2);
  1119. $timeArr[] = substr($time,2,2);
  1120. $timeArr[] = substr($time,-2,2);
  1121. if($i==1){
  1122. $resultArr['status'] = (string)$activityTag->Status->StatusType->Description;
  1123. $resultArr['deliverydate'] = implode('-',$dateArr);//YYYY-MM-DD
  1124. $resultArr['deliverytime'] = implode(':',$timeArr);//HH:MM:SS
  1125. $resultArr['deliverylocation'] = (string)$activityTag->ActivityLocation->Description;
  1126. $resultArr['signedby'] = (string)$activityTag->ActivityLocation->SignedForByName;
  1127. if ($addArr) {
  1128. $resultArr['deliveryto']=implode(', ',$addArr);
  1129. }
  1130. }else{
  1131. $tempArr=array();
  1132. $tempArr['activity'] = (string)$activityTag->Status->StatusType->Description;
  1133. $tempArr['deliverydate'] = implode('-',$dateArr);//YYYY-MM-DD
  1134. $tempArr['deliverytime'] = implode(':',$timeArr);//HH:MM:SS
  1135. if ($addArr) {
  1136. $tempArr['deliverylocation']=implode(', ',$addArr);
  1137. }
  1138. $packageProgress[] = $tempArr;
  1139. }
  1140. $i++;
  1141. }
  1142. $resultArr['progressdetail'] = $packageProgress;
  1143. }
  1144. } else {
  1145. $arr = $xml->getXpath("//TrackResponse/Response/Error/ErrorDescription/text()");
  1146. $errorTitle = (string)$arr[0][0];
  1147. }
  1148. }
  1149. if (!$this->_result) {
  1150. $this->_result = Mage::getModel('shipping/tracking_result');
  1151. }
  1152. $defaults = $this->getDefaults();
  1153. if ($resultArr) {
  1154. $tracking = Mage::getModel('shipping/tracking_result_status');
  1155. $tracking->setCarrier('ups');
  1156. $tracking->setCarrierTitle($this->getConfigData('title'));
  1157. $tracking->setTracking($trackingvalue);
  1158. $tracking->addData($resultArr);
  1159. $this->_result->append($tracking);
  1160. } else {
  1161. $error = Mage::getModel('shipping/tracking_result_error');
  1162. $error->setCarrier('ups');
  1163. $error->setCarrierTitle($this->getConfigData('title'));
  1164. $error->setTracking($trackingvalue);
  1165. $error->setErrorMessage($errorTitle);
  1166. $this->_result->append($error);
  1167. }
  1168. return $this->_result;
  1169. }
  1170. /**
  1171. * Get tracking response
  1172. *
  1173. * @return string
  1174. */
  1175. public function getResponse()
  1176. {
  1177. $statuses = '';
  1178. if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result){
  1179. if ($trackings = $this->_result->getAllTrackings()) {
  1180. foreach ($trackings as $tracking){
  1181. if($data = $tracking->getAllData()){
  1182. if (isset($data['status'])) {
  1183. $statuses .= Mage::helper('usa')->__($data['status']);
  1184. } else {
  1185. $statuses .= Mage::helper('usa')->__($data['error_message']);
  1186. }
  1187. }
  1188. }
  1189. }
  1190. }
  1191. if (empty($statuses)) {
  1192. $statuses = Mage::helper('usa')->__('Empty response');
  1193. }
  1194. return $statuses;
  1195. }
  1196. /**
  1197. * Get allowed shipping methods
  1198. *
  1199. * @return array
  1200. */
  1201. public function getAllowedMethods()
  1202. {
  1203. $allowed = explode(',', $this->getConfigData('allowed_methods'));
  1204. $arr = array();
  1205. $isByCode = $this->getConfigData('type') == 'UPS_XML';
  1206. foreach ($allowed as $k) {
  1207. $arr[$k] = $isByCode ? $this->getShipmentByCode($k) : $this->getCode('method', $k);
  1208. }
  1209. return $arr;
  1210. }
  1211. /**
  1212. * Form XML for shipment request
  1213. *
  1214. * @param Varien_Object $request
  1215. * @return string
  1216. */
  1217. protected function _formShipmentRequest(Varien_Object $request)
  1218. {
  1219. $packageParams = $request->getPackageParams();
  1220. $height = $packageParams->getHeight();
  1221. $width = $packageParams->getWidth();
  1222. $length = $packageParams->getLength();
  1223. $weightUnits = $packageParams->getWeightUnits() == Zend_Measure_Weight::POUND ? 'LBS' : 'KGS';
  1224. $dimensionsUnits = $packageParams->getDimensionUnits() == Zend_Measure_Length::INCH ? 'IN' : 'CM';
  1225. $itemsDesc = array();
  1226. $itemsShipment = $request->getPackageItems();
  1227. foreach ($itemsShipment as $itemShipment) {
  1228. $item = new Varien_Object();
  1229. $item->setData($itemShipment);
  1230. $itemsDesc[] = $item->getName();
  1231. }
  1232. $xmlRequest = new SimpleXMLElement('<?xml version = "1.0" ?><ShipmentConfirmRequest xml:lang="en-US"/>');
  1233. $requestPart = $xmlRequest->addChild('Request');
  1234. $requestPart->addChild('RequestAction', 'ShipConfirm');
  1235. $requestPart->addChild('RequestOption', 'nonvalidate');
  1236. $shipmentPart = $xmlRequest->addChild('Shipment');
  1237. if ($request->getIsReturn()) {
  1238. $returnPart = $shipmentPart->addChild('ReturnService');
  1239. // UPS Print Return Label
  1240. $returnPart->addChild('Code', '9');
  1241. }
  1242. $shipmentPart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));//empirical
  1243. $shipperPart = $shipmentPart->addChild('Shipper');
  1244. if ($request->getIsReturn()) {
  1245. $shipperPart->addChild('Name', $request->getRecipientContactCompanyName());
  1246. $shipperPart->addChild('AttentionName', $request->getRecipientContactPersonName());
  1247. $shipperPart->addChild('ShipperNumber', $this->getConfigData('shipper_number'));
  1248. $shipperPart->addChild('PhoneNumber', $request->getRecipientContactPhoneNumber());
  1249. $addressPart = $shipperPart->addChild('Address');
  1250. $addressPart->addChild('AddressLine1', $request->getRecipientAddressStreet());
  1251. $addressPart->addChild('AddressLine2', $request->getRecipientAddressStreet2());
  1252. $addressPart->addChild('City', $request->getRecipientAddressCity());
  1253. $addressPart->addChild('CountryCode', $request->getRecipientAddressCountryCode());
  1254. $addressPart->addChild('PostalCode', $request->getRecipientAddressPostalCode());
  1255. if ($request->getRecipientAddressStateOrProvinceCode()) {
  1256. $addressPart->addChild('StateProvinceCode', $request->getRecipientAddressStateOrProvinceCode());
  1257. }
  1258. } else {
  1259. $shipperPart->addChild('Name', $request->getShipperContactCompanyName());
  1260. $shipperPart->addChild('AttentionName', $request->getShipperContactPersonName());
  1261. $shipperPart->addChild('ShipperNumber', $this->getConfigData('shipper_number'));
  1262. $shipperPart->addChild('PhoneNumber', $request->getShipperContactPhoneNumber());
  1263. $addressPart = $shipperPart->addChild('Address');
  1264. $addressPart->addChild('AddressLine1', $request->getShipperAddressStreet());
  1265. $addressPart->addChild('AddressLine2', $request->getShipperAddressStreet2());
  1266. $addressPart->addChild('City', $request->getShipperAddressCity());
  1267. $addressPart->addChild('CountryCode', $request->getShipperAddressCountryCode());
  1268. $addressPart->addChild('PostalCode', $request->getShipperAddressPostalCode());
  1269. if ($request->getShipperAddressStateOrProvinceCode()) {
  1270. $addressPart->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
  1271. }
  1272. }
  1273. $shipToPart = $shipmentPart->addChild('ShipTo');
  1274. $shipToPart->addChild('AttentionName', $request->getRecipientContactPersonName());
  1275. $shipToPart->addChild('CompanyName', $request->getRecipientContactCompanyName()
  1276. ? $request->getRecipientContactCompanyName()
  1277. : 'N/A');
  1278. $shipToPart->addChild('PhoneNumber', $request->getRecipientContactPhoneNumber());
  1279. $addressPart = $shipToPart->addChild('Address');
  1280. $addressPart->addChild('AddressLine1', $request->getRecipientAddressStreet1());
  1281. $addressPart->addChild('AddressLine2', $request->getRecipientAddressStreet2());
  1282. $addressPart->addChild('City', $request->getRecipientAddressCity());
  1283. $addressPart->addChild('CountryCode', $request->getRecipientAddressCountryCode());
  1284. $addressPart->addChild('PostalCode', $request->getRecipientAddressPostalCode());
  1285. if ($request->getRecipientAddressStateOrProvinceCode()) {
  1286. $addressPart->addChild('StateProvinceCode', $request->getRecipientAddressRegionCode());
  1287. }
  1288. if ($this->getConfigData('dest_type') == 'RES') {
  1289. $addressPart->addChild('ResidentialAddress');
  1290. }
  1291. if ($request->getIsReturn()) {
  1292. $shipFromPart = $shipmentPart->addChild('ShipFrom');
  1293. $shipFromPart->addChild('AttentionName', $request->getShipperContactPersonName());
  1294. $shipFromPart->addChild('CompanyName', $request->getShipperContactCompanyName()
  1295. ? $request->getShipperContactCompanyName()
  1296. : $request->getShipperContactPersonName());
  1297. $shipFromAddress = $shipFromPart->addChild('Address');
  1298. $shipFromAddress->addChild('AddressLine1', $request->getShipperAddressStreet1());
  1299. $shipFromAddress->addChild('AddressLine2', $request->getShipperAddressStreet2());
  1300. $shipFromAddress->addChild('City', $request->getShipperAddressCity());
  1301. $shipFromAddress->addChild('CountryCode', $request->getShipperAddressCountryCode());
  1302. $shipFromAddress->addChild('PostalCode', $request->getShipperAddressPostalCode());
  1303. if ($request->getShipperAddressStateOrProvinceCode()) {
  1304. $shipFromAddress->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
  1305. }
  1306. $addressPart = $shipToPart->addChild('Address');
  1307. $addressPart->addChild('AddressLine1', $request->getShipperAddressStreet1());
  1308. $addressPart->addChild('AddressLine2', $request->getShipperAddressStreet2());
  1309. $addressPart->addChild('City', $request->getShipperAddressCity());
  1310. $addressPart->addChild('CountryCode', $request->getShipperAddressCountryCode());
  1311. $addressPart->addChild('PostalCode', $request->getShipperAddressPostalCode());
  1312. if ($request->getShipperAddressStateOrProvinceCode()) {
  1313. $addressPart->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
  1314. }
  1315. if ($this->getConfigData('dest_type') == 'RES') {
  1316. $addressPart->addChild('ResidentialAddress');
  1317. }
  1318. }
  1319. $servicePart = $shipmentPart->addChild('Service');
  1320. $servicePart->addChild('Code', $request->getShippingMethod());
  1321. $packagePart = $shipmentPart->addChild('Package');
  1322. $packagePart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));//empirical
  1323. $packagePart->addChild('PackagingType')
  1324. ->addChild('Code', $request->getPackagingType());
  1325. $packageWeight = $packagePart->addChild('PackageWeight');
  1326. $packageWeight->addChild('Weight', $request->getPackageWeight());
  1327. $packageWeight->addChild('UnitOfMeasurement')->addChild('Code', $weightUnits);
  1328. // set dimensions
  1329. if ($length || $width || $height) {
  1330. $packageDimensions = $packagePart->addChild('Dimensions');
  1331. $packageDimensions->addChild('UnitOfMeasurement')->addChild('Code', $dimensionsUnits);
  1332. $packageDimensions->addChild('Length', $length);
  1333. $packageDimensions->addChild('Width', $width);
  1334. $packageDimensions->addChild('Height', $height);
  1335. }
  1336. // ups support reference number only for domestic service
  1337. if ($this->_isUSCountry($request->getRecipientAddressCountryCode())
  1338. && $this->_isUSCountry($request->getShipperAddressCountryCode())
  1339. ) {
  1340. if ($request->getReferenceData()) {
  1341. $referenceData = $request->getReferenceData() . $request->getPackageId();
  1342. } else {
  1343. $referenceData = 'Order #'
  1344. . $request->getOrderShipment()->getOrder()->getIncrementId()
  1345. . ' P'
  1346. . $request->getPackageId();
  1347. }
  1348. $referencePart = $packagePart->addChild('ReferenceNumber');
  1349. $referencePart->addChild('Code', '02');
  1350. $referencePart->addChild('Value', $referenceData);
  1351. }
  1352. $deliveryConfirmation = $packageParams->getDeliveryConfirmation();
  1353. if ($deliveryConfirmation) {
  1354. /** @var $serviceOptionsNode SimpleXMLElement */
  1355. $serviceOptionsNode = null;
  1356. switch ($this->_getDeliveryConfirmationLevel($request->getRecipientAddressCountryCode())) {
  1357. case self::DELIVERY_CONFIRMATION_PACKAGE:
  1358. $serviceOptionsNode = $packagePart->addChild('PackageServiceOptions');
  1359. break;
  1360. case self::DELIVERY_CONFIRMATION_SHIPMENT:
  1361. $serviceOptionsNode = $shipmentPart->addChild('ShipmentServiceOptions');
  1362. break;
  1363. }
  1364. if (!is_null($serviceOptionsNode)) {
  1365. $serviceOptionsNode
  1366. ->addChild('DeliveryConfirmation')
  1367. ->addChild('DCISType', $packageParams->getDeliveryConfirmation());
  1368. }
  1369. }
  1370. $shipmentPart->addChild('PaymentInformation')
  1371. ->addChild('Prepaid')
  1372. ->addChild('BillShipper')
  1373. ->addChild('AccountNumber', $this->getConfigData('shipper_number'));
  1374. if ($request->getPackagingType() != $this->getCode('container', 'ULE')
  1375. && $request->getShipperAddressCountryCode() == Mage_Usa_Model_Shipping_Carrier_Abstract::USA_COUNTRY_ID
  1376. && ($request->getRecipientAddressCountryCode() == 'CA' //Canada
  1377. || $request->getRecipientAddressCountryCode() == 'PR' //Puerto Rico
  1378. )) {
  1379. $invoiceLineTotalPart = $shipmentPart->addChild('InvoiceLineTotal');
  1380. $invoiceLineTotalPart->addChild('CurrencyCode', $request->getBaseCurrencyCode());
  1381. $invoiceLineTotalPart->addChild('MonetaryValue', ceil($packageParams->getCustomsValue()));
  1382. }
  1383. $labelPart = $xmlRequest->addChild('LabelSpecification');
  1384. $labelPart->addChild('LabelPrintMethod')
  1385. ->addChild('Code', 'GIF');
  1386. $labelPart->addChild('LabelImageFormat')
  1387. ->addChild('Code', 'GIF');
  1388. $this->setXMLAccessRequest();
  1389. $xmlRequest = $this->_xmlAccessRequest . $xmlRequest->asXml();
  1390. return $xmlRequest;
  1391. }
  1392. /**
  1393. * Send and process shipment accept request
  1394. *
  1395. * @param SimpleXMLElement
  1396. * @return Varien_Object
  1397. */
  1398. protected function _sendShipmentAcceptRequest(SimpleXMLElement $shipmentConfirmResponse)
  1399. {
  1400. $xmlRequest = new SimpleXMLElement('<?xml version = "1.0" ?><ShipmentAcceptRequest/>');
  1401. $request = $xmlRequest->addChild('Request');
  1402. $request->addChild('RequestAction', 'ShipAccept');
  1403. $xmlRequest->addChild('ShipmentDigest', $shipmentConfirmResponse->ShipmentDigest);
  1404. $debugData = array('request' => $xmlRequest->asXML());
  1405. try {
  1406. $url = $this->_defaultUrls['ShipAccept'];
  1407. $ch = curl_init($url);
  1408. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1409. curl_setopt($ch, CURLOPT_HEADER, 0);
  1410. curl_setopt($ch, CURLOPT_POST, 1);
  1411. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_xmlAccessRequest . $xmlRequest->asXML());
  1412. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  1413. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (boolean)$this->getConfigFlag('mode_xml'));
  1414. $xmlResponse = curl_exec ($ch);
  1415. $debugData['result'] = $xmlResponse;
  1416. $this->_setCachedQuotes($xmlRequest, $xmlResponse);
  1417. } catch (Exception $e) {
  1418. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  1419. $xmlResponse = '';
  1420. }
  1421. try {
  1422. $response = new SimpleXMLElement($xmlResponse);
  1423. } catch (Exception $e) {
  1424. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  1425. }
  1426. $result = new Varien_Object();
  1427. if (isset($response->Error)) {
  1428. $result->setErrors((string)$response->Error->ErrorDescription);
  1429. } else {
  1430. $shippingLabelContent = (string)$response->ShipmentResults->PackageResults->LabelImage->GraphicImage;
  1431. $trackingNumber = (string)$response->ShipmentResults->PackageResults->TrackingNumber;
  1432. $result->setShippingLabelContent(base64_decode($shippingLabelContent));
  1433. $result->setTrackingNumber($trackingNumber);
  1434. }
  1435. $this->_debug($debugData);
  1436. return $result;
  1437. }
  1438. /**
  1439. * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
  1440. *
  1441. * @param Varien_Object $request
  1442. * @return Varien_Object
  1443. */
  1444. protected function _doShipmentRequest(Varien_Object $request)
  1445. {
  1446. $this->_prepareShipmentRequest($request);
  1447. $result = new Varien_Object();
  1448. $xmlRequest = $this->_formShipmentRequest($request);
  1449. $xmlResponse = $this->_getCachedQuotes($xmlRequest);
  1450. if ($xmlResponse === null) {
  1451. $url = $this->getConfigData('url');
  1452. if (!$url) {
  1453. $url = $this->_defaultUrls['ShipConfirm'];
  1454. }
  1455. $debugData = array('request' => $xmlRequest);
  1456. $ch = curl_init();
  1457. curl_setopt($ch, CURLOPT_URL, $url);
  1458. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1459. curl_setopt($ch, CURLOPT_HEADER, 0);
  1460. curl_setopt($ch, CURLOPT_POST, 1);
  1461. curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
  1462. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  1463. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (boolean)$this->getConfigFlag('mode_xml'));
  1464. $xmlResponse = curl_exec($ch);
  1465. if ($xmlResponse === false) {
  1466. throw new Exception(curl_error($ch));
  1467. } else {
  1468. $debugData['result'] = $xmlResponse;
  1469. $this->_setCachedQuotes($xmlRequest, $xmlResponse);
  1470. }
  1471. }
  1472. try {
  1473. $response = new SimpleXMLElement($xmlResponse);
  1474. } catch (Exception $e) {
  1475. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  1476. $result->setErrors($e->getMessage());
  1477. }
  1478. if (isset($response->Response->Error)
  1479. && in_array($response->Response->Error->ErrorSeverity, array('Hard', 'Transient'))
  1480. ) {
  1481. $result->setErrors((string)$response->Response->Error->ErrorDescription);
  1482. }
  1483. $this->_debug($debugData);
  1484. if ($result->hasErrors() || empty($response)) {
  1485. return $result;
  1486. } else {
  1487. return $this->_sendShipmentAcceptRequest($response);
  1488. }
  1489. }
  1490. /**
  1491. * Return container types of carrier
  1492. *
  1493. * @param Varien_Object|null $params
  1494. * @return array|bool
  1495. */
  1496. public function getContainerTypes(Varien_Object $params = null)
  1497. {
  1498. if ($params == null) {
  1499. return $this->_getAllowedContainers($params);
  1500. }
  1501. $method = $params->getMethod();
  1502. $countryShipper = $params->getCountryShipper();
  1503. $countryRecipient = $params->getCountryRecipient();
  1504. if (($countryShipper == self::USA_COUNTRY_ID
  1505. && $countryRecipient == self::CANADA_COUNTRY_ID)
  1506. || ($countryShipper == self::CANADA_COUNTRY_ID
  1507. && $countryRecipient == self::USA_COUNTRY_ID)
  1508. || ($countryShipper == self::MEXICO_COUNTRY_ID
  1509. && $countryRecipient == self::USA_COUNTRY_ID)
  1510. && $method == '11' // UPS Standard
  1511. ) {
  1512. $containerTypes = array();
  1513. if ($method == '07' // Worldwide Express
  1514. || $method == '08' // Worldwide Expedited
  1515. || $method == '65' // Worldwide Saver
  1516. ) {
  1517. // Worldwide Expedited
  1518. if ($method != '08') {
  1519. $containerTypes = array(
  1520. '01' => Mage::helper('usa')->__('UPS Letter Envelope'),
  1521. '24' => Mage::helper('usa')->__('UPS Worldwide 25 kilo'),
  1522. '25' => Mage::helper('usa')->__('UPS Worldwide 10 kilo'),
  1523. );
  1524. }
  1525. $containerTypes = $containerTypes + array(
  1526. '03' => Mage::helper('usa')->__('UPS Tube'),
  1527. '04' => Mage::helper('usa')->__('PAK'),
  1528. '2a' => Mage::helper('usa')->__('Small Express Box'),
  1529. '2b' => Mage::helper('usa')->__('Medium Express Box'),
  1530. '2c' => Mage::helper('usa')->__('Large Express Box'),
  1531. );
  1532. }
  1533. return array('00' => Mage::helper('usa')->__('Customer Packaging')) + $containerTypes;
  1534. } elseif ($countryShipper == self::USA_COUNTRY_ID && $countryRecipient == self::PUERTORICO_COUNTRY_ID
  1535. && ($method == '03' // UPS Ground
  1536. || $method == '02' // UPS Second Day Air
  1537. || $method == '01' // UPS Next Day Air
  1538. )) {
  1539. // Container types should be the same as for domestic
  1540. $params->setCountryRecipient(self::USA_COUNTRY_ID);
  1541. $containerTypes = $this->_getAllowedContainers($params);
  1542. $params->setCountryRecipient($countryRecipient);
  1543. return $containerTypes;
  1544. }
  1545. return $this->_getAllowedContainers($params);
  1546. }
  1547. /**
  1548. * Return all container types of carrier
  1549. *
  1550. * @return array|bool
  1551. */
  1552. public function getContainerTypesAll()
  1553. {
  1554. $codes = $this->getCode('container');
  1555. $descriptions = $this->getCode('container_description');
  1556. $result = array();
  1557. foreach ($codes as $key => &$code) {
  1558. $result[$code] = $descriptions[$key];
  1559. }
  1560. return $result;
  1561. }
  1562. /**
  1563. * Return structured data of containers witch related with shipping methods
  1564. *
  1565. * @return array|bool
  1566. */
  1567. public function getContainerTypesFilter()
  1568. {
  1569. return $this->getCode('containers_filter');
  1570. }
  1571. /**
  1572. * Return delivery confirmation types of carrier
  1573. *
  1574. * @param Varien_Object|null $params
  1575. * @return array|bool
  1576. */
  1577. public function getDeliveryConfirmationTypes(Varien_Object $params = null)
  1578. {
  1579. $countryRecipient = $params != null ? $params->getCountryRecipient() : null;
  1580. $deliveryConfirmationTypes = array();
  1581. switch ($this->_getDeliveryConfirmationLevel($countryRecipient)) {
  1582. case self::DELIVERY_CONFIRMATION_PACKAGE:
  1583. $deliveryConfirmationTypes = array(
  1584. 1 => Mage::helper('usa')->__('Delivery Confirmation'),
  1585. 2 => Mage::helper('usa')->__('Signature Required'),
  1586. 3 => Mage::helper('usa')->__('Adult Signature Required'),
  1587. );
  1588. break;
  1589. case self::DELIVERY_CONFIRMATION_SHIPMENT:
  1590. $deliveryConfirmationTypes = array(
  1591. 1 => Mage::helper('usa')->__('Signature Required'),
  1592. 2 => Mage::helper('usa')->__('Adult Signature Required'),
  1593. );
  1594. }
  1595. array_unshift($deliveryConfirmationTypes, Mage::helper('usa')->__('Not Required'));
  1596. return $deliveryConfirmationTypes;
  1597. }
  1598. /**
  1599. * Get Container Types, that could be customized for UPS carrier
  1600. *
  1601. * @return array
  1602. */
  1603. public function getCustomizableContainerTypes()
  1604. {
  1605. $result = array();
  1606. $containerTypes = $this->getCode('container');
  1607. foreach (parent::getCustomizableContainerTypes() as $containerType) {
  1608. $result[$containerType] = $containerTypes[$containerType];
  1609. }
  1610. return $result;
  1611. }
  1612. /**
  1613. * Get delivery confirmation level based on origin/destination
  1614. * Return null if delivery confirmation is not acceptable
  1615. *
  1616. * @var string $countyDest
  1617. * @return int|null
  1618. */
  1619. protected function _getDeliveryConfirmationLevel($countyDest = null) {
  1620. if (is_null($countyDest)) {
  1621. return null;
  1622. }
  1623. if ($countyDest == Mage_Usa_Model_Shipping_Carrier_Abstract::USA_COUNTRY_ID) {
  1624. return self::DELIVERY_CONFIRMATION_PACKAGE;
  1625. }
  1626. return self::DELIVERY_CONFIRMATION_SHIPMENT;
  1627. }
  1628. }