PageRenderTime 76ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/shipping/usps.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 891 lines | 690 code | 81 blank | 120 comment | 139 complexity | 9e2a3511309c4d72f4f86725818c1c83 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. * MODIFIED BY Corey Koltz (http://www.koltz.com)
  5. * Code updated to work with multiple shipping options and
  6. * updated for May 2007 changes by USPS.
  7. *
  8. * @version $Id: usps.php,v 3.0 2007/06/21 by Corey Koltz
  9. * Original code by Soeren Eberhardt
  10. * @package VirtueMart
  11. * @subpackage shipping
  12. * @copyright Copyright (C) 2004-2007 soeren - All rights reserved.
  13. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  14. * VirtueMart is free software. This version may have been modified pursuant
  15. * to the GNU General Public License, and as distributed it includes or
  16. * is derivative of works licensed under the GNU General Public License or
  17. * other free or open source software licenses.
  18. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  19. *
  20. * http://virtuemart.net
  21. */
  22. /**
  23. * This is the Shipping class for
  24. * using a part of the USPS Online Tools:
  25. * = Rates and Service Selection =
  26. *
  27. * @copyright (C) 2005 E-Z E
  28. */
  29. class usps {
  30. function list_rates( &$d ) {
  31. global $VM_LANG, $CURRENCY_DISPLAY, $mosConfig_absolute_path;
  32. $db = new ps_DB;
  33. $dbv = new ps_DB;
  34. $dbc = new ps_DB;
  35. /** Read current Configuration ***/
  36. require_once(CLASSPATH ."shipping/".__CLASS__.".cfg.php");
  37. $q = "SELECT * FROM `#__{vm}_user_info`, `#__{vm}_country` WHERE user_info_id='" . $db->getEscaped($d["ship_to_info_id"])."' AND ( country=country_2_code OR country=country_3_code)";
  38. $db->query($q);
  39. $db->next_record();
  40. $q = "SELECT * FROM #__{vm}_vendor WHERE vendor_id='".$_SESSION['ps_vendor_id']."'";
  41. $dbv->query($q);
  42. $dbv->next_record();
  43. $order_weight = $d['weight'];
  44. if($order_weight > 0) {
  45. //USPS Username
  46. $usps_username = USPS_USERNAME;
  47. //USPS Password
  48. $usps_password = USPS_PASSWORD;
  49. //USPS Server
  50. $usps_server = USPS_SERVER;
  51. //USPS Path
  52. $usps_path = USPS_PATH;
  53. //USPS package size
  54. $usps_packagesize = USPS_PACKAGESIZE;
  55. //USPS Package ID
  56. $usps_packageid = 0;
  57. //USPS International Per Pound Rate
  58. $usps_intllbrate = USPS_INTLLBRATE;
  59. //USPS International handling fee
  60. $usps_intlhandlingfee = USPS_INTLHANDLINGFEE;
  61. //Pad the shipping weight to allow weight for shipping materials
  62. $usps_padding = USPS_PADDING;
  63. $usps_padding = $usps_padding * 0.01;
  64. $order_weight = ($order_weight * $usps_padding) + $order_weight;
  65. //USPS Machinable for Parcel Post
  66. $usps_machinable = USPS_MACHINABLE;
  67. if ($usps_machinable == '1') $usps_machinable = 'TRUE';
  68. else $usps_machinable = 'FALSE';
  69. //USPS Shipping Options to display
  70. $usps_ship[0] = USPS_SHIP0;
  71. $usps_ship[1] = USPS_SHIP1;
  72. $usps_ship[2] = USPS_SHIP2;
  73. $usps_ship[3] = USPS_SHIP3;
  74. $usps_ship[4] = USPS_SHIP4;
  75. $usps_ship[5] = USPS_SHIP5;
  76. $usps_ship[6] = USPS_SHIP6;
  77. $usps_ship[7] = USPS_SHIP7;
  78. $usps_ship[8] = USPS_SHIP8;
  79. $usps_ship[9] = USPS_SHIP9;
  80. $usps_ship[10] = USPS_SHIP10;
  81. foreach ($usps_ship as $key => $value){
  82. if ($value == '1') $usps_ship[$key] = 'TRUE';
  83. else $usps_ship[$key] = 'FALSE';
  84. }
  85. $usps_intl[0] = USPS_INTL0;
  86. $usps_intl[1] = USPS_INTL1;
  87. $usps_intl[2] = USPS_INTL2;
  88. $usps_intl[3] = USPS_INTL3;
  89. $usps_intl[4] = USPS_INTL4;
  90. $usps_intl[5] = USPS_INTL5;
  91. $usps_intl[6] = USPS_INTL6;
  92. $usps_intl[7] = USPS_INTL7;
  93. $usps_intl[8] = USPS_INTL8;
  94. // $usps_intl[9] = USPS_INTL9;
  95. foreach ($usps_intl as $key => $value){
  96. if ($value == '1') $usps_intl[$key] = 'TRUE';
  97. else $usps_intl[$key] = 'FALSE';
  98. }
  99. //Title for your request
  100. $request_title = "Shipping Estimate";
  101. //The zip that you are shipping from
  102. $source_zip = substr($dbv->f("vendor_zip"),0,5);
  103. $shpService = 'All'; //"Priority";
  104. //The zip that you are shipping to
  105. $dest_country = $db->f("country_2_code");
  106. if ($dest_country == "GB") {
  107. $q = "SELECT state_name FROM #__{vm}_state WHERE state_2_code='".$db->f("state")."'";
  108. $dbc->query($q);
  109. $dbc->next_record();
  110. $dest_country_name = $dbc->f("state_name");
  111. }
  112. else {
  113. $dest_country_name = $db->f("country_name");
  114. }
  115. $dest_state = $db->f("state");
  116. $dest_zip = substr($db->f("zip"),0,5);
  117. //$weight_measure
  118. if ($order_weight < 1) {
  119. $shipping_pounds_intl = 0;
  120. } else {
  121. $shipping_pounds_intl = ceil ($order_weight);
  122. }
  123. if ($order_weight < 0.88) {
  124. $shipping_pounds = 0;
  125. $shipping_ounces = round(16 * ($order_weight - floor($order_weight)));
  126. }
  127. else {
  128. $shipping_pounds = ceil ($order_weight);
  129. $shipping_ounces = 0;
  130. }
  131. $os = array("Mac", "NT", "Irix", "Linux");
  132. $states = array("AL","AK","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WI","WV","WY");
  133. //If weight is over 70 pounds, round down to 70 for now.
  134. //Will update in the future to be able to split the package or something?
  135. if( $order_weight > 70.00 ) {
  136. echo "We are unable to ship USPS as the package weight exceeds the 70 pound limit,<br>please select another shipping method.";
  137. }
  138. else {
  139. if( ( $dest_country == "US") && in_array($dest_state,$states) ) {
  140. /******START OF DOMESTIC RATE******/
  141. //the xml that will be posted to usps
  142. $xmlPost = 'API=RateV2&XML=<RateV2Request USERID="'.$usps_username.'" PASSWORD="'.$usps_password.'">';
  143. $xmlPost .= '<Package ID="'.$usps_packageid.'">';
  144. $xmlPost .= "<Service>".$shpService."</Service>";
  145. $xmlPost .= "<ZipOrigination>".$source_zip."</ZipOrigination>";
  146. $xmlPost .= "<ZipDestination>".$dest_zip."</ZipDestination>";
  147. $xmlPost .= "<Pounds>".$shipping_pounds."</Pounds>";
  148. $xmlPost .= "<Ounces>".$shipping_ounces."</Ounces>";
  149. $xmlPost .= "<Size>".$usps_packagesize."</Size>";
  150. $xmlPost .= "<Machinable>".$usps_machinable."</Machinable>";
  151. $xmlPost .= "</Package></RateV2Request>";
  152. // echo htmlentities( $xmlPost );
  153. $host = $usps_server;
  154. //$host = "production.shippingapis.com";
  155. $path = $usps_path; //"/ups.app/xml/Rate";
  156. //$path = "/ShippingAPI.dll";
  157. $port = 80;
  158. $protocol = "http";
  159. $html = "";
  160. //echo "<textarea>".$protocol."://".$host.$path."?API=Rate&XML=".$xmlPost."</textarea>";
  161. // Using cURL is Up-To-Date and easier!!
  162. if( function_exists( "curl_init" )) {
  163. $CR = curl_init();
  164. curl_setopt($CR, CURLOPT_URL, $protocol."://".$host.$path); //"?API=RateV2&XML=".$xmlPost);
  165. curl_setopt($CR, CURLOPT_POST, 1);
  166. curl_setopt($CR, CURLOPT_FAILONERROR, true);
  167. curl_setopt($CR, CURLOPT_POSTFIELDS, $xmlPost);
  168. curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
  169. $xmlResult = curl_exec( $CR );
  170. $error = curl_error( $CR );
  171. if( !empty( $error )) {
  172. $vmLogger->err( curl_error( $CR ) );
  173. $html = "<br/><span class=\"message\">".$VM_LANG->_('PHPSHOP_INTERNAL_ERROR')." USPS.com</span>";
  174. $error = true;
  175. }
  176. else {
  177. /* XML Parsing */
  178. require_once( $mosConfig_absolute_path. '/includes/domit/xml_domit_lite_include.php' );
  179. $xmlDoc = new DOMIT_Lite_Document();
  180. $xmlDoc -> parseXML( $xmlResult, false, true );
  181. /* Let's check wether the response from USPS is Success or Failure ! */
  182. if( strstr( $xmlResult, "Error" ) ) {
  183. $error = true;
  184. $html = "<span class=\"message\">".$VM_LANG->_('PHPSHOP_USPS_RESPONSE_ERROR')."</span><br/>";
  185. $error_code = $xmlDoc->getElementsByTagName( "Number" );
  186. $error_code = $error_code->item(0);
  187. $error_code = $error_code->getText();
  188. $html .= $VM_LANG->_('PHPSHOP_ERROR_CODE').": ".$error_code."<br/>";
  189. $error_desc = $xmlDoc->getElementsByTagName( "Description" );
  190. $error_desc = $error_desc->item(0);
  191. $error_desc = $error_desc->getText();
  192. $html .= $VM_LANG->_('PHPSHOP_ERROR_DESC').": ".$error_desc."<br/>";
  193. }
  194. }
  195. curl_close( $CR );
  196. }
  197. else {
  198. $protocol = "http";
  199. $fp = fsockopen($protocol."://".$host, $errno, $errstr, $timeout = 60);
  200. if( !$fp ) {
  201. $error = true;
  202. $html = $VM_LANG->_('PHPSHOP_INTERNAL_ERROR').": $errstr ($errno)";
  203. }
  204. else {
  205. //send the server request
  206. fputs($fp, "POST $path HTTP/1.1\r\n");
  207. fputs($fp, "Host: $host\r\n");
  208. fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  209. fputs($fp, "Content-length: ".strlen($xmlPost)."\r\n");
  210. fputs($fp, "Connection: close\r\n\r\n");
  211. fputs($fp, $xmlPost . "\r\n\r\n");
  212. $xmlResult = '';
  213. while(!feof($fp)) {
  214. $xmlResult .= fgets($fp, 4096);
  215. }
  216. if( stristr( $xmlResult, "Success" )) {
  217. /* XML Parsing */
  218. require_once( $mosConfig_absolute_path. '/includes/domit/xml_domit_lite_include.php' );
  219. $xmlDoc =& new DOMIT_Lite_Document();
  220. $xmlDoc->parseXML( $xmlResult, false, true );
  221. $error = false;
  222. }
  223. else {
  224. $html = "Error processing the Request to USPS.com";
  225. $error = true;
  226. }
  227. }
  228. }
  229. if (DEBUG){
  230. echo "XML Post: <br>";
  231. echo "<textarea cols='80'>".$protocol."://".$host.$path."?".$xmlPost."</textarea>";
  232. echo "<br>";
  233. echo "XML Result: <br>";
  234. echo "<textarea cols='80' rows='10'>".$xmlResult."</textarea>";
  235. echo "<br>";
  236. echo "Cart Contents: ".$order_weight. " ".$weight_measure."<br><br>\n";
  237. }
  238. if( $error ) {
  239. // comment out, if you don't want the Errors to be shown!!
  240. //$vmLogger->err( $html );
  241. // Switch to StandardShipping on Error !!!
  242. //require_once( CLASSPATH . 'shipping/standard_shipping.php' );
  243. //$shipping =& new standard_shipping();
  244. //$shipping->list_rates( $d );
  245. echo "We are unable to ship USPS as the there was an error,<br> please select another shipping method.";
  246. return;
  247. }
  248. // Domestic shipping - add how long it might take
  249. $ship_commit[0]="1 - 2 Days";
  250. $ship_commit[1]="1 - 2 Days";
  251. $ship_commit[2]="1 - 2 Days";
  252. $ship_commit[3]="1 - 3 Days";
  253. $ship_commit[4]="1 - 3 Days";
  254. $ship_commit[5]="1 - 3 Days";
  255. $ship_commit[6]="2 - 9 Days";
  256. $ship_commit[7]="2 - 9 Days";
  257. $ship_commit[8]="2 - 9 Days";
  258. $ship_commit[9]="2 - 9 Days";
  259. $ship_commit[10]="2 Days or More";
  260. // retrieve the service and postage items
  261. $i = 0;
  262. if ($order_weight > 15) {
  263. $count = 8;
  264. $usps_ship[6] = $usps_ship[7];
  265. $usps_ship[7] = $usps_ship[9];
  266. $usps_ship[8] = $usps_ship[10];
  267. }
  268. else if ($order_weight >= 0.86) {
  269. $count = 9;
  270. $usps_ship[6] = $usps_ship[7];
  271. $usps_ship[7] = $usps_ship[8];
  272. $usps_ship[8] = $usps_ship[9];
  273. $usps_ship[9] = $usps_ship[10];
  274. }
  275. else {
  276. $count = 10;
  277. }
  278. while ($i <= $count) {
  279. if( isset( $xmlDoc)) {
  280. $ship_service[$i] = $xmlDoc->getElementsByTagName( 'MailService' );
  281. $ship_service[$i] = $ship_service[$i]->item($i);
  282. $ship_service[$i] = $ship_service[$i]->getText();
  283. $ship_postage[$i] = $xmlDoc->getElementsByTagName( 'Rate' );
  284. $ship_postage[$i] = $ship_postage[$i]->item($i);
  285. $ship_postage[$i] = $ship_postage[$i]->getText();
  286. if (preg_match('/%$/',USPS_HANDLINGFEE)) {
  287. $ship_postage[$i] = $ship_postage[$i] * (1+substr(USPS_HANDLINGFEE,0,-1)/100);
  288. } else {
  289. $ship_postage[$i] = $ship_postage[$i] + USPS_HANDLINGFEE;
  290. }
  291. $i++;
  292. }
  293. }
  294. /******END OF DOMESTIC RATE******/
  295. }
  296. else {
  297. /******START INTERNATIONAL RATE******/
  298. //the xml that will be posted to usps
  299. $xmlPost = 'API=IntlRate&XML=<IntlRateRequest USERID="'.$usps_username.'" PASSWORD="'.$usps_password.'">';
  300. $xmlPost .= '<Package ID="'.$usps_packageid.'">';
  301. $xmlPost .= "<Pounds>".$shipping_pounds_intl."</Pounds>";
  302. $xmlPost .= "<Ounces>".$shipping_ounces."</Ounces>";
  303. $xmlPost .= "<MailType>Package</MailType>";
  304. $xmlPost .= "<Country>".$dest_country_name."</Country>";
  305. $xmlPost .= "</Package></IntlRateRequest>";
  306. // echo htmlentities( $xmlPost );
  307. $host = $usps_server;
  308. //$host = "production.shippingapis.com";
  309. $path = $usps_path; //"/ups.app/xml/Rate";
  310. //$path = "/ShippingAPI.dll";
  311. $port = 80;
  312. $protocol = "http";
  313. //echo "<textarea>".$protocol."://".$host.$path."?API=Rate&XML=".$xmlPost."</textarea>";
  314. // Using cURL is Up-To-Date and easier!!
  315. if( function_exists( "curl_init" )) {
  316. $CR = curl_init();
  317. curl_setopt($CR, CURLOPT_URL, $protocol."://".$host.$path); //"?API=RateV2&XML=".$xmlPost);
  318. curl_setopt($CR, CURLOPT_POST, 1);
  319. curl_setopt($CR, CURLOPT_FAILONERROR, true);
  320. curl_setopt($CR, CURLOPT_POSTFIELDS, $xmlPost);
  321. curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
  322. $xmlResult = curl_exec( $CR );
  323. //echo "<textarea>".$xmlResult."</textarea>";
  324. $error = curl_error( $CR );
  325. if( !empty( $error )) {
  326. $vmLogger->err( curl_error( $CR ) );
  327. $html = "<br/><span class=\"message\">".$VM_LANG->_('PHPSHOP_INTERNAL_ERROR')." USPS.com</span>";
  328. $error = true;
  329. }
  330. else {
  331. /* XML Parsing */
  332. require_once( $mosConfig_absolute_path. '/includes/domit/xml_domit_lite_include.php' );
  333. $xmlDoc = new DOMIT_Lite_Document();
  334. $xmlDoc->parseXML( $xmlResult, false, true );
  335. /* Let's check wether the response from USPS is Success or Failure ! */
  336. if( strstr( $xmlResult, "Error" ) ) {
  337. $error = true;
  338. $html = "<span class=\"message\">".$VM_LANG->_('PHPSHOP_USPS_RESPONSE_ERROR')."</span><br/>";
  339. $error_code = $xmlDoc->getElementsByTagName( "Number" );
  340. $error_code = $error_code->item(0);
  341. $error_code = $error_code->getText();
  342. $html .= $VM_LANG->_('PHPSHOP_ERROR_CODE').": ".$error_code."<br/>";
  343. $error_desc = $xmlDoc->getElementsByTagName( "Description" );
  344. $error_desc = $error_desc->item(0);
  345. $error_desc = $error_desc->getText();
  346. $html .= $VM_LANG->_('PHPSHOP_ERROR_DESC').": ".$error_desc."<br/>";
  347. }
  348. }
  349. curl_close( $CR );
  350. }
  351. else {
  352. $protocol = "http";
  353. $fp = fsockopen($protocol."://".$host, $errno, $errstr, $timeout = 60);
  354. if( !$fp ) {
  355. $error = true;
  356. $html = $VM_LANG->_('PHPSHOP_INTERNAL_ERROR').": $errstr ($errno)";
  357. }
  358. else {
  359. //send the server request
  360. fputs($fp, "POST $path HTTP/1.1\r\n");
  361. fputs($fp, "Host: $host\r\n");
  362. fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  363. fputs($fp, "Content-length: ".strlen($xmlPost)."\r\n");
  364. fputs($fp, "Connection: close\r\n\r\n");
  365. fputs($fp, $xmlPost . "\r\n\r\n");
  366. $xmlResult = '';
  367. while(!feof($fp)) {
  368. $xmlResult .= fgets($fp, 4096);
  369. }
  370. if( stristr( $xmlResult, "Success" )) {
  371. /* XML Parsing */
  372. require_once( $mosConfig_absolute_path. '/includes/domit/xml_domit_lite_include.php' );
  373. $xmlDoc = new DOMIT_Lite_Document();
  374. $xmlDoc->parseXML( $xmlResult, false, true );
  375. $error = false;
  376. }
  377. else {
  378. $html = "Error processing the Request to USPS.com";
  379. $error = true;
  380. }
  381. }
  382. }
  383. if (DEBUG){
  384. echo "XML Post: <br>";
  385. echo "<textarea cols='80'>".$protocol."://".$host.$path."?".$xmlPost."</textarea>";
  386. echo "<br>";
  387. echo "XML Result: <br>";
  388. echo "<textarea cols='80' rows='10'>".$xmlResult."</textarea>";
  389. echo "<br>";
  390. echo "Cart Contents: ".$order_weight. " ".$weight_measure."<br><br>\n";
  391. }
  392. if( $error ) {
  393. // comment out, if you don't want the Errors to be shown!!
  394. //$vmLogger->err( $html );
  395. // Switch to StandardShipping on Error !!!
  396. //require_once( CLASSPATH . 'shipping/standard_shipping.php' );
  397. //$shipping =& new standard_shipping();
  398. //$shipping->list_rates( $d );
  399. //return;
  400. echo "We are unable to ship USPS as there was an error,<br> please select another shipping method.";
  401. }
  402. // retrieve the service and postage items
  403. $i = 0;
  404. $numChildren = 0;
  405. $numChildren = $xmlDoc->documentElement->firstChild->childCount;
  406. $numChildren = ($numChildren - 7); // this line removes the preceeding 6 lines of crap not needed plus 1 to make up for the $i starting at 0
  407. while ($i <= $numChildren) {
  408. if( isset( $xmlDoc)) {
  409. $ship_service[$i] = $xmlDoc->getElementsByTagName( "SvcDescription" );
  410. $ship_service[$i] = $ship_service[$i]->item($i);
  411. $ship_service[$i] = $ship_service[$i]->getText();
  412. $ship_weight[$i] = $xmlDoc->getElementsByTagName( "MaxWeight" );
  413. $ship_weight[$i] = $ship_weight[$i]->item($i);
  414. $ship_weight[$i] = $ship_weight[$i]->getText($i);
  415. }
  416. $i++;
  417. }
  418. // retrieve postage for countries that support all nine shipping methods and weights
  419. $ship_weight[8] = ($ship_weight[8]/16);
  420. if ( $order_weight <= $ship_weight[0] && $ship_weight[1] && $ship_weight[2] && $ship_weight[3] && $ship_weight[4] && $ship_weight[5] && $ship_weight [6] && $ship_weight[7] && $ship_weight[8] ) {
  421. $count = 8;
  422. }
  423. // retrieve postage for countries that support eight of the nine shipping methods and weights
  424. elseif ( $order_weight <= $ship_weight[0] && $ship_weight[1] && $ship_weight[2] && $ship_weight[3] && $ship_weight[4] && $ship_weight[5] && $ship_weight [6] && $ship_weight[7] ) {
  425. $count = 7;
  426. // $usps_intl[6] = $usps_intl[7];
  427. }
  428. // retrieve postage for countries that support seven of the nine shipping methods and weights
  429. elseif ( $order_weight <= $ship_weight[0] && $ship_weight[1] && $ship_weight[2] && $ship_weight[3] && $ship_weight[4] && $ship_weight[5] && $ship_weight [6] ) {
  430. $count = 6;
  431. }
  432. // retrieve postage for countries that support six of the nine shipping methods and weights
  433. elseif ( $order_weight <= $ship_weight[0] && $ship_weight[1] && $ship_weight[2] && $ship_weight[3] && $ship_weight[4] && $ship_weight[5] ) {
  434. $count = 5;
  435. }
  436. // retrieve postage for countries that support five of the nine shipping methods and weights
  437. elseif ( $order_weight <= $ship_weight[0] && $ship_weight[1] && $ship_weight[2] && $ship_weight[3] && $ship_weight[4] ) {
  438. $count = 4;
  439. }
  440. // retrieve postage for countries that support four of the nine shipping methods and weights
  441. elseif ( $order_weight <= $ship_weight[0] && $ship_weight[1] && $ship_weight[2] && $ship_weight[3] ) {
  442. $count = 3;
  443. }
  444. // retrieve postage for countries that support three of the nine shipping methods and weights
  445. elseif ( $order_weight <= $ship_weight[0] && $ship_weight[1] && $ship_weight[2] ) {
  446. $count = 2;
  447. }
  448. // retrieve postage for countries that support two of the nine shipping methods and weights
  449. elseif ( $order_weight <= $ship_weight[0] && $ship_weight[1] ) {
  450. $count = 1;
  451. }
  452. // retrieve postage for countries that support one of the nine shipping methods and weights
  453. elseif ( $order_weight <= $ship_weight[0] ) {
  454. $count = 0;
  455. }
  456. else {
  457. echo "We are unable to ship USPS as the package weight exceeds what your<br>country allows, please select another shipping method.";
  458. }
  459. $i = 0;
  460. while ($i <= $numChildren) {
  461. if( isset( $xmlDoc)) {
  462. $ship_service[$i] = $xmlDoc->getElementsByTagName( "SvcDescription" );
  463. $ship_service[$i] = $ship_service[$i]->item($i);
  464. $ship_service[$i] = $ship_service[$i]->getText();
  465. $ship_commit[$i] = $xmlDoc->getElementsByTagName( "SvcCommitments");
  466. $ship_commit[$i] = $ship_commit[$i]->item($i);
  467. $ship_commit[$i] = $ship_commit[$i]->getText();
  468. $ship_postage[$i] = $xmlDoc->getElementsByTagName( "Postage" );
  469. $ship_postage[$i] = $ship_postage[$i]->item($i);
  470. $ship_postage[$i] = $ship_postage[$i]->getText($i);
  471. $ship_postage[$i] = $ship_postage[$i] + USPS_INTLHANDLINGFEE;
  472. $i++;
  473. }
  474. /******END INTERNATIONAL RATE******/
  475. }
  476. }
  477. $i = 0;
  478. while ($i <= $count) {
  479. $html = "";
  480. // USPS returns Charges in USD.
  481. $charge[$i] = $ship_postage[$i];
  482. $ship_postage[$i] = $CURRENCY_DISPLAY->getFullValue($charge[$i]);
  483. $shipping_rate_id = urlencode(__CLASS__."|USPS|".$ship_service[$i]."|".$charge[$i]);
  484. //$checked = (@$d["shipping_rate_id"] == $value) ? "checked=\"checked\"" : "";
  485. $html .= "\n<input type=\"radio\" name=\"shipping_rate_id\" checked=\"checked\" value=\"$shipping_rate_id\" id=\"$shipping_rate_id\" />\n";
  486. $_SESSION[$shipping_rate_id] = 1;
  487. $html .= "<label for=\"$shipping_rate_id\">";
  488. $html .= "USPS ".$ship_service[$i]." ";
  489. $html .= "<strong>(".$ship_postage[$i].")</strong>";
  490. if (USPS_SHOW_DELIVERY_QUOTE == 1) {
  491. $html .= "&nbsp;&nbsp;-&nbsp;&nbsp;".$ship_commit[$i];
  492. }
  493. $html .= "</label>";
  494. $html .= "<br />";
  495. if ($dest_country_name == "United States" && $usps_ship[$i] == "TRUE") {
  496. echo $html;
  497. }
  498. else if ($dest_country_name != "United States" && $usps_intl[$i] == "TRUE") {
  499. echo $html;
  500. }
  501. $i++;
  502. }
  503. }
  504. }
  505. return true;
  506. } //end function list_rates
  507. function get_rate( &$d ) {
  508. $shipping_rate_id = $d["shipping_rate_id"];
  509. $is_arr = explode("|", urldecode(urldecode($shipping_rate_id)) );
  510. $order_shipping = (float)$is_arr[3];
  511. return $order_shipping;
  512. } //end function get_rate
  513. function get_tax_rate() {
  514. /** Read current Configuration ***/
  515. require_once(CLASSPATH ."shipping/".__CLASS__.".cfg.php");
  516. if( intval(USPS_TAX_CLASS)== 0 )
  517. return( 0 );
  518. else {
  519. require_once( CLASSPATH. "ps_tax.php" );
  520. $tax_rate = ps_tax::get_taxrate_by_id( intval(USPS_TAX_CLASS) );
  521. return $tax_rate;
  522. }
  523. }
  524. /**
  525. * Validate this Shipping method by checking if the SESSION contains the key
  526. * @returns boolean False when the Shipping method is not in the SESSION
  527. */
  528. function validate( $d ) {
  529. $shipping_rate_id = $d["shipping_rate_id"];
  530. if( array_key_exists( $shipping_rate_id, $_SESSION )) {
  531. return true;
  532. }
  533. else {
  534. return false;
  535. }
  536. } //end function validate
  537. /**
  538. * Show all configuration parameters for this Shipping method
  539. * @returns boolean False when the Shipping method has no configration
  540. */
  541. function show_configuration() {
  542. global $VM_LANG;
  543. /** Read current Configuration ***/
  544. require_once(CLASSPATH ."shipping/".__CLASS__.".cfg.php");
  545. ?>
  546. <table>
  547. <tr>
  548. <td><strong><?php echo $VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_USERNAME') ?></strong></td>
  549. <td><input type="text" name="USPS_USERNAME" class="inputbox"
  550. value="<?php echo USPS_USERNAME ?>" /></td>
  551. <td>
  552. <?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_USERNAME_TOOLTIP')) ?>
  553. </td>
  554. </tr>
  555. <tr>
  556. <td><strong><?php echo $VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_PASSWORD') ?></strong>
  557. </td>
  558. <td><input type="text" name="USPS_PASSWORD" class="inputbox"
  559. value="<?php echo USPS_PASSWORD ?>" /></td>
  560. <td>
  561. <?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_PASSWORD_TOOLTIP')) ?>
  562. </td>
  563. </tr>
  564. <tr>
  565. <td><strong><?php echo $VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_SERVER') ?></strong>
  566. </td>
  567. <td><input type="text" name="USPS_SERVER" class="inputbox"
  568. value="<?php echo USPS_SERVER ?>" /></td>
  569. <td>
  570. <?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_SERVER_TOOLTIP')) ?>
  571. </td>
  572. </tr>
  573. <tr>
  574. <td><strong><?php echo $VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_PATH') ?></strong>
  575. </td>
  576. <td><input type="text" name="USPS_PATH" class="inputbox"
  577. value="<?php echo USPS_PATH ?>" /></td>
  578. <td>
  579. <?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_PATH_TOOLTIP')) ?>
  580. </td>
  581. </tr>
  582. <tr>
  583. <td><strong><?php echo $VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_PACKAGESIZE') ?></strong>
  584. </td>
  585. <td><select name="USPS_PACKAGESIZE">
  586. <option value="REGULAR"
  587. <?php if (USPS_PACKAGESIZE == 'REGULAR') echo "selected=\"selected\""; ?>>Regular</option>
  588. <option value="LARGE"
  589. <?php if (USPS_PACKAGESIZE == 'LARGE') echo "selected=\"selected\""; ?>>Large</option>
  590. <option value="OVERSIZE"
  591. <?php if (USPS_PACKAGESIZE == 'OVERSIZE') echo "selected=\"selected\""; ?>>Oversize</option>
  592. </select></td>
  593. <td>
  594. <?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_PACKAGESIZE_TOOLTIP')) ?>
  595. </td>
  596. </tr>
  597. <tr>
  598. <td><strong><?php echo $VM_LANG->_('PHPSHOP_UPS_TAX_CLASS') ?></strong></td>
  599. <td>
  600. <?php
  601. require_once(CLASSPATH.'ps_tax.php');
  602. ps_tax::list_tax_value("USPS_TAX_CLASS", USPS_TAX_CLASS) ?>
  603. </td>
  604. <td><?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_UPS_TAX_CLASS_TOOLTIP')) ?>
  605. <td>
  606. </tr>
  607. <tr>
  608. <TD colspan="3">
  609. <HR />
  610. </td>
  611. </tr>
  612. <tr>
  613. <td><strong><?php echo $VM_LANG->_('PHPSHOP_USPS_HANDLING_FEE') ?></strong></td>
  614. <td><input class="inputbox" TYPE="text" name="USPS_HANDLINGFEE"
  615. value="<?php echo USPS_HANDLINGFEE ?>" /></td>
  616. <td><?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_USPS_HANDLING_FEE_TOOLTIP')) ?></td>
  617. </tr>
  618. <tr>
  619. <td><strong><?php echo $VM_LANG->_('PHPSHOP_USPS_PADDING') ?></strong></td>
  620. <td><input class="inputbox" TYPE="text" name="USPS_PADDING"
  621. value="<?php echo USPS_PADDING ?>" /></td>
  622. <td><?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_USPS_PADDING_TOOLTIP')) ?></td>
  623. </tr>
  624. <tr>
  625. <td><strong><?php echo $VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_INTLLBRATE') ?></strong>
  626. </td>
  627. <td><input type="text" name="USPS_INTLLBRATE" class="inputbox"
  628. value="<?php echo USPS_INTLLBRATE ?>" /></td>
  629. <td>
  630. <?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_INTLLBRATE_TOOLTIP')) ?>
  631. </td>
  632. </tr>
  633. <tr>
  634. <td><strong><?php echo $VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_INTLHANDLINGFEE') ?></strong>
  635. </td>
  636. <td><input type="text" name="USPS_INTLHANDLINGFEE" class="inputbox"
  637. value="<?php echo USPS_INTLHANDLINGFEE ?>" /></td>
  638. <td>
  639. <?php echo mm_ToolTip($VM_LANG->_('PHPSHOP_ADMIN_CFG_STORE_SHIPPING_METHOD_USPS_INTLHANDLINGFEE_TOOLTIP')) ?>
  640. </td>
  641. </tr>
  642. <tr>
  643. <td><strong><?php echo _VM_LANG_USPS_MACHINABLE ?></strong></td>
  644. <td><label> <input name="USPS_MACHINABLE" type="radio"
  645. <?php if (USPS_MACHINABLE == 1) echo "checked=\"checked\""; ?>
  646. value="1" /> Yes</label> <label> <input name="USPS_MACHINABLE"
  647. type="radio"
  648. <?php if (USPS_MACHINABLE == 0) echo "checked=\"checked\""; ?>
  649. value="0" /> No</label></td>
  650. <td><?php echo mm_ToolTip(_VM_LANG_USPS_MACHINABLE_TOOLTIP) ?></td>
  651. </tr>
  652. <tr>
  653. <td><strong><?php echo _VM_LANG_USPS_QUOTE ?></strong></td>
  654. <td><label> <input name="USPS_SHOW_DELIVERY_QUOTE" type="radio"
  655. <?php if (USPS_SHOW_DELIVERY_QUOTE == 1) echo "checked=\"checked\""; ?>
  656. value="1" /> Yes</label> <label> <input
  657. name="USPS_SHOW_DELIVERY_QUOTE" type="radio"
  658. <?php if (USPS_SHOW_DELIVERY_QUOTE == 0) echo "checked=\"checked\""; ?>
  659. value="0" /> No</label></td>
  660. <td><?php echo mm_ToolTip(_VM_LANG_USPS_QUOTE_TOOLTIP) ?></td>
  661. </tr>
  662. <tr>
  663. <td colspan="3">
  664. <hr><?php echo _VM_LANG_USPS_SHIP; ?><hr>
  665. </td>
  666. </tr>
  667. <!-- added for new shipping rate V2 code ... Domestic Shipping-->
  668. <?php $count = 10; $i = 0; ?>
  669. <?php while ($i <= $count):
  670. $dom_option = constant("USPS_SHIP".$i);
  671. ?>
  672. <tr>
  673. <td><strong><?php $var_name = "_VM_LANG_USPS_SHIP$i"; eval("\$var = $var_name;"); echo $var; ?></strong></td>
  674. <td><label> <input name="USPS_SHIP<?php echo $i; ?>" type="radio"
  675. <?php $var_name = "\$dom_option"; eval("\$var = $var_name;"); if ($var == 1) echo "checked=\"checked\""; ?>
  676. value="1" /> Yes</label> <label> <input
  677. name="USPS_SHIP<?php echo $i; ?>" type="radio"
  678. <?php $var_name = "\$dom_option"; eval("\$var = $var_name;"); if ($var == 0) echo "checked=\"checked\""; ?>
  679. value="0" /> No</label></td>
  680. <td><?php $var_name = "_VM_LANG_USPS_SHIP".$i; eval("\$var = $var_name;"); echo mm_ToolTip($var) ?></td>
  681. </tr>
  682. <?php $i++; ?>
  683. <?php endwhile; ?>
  684. <tr>
  685. <td colspan="3">
  686. <hr><?php echo _VM_LANG_USPS_INTL; ?><hr>
  687. </td>
  688. </tr>
  689. <!-- added for new shipping rate V2 code ... International Shipping -->
  690. <?php $count = 8; $i = 0; ?>
  691. <?php while ($i <= $count):
  692. $int_option = constant("USPS_INTL".$i);
  693. ?>
  694. <tr>
  695. <td><strong><?php $var_name = "_VM_LANG_USPS_INTL$i"; eval("\$var = $var_name;"); echo $var; ?></strong></td>
  696. <td><label> <input name="USPS_INTL<?php echo $i; ?>" type="radio"
  697. <?php $var_name = "\$int_option"; eval("\$var = $var_name;"); if ($var == 1) echo "checked=\"checked\""; ?>
  698. value="1" /> Yes</label> <label> <input
  699. name="USPS_INTL<?php echo $i; ?>" type="radio"
  700. <?php $var_name = "\$int_option"; eval("\$var = $var_name;"); if ($var == 0) echo "checked=\"checked\""; ?>
  701. value="0" /> No</label></td>
  702. <td><?php $var_name = "_VM_LANG_USPS_INTL".$i; eval("\$var = $var_name;"); echo mm_ToolTip($var) ?></td>
  703. </tr>
  704. <?php $i++; ?>
  705. <?php endwhile; ?>
  706. </table>
  707. <?php
  708. // return false if there's no configuration
  709. return true;
  710. } //end function show_configuration
  711. /**
  712. * Returns the "is_writeable" status of the configuration file
  713. * @param void
  714. * @returns boolean True when the configuration file is writeable, false when not
  715. */
  716. function configfile_writeable() {
  717. return is_writeable( CLASSPATH."shipping/".__CLASS__.".cfg.php" );
  718. } //end function configfile_writable
  719. /**
  720. * Writes the configuration file for this shipping method
  721. * @param array An array of objects
  722. * @returns boolean True when writing was successful
  723. */
  724. function write_configuration( &$d ) {
  725. global $vmLogger;
  726. $my_config_array = array("USPS_USERNAME" => vmGet( $d, 'USPS_USERNAME' ),
  727. "USPS_PASSWORD" => vmGet( $d, 'USPS_PASSWORD' ),
  728. "USPS_SERVER" => vmGet( $d, 'USPS_SERVER' ),
  729. "USPS_PATH" => vmGet( $d, 'USPS_PATH' ),
  730. "USPS_PACKAGESIZE" => vmGet( $d, 'USPS_PACKAGESIZE' ),
  731. "USPS_TAX_CLASS" => vmGet( $d, 'USPS_TAX_CLASS' ),
  732. "USPS_HANDLINGFEE" => vmGet( $d, 'USPS_HANDLINGFEE' ),
  733. "USPS_PADDING" => vmGet( $d, 'USPS_PADDING' ),
  734. "USPS_INTLLBRATE" => vmGet( $d, 'USPS_INTLLBRATE' ),
  735. "USPS_INTLHANDLINGFEE" => vmGet( $d, 'USPS_INTLHANDLINGFEE' ),
  736. "USPS_MACHINABLE" => vmGet( $d, 'USPS_MACHINABLE' ),
  737. "USPS_SHOW_DELIVERY_QUOTE" => vmGet( $d, 'USPS_SHOW_DELIVERY_QUOTE' ),
  738. "USPS_SHIP0" => vmGet( $d, 'USPS_SHIP0' ),
  739. "USPS_SHIP1" => vmGet( $d, 'USPS_SHIP1' ),
  740. "USPS_SHIP2" => vmGet( $d, 'USPS_SHIP2' ),
  741. "USPS_SHIP3" => vmGet( $d, 'USPS_SHIP3' ),
  742. "USPS_SHIP4" => vmGet( $d, 'USPS_SHIP4' ),
  743. "USPS_SHIP5" => vmGet( $d, 'USPS_SHIP5' ),
  744. "USPS_SHIP6" => vmGet( $d, 'USPS_SHIP6' ),
  745. "USPS_SHIP7" => vmGet( $d, 'USPS_SHIP7' ),
  746. "USPS_SHIP8" => vmGet( $d, 'USPS_SHIP8' ),
  747. "USPS_SHIP9" => vmGet( $d, 'USPS_SHIP9' ),
  748. "USPS_SHIP10" => vmGet( $d, 'USPS_SHIP10' ),
  749. "USPS_INTL0" => vmGet( $d, 'USPS_INTL0' ),
  750. "USPS_INTL1" => vmGet( $d, 'USPS_INTL1' ),
  751. "USPS_INTL2" => vmGet( $d, 'USPS_INTL2' ),
  752. "USPS_INTL3" => vmGet( $d, 'USPS_INTL3' ),
  753. "USPS_INTL4" => vmGet( $d, 'USPS_INTL4' ),
  754. "USPS_INTL5" => vmGet( $d, 'USPS_INTL5' ),
  755. "USPS_INTL6" => vmGet( $d, 'USPS_INTL6' ),
  756. "USPS_INTL7" => vmGet( $d, 'USPS_INTL7' ),
  757. "USPS_INTL8" => vmGet( $d, 'USPS_INTL8' ),
  758. //"USPS_INTL9" => vmGet( $d, 'USPS_INTL9']
  759. );
  760. $config = "<?php\n";
  761. $config .= "if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); \n\n";
  762. foreach( $my_config_array as $key => $value ) {
  763. $value = str_replace("'", "\'", $value );
  764. $config .= "define ('$key', '$value');\n";
  765. }
  766. $config .= "?>";
  767. if ($fp = fopen(CLASSPATH ."shipping/".__CLASS__.".cfg.php", "w")) {
  768. fputs($fp, $config, strlen($config));
  769. fclose ($fp);
  770. return true;
  771. }
  772. else {
  773. $vmLogger->err( "Error writing to configuration file" );
  774. return false;
  775. }
  776. } //end function write_configuration
  777. }
  778. define( '_VM_LANG_USPS_MACHINABLE', 'Machinable Packages?' );
  779. define( '_VM_LANG_USPS_MACHINABLE_TOOLTIP', 'Can packages be processed on the machine?' );
  780. define( '_VM_LANG_USPS_QUOTE', 'Show Delivery Days Quote?' );
  781. define( '_VM_LANG_USPS_QUOTE_TOOLTIP', 'Show Delivery Days Quote?' );
  782. define( '_VM_LANG_USPS_SHIP', 'Domestic Shipping Options' );
  783. define( '_VM_LANG_USPS_PADDING_TOOLTIP', 'Pad the shipping weight to allow weight for shipping materials' );
  784. define( '_VM_LANG_USPS_SHIP0', 'USPS Express Mail PO to PO' );
  785. define( '_VM_LANG_USPS_SHIP1', 'USPS Express Mail' );
  786. define( '_VM_LANG_USPS_SHIP2', 'USPS Express Mail Flat-Rate Envelope' );
  787. define( '_VM_LANG_USPS_SHIP3', 'USPS Priority Mail' );
  788. define( '_VM_LANG_USPS_SHIP4', 'USPS Priority Mail Flat Rate Envelope' );
  789. define( '_VM_LANG_USPS_SHIP5', 'USPS Priority Mail Flat Rate Box' );
  790. define( '_VM_LANG_USPS_SHIP6', 'USPS First-Class Mail' );
  791. define( '_VM_LANG_USPS_SHIP7', 'USPS Parcel Post' );
  792. define( '_VM_LANG_USPS_SHIP8', 'USPS Bound Printed Matter' );
  793. define( '_VM_LANG_USPS_SHIP9', 'USPS Media Mail' );
  794. define( '_VM_LANG_USPS_SHIP10', 'USPS Library Mail' );
  795. define( '_VM_LANG_USPS_INTL', 'International Shipping Options' );
  796. define( '_VM_LANG_USPS_INTL0', 'USPS Global Express Guaranteed' );
  797. define( '_VM_LANG_USPS_INTL1', 'USPS Global Express Guaranteed Non-Document Rectangular' );
  798. define( '_VM_LANG_USPS_INTL2', 'USPS Global Express Non-Rectangular' );
  799. define( '_VM_LANG_USPS_INTL3', 'USPS Express Mail International (EMS)' );
  800. define( '_VM_LANG_USPS_INTL4', 'USPS Express Mail International (EMS) Flat Rate Envelope' );
  801. define( '_VM_LANG_USPS_INTL5', 'USPS Priority Mail International' );
  802. define( '_VM_LANG_USPS_INTL6', 'USPS Priority Mail International Flat Rate Envelope' );
  803. define( '_VM_LANG_USPS_INTL7', 'USPS Priority Mail International Flat Rate Box' );
  804. define( '_VM_LANG_USPS_INTL8', 'USPS First-Class Mail International' );
  805. //define( '_VM_LANG_USPS_INTL9', 'Currently Not Used - Do Not Select' );
  806. ?>