PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 910 lines | 697 code | 93 blank | 120 comment | 74 complexity | ee9469858c8e10e7ccc6c40bc4ce4cde MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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. * Destination Zip Code required flag
  40. *
  41. * @var boolean
  42. */
  43. protected $_isZipCodeRequired;
  44. protected $_code = 'usps';
  45. protected $_request = null;
  46. protected $_result = null;
  47. protected $_defaultGatewayUrl = 'http://production.shippingapis.com/ShippingAPI.dll';
  48. /**
  49. * Check is Zip Code Required
  50. *
  51. * @return boolean
  52. */
  53. public function isZipCodeRequired()
  54. {
  55. if (!is_null($this->_isZipCodeRequired)) {
  56. return $this->_isZipCodeRequired;
  57. }
  58. return parent::isZipCodeRequired();
  59. }
  60. /**
  61. * Processing additional validation to check is carrier applicable.
  62. *
  63. * @param Mage_Shipping_Model_Rate_Request $request
  64. * @return Mage_Shipping_Model_Carrier_Abstract|Mage_Shipping_Model_Rate_Result_Error|boolean
  65. */
  66. public function proccessAdditionalValidation(Mage_Shipping_Model_Rate_Request $request)
  67. {
  68. // zip code required for US
  69. $this->_isZipCodeRequired = $this->_isUSCountry($request->getDestCountryId());
  70. return parent::proccessAdditionalValidation($request);
  71. }
  72. public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  73. {
  74. if (!$this->getConfigFlag('active')) {
  75. return false;
  76. }
  77. $this->setRequest($request);
  78. $this->_result = $this->_getQuotes();
  79. $this->_updateFreeMethodQuote($request);
  80. return $this->getResult();
  81. }
  82. public function setRequest(Mage_Shipping_Model_Rate_Request $request)
  83. {
  84. $this->_request = $request;
  85. $r = new Varien_Object();
  86. if ($request->getLimitMethod()) {
  87. $r->setService($request->getLimitMethod());
  88. } else {
  89. $r->setService('ALL');
  90. }
  91. if ($request->getUspsUserid()) {
  92. $userId = $request->getUspsUserid();
  93. } else {
  94. $userId = $this->getConfigData('userid');
  95. }
  96. $r->setUserId($userId);
  97. if ($request->getUspsContainer()) {
  98. $container = $request->getUspsContainer();
  99. } else {
  100. $container = $this->getConfigData('container');
  101. }
  102. $r->setContainer($container);
  103. if ($request->getUspsSize()) {
  104. $size = $request->getUspsSize();
  105. } else {
  106. $size = $this->getConfigData('size');
  107. }
  108. $r->setSize($size);
  109. if ($request->getUspsMachinable()) {
  110. $machinable = $request->getUspsMachinable();
  111. } else {
  112. $machinable = $this->getConfigData('machinable');
  113. }
  114. $r->setMachinable($machinable);
  115. if ($request->getOrigPostcode()) {
  116. $r->setOrigPostal($request->getOrigPostcode());
  117. } else {
  118. $r->setOrigPostal(Mage::getStoreConfig(Mage_Shipping_Model_Config::XML_PATH_ORIGIN_POSTCODE, $this->getStore()));
  119. }
  120. if ($request->getDestCountryId()) {
  121. $destCountry = $request->getDestCountryId();
  122. } else {
  123. $destCountry = self::USA_COUNTRY_ID;
  124. }
  125. $r->setDestCountryId($destCountry);
  126. if (!$this->_isUSCountry($destCountry)) {
  127. $r->setDestCountryName($this->_getCountryName($destCountry));
  128. }
  129. if ($request->getDestPostcode()) {
  130. $r->setDestPostal($request->getDestPostcode());
  131. }
  132. $weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
  133. $r->setWeightPounds(floor($weight));
  134. $r->setWeightOunces(round(($weight-floor($weight)) * 16, 1));
  135. if ($request->getFreeMethodWeight()!=$request->getPackageWeight()) {
  136. $r->setFreeMethodWeight($request->getFreeMethodWeight());
  137. }
  138. $r->setValue($request->getPackageValue());
  139. $r->setValueWithDiscount($request->getPackageValueWithDiscount());
  140. $this->_rawRequest = $r;
  141. return $this;
  142. }
  143. public function getResult()
  144. {
  145. return $this->_result;
  146. }
  147. protected function _getQuotes()
  148. {
  149. return $this->_getXmlQuotes();
  150. }
  151. protected function _setFreeMethodRequest($freeMethod)
  152. {
  153. $r = $this->_rawRequest;
  154. $weight = $this->getTotalNumOfBoxes($r->getFreeMethodWeight());
  155. $r->setWeightPounds(floor($weight));
  156. $r->setWeightOunces(round(($weight-floor($weight)) * 16, 1));
  157. $r->setService($freeMethod);
  158. }
  159. /**
  160. * Build RateV3 request, send it to USPS gateway and retrieve quotes in XML format
  161. *
  162. * @link http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm
  163. * @return Mage_Shipping_Model_Rate_Result
  164. */
  165. protected function _getXmlQuotes()
  166. {
  167. $r = $this->_rawRequest;
  168. if ($this->_isUSCountry($r->getDestCountryId())) {
  169. $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><RateV3Request/>');
  170. $xml->addAttribute('USERID', $r->getUserId());
  171. $package = $xml->addChild('Package');
  172. $package->addAttribute('ID', 0);
  173. $service = $this->getCode('service_to_code', $r->getService());
  174. if (!$service) {
  175. $service = $r->getService();
  176. }
  177. $package->addChild('Service', $service);
  178. // no matter Letter, Flat or Parcel, use Parcel
  179. if ($r->getService() == 'FIRST CLASS') {
  180. $package->addChild('FirstClassMailType', 'PARCEL');
  181. }
  182. $package->addChild('ZipOrigination', $r->getOrigPostal());
  183. //only 5 chars avaialble
  184. $package->addChild('ZipDestination', substr($r->getDestPostal(),0,5));
  185. $package->addChild('Pounds', $r->getWeightPounds());
  186. $package->addChild('Ounces', $r->getWeightOunces());
  187. // $package->addChild('Pounds', '0');
  188. // $package->addChild('Ounces', '3');
  189. // Because some methods don't accept VARIABLE and (NON)RECTANGULAR containers
  190. if (strtoupper($r->getContainer()) == 'FLAT RATE ENVELOPE' || strtoupper($r->getContainer()) == 'FLAT RATE BOX') {
  191. $package->addChild('Container', $r->getContainer());
  192. }
  193. $package->addChild('Size', $r->getSize());
  194. $package->addChild('Machinable', $r->getMachinable());
  195. $api = 'RateV3';
  196. $request = $xml->asXML();
  197. } else {
  198. $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><IntlRateRequest/>');
  199. $xml->addAttribute('USERID', $r->getUserId());
  200. $package = $xml->addChild('Package');
  201. $package->addAttribute('ID', 0);
  202. $package->addChild('Pounds', $r->getWeightPounds());
  203. $package->addChild('Ounces', $r->getWeightOunces());
  204. $package->addChild('MailType', 'Package');
  205. $package->addChild('ValueOfContents', $r->getValue());
  206. $package->addChild('Country', $r->getDestCountryName());
  207. $api = 'IntlRate';
  208. $request = $xml->asXML();
  209. }
  210. $responseBody = $this->_getCachedQuotes($request);
  211. if ($responseBody === null) {
  212. $debugData = array('request' => $request);
  213. try {
  214. $url = $this->getConfigData('gateway_url');
  215. if (!$url) {
  216. $url = $this->_defaultGatewayUrl;
  217. }
  218. $client = new Zend_Http_Client();
  219. $client->setUri($url);
  220. $client->setConfig(array('maxredirects'=>0, 'timeout'=>30));
  221. $client->setParameterGet('API', $api);
  222. $client->setParameterGet('XML', $request);
  223. $response = $client->request();
  224. $responseBody = $response->getBody();
  225. $debugData['result'] = $responseBody;
  226. $this->_setCachedQuotes($request, $responseBody);
  227. }
  228. catch (Exception $e) {
  229. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  230. $responseBody = '';
  231. }
  232. $this->_debug($debugData);
  233. }
  234. return $this->_parseXmlResponse($responseBody);
  235. }
  236. /**
  237. * Parse calculated rates
  238. *
  239. * @link http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm
  240. * @param string $response
  241. * @return Mage_Shipping_Model_Rate_Result
  242. */
  243. protected function _parseXmlResponse($response)
  244. {
  245. $costArr = array();
  246. $priceArr = array();
  247. if (strlen(trim($response)) > 0) {
  248. if (strpos(trim($response), '<?xml') === 0) {
  249. if (preg_match('#<\?xml version="1.0"\?>#', $response)) {
  250. $response = str_replace('<?xml version="1.0"?>', '<?xml version="1.0" encoding="ISO-8859-1"?>', $response);
  251. }
  252. $xml = simplexml_load_string($response);
  253. if (is_object($xml)) {
  254. $r = $this->_rawRequest;
  255. $allowedMethods = explode(",", $this->getConfigData('allowed_methods'));
  256. /*
  257. * US Domestic Rates
  258. */
  259. if ($this->_isUSCountry($r->getDestCountryId())) {
  260. if (is_object($xml->Package) && is_object($xml->Package->Postage)) {
  261. foreach ($xml->Package->Postage as $postage) {
  262. $serviceName = $this->_filterServiceName((string)$postage->MailService);
  263. $postage->MailService = $serviceName;
  264. if (in_array($serviceName, $allowedMethods)) {
  265. $costArr[$serviceName] = (string)$postage->Rate;
  266. $priceArr[$serviceName] = $this->getMethodPrice((string)$postage->Rate, $serviceName);
  267. }
  268. }
  269. asort($priceArr);
  270. }
  271. } else {
  272. /*
  273. * International Rates
  274. */
  275. if (is_object($xml->Package) && is_object($xml->Package->Service)) {
  276. foreach ($xml->Package->Service as $service) {
  277. $serviceName = $this->_filterServiceName((string)$service->SvcDescription);
  278. $service->SvcDescription = $serviceName;
  279. if (in_array($serviceName, $allowedMethods)) {
  280. $costArr[$serviceName] = (string)$service->Postage;
  281. $priceArr[$serviceName] = $this->getMethodPrice((string)$service->Postage, $serviceName);
  282. }
  283. }
  284. asort($priceArr);
  285. }
  286. }
  287. }
  288. }
  289. }
  290. $result = Mage::getModel('shipping/rate_result');
  291. if (empty($priceArr)) {
  292. $error = Mage::getModel('shipping/rate_result_error');
  293. $error->setCarrier('usps');
  294. $error->setCarrierTitle($this->getConfigData('title'));
  295. $error->setErrorMessage($this->getConfigData('specificerrmsg'));
  296. $result->append($error);
  297. } else {
  298. foreach ($priceArr as $method=>$price) {
  299. $rate = Mage::getModel('shipping/rate_result_method');
  300. $rate->setCarrier('usps');
  301. $rate->setCarrierTitle($this->getConfigData('title'));
  302. $rate->setMethod($method);
  303. $rate->setMethodTitle($method);
  304. $rate->setCost($costArr[$method]);
  305. $rate->setPrice($price);
  306. $result->append($rate);
  307. }
  308. }
  309. return $result;
  310. }
  311. public function getCode($type, $code='')
  312. {
  313. $codes = array(
  314. 'service'=>array(
  315. 'FIRST CLASS' => Mage::helper('usa')->__('First-Class'),
  316. 'PRIORITY' => Mage::helper('usa')->__('Priority Mail'),
  317. 'EXPRESS' => Mage::helper('usa')->__('Express Mail'),
  318. 'BPM' => Mage::helper('usa')->__('Bound Printed Matter'),
  319. 'PARCEL' => Mage::helper('usa')->__('Parcel Post'),
  320. 'MEDIA' => Mage::helper('usa')->__('Media Mail'),
  321. 'LIBRARY' => Mage::helper('usa')->__('Library'),
  322. // 'ALL' => Mage::helper('usa')->__('All Services'),
  323. ),
  324. /*
  325. 'method'=>array(
  326. 'First-Class',
  327. 'Express Mail',
  328. 'Express Mail PO to PO',
  329. 'Priority Mail',
  330. 'Parcel Post',
  331. 'Express Mail Flat-Rate Envelope',
  332. 'Priority Mail Flat-Rate Box',
  333. 'Bound Printed Matter',
  334. 'Media Mail',
  335. 'Library Mail',
  336. 'Priority Mail Flat-Rate Envelope',
  337. 'Global Express Guaranteed',
  338. 'Global Express Guaranteed Non-Document Rectangular',
  339. 'Global Express Guaranteed Non-Document Non-Rectangular',
  340. 'Express Mail International (EMS)',
  341. 'Express Mail International (EMS) Flat Rate Envelope',
  342. 'Priority Mail International',
  343. 'Priority Mail International Flat Rate Box',
  344. ),
  345. */
  346. 'service_to_code'=>array(
  347. 'First-Class' => 'FIRST CLASS',
  348. 'First-Class Mail International Large Envelope' => 'FIRST CLASS',
  349. 'First-Class Mail International Letter' => 'FIRST CLASS',
  350. 'First-Class Mail International Package' => 'FIRST CLASS',
  351. 'First-Class Mail' => 'FIRST CLASS',
  352. 'First-Class Mail Flat' => 'FIRST CLASS',
  353. 'First-Class Mail Large Envelope' => 'FIRST CLASS',
  354. 'First-Class Mail International' => 'FIRST CLASS',
  355. 'First-Class Mail Letter' => 'FIRST CLASS',
  356. 'First-Class Mail Parcel' => 'FIRST CLASS',
  357. 'First-Class Mail Package' => 'FIRST CLASS',
  358. 'Parcel Post' => 'PARCEL',
  359. 'Bound Printed Matter' => 'BPM',
  360. 'Media Mail' => 'MEDIA',
  361. 'Library Mail' => 'LIBRARY',
  362. 'Express Mail' => 'EXPRESS',
  363. 'Express Mail PO to PO' => 'EXPRESS',
  364. 'Express Mail Flat Rate Envelope' => 'EXPRESS',
  365. 'Express Mail Flat Rate Envelope Hold For Pickup' => 'EXPRESS',
  366. 'Global Express Guaranteed (GXG)' => 'EXPRESS',
  367. 'Global Express Guaranteed Non-Document Rectangular' => 'EXPRESS',
  368. 'Global Express Guaranteed Non-Document Non-Rectangular' => 'EXPRESS',
  369. 'Express Mail International' => 'EXPRESS',
  370. 'Express Mail International Flat Rate Envelope' => 'EXPRESS',
  371. 'Priority Mail' => 'PRIORITY',
  372. 'Priority Mail Small Flat Rate Box' => 'PRIORITY',
  373. 'Priority Mail Medium Flat Rate Box' => 'PRIORITY',
  374. 'Priority Mail Large Flat Rate Box' => 'PRIORITY',
  375. 'Priority Mail Flat Rate Box' => 'PRIORITY',
  376. 'Priority Mail Flat Rate Envelope' => 'PRIORITY',
  377. 'Priority Mail International' => 'PRIORITY',
  378. 'Priority Mail International Flat Rate Envelope' => 'PRIORITY',
  379. 'Priority Mail International Small Flat Rate Box' => 'PRIORITY',
  380. 'Priority Mail International Medium Flat Rate Box' => 'PRIORITY',
  381. 'Priority Mail International Large Flat Rate Box' => 'PRIORITY',
  382. 'Priority Mail International Flat Rate Box' => 'PRIORITY'
  383. ),
  384. 'first_class_mail_type'=>array(
  385. 'LETTER' => Mage::helper('usa')->__('Letter'),
  386. 'FLAT' => Mage::helper('usa')->__('Flat'),
  387. 'PARCEL' => Mage::helper('usa')->__('Parcel'),
  388. ),
  389. 'container'=>array(
  390. 'VARIABLE' => Mage::helper('usa')->__('Variable'),
  391. 'FLAT RATE BOX' => Mage::helper('usa')->__('Flat-Rate Box'),
  392. 'FLAT RATE ENVELOPE' => Mage::helper('usa')->__('Flat-Rate Envelope'),
  393. 'RECTANGULAR' => Mage::helper('usa')->__('Rectangular'),
  394. 'NONRECTANGULAR' => Mage::helper('usa')->__('Non-rectangular'),
  395. ),
  396. 'size'=>array(
  397. 'REGULAR' => Mage::helper('usa')->__('Regular'),
  398. 'LARGE' => Mage::helper('usa')->__('Large'),
  399. 'OVERSIZE' => Mage::helper('usa')->__('Oversize'),
  400. ),
  401. 'machinable'=>array(
  402. 'true' => Mage::helper('usa')->__('Yes'),
  403. 'false' => Mage::helper('usa')->__('No'),
  404. ),
  405. );
  406. $methods = $this->getConfigData('methods');
  407. if (!empty($methods)) {
  408. $codes['method'] = explode(",", $methods);
  409. } else {
  410. $codes['method'] = array();
  411. }
  412. if (!isset($codes[$type])) {
  413. return false;
  414. } elseif (''===$code) {
  415. return $codes[$type];
  416. }
  417. if (!isset($codes[$type][$code])) {
  418. return false;
  419. } else {
  420. return $codes[$type][$code];
  421. }
  422. }
  423. public function getTracking($trackings)
  424. {
  425. $this->setTrackingReqeust();
  426. if (!is_array($trackings)) {
  427. $trackings = array($trackings);
  428. }
  429. $this->_getXmlTracking($trackings);
  430. return $this->_result;
  431. }
  432. protected function setTrackingReqeust()
  433. {
  434. $r = new Varien_Object();
  435. $userId = $this->getConfigData('userid');
  436. $r->setUserId($userId);
  437. $this->_rawTrackRequest = $r;
  438. }
  439. protected function _getXmlTracking($trackings)
  440. {
  441. $r = $this->_rawTrackRequest;
  442. foreach ($trackings as $tracking) {
  443. $xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><TrackRequest/>');
  444. $xml->addAttribute('USERID', $r->getUserId());
  445. $trackid = $xml->addChild('TrackID');
  446. $trackid->addAttribute('ID',$tracking);
  447. $api = 'TrackV2';
  448. $request = $xml->asXML();
  449. $debugData = array('request' => $request);
  450. try {
  451. $url = $this->getConfigData('gateway_url');
  452. if (!$url) {
  453. $url = $this->_defaultGatewayUrl;
  454. }
  455. $client = new Zend_Http_Client();
  456. $client->setUri($url);
  457. $client->setConfig(array('maxredirects'=>0, 'timeout'=>30));
  458. $client->setParameterGet('API', $api);
  459. $client->setParameterGet('XML', $request);
  460. $response = $client->request();
  461. $responseBody = $response->getBody();
  462. $debugData['result'] = $responseBody;
  463. }
  464. catch (Exception $e) {
  465. $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
  466. $responseBody = '';
  467. }
  468. $this->_debug($debugData);
  469. $this->_parseXmlTrackingResponse($tracking, $responseBody);
  470. }
  471. }
  472. protected function _parseXmlTrackingResponse($trackingvalue, $response)
  473. {
  474. $errorTitle = Mage::helper('usa')->__('Unable to retrieve tracking');
  475. $resultArr=array();
  476. if (strlen(trim($response)) > 0) {
  477. if (strpos(trim($response), '<?xml')===0) {
  478. $xml = simplexml_load_string($response);
  479. if (is_object($xml)) {
  480. if (isset($xml->Number) && isset($xml->Description) && (string)$xml->Description!='') {
  481. $errorTitle = (string)$xml->Description;
  482. } elseif (isset($xml->TrackInfo) && isset($xml->TrackInfo->Error) && isset($xml->TrackInfo->Error->Description) && (string)$xml->TrackInfo->Error->Description!='') {
  483. $errorTitle = (string)$xml->TrackInfo->Error->Description;
  484. } else {
  485. $errorTitle = Mage::helper('usa')->__('Unknown error');
  486. }
  487. if(isset($xml->TrackInfo) && isset($xml->TrackInfo->TrackSummary)){
  488. $resultArr['tracksummary'] = (string)$xml->TrackInfo->TrackSummary;
  489. }
  490. }
  491. }
  492. }
  493. if (!$this->_result) {
  494. $this->_result = Mage::getModel('shipping/tracking_result');
  495. }
  496. $defaults = $this->getDefaults();
  497. if ($resultArr) {
  498. $tracking = Mage::getModel('shipping/tracking_result_status');
  499. $tracking->setCarrier('usps');
  500. $tracking->setCarrierTitle($this->getConfigData('title'));
  501. $tracking->setTracking($trackingvalue);
  502. $tracking->setTrackSummary($resultArr['tracksummary']);
  503. $this->_result->append($tracking);
  504. } else {
  505. $error = Mage::getModel('shipping/tracking_result_error');
  506. $error->setCarrier('usps');
  507. $error->setCarrierTitle($this->getConfigData('title'));
  508. $error->setTracking($trackingvalue);
  509. $error->setErrorMessage($errorTitle);
  510. $this->_result->append($error);
  511. }
  512. }
  513. public function getResponse()
  514. {
  515. $statuses = '';
  516. if ($this->_result instanceof Mage_Shipping_Model_Tracking_Result) {
  517. if ($trackings = $this->_result->getAllTrackings()) {
  518. foreach ($trackings as $tracking) {
  519. if($data = $tracking->getAllData()) {
  520. if (!empty($data['track_summary'])) {
  521. $statuses .= Mage::helper('usa')->__($data['track_summary']);
  522. } else {
  523. $statuses .= Mage::helper('usa')->__('Empty response');
  524. }
  525. }
  526. }
  527. }
  528. }
  529. if (empty($statuses)) {
  530. $statuses = Mage::helper('usa')->__('Empty response');
  531. }
  532. return $statuses;
  533. }
  534. /**
  535. * Get allowed shipping methods
  536. *
  537. * @return array
  538. */
  539. public function getAllowedMethods()
  540. {
  541. $allowed = explode(',', $this->getConfigData('allowed_methods'));
  542. $arr = array();
  543. foreach ($allowed as $k) {
  544. $arr[$k] = $k;
  545. }
  546. return $arr;
  547. }
  548. /**
  549. * Check is ะกoutry U.S. Possessions and Trust Territories
  550. *
  551. * @param string $countyId
  552. * @return boolean
  553. */
  554. protected function _isUSCountry($countyId)
  555. {
  556. switch ($countyId) {
  557. case 'AS': // Samoa American
  558. case 'GU': // Guam
  559. case 'MP': // Northern Mariana Islands
  560. case 'PW': // Palau
  561. case 'PR': // Puerto Rico
  562. case 'VI': // Virgin Islands US
  563. case 'US'; // United States
  564. return true;
  565. }
  566. return false;
  567. }
  568. /**
  569. * Return USPS county name by country ISO 3166-1-alpha-2 code
  570. * Return false for unknown countries
  571. *
  572. * @param string $countryId
  573. * @return string|false
  574. */
  575. protected function _getCountryName($countryId)
  576. {
  577. $countries = array (
  578. 'AD' => 'Andorra',
  579. 'AE' => 'United Arab Emirates',
  580. 'AF' => 'Afghanistan',
  581. 'AG' => 'Antigua and Barbuda',
  582. 'AI' => 'Anguilla',
  583. 'AL' => 'Albania',
  584. 'AM' => 'Armenia',
  585. 'AN' => 'Netherlands Antilles',
  586. 'AO' => 'Angola',
  587. 'AR' => 'Argentina',
  588. 'AT' => 'Austria',
  589. 'AU' => 'Australia',
  590. 'AW' => 'Aruba',
  591. 'AX' => 'Aland Island (Finland)',
  592. 'AZ' => 'Azerbaijan',
  593. 'BA' => 'Bosnia-Herzegovina',
  594. 'BB' => 'Barbados',
  595. 'BD' => 'Bangladesh',
  596. 'BE' => 'Belgium',
  597. 'BF' => 'Burkina Faso',
  598. 'BG' => 'Bulgaria',
  599. 'BH' => 'Bahrain',
  600. 'BI' => 'Burundi',
  601. 'BJ' => 'Benin',
  602. 'BM' => 'Bermuda',
  603. 'BN' => 'Brunei Darussalam',
  604. 'BO' => 'Bolivia',
  605. 'BR' => 'Brazil',
  606. 'BS' => 'Bahamas',
  607. 'BT' => 'Bhutan',
  608. 'BW' => 'Botswana',
  609. 'BY' => 'Belarus',
  610. 'BZ' => 'Belize',
  611. 'CA' => 'Canada',
  612. 'CC' => 'Cocos Island (Australia)',
  613. 'CD' => 'Congo, Democratic Republic of the',
  614. 'CF' => 'Central African Republic',
  615. 'CG' => 'Congo, Republic of the',
  616. 'CH' => 'Switzerland',
  617. 'CI' => 'Cote d Ivoire (Ivory Coast)',
  618. 'CK' => 'Cook Islands (New Zealand)',
  619. 'CL' => 'Chile',
  620. 'CM' => 'Cameroon',
  621. 'CN' => 'China',
  622. 'CO' => 'Colombia',
  623. 'CR' => 'Costa Rica',
  624. 'CU' => 'Cuba',
  625. 'CV' => 'Cape Verde',
  626. 'CX' => 'Christmas Island (Australia)',
  627. 'CY' => 'Cyprus',
  628. 'CZ' => 'Czech Republic',
  629. 'DE' => 'Germany',
  630. 'DJ' => 'Djibouti',
  631. 'DK' => 'Denmark',
  632. 'DM' => 'Dominica',
  633. 'DO' => 'Dominican Republic',
  634. 'DZ' => 'Algeria',
  635. 'EC' => 'Ecuador',
  636. 'EE' => 'Estonia',
  637. 'EG' => 'Egypt',
  638. 'ER' => 'Eritrea',
  639. 'ES' => 'Spain',
  640. 'ET' => 'Ethiopia',
  641. 'FI' => 'Finland',
  642. 'FJ' => 'Fiji',
  643. 'FK' => 'Falkland Islands',
  644. 'FM' => 'Micronesia, Federated States of',
  645. 'FO' => 'Faroe Islands',
  646. 'FR' => 'France',
  647. 'GA' => 'Gabon',
  648. 'GB' => 'Great Britain and Northern Ireland',
  649. 'GD' => 'Grenada',
  650. 'GE' => 'Georgia, Republic of',
  651. 'GF' => 'French Guiana',
  652. 'GH' => 'Ghana',
  653. 'GI' => 'Gibraltar',
  654. 'GL' => 'Greenland',
  655. 'GM' => 'Gambia',
  656. 'GN' => 'Guinea',
  657. 'GP' => 'Guadeloupe',
  658. 'GQ' => 'Equatorial Guinea',
  659. 'GR' => 'Greece',
  660. 'GS' => 'South Georgia (Falkland Islands)',
  661. 'GT' => 'Guatemala',
  662. 'GW' => 'Guinea-Bissau',
  663. 'GY' => 'Guyana',
  664. 'HK' => 'Hong Kong',
  665. 'HN' => 'Honduras',
  666. 'HR' => 'Croatia',
  667. 'HT' => 'Haiti',
  668. 'HU' => 'Hungary',
  669. 'ID' => 'Indonesia',
  670. 'IE' => 'Ireland',
  671. 'IL' => 'Israel',
  672. 'IN' => 'India',
  673. 'IQ' => 'Iraq',
  674. 'IR' => 'Iran',
  675. 'IS' => 'Iceland',
  676. 'IT' => 'Italy',
  677. 'JM' => 'Jamaica',
  678. 'JO' => 'Jordan',
  679. 'JP' => 'Japan',
  680. 'KE' => 'Kenya',
  681. 'KG' => 'Kyrgyzstan',
  682. 'KH' => 'Cambodia',
  683. 'KI' => 'Kiribati',
  684. 'KM' => 'Comoros',
  685. 'KN' => 'Saint Kitts (St. Christopher and Nevis)',
  686. 'KP' => 'North Korea (Korea, Democratic People\'s Republic of)',
  687. 'KR' => 'South Korea (Korea, Republic of)',
  688. 'KW' => 'Kuwait',
  689. 'KY' => 'Cayman Islands',
  690. 'KZ' => 'Kazakhstan',
  691. 'LA' => 'Laos',
  692. 'LB' => 'Lebanon',
  693. 'LC' => 'Saint Lucia',
  694. 'LI' => 'Liechtenstein',
  695. 'LK' => 'Sri Lanka',
  696. 'LR' => 'Liberia',
  697. 'LS' => 'Lesotho',
  698. 'LT' => 'Lithuania',
  699. 'LU' => 'Luxembourg',
  700. 'LV' => 'Latvia',
  701. 'LY' => 'Libya',
  702. 'MA' => 'Morocco',
  703. 'MC' => 'Monaco (France)',
  704. 'MD' => 'Moldova',
  705. 'MG' => 'Madagascar',
  706. 'MK' => 'Macedonia, Republic of',
  707. 'ML' => 'Mali',
  708. 'MM' => 'Burma',
  709. 'MN' => 'Mongolia',
  710. 'MO' => 'Macao',
  711. 'MQ' => 'Martinique',
  712. 'MR' => 'Mauritania',
  713. 'MS' => 'Montserrat',
  714. 'MT' => 'Malta',
  715. 'MU' => 'Mauritius',
  716. 'MV' => 'Maldives',
  717. 'MW' => 'Malawi',
  718. 'MX' => 'Mexico',
  719. 'MY' => 'Malaysia',
  720. 'MZ' => 'Mozambique',
  721. 'NA' => 'Namibia',
  722. 'NC' => 'New Caledonia',
  723. 'NE' => 'Niger',
  724. 'NG' => 'Nigeria',
  725. 'NI' => 'Nicaragua',
  726. 'NL' => 'Netherlands',
  727. 'NO' => 'Norway',
  728. 'NP' => 'Nepal',
  729. 'NR' => 'Nauru',
  730. 'NZ' => 'New Zealand',
  731. 'OM' => 'Oman',
  732. 'PA' => 'Panama',
  733. 'PE' => 'Peru',
  734. 'PF' => 'French Polynesia',
  735. 'PG' => 'Papua New Guinea',
  736. 'PH' => 'Philippines',
  737. 'PK' => 'Pakistan',
  738. 'PL' => 'Poland',
  739. 'PM' => 'Saint Pierre and Miquelon',
  740. 'PN' => 'Pitcairn Island',
  741. 'PT' => 'Portugal',
  742. 'PY' => 'Paraguay',
  743. 'QA' => 'Qatar',
  744. 'RE' => 'Reunion',
  745. 'RO' => 'Romania',
  746. 'RS' => 'Serbia',
  747. 'RU' => 'Russia',
  748. 'RW' => 'Rwanda',
  749. 'SA' => 'Saudi Arabia',
  750. 'SB' => 'Solomon Islands',
  751. 'SC' => 'Seychelles',
  752. 'SD' => 'Sudan',
  753. 'SE' => 'Sweden',
  754. 'SG' => 'Singapore',
  755. 'SH' => 'Saint Helena',
  756. 'SI' => 'Slovenia',
  757. 'SK' => 'Slovak Republic',
  758. 'SL' => 'Sierra Leone',
  759. 'SM' => 'San Marino',
  760. 'SN' => 'Senegal',
  761. 'SO' => 'Somalia',
  762. 'SR' => 'Suriname',
  763. 'ST' => 'Sao Tome and Principe',
  764. 'SV' => 'El Salvador',
  765. 'SY' => 'Syrian Arab Republic',
  766. 'SZ' => 'Swaziland',
  767. 'TC' => 'Turks and Caicos Islands',
  768. 'TD' => 'Chad',
  769. 'TG' => 'Togo',
  770. 'TH' => 'Thailand',
  771. 'TJ' => 'Tajikistan',
  772. 'TK' => 'Tokelau (Union) Group (Western Samoa)',
  773. 'TL' => 'East Timor (Indonesia)',
  774. 'TM' => 'Turkmenistan',
  775. 'TN' => 'Tunisia',
  776. 'TO' => 'Tonga',
  777. 'TR' => 'Turkey',
  778. 'TT' => 'Trinidad and Tobago',
  779. 'TV' => 'Tuvalu',
  780. 'TW' => 'Taiwan',
  781. 'TZ' => 'Tanzania',
  782. 'UA' => 'Ukraine',
  783. 'UG' => 'Uganda',
  784. 'UY' => 'Uruguay',
  785. 'UZ' => 'Uzbekistan',
  786. 'VA' => 'Vatican City',
  787. 'VC' => 'Saint Vincent and the Grenadines',
  788. 'VE' => 'Venezuela',
  789. 'VG' => 'British Virgin Islands',
  790. 'VN' => 'Vietnam',
  791. 'VU' => 'Vanuatu',
  792. 'WF' => 'Wallis and Futuna Islands',
  793. 'WS' => 'Western Samoa',
  794. 'YE' => 'Yemen',
  795. 'YT' => 'Mayotte (France)',
  796. 'ZA' => 'South Africa',
  797. 'ZM' => 'Zambia',
  798. 'ZW' => 'Zimbabwe',
  799. );
  800. if (isset($countries[$countryId])) {
  801. return $countries[$countryId];
  802. }
  803. return false;
  804. }
  805. /**
  806. * Clean service name from unsupported strings and characters
  807. *
  808. * @param string $name
  809. * @return string
  810. */
  811. protected function _filterServiceName($name)
  812. {
  813. $name = (string)preg_replace(array('~<[^/!][^>]+>.*</[^>]+>~sU', '~\<!--.*--\>~isU', '~<[^>]+>~is'), '', html_entity_decode($name));
  814. $name = str_replace('*', '', $name);
  815. return $name;
  816. }
  817. }