PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 1799 lines | 1387 code | 132 blank | 280 comment | 194 complexity | 36d1da82b811de523cc7ce2e1223c771 MD5 | raw file
Possible License(s): LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  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. * USPS shipping rates estimation
  28. *
  29. * @link http://www.usps.com/webtools/htm/Development-Guide-v3-0b.htm
  30. * @category Mage
  31. * @package Mage_Usa
  32. * @author Magento Core Team <core@magentocommerce.com>
  33. */
  34. class Mage_Usa_Model_Shipping_Carrier_Usps
  35. extends Mage_Usa_Model_Shipping_Carrier_Abstract
  36. implements Mage_Shipping_Model_Carrier_Interface
  37. {
  38. /**
  39. * USPS containers
  40. */
  41. const CONTAINER_VARIABLE = 'VARIABLE';
  42. const CONTAINER_FLAT_RATE_BOX = 'FLAT RATE BOX';
  43. const CONTAINER_FLAT_RATE_ENVELOPE = 'FLAT RATE ENVELOPE';
  44. const CONTAINER_RECTANGULAR = 'RECTANGULAR';
  45. const CONTAINER_NONRECTANGULAR = 'NONRECTANGULAR';
  46. /**
  47. * USPS size
  48. */
  49. const SIZE_REGULAR = 'REGULAR';
  50. const SIZE_LARGE = 'LARGE';
  51. /**
  52. * Default api revision
  53. *
  54. * @var int
  55. */
  56. const DEFAULT_REVISION = 2;
  57. /**
  58. * Code of the carrier
  59. *
  60. * @var string
  61. */
  62. const CODE = 'usps';
  63. /**
  64. * Ounces in one pound for conversion
  65. */
  66. const OUNCES_POUND = 16;
  67. /**
  68. * Code of the carrier
  69. *
  70. * @var string
  71. */
  72. protected $_code = self::CODE;
  73. /**
  74. * Destination Zip Code required flag
  75. *
  76. * @var boolean
  77. * @deprecated since 1.7.0 functionality implemented in Mage_Usa_Model_Shipping_Carrier_Abstract
  78. */
  79. protected $_isZipCodeRequired;
  80. /**
  81. * Rate request data
  82. *
  83. * @var Mage_Shipping_Model_Rate_Request|null
  84. */
  85. protected $_request = null;
  86. /**
  87. * Raw rate request data
  88. *
  89. * @var Varien_Object|null
  90. */
  91. protected $_rawRequest = null;
  92. /**
  93. * Rate result data
  94. *
  95. * @var Mage_Shipping_Model_Rate_Result|null
  96. */
  97. protected $_result = null;
  98. /**
  99. * Default cgi gateway url
  100. *
  101. * @var string
  102. */
  103. protected $_defaultGatewayUrl = 'http://production.shippingapis.com/ShippingAPI.dll';
  104. /**
  105. * Container types that could be customized for USPS carrier
  106. *
  107. * @var array
  108. */
  109. protected $_customizableContainerTypes = array('VARIABLE', 'RECTANGULAR', 'NONRECTANGULAR');
  110. /**
  111. * Collect and get rates
  112. *
  113. * @param Mage_Shipping_Model_Rate_Request $request
  114. * @return Mage_Shipping_Model_Rate_Result|bool|null
  115. */
  116. public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  117. {
  118. if (!$this->getConfigFlag($this->_activeFlag)) {
  119. return false;
  120. }
  121. $this->setRequest($request);
  122. $this->_result = $this->_getQuotes();
  123. $this->_updateFreeMethodQuote($request);
  124. return $this->getResult();
  125. }
  126. /**
  127. * Prepare and set request to this instance
  128. *
  129. * @param Mage_Shipping_Model_Rate_Request $request
  130. * @return Mage_Usa_Model_Shipping_Carrier_Usps
  131. */
  132. public function setRequest(Mage_Shipping_Model_Rate_Request $request)
  133. {
  134. $this->_request = $request;
  135. $r = new Varien_Object();
  136. if ($request->getLimitMethod()) {
  137. $r->setService($request->getLimitMethod());
  138. } else {
  139. $r->setService('ALL');
  140. }
  141. if ($request->getUspsUserid()) {
  142. $userId = $request->getUspsUserid();
  143. } else {
  144. $userId = $this->getConfigData('userid');
  145. }
  146. $r->setUserId($userId);
  147. if ($request->getUspsContainer()) {
  148. $container = $request->getUspsContainer();
  149. } else {
  150. $container = $this->getConfigData('container');
  151. }
  152. $r->setContainer($container);
  153. if ($request->getUspsSize()) {
  154. $size = $request->getUspsSize();
  155. } else {
  156. $size = $this->getConfigData('size');
  157. }
  158. $r->setSize($size);
  159. if ($request->getGirth()) {
  160. $girth = $request->getGirth();
  161. } else {
  162. $girth = $this->getConfigData('girth');
  163. }
  164. $r->setGirth($girth);
  165. if ($request->getHeight()) {
  166. $height = $request->getHeight();
  167. } else {
  168. $height = $this->getConfigData('height');
  169. }
  170. $r->setHeight($height);
  171. if ($request->getLength()) {
  172. $length = $request->getLength();
  173. } else {
  174. $length = $this->getConfigData('length');
  175. }
  176. $r->setLength($length);
  177. if ($request->getWidth()) {
  178. $width = $request->getWidth();
  179. } else {
  180. $width = $this->getConfigData('width');
  181. }
  182. $r->setWidth($width);
  183. if ($request->getUspsMachinable()) {
  184. $machinable = $request->getUspsMachinable();
  185. } else {
  186. $machinable = $this->getConfigData('machinable');
  187. }
  188. $r->setMachinable($machinable);
  189. if ($request->getOrigPostcode()) {
  190. $r->setOrigPostal($request->getOrigPostcode());
  191. } else {
  192. $r->setOrigPostal(Mage::getStoreConfig(
  193. Mage_Shipping_Model_Shipping::XML_PATH_STORE_ZIP,
  194. $request->getStoreId()
  195. ));
  196. }
  197. if ($request->getOrigCountryId()) {
  198. $r->setOrigCountryId($request->getOrigCountryId());
  199. } else {
  200. $r->setOrigCountryId(Mage::getStoreConfig(
  201. Mage_Shipping_Model_Shipping::XML_PATH_STORE_COUNTRY_ID,
  202. $request->getStoreId()
  203. ));
  204. }
  205. if ($request->getDestCountryId()) {
  206. $destCountry = $request->getDestCountryId();
  207. } else {
  208. $destCountry = self::USA_COUNTRY_ID;
  209. }
  210. $r->setDestCountryId($destCountry);
  211. if (!$this->_isUSCountry($destCountry)) {
  212. $r->setDestCountryName($this->_getCountryName($destCountry));
  213. }
  214. if ($request->getDestPostcode()) {
  215. $r->setDestPostal($request->getDestPostcode());
  216. }
  217. $weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
  218. $r->setWeightPounds(floor($weight));
  219. $r->setWeightOunces(round(($weight-floor($weight)) * self::OUNCES_POUND, 1));
  220. if ($request->getFreeMethodWeight()!=$request->getPackageWeight()) {
  221. $r->setFreeMethodWeight($request->getFreeMethodWeight());
  222. }
  223. $r->setValue($request->getPackageValue());
  224. $r->setValueWithDiscount($request->getPackageValueWithDiscount());
  225. $r->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax());
  226. $this->_rawRequest = $r;
  227. return $this;
  228. }
  229. /**
  230. * Get result of request
  231. *
  232. * @return mixed
  233. */
  234. public function getResult()
  235. {
  236. return $this->_result;
  237. }
  238. /**
  239. * Get quotes
  240. *
  241. * @return Mage_Shipping_Model_Rate_Result
  242. */
  243. protected function _getQuotes()
  244. {
  245. return $this->_getXmlQuotes();
  246. }
  247. /**
  248. * Set free method request
  249. *
  250. * @param $freeMethod
  251. * @return void
  252. */
  253. protected function _setFreeMethodRequest($freeMethod)
  254. {
  255. $r = $this->_rawRequest;
  256. $weight = $this->getTotalNumOfBoxes($r->getFreeMethodWeight());
  257. $r->setWeightPounds(floor($weight));
  258. $r->setWeightOunces(round(($weight-floor($weight)) * self::OUNCES_POUND, 1));
  259. $r->setService($freeMethod);
  260. }
  261. /**
  262. * Build RateV3 request, send it to USPS gateway and retrieve quotes in XML format
  263. *
  264. * @link http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm
  265. * @return Mage_Shipping_Model_Rate_Result
  266. */
  267. protected function _getXmlQuotes()
  268. {
  269. $r = $this->_rawRequest;
  270. // The origin address(shipper) must be only in USA
  271. if(!$this->_isUSCountry($r->getOrigCountryId())){
  272. $responseBody = '';
  273. return $this->_parseXmlResponse($responseBody);
  274. }
  275. if ($this->_isUSCountry($r->getDestCountryId())) {
  276. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><RateV4Request/>');
  277. $xml->addAttribute('USERID', $r->getUserId());
  278. // according to usps v4 documentation
  279. $xml->addChild('Revision', '2');
  280. $package = $xml->addChild('Package');
  281. $package->addAttribute('ID', 0);
  282. $service = $this->getCode('service_to_code', $r->getService());
  283. if (!$service) {
  284. $service = $r->getService();
  285. }
  286. if ($r->getContainer() == 'FLAT RATE BOX' || $r->getContainer() == 'FLAT RATE ENVELOPE') {
  287. $service = 'PRIORITY';
  288. }
  289. $package->addChild('Service', $service);
  290. // no matter Letter, Flat or Parcel, use Parcel
  291. if ($r->getService() == 'FIRST CLASS' || $r->getService() == 'FIRST CLASS HFP COMMERCIAL') {
  292. $package->addChild('FirstClassMailType', 'PARCEL');
  293. }
  294. $package->addChild('ZipOrigination', $r->getOrigPostal());
  295. //only 5 chars avaialble
  296. $package->addChild('ZipDestination', substr($r->getDestPostal(), 0, 5));
  297. $package->addChild('Pounds', $r->getWeightPounds());
  298. $package->addChild('Ounces', $r->getWeightOunces());
  299. // Because some methods don't accept VARIABLE and (NON)RECTANGULAR containers
  300. $package->addChild('Container', $r->getContainer());
  301. $package->addChild('Size', $r->getSize());
  302. if ($r->getSize() == 'LARGE') {
  303. $package->addChild('Width', $r->getWidth());
  304. $package->addChild('Length', $r->getLength());
  305. $package->addChild('Height', $r->getHeight());
  306. if ($r->getContainer() == 'NONRECTANGULAR' || $r->getContainer() == 'VARIABLE') {
  307. $package->addChild('Girth', $r->getGirth());
  308. }
  309. }
  310. $package->addChild('Machinable', $r->getMachinable());
  311. $api = 'RateV4';
  312. } else {
  313. $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><IntlRateV2Request/>');
  314. $xml->addAttribute('USERID', $r->getUserId());
  315. // according to usps v4 documentation
  316. $xml->addChild('Revision', '2');
  317. $package = $xml->addChild('Package');
  318. $package->addAttribute('ID', 0);
  319. $package->addChild('Pounds', $r->getWeightPounds());
  320. $package->addChild('Ounces', $r->getWeightOunces());
  321. $package->addChild('MailType', 'All');
  322. $package->addChild('ValueOfContents', $r->getValue());
  323. $package->addChild('Country', $r->getDestCountryName());
  324. $package->addChild('Container', $r->getContainer());
  325. $package->addChild('Size', $r->getSize());
  326. $width = $length = $height = $girth = '';
  327. if ($r->getSize() == 'LARGE') {
  328. $width = $r->getWidth();
  329. $length = $r->getLength();
  330. $height = $r->getHeight();
  331. if ($r->getContainer() == 'NONRECTANGULAR') {
  332. $girth = $r->getGirth();
  333. }
  334. }
  335. $package->addChild('Width', $width);
  336. $package->addChild('Length', $length);
  337. $package->addChild('Height', $height);
  338. $package->addChild('Girth', $girth);
  339. $api = 'IntlRateV2';
  340. }
  341. $request = $xml->asXML();
  342. $responseBody = $this->_getCachedQuotes($request);
  343. if ($responseBody === null) {
  344. $debugData = array('request' => $request);
  345. try {
  346. $url = $this->getConfigData('gateway_url');
  347. if (!$url) {
  348. $url = $this->_defaultGatewayUrl;
  349. }
  350. $client = new Zend_Http_Client();
  351. $client->setUri($url);
  352. $client->setConfig(array('maxredirects'=>0, 'timeout'=>30));
  353. $client->setParameterGet('API', $api);
  354. $client->setParameterGet('XML', $request);
  355. $response = $client->request();
  356. $responseBody = $response->getBody();
  357. $debugData['result'] = $responseBody;
  358. $this->_setCachedQuotes($request, $responseBody);
  359. } catch (Exception $e) {
  360. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  361. $responseBody = '';
  362. }
  363. $this->_debug($debugData);
  364. }
  365. return $this->_parseXmlResponse($responseBody);
  366. }
  367. /**
  368. * Parse calculated rates
  369. *
  370. * @link http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm
  371. * @param string $response
  372. * @return Mage_Shipping_Model_Rate_Result
  373. */
  374. protected function _parseXmlResponse($response)
  375. {
  376. $costArr = array();
  377. $priceArr = array();
  378. if (strlen(trim($response)) > 0) {
  379. if (strpos(trim($response), '<?xml') === 0) {
  380. if (strpos($response, '<?xml version="1.0"?>') !== false) {
  381. $response = str_replace(
  382. '<?xml version="1.0"?>',
  383. '<?xml version="1.0" encoding="ISO-8859-1"?>',
  384. $response
  385. );
  386. }
  387. $xml = simplexml_load_string($response);
  388. if (is_object($xml)) {
  389. if (is_object($xml->Number) && is_object($xml->Description) && (string)$xml->Description!='') {
  390. $errorTitle = (string)$xml->Description;
  391. } elseif (is_object($xml->Package)
  392. && is_object($xml->Package->Error)
  393. && is_object($xml->Package->Error->Description)
  394. && (string)$xml->Package->Error->Description!=''
  395. ) {
  396. $errorTitle = (string)$xml->Package->Error->Description;
  397. } else {
  398. $errorTitle = 'Unknown error';
  399. }
  400. $r = $this->_rawRequest;
  401. $allowedMethods = explode(",", $this->getConfigData('allowed_methods'));
  402. $allMethods = $this->getCode('method');
  403. $newMethod = false;
  404. if ($this->_isUSCountry($r->getDestCountryId())) {
  405. if (is_object($xml->Package) && is_object($xml->Package->Postage)) {
  406. foreach ($xml->Package->Postage as $postage) {
  407. $serviceName = $this->_filterServiceName((string)$postage->MailService);
  408. $postage->MailService = $serviceName;
  409. if (in_array($serviceName, $allowedMethods)) {
  410. $costArr[$serviceName] = (string)$postage->Rate;
  411. $priceArr[$serviceName] = $this->getMethodPrice(
  412. (string)$postage->Rate,
  413. $serviceName
  414. );
  415. } elseif (!in_array($serviceName, $allMethods)) {
  416. $allMethods[] = $serviceName;
  417. $newMethod = true;
  418. }
  419. }
  420. asort($priceArr);
  421. }
  422. } else {
  423. /*
  424. * International Rates
  425. */
  426. if (is_object($xml->Package) && is_object($xml->Package->Service)) {
  427. foreach ($xml->Package->Service as $service) {
  428. $serviceName = $this->_filterServiceName((string)$service->SvcDescription);
  429. $service->SvcDescription = $serviceName;
  430. if (in_array($serviceName, $allowedMethods)) {
  431. $costArr[$serviceName] = (string)$service->Postage;
  432. $priceArr[$serviceName] = $this->getMethodPrice(
  433. (string)$service->Postage,
  434. $serviceName
  435. );
  436. } elseif (!in_array($serviceName, $allMethods)) {
  437. $allMethods[] = $serviceName;
  438. $newMethod = true;
  439. }
  440. }
  441. asort($priceArr);
  442. }
  443. }
  444. /**
  445. * following if statement is obsolete
  446. * we don't have adminhtml/config resoure model
  447. */
  448. if (false && $newMethod) {
  449. sort($allMethods);
  450. $insert['usps']['fields']['methods']['value'] = $allMethods;
  451. Mage::getResourceModel('adminhtml/config')->saveSectionPost('carriers','','',$insert);
  452. }
  453. }
  454. } else {
  455. $errorTitle = 'Response is in the wrong format';
  456. }
  457. }
  458. $result = Mage::getModel('shipping/rate_result');
  459. if (empty($priceArr)) {
  460. $error = Mage::getModel('shipping/rate_result_error');
  461. $error->setCarrier('usps');
  462. $error->setCarrierTitle($this->getConfigData('title'));
  463. $error->setErrorMessage($this->getConfigData('specificerrmsg'));
  464. $result->append($error);
  465. } else {
  466. foreach ($priceArr as $method=>$price) {
  467. $rate = Mage::getModel('shipping/rate_result_method');
  468. $rate->setCarrier('usps');
  469. $rate->setCarrierTitle($this->getConfigData('title'));
  470. $rate->setMethod($method);
  471. $rate->setMethodTitle($method);
  472. $rate->setCost($costArr[$method]);
  473. $rate->setPrice($price);
  474. $result->append($rate);
  475. }
  476. }
  477. return $result;
  478. }
  479. /**
  480. * Get configuration data of carrier
  481. *
  482. * @param string $type
  483. * @param string $code
  484. * @return array|bool
  485. */
  486. public function getCode($type, $code='')
  487. {
  488. $codes = array(
  489. 'service'=>array(
  490. 'FIRST CLASS' => Mage::helper('usa')->__('First-Class'),
  491. 'PRIORITY' => Mage::helper('usa')->__('Priority Mail'),
  492. 'EXPRESS' => Mage::helper('usa')->__('Express Mail'),
  493. 'BPM' => Mage::helper('usa')->__('Bound Printed Matter'),
  494. 'PARCEL' => Mage::helper('usa')->__('Parcel Post'),
  495. 'MEDIA' => Mage::helper('usa')->__('Media Mail'),
  496. 'LIBRARY' => Mage::helper('usa')->__('Library'),
  497. ),
  498. 'service_to_code'=>array(
  499. 'First-Class' => 'FIRST CLASS',
  500. 'First-Class Mail International Large Envelope' => 'FIRST CLASS',
  501. 'First-Class Mail International Letter' => 'FIRST CLASS',
  502. 'First-Class Mail International Package' => 'FIRST CLASS',
  503. 'First-Class Mail International Parcel' => 'FIRST CLASS',
  504. 'First-Class Mail' => 'FIRST CLASS',
  505. 'First-Class Mail Flat' => 'FIRST CLASS',
  506. 'First-Class Mail Large Envelope' => 'FIRST CLASS',
  507. 'First-Class Mail International' => 'FIRST CLASS',
  508. 'First-Class Mail Letter' => 'FIRST CLASS',
  509. 'First-Class Mail Parcel' => 'FIRST CLASS',
  510. 'First-Class Mail Package' => 'FIRST CLASS',
  511. 'Parcel Post' => 'PARCEL',
  512. 'Bound Printed Matter' => 'BPM',
  513. 'Media Mail' => 'MEDIA',
  514. 'Library Mail' => 'LIBRARY',
  515. 'Express Mail' => 'EXPRESS',
  516. 'Express Mail PO to PO' => 'EXPRESS',
  517. 'Express Mail Flat Rate Envelope' => 'EXPRESS',
  518. 'Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee' => 'EXPRESS',
  519. 'Express Mail Sunday/Holiday Guarantee' => 'EXPRESS',
  520. 'Express Mail Flat Rate Envelope Hold For Pickup' => 'EXPRESS',
  521. 'Express Mail Hold For Pickup' => 'EXPRESS',
  522. 'Global Express Guaranteed (GXG)' => 'EXPRESS',
  523. 'Global Express Guaranteed Non-Document Rectangular' => 'EXPRESS',
  524. 'Global Express Guaranteed Non-Document Non-Rectangular' => 'EXPRESS',
  525. 'USPS GXG Envelopes' => 'EXPRESS',
  526. 'Express Mail International' => 'EXPRESS',
  527. 'Express Mail International Flat Rate Envelope' => 'EXPRESS',
  528. 'Priority Mail' => 'PRIORITY',
  529. 'Priority Mail Small Flat Rate Box' => 'PRIORITY',
  530. 'Priority Mail Medium Flat Rate Box' => 'PRIORITY',
  531. 'Priority Mail Large Flat Rate Box' => 'PRIORITY',
  532. 'Priority Mail Flat Rate Box' => 'PRIORITY',
  533. 'Priority Mail Flat Rate Envelope' => 'PRIORITY',
  534. 'Priority Mail International' => 'PRIORITY',
  535. 'Priority Mail International Flat Rate Envelope' => 'PRIORITY',
  536. 'Priority Mail International Small Flat Rate Box' => 'PRIORITY',
  537. 'Priority Mail International Medium Flat Rate Box' => 'PRIORITY',
  538. 'Priority Mail International Large Flat Rate Box' => 'PRIORITY',
  539. 'Priority Mail International Flat Rate Box' => 'PRIORITY'
  540. ),
  541. 'first_class_mail_type'=>array(
  542. 'LETTER' => Mage::helper('usa')->__('Letter'),
  543. 'FLAT' => Mage::helper('usa')->__('Flat'),
  544. 'PARCEL' => Mage::helper('usa')->__('Parcel'),
  545. ),
  546. 'container'=>array(
  547. 'VARIABLE' => Mage::helper('usa')->__('Variable'),
  548. 'FLAT RATE BOX' => Mage::helper('usa')->__('Flat-Rate Box'),
  549. 'FLAT RATE ENVELOPE' => Mage::helper('usa')->__('Flat-Rate Envelope'),
  550. 'RECTANGULAR' => Mage::helper('usa')->__('Rectangular'),
  551. 'NONRECTANGULAR' => Mage::helper('usa')->__('Non-rectangular'),
  552. ),
  553. 'containers_filter' => array(
  554. array(
  555. 'containers' => array('VARIABLE'),
  556. 'filters' => array(
  557. 'within_us' => array(
  558. 'method' => array(
  559. 'Express Mail Flat Rate Envelope',
  560. 'Express Mail Flat Rate Envelope Hold For Pickup',
  561. 'Priority Mail Flat Rate Envelope',
  562. 'Priority Mail Large Flat Rate Box',
  563. 'Priority Mail Medium Flat Rate Box',
  564. 'Priority Mail Small Flat Rate Box',
  565. 'Express Mail',
  566. 'Priority Mail',
  567. 'Parcel Post',
  568. 'Media Mail',
  569. 'First-Class Mail Large Envelope',
  570. )
  571. ),
  572. 'from_us' => array(
  573. 'method' => array(
  574. 'Express Mail International Flat Rate Envelope',
  575. 'Priority Mail International Flat Rate Envelope',
  576. 'Priority Mail International Large Flat Rate Box',
  577. 'Priority Mail International Medium Flat Rate Box',
  578. 'Priority Mail International Small Flat Rate Box',
  579. 'Global Express Guaranteed (GXG)',
  580. 'USPS GXG Envelopes',
  581. 'Express Mail International',
  582. 'Priority Mail International',
  583. 'First-Class Mail International Package',
  584. 'First-Class Mail International Large Envelope',
  585. 'First-Class Mail International Parcel',
  586. )
  587. )
  588. )
  589. ),
  590. array(
  591. 'containers' => array('FLAT RATE BOX'),
  592. 'filters' => array(
  593. 'within_us' => array(
  594. 'method' => array(
  595. 'Priority Mail Large Flat Rate Box',
  596. 'Priority Mail Medium Flat Rate Box',
  597. 'Priority Mail Small Flat Rate Box',
  598. )
  599. ),
  600. 'from_us' => array(
  601. 'method' => array(
  602. 'Priority Mail International Large Flat Rate Box',
  603. 'Priority Mail International Medium Flat Rate Box',
  604. 'Priority Mail International Small Flat Rate Box',
  605. )
  606. )
  607. )
  608. ),
  609. array(
  610. 'containers' => array('FLAT RATE ENVELOPE'),
  611. 'filters' => array(
  612. 'within_us' => array(
  613. 'method' => array(
  614. 'Express Mail Flat Rate Envelope',
  615. 'Express Mail Flat Rate Envelope Hold For Pickup',
  616. 'Priority Mail Flat Rate Envelope',
  617. )
  618. ),
  619. 'from_us' => array(
  620. 'method' => array(
  621. 'Express Mail International Flat Rate Envelope',
  622. 'Priority Mail International Flat Rate Envelope',
  623. )
  624. )
  625. )
  626. ),
  627. array(
  628. 'containers' => array('RECTANGULAR'),
  629. 'filters' => array(
  630. 'within_us' => array(
  631. 'method' => array(
  632. 'Express Mail',
  633. 'Priority Mail',
  634. 'Parcel Post',
  635. 'Media Mail',
  636. )
  637. ),
  638. 'from_us' => array(
  639. 'method' => array(
  640. 'USPS GXG Envelopes',
  641. 'Express Mail International',
  642. 'Priority Mail International',
  643. 'First-Class Mail International Package',
  644. 'First-Class Mail International Parcel',
  645. )
  646. )
  647. )
  648. ),
  649. array(
  650. 'containers' => array('NONRECTANGULAR'),
  651. 'filters' => array(
  652. 'within_us' => array(
  653. 'method' => array(
  654. 'Express Mail',
  655. 'Priority Mail',
  656. 'Parcel Post',
  657. 'Media Mail',
  658. )
  659. ),
  660. 'from_us' => array(
  661. 'method' => array(
  662. 'Global Express Guaranteed (GXG)',
  663. 'USPS GXG Envelopes',
  664. 'Express Mail International',
  665. 'Priority Mail International',
  666. 'First-Class Mail International Package',
  667. 'First-Class Mail International Parcel',
  668. )
  669. )
  670. )
  671. ),
  672. ),
  673. 'size'=>array(
  674. 'REGULAR' => Mage::helper('usa')->__('Regular'),
  675. 'LARGE' => Mage::helper('usa')->__('Large'),
  676. ),
  677. 'machinable'=>array(
  678. 'true' => Mage::helper('usa')->__('Yes'),
  679. 'false' => Mage::helper('usa')->__('No'),
  680. ),
  681. 'delivery_confirmation_types' => array(
  682. 'True' => Mage::helper('usa')->__('Not Required'),
  683. 'False' => Mage::helper('usa')->__('Required'),
  684. ),
  685. );
  686. $methods = $this->getConfigData('methods');
  687. if (!empty($methods)) {
  688. $codes['method'] = explode(",", $methods);
  689. } else {
  690. $codes['method'] = array();
  691. }
  692. if (!isset($codes[$type])) {
  693. return false;
  694. } elseif (''===$code) {
  695. return $codes[$type];
  696. }
  697. if (!isset($codes[$type][$code])) {
  698. return false;
  699. } else {
  700. return $codes[$type][$code];
  701. }
  702. }
  703. /**
  704. * Get tracking
  705. *
  706. * @param mixed $trackings
  707. * @return mixed
  708. */
  709. public function getTracking($trackings)
  710. {
  711. $this->setTrackingReqeust();
  712. if (!is_array($trackings)) {
  713. $trackings = array($trackings);
  714. }
  715. $this->_getXmlTracking($trackings);
  716. return $this->_result;
  717. }
  718. /**
  719. * Set tracking request
  720. *
  721. * @return null
  722. */
  723. protected function setTrackingReqeust()
  724. {
  725. $r = new Varien_Object();
  726. $userId = $this->getConfigData('userid');
  727. $r->setUserId($userId);
  728. $this->_rawTrackRequest = $r;
  729. }
  730. /**
  731. * Send request for tracking
  732. *
  733. * @param array $tracking
  734. * @return null
  735. */
  736. protected function _getXmlTracking($trackings)
  737. {
  738. $r = $this->_rawTrackRequest;
  739. foreach ($trackings as $tracking) {
  740. $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><TrackRequest/>');
  741. $xml->addAttribute('USERID', $r->getUserId());
  742. $trackid = $xml->addChild('TrackID');
  743. $trackid->addAttribute('ID',$tracking);
  744. $api = 'TrackV2';
  745. $request = $xml->asXML();
  746. $debugData = array('request' => $request);
  747. try {
  748. $url = $this->getConfigData('gateway_url');
  749. if (!$url) {
  750. $url = $this->_defaultGatewayUrl;
  751. }
  752. $client = new Zend_Http_Client();
  753. $client->setUri($url);
  754. $client->setConfig(array('maxredirects'=>0, 'timeout'=>30));
  755. $client->setParameterGet('API', $api);
  756. $client->setParameterGet('XML', $request);
  757. $response = $client->request();
  758. $responseBody = $response->getBody();
  759. $debugData['result'] = $responseBody;
  760. }
  761. catch (Exception $e) {
  762. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  763. $responseBody = '';
  764. }
  765. $this->_debug($debugData);
  766. $this->_parseXmlTrackingResponse($tracking, $responseBody);
  767. }
  768. }
  769. /**
  770. * Parse xml tracking response
  771. *
  772. * @param array $trackingvalue
  773. * @param string $response
  774. * @return null
  775. */
  776. protected function _parseXmlTrackingResponse($trackingvalue, $response)
  777. {
  778. $errorTitle = Mage::helper('usa')->__('Unable to retrieve tracking');
  779. $resultArr=array();
  780. if (strlen(trim($response)) > 0) {
  781. if (strpos(trim($response), '<?xml')===0) {
  782. $xml = simplexml_load_string($response);
  783. if (is_object($xml)) {
  784. if (isset($xml->Number) && isset($xml->Description) && (string)$xml->Description!='') {
  785. $errorTitle = (string)$xml->Description;
  786. } elseif (isset($xml->TrackInfo)
  787. && isset($xml->TrackInfo->Error)
  788. && isset($xml->TrackInfo->Error->Description)
  789. && (string)$xml->TrackInfo->Error->Description!=''
  790. ) {
  791. $errorTitle = (string)$xml->TrackInfo->Error->Description;
  792. } else {
  793. $errorTitle = Mage::helper('usa')->__('Unknown error');
  794. }
  795. if(isset($xml->TrackInfo) && isset($xml->TrackInfo->TrackSummary)){
  796. $resultArr['tracksummary'] = (string)$xml->TrackInfo->TrackSummary;
  797. }
  798. }
  799. }
  800. }
  801. if (!$this->_result) {
  802. $this->_result = Mage::getModel('shipping/tracking_result');
  803. }
  804. $defaults = $this->getDefaults();
  805. if ($resultArr) {
  806. $tracking = Mage::getModel('shipping/tracking_result_status');
  807. $tracking->setCarrier('usps');
  808. $tracking->setCarrierTitle($this->getConfigData('title'));
  809. $tracking->setTracking($trackingvalue);
  810. $tracking->setTrackSummary($resultArr['tracksummary']);
  811. $this->_result->append($tracking);
  812. } else {
  813. $error = Mage::getModel('shipping/tracking_result_error');
  814. $error->setCarrier('usps');
  815. $error->setCarrierTitle($this->getConfigData('title'));
  816. $error->setTracking($trackingvalue);
  817. $error->setErrorMessage($errorTitle);
  818. $this->_result->append($error);
  819. }
  820. }
  821. /**
  822. * Get tracking response
  823. *
  824. * @return string
  825. */
  826. public function getResponse()
  827. {
  828. $statuses = '';
  829. if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result) {
  830. if ($trackings = $this->_result->getAllTrackings()) {
  831. foreach ($trackings as $tracking) {
  832. if($data = $tracking->getAllData()) {
  833. if (!empty($data['track_summary'])) {
  834. $statuses .= Mage::helper('usa')->__($data['track_summary']);
  835. } else {
  836. $statuses .= Mage::helper('usa')->__('Empty response');
  837. }
  838. }
  839. }
  840. }
  841. }
  842. if (empty($statuses)) {
  843. $statuses = Mage::helper('usa')->__('Empty response');
  844. }
  845. return $statuses;
  846. }
  847. /**
  848. * Get allowed shipping methods
  849. *
  850. * @return array
  851. */
  852. public function getAllowedMethods()
  853. {
  854. $allowed = explode(',', $this->getConfigData('allowed_methods'));
  855. $arr = array();
  856. foreach ($allowed as $k) {
  857. $arr[$k] = $k;
  858. }
  859. return $arr;
  860. }
  861. /**
  862. * Return USPS county name by country ISO 3166-1-alpha-2 code
  863. * Return false for unknown countries
  864. *
  865. * @param string $countryId
  866. * @return string|false
  867. */
  868. protected function _getCountryName($countryId)
  869. {
  870. $countries = array (
  871. 'AD' => 'Andorra',
  872. 'AE' => 'United Arab Emirates',
  873. 'AF' => 'Afghanistan',
  874. 'AG' => 'Antigua and Barbuda',
  875. 'AI' => 'Anguilla',
  876. 'AL' => 'Albania',
  877. 'AM' => 'Armenia',
  878. 'AN' => 'Netherlands Antilles',
  879. 'AO' => 'Angola',
  880. 'AR' => 'Argentina',
  881. 'AT' => 'Austria',
  882. 'AU' => 'Australia',
  883. 'AW' => 'Aruba',
  884. 'AX' => 'Aland Island (Finland)',
  885. 'AZ' => 'Azerbaijan',
  886. 'BA' => 'Bosnia-Herzegovina',
  887. 'BB' => 'Barbados',
  888. 'BD' => 'Bangladesh',
  889. 'BE' => 'Belgium',
  890. 'BF' => 'Burkina Faso',
  891. 'BG' => 'Bulgaria',
  892. 'BH' => 'Bahrain',
  893. 'BI' => 'Burundi',
  894. 'BJ' => 'Benin',
  895. 'BM' => 'Bermuda',
  896. 'BN' => 'Brunei Darussalam',
  897. 'BO' => 'Bolivia',
  898. 'BR' => 'Brazil',
  899. 'BS' => 'Bahamas',
  900. 'BT' => 'Bhutan',
  901. 'BW' => 'Botswana',
  902. 'BY' => 'Belarus',
  903. 'BZ' => 'Belize',
  904. 'CA' => 'Canada',
  905. 'CC' => 'Cocos Island (Australia)',
  906. 'CD' => 'Congo, Democratic Republic of the',
  907. 'CF' => 'Central African Republic',
  908. 'CG' => 'Congo, Republic of the',
  909. 'CH' => 'Switzerland',
  910. 'CI' => 'Cote d Ivoire (Ivory Coast)',
  911. 'CK' => 'Cook Islands (New Zealand)',
  912. 'CL' => 'Chile',
  913. 'CM' => 'Cameroon',
  914. 'CN' => 'China',
  915. 'CO' => 'Colombia',
  916. 'CR' => 'Costa Rica',
  917. 'CU' => 'Cuba',
  918. 'CV' => 'Cape Verde',
  919. 'CX' => 'Christmas Island (Australia)',
  920. 'CY' => 'Cyprus',
  921. 'CZ' => 'Czech Republic',
  922. 'DE' => 'Germany',
  923. 'DJ' => 'Djibouti',
  924. 'DK' => 'Denmark',
  925. 'DM' => 'Dominica',
  926. 'DO' => 'Dominican Republic',
  927. 'DZ' => 'Algeria',
  928. 'EC' => 'Ecuador',
  929. 'EE' => 'Estonia',
  930. 'EG' => 'Egypt',
  931. 'ER' => 'Eritrea',
  932. 'ES' => 'Spain',
  933. 'ET' => 'Ethiopia',
  934. 'FI' => 'Finland',
  935. 'FJ' => 'Fiji',
  936. 'FK' => 'Falkland Islands',
  937. 'FM' => 'Micronesia, Federated States of',
  938. 'FO' => 'Faroe Islands',
  939. 'FR' => 'France',
  940. 'GA' => 'Gabon',
  941. 'GB' => 'Great Britain and Northern Ireland',
  942. 'GD' => 'Grenada',
  943. 'GE' => 'Georgia, Republic of',
  944. 'GF' => 'French Guiana',
  945. 'GH' => 'Ghana',
  946. 'GI' => 'Gibraltar',
  947. 'GL' => 'Greenland',
  948. 'GM' => 'Gambia',
  949. 'GN' => 'Guinea',
  950. 'GP' => 'Guadeloupe',
  951. 'GQ' => 'Equatorial Guinea',
  952. 'GR' => 'Greece',
  953. 'GS' => 'South Georgia (Falkland Islands)',
  954. 'GT' => 'Guatemala',
  955. 'GW' => 'Guinea-Bissau',
  956. 'GY' => 'Guyana',
  957. 'HK' => 'Hong Kong',
  958. 'HN' => 'Honduras',
  959. 'HR' => 'Croatia',
  960. 'HT' => 'Haiti',
  961. 'HU' => 'Hungary',
  962. 'ID' => 'Indonesia',
  963. 'IE' => 'Ireland',
  964. 'IL' => 'Israel',
  965. 'IN' => 'India',
  966. 'IQ' => 'Iraq',
  967. 'IR' => 'Iran',
  968. 'IS' => 'Iceland',
  969. 'IT' => 'Italy',
  970. 'JM' => 'Jamaica',
  971. 'JO' => 'Jordan',
  972. 'JP' => 'Japan',
  973. 'KE' => 'Kenya',
  974. 'KG' => 'Kyrgyzstan',
  975. 'KH' => 'Cambodia',
  976. 'KI' => 'Kiribati',
  977. 'KM' => 'Comoros',
  978. 'KN' => 'Saint Kitts (St. Christopher and Nevis)',
  979. 'KP' => 'North Korea (Korea, Democratic People\'s Republic of)',
  980. 'KR' => 'South Korea (Korea, Republic of)',
  981. 'KW' => 'Kuwait',
  982. 'KY' => 'Cayman Islands',
  983. 'KZ' => 'Kazakhstan',
  984. 'LA' => 'Laos',
  985. 'LB' => 'Lebanon',
  986. 'LC' => 'Saint Lucia',
  987. 'LI' => 'Liechtenstein',
  988. 'LK' => 'Sri Lanka',
  989. 'LR' => 'Liberia',
  990. 'LS' => 'Lesotho',
  991. 'LT' => 'Lithuania',
  992. 'LU' => 'Luxembourg',
  993. 'LV' => 'Latvia',
  994. 'LY' => 'Libya',
  995. 'MA' => 'Morocco',
  996. 'MC' => 'Monaco (France)',
  997. 'MD' => 'Moldova',
  998. 'MG' => 'Madagascar',
  999. 'MK' => 'Macedonia, Republic of',
  1000. 'ML' => 'Mali',
  1001. 'MM' => 'Burma',
  1002. 'MN' => 'Mongolia',
  1003. 'MO' => 'Macao',
  1004. 'MQ' => 'Martinique',
  1005. 'MR' => 'Mauritania',
  1006. 'MS' => 'Montserrat',
  1007. 'MT' => 'Malta',
  1008. 'MU' => 'Mauritius',
  1009. 'MV' => 'Maldives',
  1010. 'MW' => 'Malawi',
  1011. 'MX' => 'Mexico',
  1012. 'MY' => 'Malaysia',
  1013. 'MZ' => 'Mozambique',
  1014. 'NA' => 'Namibia',
  1015. 'NC' => 'New Caledonia',
  1016. 'NE' => 'Niger',
  1017. 'NG' => 'Nigeria',
  1018. 'NI' => 'Nicaragua',
  1019. 'NL' => 'Netherlands',
  1020. 'NO' => 'Norway',
  1021. 'NP' => 'Nepal',
  1022. 'NR' => 'Nauru',
  1023. 'NZ' => 'New Zealand',
  1024. 'OM' => 'Oman',
  1025. 'PA' => 'Panama',
  1026. 'PE' => 'Peru',
  1027. 'PF' => 'French Polynesia',
  1028. 'PG' => 'Papua New Guinea',
  1029. 'PH' => 'Philippines',
  1030. 'PK' => 'Pakistan',
  1031. 'PL' => 'Poland',
  1032. 'PM' => 'Saint Pierre and Miquelon',
  1033. 'PN' => 'Pitcairn Island',
  1034. 'PT' => 'Portugal',
  1035. 'PY' => 'Paraguay',
  1036. 'QA' => 'Qatar',
  1037. 'RE' => 'Reunion',
  1038. 'RO' => 'Romania',
  1039. 'RS' => 'Serbia',
  1040. 'RU' => 'Russia',
  1041. 'RW' => 'Rwanda',
  1042. 'SA' => 'Saudi Arabia',
  1043. 'SB' => 'Solomon Islands',
  1044. 'SC' => 'Seychelles',
  1045. 'SD' => 'Sudan',
  1046. 'SE' => 'Sweden',
  1047. 'SG' => 'Singapore',
  1048. 'SH' => 'Saint Helena',
  1049. 'SI' => 'Slovenia',
  1050. 'SK' => 'Slovak Republic',
  1051. 'SL' => 'Sierra Leone',
  1052. 'SM' => 'San Marino',
  1053. 'SN' => 'Senegal',
  1054. 'SO' => 'Somalia',
  1055. 'SR' => 'Suriname',
  1056. 'ST' => 'Sao Tome and Principe',
  1057. 'SV' => 'El Salvador',
  1058. 'SY' => 'Syrian Arab Republic',
  1059. 'SZ' => 'Swaziland',
  1060. 'TC' => 'Turks and Caicos Islands',
  1061. 'TD' => 'Chad',
  1062. 'TG' => 'Togo',
  1063. 'TH' => 'Thailand',
  1064. 'TJ' => 'Tajikistan',
  1065. 'TK' => 'Tokelau (Union) Group (Western Samoa)',
  1066. 'TL' => 'East Timor (Indonesia)',
  1067. 'TM' => 'Turkmenistan',
  1068. 'TN' => 'Tunisia',
  1069. 'TO' => 'Tonga',
  1070. 'TR' => 'Turkey',
  1071. 'TT' => 'Trinidad and Tobago',
  1072. 'TV' => 'Tuvalu',
  1073. 'TW' => 'Taiwan',
  1074. 'TZ' => 'Tanzania',
  1075. 'UA' => 'Ukraine',
  1076. 'UG' => 'Uganda',
  1077. 'UY' => 'Uruguay',
  1078. 'UZ' => 'Uzbekistan',
  1079. 'VA' => 'Vatican City',
  1080. 'VC' => 'Saint Vincent and the Grenadines',
  1081. 'VE' => 'Venezuela',
  1082. 'VG' => 'British Virgin Islands',
  1083. 'VN' => 'Vietnam',
  1084. 'VU' => 'Vanuatu',
  1085. 'WF' => 'Wallis and Futuna Islands',
  1086. 'WS' => 'Western Samoa',
  1087. 'YE' => 'Yemen',
  1088. 'YT' => 'Mayotte (France)',
  1089. 'ZA' => 'South Africa',
  1090. 'ZM' => 'Zambia',
  1091. 'ZW' => 'Zimbabwe',
  1092. 'US' => 'United States',
  1093. );
  1094. if (isset($countries[$countryId])) {
  1095. return $countries[$countryId];
  1096. }
  1097. return false;
  1098. }
  1099. /**
  1100. * Clean service name from unsupported strings and characters
  1101. *
  1102. * @param string $name
  1103. * @return string
  1104. */
  1105. protected function _filterServiceName($name)
  1106. {
  1107. $name = (string)preg_replace(array('~<[^/!][^>]+>.*</[^>]+>~sU', '~\<!--.*--\>~isU', '~<[^>]+>~is'), '',
  1108. html_entity_decode($name)
  1109. );
  1110. $name = str_replace('*', '', $name);
  1111. return $name;
  1112. }
  1113. /**
  1114. * Form XML for US shipment request
  1115. * As integration guide it is important to follow appropriate sequence for tags e.g.: <FromLastName /> must be
  1116. * after <FromFirstName />
  1117. *
  1118. * @param Varien_Object $request
  1119. * @return string
  1120. */
  1121. protected function _formUsExpressShipmentRequest(Varien_Object $request)
  1122. {
  1123. $packageParams = $request->getPackageParams();
  1124. $packageWeight = $request->getPackageWeight();
  1125. if ($packageParams->getWeightUnits() != Zend_Measure_Weight::OUNCE) {
  1126. $packageWeight = round(Mage::helper('usa')->convertMeasureWeight(
  1127. $request->getPackageWeight(),
  1128. $packageParams->getWeightUnits(),
  1129. Zend_Measure_Weight::OUNCE
  1130. ));
  1131. }
  1132. list($fromZip5, $fromZip4) = $this->_parseZip($request->getShipperAddressPostalCode());
  1133. list($toZip5, $toZip4) = $this->_parseZip($request->getRecipientAddressPostalCode(), true);
  1134. $rootNode = 'ExpressMailLabelRequest';
  1135. // the wrap node needs for remove xml declaration above
  1136. $xmlWrap = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><wrap/>');
  1137. $xml = $xmlWrap->addChild($rootNode);
  1138. $xml->addAttribute('USERID', $this->getConfigData('userid'));
  1139. $xml->addAttribute('PASSWORD', $this->getConfigData('password'));
  1140. $xml->addChild('Option');
  1141. $xml->addChild('Revision');
  1142. $xml->addChild('EMCAAccount');
  1143. $xml->addChild('EMCAPassword');
  1144. $xml->addChild('ImageParameters');
  1145. $xml->addChild('FromFirstName', $request->getShipperContactPersonFirstName());
  1146. $xml->addChild('FromLastName', $request->getShipperContactPersonLastName());
  1147. $xml->addChild('FromFirm', $request->getShipperContactCompanyName());
  1148. $xml->addChild('FromAddress1', $request->getShipperAddressStreet2());
  1149. $xml->addChild('FromAddress2', $request->getShipperAddressStreet1());
  1150. $xml->addChild('FromCity', $request->getShipperAddressCity());
  1151. $xml->addChild('FromState', $request->getShipperAddressStateOrProvinceCode());
  1152. $xml->addChild('FromZip5', $fromZip5);
  1153. $xml->addChild('FromZip4', $fromZip4);
  1154. $xml->addChild('FromPhone', $request->getShipperContactPhoneNumber());
  1155. $xml->addChild('ToFirstName', $request->getRecipientContactPersonFirstName());
  1156. $xml->addChild('ToLastName', $request->getRecipientContactPersonLastName());
  1157. $xml->addChild('ToFirm', $request->getRecipientContactCompanyName());
  1158. $xml->addChild('ToAddress1', $request->getRecipientAddressStreet2());
  1159. $xml->addChild('ToAddress2', $request->getRecipientAddressStreet1());
  1160. $xml->addChild('ToCity', $request->getRecipientAddressCity());
  1161. $xml->addChild('ToState', $request->getRecipientAddressStateOrProvinceCode());
  1162. $xml->addChild('ToZip5', $toZip5);
  1163. $xml->addChild('ToZip4', $toZip4);
  1164. $xml->addChild('ToPhone', $request->getRecipientContactPhoneNumber());
  1165. $xml->addChild('WeightInOunces', $packageWeight);
  1166. $xml->addChild('WaiverOfSignature', $packageParams->getDeliveryConfirmation());
  1167. $xml->addChild('POZipCode');
  1168. $xml->addChild('ImageType', 'PDF');
  1169. $xml = $xmlWrap->{$rootNode}->asXML();
  1170. return $xml;
  1171. }
  1172. /**
  1173. * Form XML for US Signature Confirmation request
  1174. * As integration guide it is important to follow appropriate sequence for tags e.g.: <FromLastName /> must be
  1175. * after <FromFirstName />
  1176. *
  1177. * @param Varien_Object $request
  1178. * @param string $serviceType
  1179. * @return string
  1180. */
  1181. protected function _formUsSignatureConfirmationShipmentRequest(Varien_Object $request, $serviceType)
  1182. {
  1183. switch ($serviceType) {
  1184. case 'PRIORITY':
  1185. $serviceType = 'Priority';
  1186. break;
  1187. case 'FIRST CLASS':
  1188. $serviceType = 'First Class';
  1189. break;
  1190. case 'PARCEL':
  1191. $serviceType = 'Parcel Post';
  1192. break;
  1193. case 'MEDIA':
  1194. $serviceType = 'Media Mail';
  1195. break;
  1196. case 'LIBRARY':
  1197. $serviceType = 'Library Mail';
  1198. break;
  1199. default:
  1200. throw new Exception(Mage::helper('usa')->__('Service type does not match'));
  1201. }
  1202. $packageParams = $request->getPackageParams();
  1203. $packageWeight = $request->getPackageWeight();
  1204. if ($packageParams->getWeightUnits() != Zend_Measure_Weight::OUNCE) {
  1205. $packageWeight = round(Mage::helper('usa')->convertMeasureWeight(
  1206. $request->getPackageWeight(),
  1207. $packageParams->getWeightUnits(),
  1208. Zend_Measure_Weight::OUNCE
  1209. ));
  1210. }
  1211. list($fromZip5, $fromZip4) = $this->_parseZip($request->getShipperAddressPostalCode());
  1212. list($toZip5, $toZip4) = $this->_parseZip($request->getRecipientAddressPostalCode(), true);
  1213. if ($this->getConfigData('mode')) {
  1214. $rootNode = 'SignatureConfirmationV3.0Request';
  1215. } else {
  1216. $rootNode = 'SigConfirmCertifyV3.0Request';
  1217. }
  1218. // the wrap node needs for remove xml declaration above
  1219. $xmlWrap = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><wrap/>');
  1220. $

Large files files are truncated, but you can click here to view the full file