Advertisement
Guest User

Untitled

a guest
Feb 17th, 2012
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 86.61 KB | None | 0 0
  1. <?php
  2. /*
  3. $Id: usps.php 6.1 by Kevin L Shelton on September 7, 2011
  4. +++++ Original contribution by Brad Waite and Fritz Clapp ++++
  5. ++++ Revisions and Modifications made by Greg Deeth, 2008 ++++
  6. Copyright 2008 osCommerce
  7. Released under the GNU General Public License
  8. //VERSION: 5.2.1 ALPHA LAST UPDATED: January 23rd, 2011 by Fulluv Scents
  9. */
  10.  
  11. // Incorporate the XML conversion library
  12. if (PHP_VERSION >= '5.0.0') { // PHP 5 does not need to use call-time pass by reference
  13. require_once (DIR_WS_CLASSES . 'xml_5.php');
  14. } else {
  15. require_once (DIR_WS_CLASSES . 'xml.php');
  16. }
  17.  
  18. if ( !function_exists('htmlspecialchars_decode') ) {
  19. function htmlspecialchars_decode($text) {
  20. return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  21. }
  22. }
  23. // Sets up USPS Class
  24. class usps {
  25.  
  26. // Sets Variables
  27. var $code, $title, $description, $icon, $enabled, $countries;
  28.  
  29. function usps() {
  30. global $order, $packing;
  31. $this->code = 'usps';
  32. $this->title = MODULE_SHIPPING_USPS_TEXT_TITLE;
  33. $this->description = MODULE_SHIPPING_USPS_TEXT_DESCRIPTION;
  34. $this->icon = DIR_WS_ICONS . 'shipping_usps.gif';
  35. $this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
  36. if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPS_ZONE > 0) ) {
  37. $check_flag = false;
  38. $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_USPS_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
  39. while ($check = tep_db_fetch_array($check_query)) {
  40. if ($check['zone_id'] < 1) {
  41. $check_flag = true;
  42. break;
  43. } elseif ($check['zone_id'] == $order->delivery['zone_id']) {
  44. $check_flag = true;
  45. break;
  46. }
  47. }
  48. if ($check_flag == false) {
  49. $this->enabled = false;
  50. }
  51. }
  52. if (defined('SHIPPING_DIMENSIONS_SUPPORT') && SHIPPING_DIMENSIONS_SUPPORT == 'Ready-to-ship only') {
  53. $this->dimensions_support = 1;
  54. } elseif (defined('SHIPPING_DIMENSIONS_SUPPORT') && SHIPPING_DIMENSIONS_SUPPORT == 'With product dimensions') {
  55. $this->dimensions_support = 2;
  56. } else {
  57. $this->dimensions_support = 0;
  58. }
  59. $this->countries = $this->country_list();
  60. $this->sort_order = MODULE_SHIPPING_USPS_SORT_ORDER;
  61. $this->tax_class = MODULE_SHIPPING_USPS_TAX_CLASS;
  62. $this->sig_conf_thresh = MODULE_SHIPPING_USPS_SIG_THRESH;
  63. $options = explode(', ', MODULE_SHIPPING_USPS_OPTIONS);
  64. $this->display_weight = in_array('Display Weight', $options);
  65. $this->display_transit = in_array('Display Transit Time', $options);
  66. $this->display_insurance = in_array('Display Insurance', $options);
  67. $this->display_confirmation = in_array('Display Sig./Del. Confirmation', $options);
  68. $this->types = array(
  69. 'Express Mail' => 'Express Mail<sup>&reg;</sup>',
  70. 'Express Mail Hold For Pickup' => 'Express Mail<sup>&reg;</sup> Hold For Pickup',
  71. 'Express Mail Sunday/Holiday Delivery' => 'Express Mail<sup>&reg;</sup> Sunday/Holiday Delivery',
  72. 'Express Mail Flat Rate Envelope' => 'Express Mail<sup>&reg;</sup> Flat Rate Envelope',
  73. 'Express Mail Flat Rate Envelope Hold For Pickup' => 'Express Mail<sup>&reg;</sup> Flat Rate Envelope Hold For Pickup',
  74. 'Express Mail Sunday/Holiday Delivery Flat Rate Envelope' => 'Express Mail<sup>&reg;</sup> Sunday/Holiday Delivery Flat Rate Envelope',
  75. 'Express Mail Legal Flat Rate Envelope' => 'Express Mail<sup>&reg;</sup> Legal Flat Rate Envelope',
  76. 'Express Mail Legal Flat Rate Envelope Hold For Pickup' => 'Express Mail<sup>&reg;</sup> Legal Flat Rate Envelope Hold For Pickup',
  77. 'Express Mail Sunday/Holiday Delivery Legal Flat Rate Envelope' => 'Express Mail<sup>&reg;</sup> Sunday/Holiday Delivery Legal Flat Rate Envelope',
  78. 'Priority Mail' => 'Priority Mail<sup>&reg;</sup>',
  79. 'Priority Mail Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Hold For Pickup',
  80. 'Priority Mail Large Flat Rate Box' => 'Priority Mail<sup>&reg;</sup> Large Flat Rate Box',
  81. 'Priority Mail Large Flat Rate Box Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Large Flat Rate Box Hold For Pickup',
  82. 'Priority Mail Medium Flat Rate Box' => 'Priority Mail<sup>&reg;</sup> Medium Flat Rate Box',
  83. 'Priority Mail Medium Flat Rate Box Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Medium Flat Rate Box Hold For Pickup',
  84. 'Priority Mail Small Flat Rate Box' => 'Priority Mail<sup>&reg;</sup> Small Flat Rate Box',
  85. 'Priority Mail Small Flat Rate Box Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Small Flat Rate Box Hold For Pickup',
  86. 'Priority Mail Regional Rate Box A' => 'Priority Mail<sup>&reg;</sup> Regional Rate Box A',
  87. 'Priority Mail Regional Rate Box A Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Regional Rate Box A Hold For Pickup',
  88. 'Priority Mail Regional Rate Box B' => 'Priority Mail<sup>&reg;</sup> Regional Rate Box B',
  89. 'Priority Mail Regional Rate Box B Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Regional Rate Box B Hold For Pickup',
  90. 'Priority Mail Flat Rate Envelope' => 'Priority Mail<sup>&reg;</sup> Flat Rate Envelope',
  91. 'Priority Mail Flat Rate Envelope Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Flat Rate Envelope Hold For Pickup',
  92. 'Priority Mail Legal Flat Rate Envelope' => 'Priority Mail<sup>&reg;</sup> Legal Flat Rate Envelope',
  93. 'Priority Mail Legal Flat Rate Envelope Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Legal Flat Rate Envelope Hold For Pickup',
  94. 'Priority Mail Padded Flat Rate Envelope' => 'Priority Mail<sup>&reg;</sup> Padded Flat Rate Envelope',
  95. 'Priority Mail Padded Flat Rate Envelope Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Padded Flat Rate Envelope Hold For Pickup',
  96. 'Priority Mail Gift Card Flat Rate Envelope' => 'Priority Mail<sup>&reg;</sup> Gift Card Flat Rate Envelope',
  97. 'Priority Mail Gift Card Flat Rate Envelope Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Gift Card Flat Rate Envelope Hold For Pickup',
  98. 'Priority Mail Small Flat Rate Envelope' => 'Priority Mail<sup>&reg;</sup> Small Flat Rate Envelope',
  99. 'Priority Mail Small Flat Rate Envelope Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Small Flat Rate Envelope Hold For Pickup',
  100. 'Priority Mail Window Flat Rate Envelope' => 'Priority Mail<sup>&reg;</sup> Window Flat Rate Envelope',
  101. 'Priority Mail Window Flat Rate Envelope Hold For Pickup' => 'Priority Mail<sup>&reg;</sup> Window Flat Rate Envelope Hold For Pickup',
  102. 'First-Class Parcel' => 'First-Class Mail<sup>&reg;</sup> Parcel',
  103. 'First-Class Mail Parcel Hold For Pickup' => 'First-Class Mail<sup>&reg;</sup> Parcel Hold For Pickup',
  104. 'First-Class Mail Large Envelope' => 'First-Class Mail<sup>&reg;</sup> Large Envelope',
  105. 'First-Class Mail Letter' => 'First-Class Mail<sup>&reg;</sup> Letter',
  106. 'Parcel Post' => 'Parcel Post<sup>&reg;</sup>',
  107. 'Media Mail' => 'Media Mail<sup>&reg;</sup>',
  108. 'Library Mail' => 'Library Mail'
  109. );
  110. $this->type_to_request = array(
  111. 'Express Mail' => 'Express Commercial',
  112. 'Express Mail Hold For Pickup' => 'Express HFP Commercial',
  113. 'Express Mail Sunday/Holiday Delivery' => 'Express SH Commercial',
  114. 'Express Mail Flat Rate Envelope' => 'Express Commercial',
  115. 'Express Mail Flat Rate Envelope Hold For Pickup' => 'Express HFP Commercial',
  116. 'Express Mail Sunday/Holiday Delivery Flat Rate Envelope' => 'Express SH Commercial',
  117. 'Express Mail Legal Flat Rate Envelope' => 'Express Commercial',
  118. 'Express Mail Legal Flat Rate Envelope Hold For Pickup' => 'Express HFP Commercial',
  119. 'Express Mail Sunday/Holiday Delivery Legal Flat Rate Envelope' => 'Express SH Commercial',
  120. 'Priority Mail' => 'Priority Commercial',
  121. 'Priority Mail Hold For Pickup' => 'Priority HFP Commercial',
  122. 'Priority Mail Large Flat Rate Box' => 'Priority Commercial',
  123. 'Priority Mail Large Flat Rate Box Hold For Pickup' => 'Priority HFP Commercial',
  124. 'Priority Mail Medium Flat Rate Box' => 'Priority Commercial',
  125. 'Priority Mail Medium Flat Rate Box Hold For Pickup' => 'Priority HFP Commercial',
  126. 'Priority Mail Small Flat Rate Box' => 'Priority Commercial',
  127. 'Priority Mail Small Flat Rate Box Hold For Pickup' => 'Priority HFP Commercial',
  128. 'Priority Mail Regional Rate Box A' => 'Priority Commercial',
  129. 'Priority Mail Regional Rate Box A Hold For Pickup' => 'Priority HFP Commercial',
  130. 'Priority Mail Regional Rate Box B' => 'Priority Commercial',
  131. 'Priority Mail Regional Rate Box B Hold For Pickup' => 'Priority HFP Commercial',
  132. 'Priority Mail Flat Rate Envelope' => 'Priority Commercial',
  133. 'Priority Mail Flat Rate Envelope Hold For Pickup' => 'Priority HFP Commercial',
  134. 'Priority Mail Legal Flat Rate Envelope' => 'Priority Commercial',
  135. 'Priority Mail Legal Flat Rate Envelope Hold For Pickup' => 'Priority HFP Commercial',
  136. 'Priority Mail Padded Flat Rate Envelope' => 'Priority Commercial',
  137. 'Priority Mail Padded Flat Rate Envelope Hold For Pickup' => 'Priority HFP Commercial',
  138. 'Priority Mail Gift Card Flat Rate Envelope' => 'Priority Commercial',
  139. 'Priority Mail Gift Card Flat Rate Envelope Hold For Pickup' => 'Priority HFP Commercial',
  140. 'Priority Mail Small Flat Rate Envelope' => 'Priority Commercial',
  141. 'Priority Mail Small Flat Rate Envelope Hold For Pickup' => 'Priority HFP Commercial',
  142. 'Priority Mail Window Flat Rate Envelope' => 'Priority Commercial',
  143. 'Priority Mail Window Flat Rate Envelope Hold For Pickup' => 'Priority HFP Commercial',
  144. 'First-Class Mail Parcel' => 'First Class Commercial',
  145. 'First-Class Mail Parcel Hold For Pickup' => 'First Class HFP Commercial',
  146. 'First-Class Mail Large Envelope' => 'First Class',
  147. 'First-Class Mail Letter' => 'First Class',
  148. 'Parcel Post' => 'Parcel',
  149. 'Media Mail' => 'Media',
  150. 'Library Mail' => 'Library'
  151. );
  152. $this->type_to_container = array(
  153. 'Express Mail' => '',
  154. 'Express Mail Hold For Pickup' => '',
  155. 'Express Mail Sunday/Holiday Delivery' => '',
  156. 'Express Mail Flat Rate Envelope' => 'Flat Rate Envelope',
  157. 'Express Mail Flat Rate Envelope Hold For Pickup' => 'Flat Rate Envelope',
  158. 'Express Mail Sunday/Holiday Delivery Flat Rate Envelope' => 'Flat Rate Envelope',
  159. 'Express Mail Legal Flat Rate Envelope' => 'Legal Flat Rate Envelope',
  160. 'Express Mail Legal Flat Rate Envelope Hold For Pickup' => 'Legal Flat Rate Envelope',
  161. 'Express Mail Sunday/Holiday Delivery Legal Flat Rate Envelope' => 'Legal Flat Rate Envelope',
  162. 'Priority Mail' => '',
  163. 'Priority Mail Hold For Pickup' => '',
  164. 'Priority Mail Large Flat Rate Box' => 'LG Flat Rate Box',
  165. 'Priority Mail Large Flat Rate Box Hold For Pickup' => 'LG Flat Rate Box',
  166. 'Priority Mail Medium Flat Rate Box' => 'MD Flat Rate Box',
  167. 'Priority Mail Medium Flat Rate Box Hold For Pickup' => 'MD Flat Rate Box',
  168. 'Priority Mail Small Flat Rate Box' => 'SM Flat Rate Box',
  169. 'Priority Mail Small Flat Rate Box Hold For Pickup' => 'SM Flat Rate Box',
  170. 'Priority Mail Regional Rate Box A' => 'Regional Rate Box A',
  171. 'Priority Mail Regional Rate Box A Hold For Pickup' => 'Regional Rate Box A',
  172. 'Priority Mail Regional Rate Box B' => 'Regional Rate Box B',
  173. 'Priority Mail Regional Rate Box B Hold For Pickup' => 'Regional Rate Box B',
  174. 'Priority Mail Flat Rate Envelope' => 'Flat Rate Envelope',
  175. 'Priority Mail Flat Rate Envelope Hold For Pickup' => 'Flat Rate Envelope',
  176. 'Priority Mail Legal Flat Rate Envelope' => 'Legal Flat Rate Envelope',
  177. 'Priority Mail Legal Flat Rate Envelope Hold For Pickup' => 'Legal Flat Rate Envelope',
  178. 'Priority Mail Padded Flat Rate Envelope' => 'Padded Flat Rate Envelope',
  179. 'Priority Mail Padded Flat Rate Envelope Hold For Pickup' => 'Padded Flat Rate Envelope',
  180. 'Priority Mail Gift Card Flat Rate Envelope' => 'Gift Card Flat Rate Envelope',
  181. 'Priority Mail Gift Card Flat Rate Envelope Hold For Pickup' => 'Gift Card Flat Rate Envelope',
  182. 'Priority Mail Small Flat Rate Envelope' => 'SM Flat Rate Envelope',
  183. 'Priority Mail Small Flat Rate Envelope Hold For Pickup' => 'SM Flat Rate Envelope',
  184. 'Priority Mail Window Flat Rate Envelope' => 'Window Flat Rate Envelope',
  185. 'Priority Mail Window Flat Rate Envelope Hold For Pickup' => 'Window Flat Rate Envelope',
  186. 'First-Class Mail Parcel' => '',
  187. 'First-Class Mail Parcel Hold For Pickup' => '',
  188. 'First-Class Mail Large Envelope' => '',
  189. 'First-Class Mail Letter' => '',
  190. 'Parcel Post' => '',
  191. 'Media Mail' => '',
  192. 'Library Mail' => ''
  193. );
  194. $this->intl_types = array(
  195. 'USPS GXG Envelopes**' => 'USPS GXG<sup>&trade;</sup> Envelopes**',
  196. 'Express Mail International' => 'Express Mail<sup>&reg;</sup> International',
  197. 'Express Mail International Flat Rate Envelope' => 'Express Mail<sup>&reg;</sup> International Flat Rate Envelope',
  198. 'Express Mail International Legal Flat Rate Envelope' => 'Express Mail<sup>&reg;</sup> International Legal Flat Rate Envelope',
  199. 'Priority Mail International' => 'Priority Mail<sup>&reg;</sup> International',
  200. 'Priority Mail International Large Flat Rate Box' => 'Priority Mail<sup>&reg;</sup> International Large Flat Rate Box',
  201. 'Priority Mail International Medium Flat Rate Box' => 'Priority Mail<sup>&reg;</sup> International Medium Flat Rate Box',
  202. 'Priority Mail International Small Flat Rate Box**' => 'Priority Mail<sup>&reg;</sup> International Small Flat Rate Box**',
  203. 'Priority Mail International DVD Flat Rate Box**' => 'Priority Mail<sup>&reg;</sup> International DVD Flat Rate Box**',
  204. 'Priority Mail International Large Video Flat Rate Box**' => 'Priority Mail<sup>&reg;</sup> International Large Video Flat Rate Box**',
  205. 'Priority Mail International Flat Rate Envelope**' => 'Priority Mail<sup>&reg;</sup> International Flat Rate Envelope**',
  206. 'Priority Mail International Legal Flat Rate Envelope**' => 'Priority Mail<sup>&reg;</sup> International Legal Flat Rate Envelope**',
  207. 'Priority Mail International Padded Flat Rate Envelope**' => 'Priority Mail<sup>&reg;</sup> International Padded Flat Rate Envelope**',
  208. 'Priority Mail International Gift Card Flat Rate Envelope**' => 'Priority Mail<sup>&reg;</sup> International Gift Card Flat Rate Envelope**',
  209. 'Priority Mail International Small Flat Rate Envelope**' => 'Priority Mail<sup>&reg;</sup> International Small Flat Rate Envelope**',
  210. 'Priority Mail International Window Flat Rate Envelope**' => 'Priority Mail<sup>&reg;</sup> International Window Flat Rate Envelope**',
  211. 'First-Class Mail International Parcel**' => 'First-Class Mail<sup>&reg;</sup> International Parcel**',
  212. 'First-Class Mail International Large Envelope**' => 'First-Class Mail<sup>&reg;</sup> International Large Envelope**',
  213. 'First-Class Mail International Letter**' => 'First-Class Mail<sup>&reg;</sup> International Letter**',
  214. 'Global Express Guaranteed (GXG)**' => 'Global Express Guaranteed<sup>&reg;</sup> (GXG)**'
  215. );
  216. $this->dmstc_available = explode(', ', MODULE_SHIPPING_USPS_DMSTC_TYPES);
  217. $this->intl_available = explode(', ', MODULE_SHIPPING_USPS_INTL_TYPES);
  218. list($length, $width, $height, $ounces) = explode(', ', MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_LETTER);
  219. $this->first_class_letter = array('length' => $length, 'width' => $width, 'height' => $height, 'ounces' => $ounces);
  220. list($length, $width, $height, $ounces) = explode(', ', MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_LRGLTR);
  221. $this->first_class_large_letter = array('length' => $length, 'width' => $width, 'height' => $height, 'ounces' => $ounces);
  222. $this->first_class_parcel_max_ounces = MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_PARCEL;
  223. $handling = explode( ", ", MODULE_SHIPPING_USPS_DMSTC_HANDLING);
  224. $x = 0;
  225. $tmp = array(); // handling values are stored in same order as types
  226. foreach ($this->types as $key => $title) { // create handling array keyed by method
  227. $tmp[$key] = $handling[$x];
  228. $x++;
  229. }
  230. $this->dmstc_handling = $tmp;
  231. $handling = explode( ", ", MODULE_SHIPPING_USPS_INTL_HANDLING);
  232. $x = 0;
  233. $tmp = array();
  234. foreach ($this->intl_types as $key => $title) { // create handling array keyed by method
  235. $tmp[$key] = $handling[$x];
  236. $x++;
  237. }
  238. $this->intl_handling = $tmp;
  239. } // end function usps
  240. // Ends Variable Set Up
  241.  
  242. // Sets Country List
  243. function country_list() {
  244. $list = array(
  245. 'AF' => 'Afghanistan',
  246. 'AX' => 'Aland Island (Finland)',
  247. 'AL' => 'Albania',
  248. 'DZ' => 'Algeria',
  249. 'AD' => 'Andorra',
  250. 'AO' => 'Angola',
  251. 'AI' => 'Anguilla',
  252. 'AG' => 'Antigua and Barbuda',
  253. 'AR' => 'Argentina',
  254. 'AM' => 'Armenia',
  255. 'AW' => 'Aruba',
  256. 'AU' => 'Australia',
  257. 'AT' => 'Austria',
  258. 'AZ' => 'Azerbaijan',
  259. 'BS' => 'Bahamas',
  260. 'BH' => 'Bahrain',
  261. 'BD' => 'Bangladesh',
  262. 'BB' => 'Barbados',
  263. 'BY' => 'Belarus',
  264. 'BE' => 'Belgium',
  265. 'BZ' => 'Belize',
  266. 'BJ' => 'Benin',
  267. 'BM' => 'Bermuda',
  268. 'BT' => 'Bhutan',
  269. 'BO' => 'Bolivia',
  270. 'BQ' => 'Bonaire (Netherlands Antilles)',
  271. 'BA' => 'Bosnia-Herzegovina',
  272. 'BW' => 'Botswana',
  273. 'BR' => 'Brazil',
  274. 'VG' => 'British Virgin Islands',
  275. 'BN' => 'Brunei Darussalam',
  276. 'BG' => 'Bulgaria',
  277. 'BF' => 'Burkina Faso',
  278. 'MM' => 'Burma',
  279. 'BI' => 'Burundi',
  280. 'KH' => 'Cambodia',
  281. 'CM' => 'Cameroon',
  282. 'CA' => 'Canada',
  283. 'CV' => 'Cape Verde',
  284. 'KY' => 'Cayman Islands',
  285. 'CF' => 'Central African Republic',
  286. 'TD' => 'Chad',
  287. 'CL' => 'Chile',
  288. 'CN' => 'China',
  289. 'CX' => 'Christmas Island (Australia)',
  290. 'CC' => 'Cocos Island (Australia)',
  291. 'CO' => 'Colombia',
  292. 'KM' => 'Comoros',
  293. 'CG' => 'Congo, Republic of the',
  294. 'CD' => 'Congo, Democratic Republic of the',
  295. 'CK' => 'Cook Islands (New Zealand)',
  296. 'CR' => 'Costa Rica',
  297. 'CI' => 'Cote d\'Ivoire',
  298. 'HR' => 'Croatia',
  299. 'CU' => 'Cuba',
  300. 'CW' => 'Curacao (Netherlands Antilles)',
  301. 'CY' => 'Cyprus',
  302. 'CZ' => 'Czech Republic',
  303. 'DK' => 'Denmark',
  304. 'DJ' => 'Djibouti',
  305. 'DM' => 'Dominica',
  306. 'DO' => 'Dominican Republic',
  307. 'TL' => 'East Timor (Indonesia)',
  308. 'EC' => 'Ecuador',
  309. 'EG' => 'Egypt',
  310. 'SV' => 'El Salvador',
  311. 'GQ' => 'Equatorial Guinea',
  312. 'ER' => 'Eritrea',
  313. 'EE' => 'Estonia',
  314. 'ET' => 'Ethiopia',
  315. 'FK' => 'Falkland Islands',
  316. 'FO' => 'Faroe Islands',
  317. 'FJ' => 'Fiji',
  318. 'FI' => 'Finland',
  319. 'FR' => 'France',
  320. 'GF' => 'French Guiana',
  321. 'PF' => 'French Polynesia',
  322. 'GA' => 'Gabon',
  323. 'GM' => 'Gambia',
  324. 'GE' => 'Georgia, Republic of',
  325. 'DE' => 'Germany',
  326. 'GH' => 'Ghana',
  327. 'GI' => 'Gibraltar',
  328. 'GB' => 'Great Britain and Northern Ireland',
  329. 'GR' => 'Greece',
  330. 'GL' => 'Greenland',
  331. 'GD' => 'Grenada',
  332. 'GP' => 'Guadeloupe',
  333. 'GT' => 'Guatemala',
  334. 'GG' => 'Guernsey',
  335. 'GN' => 'Guinea',
  336. 'GW' => 'Guinea-Bissau',
  337. 'GY' => 'Guyana',
  338. 'HT' => 'Haiti',
  339. 'HN' => 'Honduras',
  340. 'HK' => 'Hong Kong',
  341. 'HU' => 'Hungary',
  342. 'IS' => 'Iceland',
  343. 'IN' => 'India',
  344. 'ID' => 'Indonesia',
  345. 'IR' => 'Iran',
  346. 'IQ' => 'Iraq',
  347. 'IE' => 'Ireland',
  348. 'IM' => 'Isle of Man (Great Britain and Northern Ireland)',
  349. 'IL' => 'Israel',
  350. 'IT' => 'Italy',
  351. 'JM' => 'Jamaica',
  352. 'JP' => 'Japan',
  353. 'JE' => 'Jersey (Channel Islands) (Great Britain and Northern Ireland)',
  354. 'JO' => 'Jordan',
  355. 'KZ' => 'Kazakhstan',
  356. 'KE' => 'Kenya',
  357. 'KI' => 'Kiribati',
  358. 'KW' => 'Kuwait',
  359. 'KG' => 'Kyrgyzstan',
  360. 'LA' => 'Laos',
  361. 'LV' => 'Latvia',
  362. 'LB' => 'Lebanon',
  363. 'LS' => 'Lesotho',
  364. 'LR' => 'Liberia',
  365. 'LY' => 'Libya',
  366. 'LI' => 'Liechtenstein',
  367. 'LT' => 'Lithuania',
  368. 'LU' => 'Luxembourg',
  369. 'MO' => 'Macao',
  370. 'MK' => 'Macedonia, Republic of',
  371. 'MG' => 'Madagascar',
  372. 'MW' => 'Malawi',
  373. 'MY' => 'Malaysia',
  374. 'MV' => 'Maldives',
  375. 'ML' => 'Mali',
  376. 'MT' => 'Malta',
  377. 'MQ' => 'Martinique',
  378. 'MR' => 'Mauritania',
  379. 'MU' => 'Mauritius',
  380. 'YT' => 'Mayotte (France)',
  381. 'MX' => 'Mexico',
  382. 'MD' => 'Moldova',
  383. 'MC' => 'Monaco (France)',
  384. 'MN' => 'Mongolia',
  385. 'ME' => 'Montenegro',
  386. 'MS' => 'Montserrat',
  387. 'MA' => 'Morocco',
  388. 'MZ' => 'Mozambique',
  389. 'NA' => 'Namibia',
  390. 'NR' => 'Nauru',
  391. 'NP' => 'Nepal',
  392. 'NL' => 'Netherlands',
  393. 'AN' => 'Netherlands Antilles',
  394. 'NC' => 'New Caledonia',
  395. 'NZ' => 'New Zealand',
  396. 'NI' => 'Nicaragua',
  397. 'NE' => 'Niger',
  398. 'NG' => 'Nigeria',
  399. 'NU' => 'Niue',
  400. 'NF' => 'Norfolk Island',
  401. 'KP' => 'North Korea (Korea, Democratic People\'s Republic of)',
  402. 'NO' => 'Norway',
  403. 'OM' => 'Oman',
  404. 'PK' => 'Pakistan',
  405. 'PA' => 'Panama',
  406. 'PG' => 'Papua New Guinea',
  407. 'PY' => 'Paraguay',
  408. 'PE' => 'Peru',
  409. 'PH' => 'Philippines',
  410. 'PN' => 'Pitcairn Island',
  411. 'PL' => 'Poland',
  412. 'PT' => 'Portugal',
  413. 'QA' => 'Qatar',
  414. 'RE' => 'Reunion',
  415. 'RO' => 'Romania',
  416. 'RU' => 'Russia',
  417. 'RW' => 'Rwanda',
  418. 'BL' => 'Saint Barthelemy (Guadeloupe)',
  419. 'SH' => 'Saint Helena',
  420. 'KN' => 'Saint Kitts (Saint Christopher and Nevis)',
  421. 'LC' => 'Saint Lucia',
  422. 'MF' => 'Saint Martin (French) (Guadeloupe)',
  423. 'PM' => 'Saint Pierre and Miquelon',
  424. 'VC' => 'Saint Vincent and the Grenadines',
  425. 'SM' => 'San Marino',
  426. 'ST' => 'Sao Tome and Principe',
  427. 'SA' => 'Saudi Arabia',
  428. 'SN' => 'Senegal',
  429. 'RS' => 'Serbia, Republic of',
  430. 'SC' => 'Seychelles',
  431. 'SL' => 'Sierra Leone',
  432. 'SG' => 'Singapore',
  433. 'SX' => 'Saint Maarten (Dutch) (Netherlands Antilles)',
  434. 'SK' => 'Slovak Republic',
  435. 'SI' => 'Slovenia',
  436. 'SB' => 'Solomon Islands',
  437. 'SO' => 'Somalia',
  438. 'ZA' => 'South Africa',
  439. 'GS' => 'South Georgia (Falkland Islands)',
  440. 'KR' => 'South Korea (Korea, Republic of)',
  441. 'SS' => 'Sudan', // South Sudan, not listed separately from Sudan by USPS
  442. 'ES' => 'Spain',
  443. 'LK' => 'Sri Lanka',
  444. 'SD' => 'Sudan',
  445. 'SR' => 'Suriname',
  446. 'SZ' => 'Swaziland',
  447. 'SE' => 'Sweden',
  448. 'CH' => 'Switzerland',
  449. 'SY' => 'Syrian Arab Republic',
  450. 'TW' => 'Taiwan',
  451. 'TJ' => 'Tajikistan',
  452. 'TZ' => 'Tanzania',
  453. 'TH' => 'Thailand',
  454. 'TG' => 'Togo',
  455. 'TK' => 'Tokelau (Union Group) (Western Samoa)',
  456. 'TO' => 'Tonga',
  457. 'TT' => 'Trinidad and Tobago',
  458. 'TN' => 'Tunisia',
  459. 'TR' => 'Turkey',
  460. 'TM' => 'Turkmenistan',
  461. 'TC' => 'Turks and Caicos Islands',
  462. 'TV' => 'Tuvalu',
  463. 'UG' => 'Uganda',
  464. 'UA' => 'Ukraine',
  465. 'AE' => 'United Arab Emirates',
  466. 'UY' => 'Uruguay',
  467. 'UZ' => 'Uzbekistan',
  468. 'VU' => 'Vanuatu',
  469. 'VA' => 'Vatican City',
  470. 'VE' => 'Venezuela',
  471. 'VN' => 'Vietnam',
  472. 'WF' => 'Wallis and Futuna Islands',
  473. 'WS' => 'Western Samoa',
  474. 'YE' => 'Yemen',
  475. 'ZM' => 'Zambia',
  476. 'ZW' => 'Zimbabwe');
  477. return $list;
  478. }
  479. // Ends Country List
  480.  
  481. // Sets Quote
  482. function quote($method = '') {
  483. global $order, $shipping_weight, $shipping_num_boxes, $transittime, $dispinsure, $packing;
  484. if ($this->dimensions_support > 0 && is_object($packing)) {
  485. $totalWeight = $packing->getTotalWeight();
  486. $boxesToShip = $packing->getPackedBoxes();
  487. if ($this->dimensions_support == 1) { // only ready to ship items are set with dimensions
  488. for ($i = 0; $i < sizeof($boxesToShip); $i++) {
  489. if ($boxesToShip[$i]['item_length'] == 0) { // size wasn't set
  490. if ($boxesToShip[$i]['item_weight'] > 60) { // use module estimated dimesions when size not set
  491. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER60);
  492. } elseif ($boxesToShip[$i]['item_weight'] > 50) {
  493. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER50);
  494. } elseif ($boxesToShip[$i]['item_weight'] > 40) {
  495. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER40);
  496. } elseif ($boxesToShip[$i]['item_weight'] > 30) {
  497. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER30);
  498. } elseif ($boxesToShip[$i]['item_weight'] > 20) {
  499. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER20);
  500. } elseif ($boxesToShip[$i]['item_weight'] > 10) {
  501. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER10);
  502. } elseif ($boxesToShip[$i]['item_weight'] > 5) {
  503. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER5);
  504. } else {
  505. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_LESS5);
  506. }
  507. $boxesToShip[$i]['item_length'] = $length;
  508. $boxesToShip[$i]['item_width'] = $width;
  509. $boxesToShip[$i]['item_height'] = $height;
  510. }
  511. }
  512. }
  513. $numBoxes = $packing->getNumberOfBoxes();
  514. if (SHIPPING_UNIT_LENGTH == 'CM') { // must convert centimeters to inches before getting quote
  515. for ($i = 0; $i < sizeof($boxesToShip); $i++) {
  516. $boxesToShip[$i]['item_length'] *= 0.39370079;
  517. $boxesToShip[$i]['item_width'] *= 0.39370079;
  518. $boxesToShip[$i]['item_height'] *= 0.39370079;
  519. }
  520. }
  521. } else { // The old method. Let osCommerce tell us how many boxes plus the box weight
  522. if ($shipping_weight > 60) { // these are defined in inches and don't need converting
  523. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER60);
  524. } elseif ($shipping_weight > 50) {
  525. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER50);
  526. } elseif ($shipping_weight > 40) {
  527. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER40);
  528. } elseif ($shipping_weight > 30) {
  529. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER30);
  530. } elseif ($shipping_weight > 20) {
  531. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER20);
  532. } elseif ($shipping_weight > 10) {
  533. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER10);
  534. } elseif ($shipping_weight > 5) {
  535. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_OVER5);
  536. } else {
  537. list($length, $width, $height) = explode(', ', MODULE_SHIPPING_USPS_PKG_SIZE_LESS5);
  538. }
  539. $package = array('item_weight' => $shipping_weight,
  540. 'item_price' => round($order->info['subtotal'] / $shipping_num_boxes, 2),
  541. 'item_length' => $length,
  542. 'item_width' => $width,
  543. 'item_height' => $height);
  544. $boxesToShip = array();
  545. for ($i = 0; $i < $shipping_num_boxes; $i++) {
  546. $boxesToShip[] = $package;
  547. }
  548. $totalWeight = round($shipping_weight * $shipping_num_boxes, 2);
  549. $numBoxes = $shipping_num_boxes;
  550. }
  551. if (SHIPPING_UNIT_WEIGHT == 'KGS') { // must convert kilograms to pounds before getting quote
  552. for ($i = 0; $i < sizeof($boxesToShip); $i++) {
  553. $boxesToShip[$i]['item_weight'] *= 2.2046226;
  554. }
  555. }
  556. if ($this->display_weight) {
  557. $shiptitle = sprintf(MODULE_SHIPPING_USPS_TEXT_WEIGHT_DISPLAY, $numBoxes, $totalWeight);
  558. } else {
  559. $shiptitle = '';
  560. }
  561. $this->dest_zip = str_replace(' ', '', $order->delivery['postcode']);
  562. if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) { // domestic quote
  563. $this->dest_zip = substr($this->dest_zip, 0, 5);
  564. $dmstcquotes = array();
  565. if ($this->display_transit) $trnstime = $this->_getDmstcTransitTimes();
  566. $error = false;
  567. foreach ($boxesToShip as $package) {
  568. $pkgQuote = $this->_getDmstcQuote($package, $method);
  569. if (isset($pkgQuote['error'])) {
  570. $uspsQuote = $pkgQuote;
  571. $error = true;
  572. break; // stop if an error is returned
  573. }
  574. foreach ($pkgQuote as $quote) { // combine quotes for package with the quotes for other packages
  575. if (isset($dmstcquotes[$quote['id']])) { // already set for this mail type?
  576. $dmstcquotes[$quote['id']]['retailavail'] = ($dmstcquotes[$quote['id']]['retailavail'] && $quote['retailavail']);
  577. $dmstcquotes[$quote['id']]['onlineavail'] = ($dmstcquotes[$quote['id']]['onlineavail'] && $quote['onlineavail']);
  578. $dmstcquotes[$quote['id']]['retailrate'] += $quote['retailrate'];
  579. $dmstcquotes[$quote['id']]['onlinerate'] += $quote['onlinerate'];
  580. $dmstcquotes[$quote['id']]['retailinsval'] += $quote['retailinsval'];
  581. $dmstcquotes[$quote['id']]['onlineinsval'] += $quote['onlineinsval'];
  582. $dmstcquotes[$quote['id']]['count']++;
  583. } else { // create combined quote since it didn't exist
  584. $dmstcquotes[$quote['id']] = $quote;
  585. $dmstcquotes[$quote['id']]['count'] = 1;
  586. }
  587. }
  588. }
  589. if (!$error) {
  590. $methods = array();
  591. $transtypes = array();
  592. foreach ($dmstcquotes as $quote) {
  593. if ($quote['count'] != $numBoxes) continue; // skip methods that don't work for all packages
  594. if ((MODULE_SHIPPING_USPS_DMSTC_RATE == 'Internet') && $quote['onlineavail']) {
  595. $title = $this->types[$quote['id']];
  596. if ($this->display_insurance && ($quote['onlineinsval'] > 0)) $title .= '<br>---' . MODULE_SHIPPING_USPS_TEXT_INSURED . '$' . (ceil($quote['onlineinsval'] * 100) / 100);
  597. if ($this->display_confirmation && tep_not_null($quote['onlineconf'])) $title .= '<br>---' . $quote['onlineconf'];
  598. if ($this->display_transit && ($trnstime !== false)) {
  599. $time = '';
  600. if ((strpos($quote['id'], 'First') !== false) || (strpos($quote['id'], 'Priority') !== false)) {
  601. $time = $trnstime['priority'];
  602. } elseif (strpos($quote['id'], 'Express') !== false) {
  603. $time = $trnstime['express'];
  604. } else {
  605. $time = $trnstime['parcel'];
  606. }
  607. if ($time != '') $title .= '<br>---' . $time;
  608. }
  609. if (MODULE_SHIPPING_USPS_HANDLING_TYPE == 'Per Shipment') {
  610. $cost = $quote['onlinerate'] + $this->dmstc_handling[$quote['id']];
  611. } else {
  612. $cost = $quote['onlinerate'] + ($this->dmstc_handling[$quote['id']] * $numBoxes);
  613. }
  614. $methods[] = array('id' => $quote['id'],
  615. 'title' => $title,
  616. 'cost' => $cost);
  617. } elseif ($quote['retailavail']) {
  618. $title = $this->types[$quote['id']];
  619. if ($this->display_insurance && ($quote['retailinsval'] > 0)) $title .= '<br>---' . MODULE_SHIPPING_USPS_TEXT_INSURED . '$' . (ceil($quote['retailinsval'] * 100) / 100);
  620. if ($this->display_confirmation && tep_not_null($quote['retailconf'])) $title .= '<br>---' . $quote['retailconf'];
  621. if ($this->display_transit && ($trnstime !== false)) {
  622. $time = '';
  623. if ((strpos($quote['id'], 'First') !== false) || (strpos($quote['id'], 'Priority') !== false)) {
  624. $time = $trnstime['priority'];
  625. } elseif (strpos($quote['id'], 'Express') !== false) {
  626. $time = $trnstime['express'];
  627. } else {
  628. $time = $trnstime['parcel'];
  629. }
  630. if ($time != '') $title .= '<br>---' . $time;
  631. }
  632. if (MODULE_SHIPPING_USPS_HANDLING_TYPE == 'Per Shipment') {
  633. $cost = $quote['retailrate'] + $this->dmstc_handling[$quote['id']];
  634. } else {
  635. $cost = $quote['retailrate'] + ($this->dmstc_handling[$quote['id']] * $numBoxes);
  636. }
  637. $methods[] = array('id' => $quote['id'],
  638. 'title' => $title,
  639. 'cost' => $cost);
  640. }
  641. } // end foreach $dmstcquotes
  642. if (empty($methods)) { // no quotes valid for all packages
  643. $uspsQuote = false;
  644. } else {
  645. $uspsQuote = $methods;
  646. }
  647. }
  648. } else { // international quote
  649. $maxinsurance_query = tep_db_query("select distinct(max_insurance) from USPS_intl_maxins where insurable and country_code = '" . tep_db_input($order->delivery['country']['iso_code_2']) . "' order by max_insurance");
  650. $this->intl_maxinsure = array();
  651. while ($x = tep_db_fetch_array($maxinsurance_query)) {
  652. $this->intl_maxinsure[] = $x['max_insurance'];
  653. }
  654. $intlquotes = array();
  655. foreach ($boxesToShip as $package) {
  656. $pkgQuote = $this->_getIntlQuote($package, $method);
  657. if (isset($pkgQuote['error'])) {
  658. $uspsQuote = $pkgQuote;
  659. $error = true;
  660. break; // stop if an error is returned
  661. }
  662. foreach ($pkgQuote as $quote) { // combine quotes for package with the quotes for other packages
  663. if (isset($intlquotes[$quote['id']])) { // already set for this mail type?
  664. $intlquotes[$quote['id']]['retailavail'] = ($intlquotes[$quote['id']]['retailavail'] && $quote['retailavail']);
  665. $intlquotes[$quote['id']]['onlineavail'] = ($intlquotes[$quote['id']]['onlineavail'] && $quote['onlineavail']);
  666. $intlquotes[$quote['id']]['retailrate'] += $quote['retailrate'];
  667. $intlquotes[$quote['id']]['onlinerate'] += $quote['onlinerate'];
  668. $intlquotes[$quote['id']]['retailinsval'] += $quote['retailinsval'];
  669. $intlquotes[$quote['id']]['onlineinsval'] += $quote['onlineinsval'];
  670. $intlquotes[$quote['id']]['count']++;
  671. } else { // create combined quote since it didn't exist
  672. $intlquotes[$quote['id']] = $quote;
  673. $intlquotes[$quote['id']]['count'] = 1;
  674. }
  675. }
  676. }
  677. if (!$error) {
  678. $methods = array();
  679. $transtypes = array();
  680. foreach ($intlquotes as $quote) {
  681. if ($quote['count'] != $numBoxes) continue; // skip methods that don't work for all packages
  682. if ((MODULE_SHIPPING_USPS_INTL_RATE == 'Internet') && $quote['onlineavail']) {
  683. $title = $this->intl_types[$quote['id']];
  684. if ($this->display_insurance && ($quote['onlineinsval'] > 0)) $title .= '<br>---' . MODULE_SHIPPING_USPS_TEXT_INSURED . '$' . (ceil($quote['onlineinsval'] * 100) / 100);
  685. if ($this->display_transit && tep_not_null($quote['transtime'])) {
  686. $title .= '<br>---' . MODULE_SHIPPING_USPS_TEXT_ESTIMATED . $quote['transtime'];
  687. }
  688. if (MODULE_SHIPPING_USPS_HANDLING_TYPE == 'Per Shipment') {
  689. $cost = $quote['onlinerate'] + $this->intl_handling[$quote['id']];
  690. } else {
  691. $cost = $quote['onlinerate'] + ($this->intl_handling[$quote['id']] * $numBoxes);
  692. }
  693. $methods[] = array('id' => $quote['id'],
  694. 'title' => $title,
  695. 'cost' => $cost);
  696. } elseif ($quote['retailavail']) {
  697. $title = $this->intl_types[$quote['id']];
  698. if ($this->display_insurance && ($quote['retailinsval'] > 0)) $title .= '<br>---' . MODULE_SHIPPING_USPS_TEXT_INSURED . '$' . (ceil($quote['retailinsval'] * 100) / 100);
  699. if ($this->display_transit && tep_not_null($quote['transtime'])) {
  700. $title .= '<br>---' . MODULE_SHIPPING_USPS_TEXT_ESTIMATED . $quote['transtime'];
  701. }
  702. if (MODULE_SHIPPING_USPS_HANDLING_TYPE == 'Per Shipment') {
  703. $cost = $quote['retailrate'] + $this->intl_handling[$quote['id']];
  704. } else {
  705. $cost = $quote['retailrate'] + ($this->intl_handling[$quote['id']] * $numBoxes);
  706. }
  707. $methods[] = array('id' => $quote['id'],
  708. 'title' => $title,
  709. 'cost' => $cost);
  710. }
  711. } // end foreach $intlquotes
  712. if (empty($methods)) { // no quotes valid for all packages
  713. $uspsQuote = false;
  714. } else {
  715. $uspsQuote = $methods;
  716. }
  717. }
  718. }
  719. if (is_array($uspsQuote)) {
  720. if (isset($uspsQuote['error'])) {
  721. $this->quotes = array('module' => $this->title . $shiptitle,
  722. 'error' => $uspsQuote['error']);
  723. } else {
  724. // sort method array using class method.
  725. usort($uspsQuote, array("usps", "usps_method_sort"));
  726.  
  727. $quotesort = array();
  728. foreach ($uspsQuote as $method) {
  729. $quotesort[$method['id']] = $method['cost'];
  730. }
  731. // arsort($quotesort); // sort methods by cost high to low
  732. $methods = array();
  733. foreach ($quotesort as $key => $cost) {
  734. foreach ($uspsQuote as $method) {
  735. if ($method['id'] == $key) $methods[] = $method;
  736. }
  737. }
  738. $this->quotes = array('id' => $this->code,
  739. 'module' => $this->title . $shiptitle,
  740. 'methods' => $methods);
  741. if ($this->tax_class > 0) {
  742. $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
  743. }
  744. }
  745. } else { // quotes was empty
  746. $this->quotes = array('module' => $this->title . $shiptitle,
  747. 'error' => MODULE_SHIPPING_USPS_TEXT_ERROR);
  748. }
  749. if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
  750. return $this->quotes;
  751. }
  752. // Ends Quote
  753.  
  754. function usps_method_sort($a, $b) {
  755. // ascending sort on cost, then ascending sort on title
  756. if ($a['cost'] == $b['cost']) {
  757. if ($a['title'] == $b['title']) {
  758. return 0;
  759. } else {
  760. return (($a['title'] > $b['title']) ? 1 : -1);
  761. }
  762. } else {
  763. return (($a['cost'] > $b['cost']) ? 1 : -1);
  764. }
  765. // return 1, 0, or -1
  766. }
  767.  
  768. function check() {
  769. if (!isset($this->_check)) {
  770. $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_USPS_STATUS'");
  771. $this->_check = tep_db_num_rows($check_query);
  772. }
  773. return $this->_check;
  774. }
  775.  
  776. // Install Module
  777. function install() {
  778. tep_db_query("ALTER TABLE `configuration` CHANGE `configuration_value` `configuration_value` TEXT NOT NULL, CHANGE `set_function` `set_function` TEXT NULL DEFAULT NULL");
  779. tep_db_query("create table if not exists USPS_intl_maxins (country_code char(2) not null, method varchar(128) not null, insurable tinyint(1) not null default 0, max_insurance smallint unsigned not null default 0, last_modified datetime not null, primary key (country_code, method))");
  780. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable USPS Shipping', 'MODULE_SHIPPING_USPS_STATUS', 'True', 'Do you want to offer USPS shipping?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  781. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Enter the USPS User ID', 'MODULE_SHIPPING_USPS_USERID', 'NONE', 'Enter the USPS USERID assigned to you. <u>You must contact USPS to have them switch you to the Production server.</u> Otherwise this module will not work!', '6', '0', now())");
  782. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_USPS_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
  783. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_USPS_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
  784. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_USPS_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '0', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
  785. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Options', 'MODULE_SHIPPING_USPS_OPTIONS', 'Display Weight, Display Transit Time, Display Insurance, Display Sig./Del. Confirmation', 'Select display options', '6', '0', 'usps_cfg_select_multioption(array(\'Display Weight\', \'Display Transit Time\', \'Display Insurance\', \'Display Sig./Del. Confirmation\'), ', now())");
  786. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size Over 60 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER60', '24, 24, 12', 'Typical package dimensions in inches required by USPS for rates when package weight exceeds 60 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  787. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size 50 - 60 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER50', '22, 22, 16', 'Typical package dimensions in inches required by USPS for rates when package weight is between 50 and 60 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  788. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size 40 - 50 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER40', '18, 18, 16', 'Typical package dimensions in inches required by USPS for rates when package weight is between 40 and 50 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  789. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size 30 - 40 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER30', '16, 16, 16', 'Typical package dimensions in inches required by USPS for rates when package weight is between 30 and 40 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  790. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size 20 - 30 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER20', '24, 12, 6', 'Typical package dimensions in inches required by USPS for rates when package weight is between 20 and 30 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  791. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size 10 - 20 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER10', '16, 12, 10', 'Typical package dimensions in inches required by USPS for rates when package weight is between 10 and 20 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  792. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size 5 - 10 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER5', '12, 12, 5', 'Typical package dimensions in inches required by USPS for rates when package weight is between 5 and 10 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  793. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Package Size Up To 5 Pounds', 'MODULE_SHIPPING_USPS_PKG_SIZE_LESS5', '12, 9, 3', 'Typical package dimensions in inches required by USPS for rates when package weight is up to 5 pounds', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Height\'), ', now())");
  794. $value = array();
  795. $cfglist = array();
  796. $handling = array();
  797. foreach ($this->types as $key => $title) {
  798. $value[] = $key;
  799. $cfglist[] = "\'" . $key . "\'";
  800. $handling[] = '0';
  801. }
  802. // Values to select domestic shipping methods and handling are set from list created in function usps
  803. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Domestic Shipping Methods', 'MODULE_SHIPPING_USPS_DMSTC_TYPES', '" . implode(', ', $value) . "', 'Select the domestic services to be offered:', '6', '0', 'usps_cfg_select_multioption(array(" . implode(', ', $cfglist) . "), ', now())");
  804. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Domestic Flat Handling Fees', 'MODULE_SHIPPING_USPS_DMSTC_HANDLING', '" . implode(', ', $handling) . "', 'Add a different handling fee for each shipping type.', '6', '0', 'usps_cfg_multiinput_list(array(" . implode(', ', $cfglist) . "), ', now())");
  805. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Domestic Rates', 'MODULE_SHIPPING_USPS_DMSTC_RATE', 'Retail', 'Charge retail pricing or internet pricing?', '6', '0', 'tep_cfg_select_option(array(\'Retail\', \'Internet\'), ', now())");
  806. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Domestic Delivery Confirmation', 'MODULE_SHIPPING_USPS_DMST_DEL_CONF', 'True', 'Automatically charge Delivery Confirmation when available? Note: Signature Confirmation will override this if it is selected.', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  807. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Domestic Signature Confirmation', 'MODULE_SHIPPING_USPS_DMST_SIG_CONF', 'True', 'Automatically charge Signature Confirmation when available and order total exceeds threshold?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  808. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Signature Confirmation Threshold', 'MODULE_SHIPPING_USPS_SIG_THRESH', '100', 'Order total required before Signature Confirmation is triggered?', '6', '0', now())");
  809. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Domestic Insurance', 'MODULE_SHIPPING_USPS_DMSTC_INSURANCE_OPTION', 'True', 'Use USPS Calculated Domestic Insurance?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  810. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum Domestic Insurance Amount', 'MODULE_SHIPPING_USPS_DMSTC_INSURANCE_MAX', '5000', 'Enter the maximum package value that the USPS allows for domestic insurance.', '6', '0', now())");
  811. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum Online Domestic Insurance', 'MODULE_SHIPPING_USPS_DMSTC_INSURANCE_MAX_ONLINE', '5000', 'Enter the maximum package value that the USPS allows for domestic insurance when using internet shipping.', '6', '0', now())");
  812. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('First Class Letter Maximums', 'MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_LETTER', '11.5, 6.125, 0.25, 3.5', 'Enter the maximum dimensions in inches and weight in ounces for a standard First Class letter.', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Thickness\', \'Ounces\'), ', now())");
  813. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('First Class Large Letter Maximums', 'MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_LRGLTR', '15, 12, 0.75, 13', 'Enter the maximum dimensions in inches and weight in ounces for a standard First Class large letter.', '6', '0', 'usps_cfg_multiinput_list(array(\'Length\', \'Width\', \'Thickness\', \'Ounces\'), ', now())");
  814. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('First Class Parcel Maximum', 'MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_PARCEL', '13', 'Enter the maximum weight in ounces for a standard First Class parcel.', '6', '0', now())");
  815. $value = array();
  816. $cfglist = array();
  817. $handling = array();
  818. foreach ($this->intl_types as $key => $title) {
  819. $value[] = $key;
  820. $cfglist[] = "\'" . $key . "\'";
  821. $handling[] = '0';
  822. }
  823. // Values to select international shipping methods and handling are set from list created in function usps
  824. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('International Shipping Methods', 'MODULE_SHIPPING_USPS_INTL_TYPES', '" . implode(', ', $value) . "', 'Select the international services to be offered:', '6', '0', 'usps_cfg_select_multioption(array(" . implode(', ', $cfglist) . "), ', now())");
  825. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('International Flat Handling Fees', 'MODULE_SHIPPING_USPS_INTL_HANDLING', '" . implode(', ', $handling) . "', 'Add a different handling fee for each shipping type.', '6', '0', 'usps_cfg_multiinput_list(array(" . implode(', ', $cfglist) . "), ', now())");
  826. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('International Rates', 'MODULE_SHIPPING_USPS_INTL_RATE', 'Retail', 'Charge retail pricing or internet pricing? (Note: If set to internet and internet pricing is not available retail rate will be returned.)', '6', '0', 'tep_cfg_select_option(array(\'Retail\', \'Internet\'), ', now())");
  827. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('International Insurance', 'MODULE_SHIPPING_USPS_INTL_INSURANCE_OPTION', 'True', 'Use USPS calculated international insurance?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  828. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum Online Domestic Insurance', 'MODULE_SHIPPING_USPS_INTL_INSURANCE_MAX_ONLINE', '500', 'Enter the maximum package value that the USPS allows for international insurance when using internet shipping.', '6', '0', now())");
  829. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Non USPS Insurance - Domestic and international', 'MODULE_SHIPPING_USPS_NON_USPS_INSURE', 'False', 'Would you like to charge insurance for packages independent of USPS, i.e, merchant provided, Stamps.com, Endicia? If used in conjunction with USPS calculated insurance, the higher of the two will apply.', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  830. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, date_added) values ('Non USPS Insurance', 'MODULE_SHIPPING_USPS_INS1', '1.75', 'Totals $.01-$50.00', '6', '0', 'currencies->format', now())");
  831. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, date_added) values ('Non USPS Insurance', 'MODULE_SHIPPING_USPS_INS2', '2.25', 'Totals $50.01-$100', '6', '0', 'currencies->format', now())");
  832. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, date_added) values ('Non USPS Insurance', 'MODULE_SHIPPING_USPS_INS3', '2.75', 'Totals $100.01-$200', '6', '0', 'currencies->format', now())");
  833. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, date_added) values ('Non USPS Insurance', 'MODULE_SHIPPING_USPS_INS4', '4.70', 'Totals $200.01-$300', '6', '0', 'currencies->format', now())");
  834. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, date_added) values ('Non USPS Insurance', 'MODULE_SHIPPING_USPS_INS5', '1.00', 'For every $100 over $300 add:', '6', '0', 'currencies->format', now())");
  835. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Handling Fees Charged', 'MODULE_SHIPPING_USPS_HANDLING_TYPE', 'Per Package', 'Select whether domestic and international handling fees are charged:', '6', '0', 'tep_cfg_select_option(array(\'Per Package\', \'Per Shipment\'), ', now())");
  836. }
  837.  
  838. function keys() {
  839. return array('MODULE_SHIPPING_USPS_STATUS', 'MODULE_SHIPPING_USPS_USERID', 'MODULE_SHIPPING_USPS_SORT_ORDER', 'MODULE_SHIPPING_USPS_TAX_CLASS', 'MODULE_SHIPPING_USPS_ZONE', 'MODULE_SHIPPING_USPS_OPTIONS', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER60', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER50', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER40', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER30', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER20', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER10', 'MODULE_SHIPPING_USPS_PKG_SIZE_OVER5', 'MODULE_SHIPPING_USPS_PKG_SIZE_LESS5', 'MODULE_SHIPPING_USPS_DMSTC_TYPES', 'MODULE_SHIPPING_USPS_DMSTC_HANDLING', 'MODULE_SHIPPING_USPS_DMSTC_RATE', 'MODULE_SHIPPING_USPS_DMST_DEL_CONF', 'MODULE_SHIPPING_USPS_DMST_SIG_CONF', 'MODULE_SHIPPING_USPS_SIG_THRESH', 'MODULE_SHIPPING_USPS_DMSTC_INSURANCE_OPTION', 'MODULE_SHIPPING_USPS_DMSTC_INSURANCE_MAX', 'MODULE_SHIPPING_USPS_DMSTC_INSURANCE_MAX_ONLINE', 'MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_LETTER', 'MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_LRGLTR', 'MODULE_SHIPPING_USPS_DMSTC_FIRSTCLASS_PARCEL', 'MODULE_SHIPPING_USPS_INTL_TYPES', 'MODULE_SHIPPING_USPS_INTL_HANDLING', 'MODULE_SHIPPING_USPS_INTL_RATE', 'MODULE_SHIPPING_USPS_INTL_INSURANCE_OPTION', 'MODULE_SHIPPING_USPS_INTL_INSURANCE_MAX_ONLINE', 'MODULE_SHIPPING_USPS_NON_USPS_INSURE', 'MODULE_SHIPPING_USPS_INS1', 'MODULE_SHIPPING_USPS_INS2', 'MODULE_SHIPPING_USPS_INS3','MODULE_SHIPPING_USPS_INS4', 'MODULE_SHIPPING_USPS_INS5', 'MODULE_SHIPPING_USPS_HANDLING_TYPE');
  840. }
  841. // End Install Module
  842.  
  843. // Remove Module
  844. function remove() {
  845. tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
  846. }
  847. // End Remove Module
  848.  
  849. // START RATE REQUEST FUNCTIONS
  850.  
  851. // START USPS DOMESTIC REQUEST
  852. function _getDmstcQuote($pkg, $method = '') {
  853. global $order;
  854. if (tep_not_null($method) && in_array($method, $this->dmstc_available)) {
  855. $request_types = array($method);
  856. } else {
  857. $request_types = $this->dmstc_available;
  858. }
  859. $group_count = 1;
  860. $type_count = 0;
  861. foreach ($request_types as $service) { // break the requested shipping methods into groups
  862. $type_count++;
  863. if ($type_count > 25) { // only 25 services allowed per request
  864. $group_count++;
  865. $type_count = 1;
  866. }
  867. $request_group[$group_count][$type_count] = $service;
  868. }
  869. $shipping_weight = ($pkg['item_weight'] < 0.0625 ? 0.0625 : $pkg['item_weight']);
  870. $shipping_pounds = floor($shipping_weight);
  871. $shipping_ounces = ceil((16 * ($shipping_weight - $shipping_pounds)) * 100) / 100; // rounded to two decimal digits
  872. $ounces = $shipping_weight * 16;
  873. $pkgvalue = $pkg['item_price'];
  874. if ($pkgvalue > MODULE_SHIPPING_USPS_DMSTC_INSURANCE_MAX) $pkgvalue = MODULE_SHIPPING_USPS_DMSTC_INSURANCE_MAX;
  875. if ($pkgvalue <= 0) $pkgvalue = 0.01;
  876. $nonuspsinsurancecost = 0;
  877. if (MODULE_SHIPPING_USPS_NON_USPS_INSURE == 'True') {
  878. if ($pkg['item_price'] <= 50) {
  879. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS1;
  880. } else if ($pkg['item_price'] <= 100) {
  881. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS2;
  882. } else if ($pkg['item_price'] <= 200) {
  883. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS3;
  884. } else if ($pkg['item_price'] <= 300) {
  885. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS4;
  886. } else {
  887. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS4 + ((ceil($pkg['item_price']/100) - 3) * MODULE_SHIPPING_USPS_INS5);
  888. }
  889. }
  890. $firstclassmailtype = ''; // determine if package can fit one of the first class mail types
  891. if (($pkg['item_length'] <= $this->first_class_letter['length']) && ($pkg['item_width'] <= $this->first_class_letter['width']) && ($pkg['item_height'] <= $this->first_class_letter['height']) && ($ounces <= $this->first_class_letter['ounces'])) {
  892. $firstclassmailtype = 'LETTER';
  893. } elseif (($pkg['item_length'] <= $this->first_class_large_letter['length']) && ($pkg['item_width'] <= $this->first_class_large_letter['width']) && ($pkg['item_height'] <= $this->first_class_large_letter['height']) && ($ounces <= $this->first_class_large_letter['ounces'])) {
  894. $firstclassmailtype = 'FLAT';
  895. } elseif ($ounces <= $this->first_class_parcel_max_ounces) {
  896. $firstclassmailtype = 'PARCEL';
  897. }
  898. $sservs = array(); // any special services to request
  899. if (MODULE_SHIPPING_USPS_DMSTC_INSURANCE_OPTION == 'True') $sservs = array(1, 11); //request insurance, both regular & express mail
  900. if ((MODULE_SHIPPING_USPS_DMST_SIG_CONF == 'True') && ($order->info['subtotal'] >= MODULE_SHIPPING_USPS_SIG_THRESH)) {
  901. $sservs[] = 15;
  902. } elseif (MODULE_SHIPPING_USPS_DMST_DEL_CONF == 'True') {
  903. $sservs[] = 13;
  904. }
  905. $sservice = '';
  906. if (!empty($sservs)) {
  907. foreach ($sservs as $id) $sservice .= '<SpecialService>' . $id . '</SpecialService>';
  908. $sservice = '<SpecialServices>' . $sservice . '</SpecialServices>';
  909. }
  910. $id = 0;
  911. $quotes = array();
  912. foreach ($request_group as $group) {
  913. $request = '<RateV4Request USERID="' . MODULE_SHIPPING_USPS_USERID . '"><Revision>2</Revision>';
  914. foreach ($group as $service) {
  915. if (strpos($service, 'First') !== false) { // if first class mail service check if will fit type
  916. if ($firstclassmailtype == '') continue; // skip if not a valid first class package
  917. $fcmt = $firstclassmailtype;
  918. if ((strpos($service, 'Letter') !== false) && ($fcmt != 'LETTER')) continue; // don't check letter type if package doesn't fit letter
  919. if (strpos($service, 'Large') !== false) {
  920. if ($firstclassmailtype == 'PARCEL') continue; // don't check large letter type if package doesn't fit
  921. $fcmt = 'FLAT'; // package type is either Flat or Letter and we need to check Flat since letter will fit in large envelope
  922. }
  923. if (strpos($service, 'Package') !== false) $fcmt = 'PARCEL'; // parcel can contain letter or large envelope so we need to check First Class Package for any determined First Class Mail Type
  924. }
  925. $cont = $this->type_to_container[$service]; // begin checking for packages larger than USPS containers
  926. // if this service type specifies a container and this package won't fit then skip this service
  927. if (strpos($cont, 'Envelope') !== false) { // if service container is envelope
  928. if ($pkg['item_height'] > 1) continue; // anything thicker than one inch won't fit an envelope
  929. if ((strpos($cont, 'SM') !== false) && (($pkg['item_length'] > 10) || ($pkg['item_width'] > 6))) continue;
  930. if ((strpos($cont, 'Window') !== false) && (($pkg['item_length'] > 10) || ($pkg['item_width'] > 5))) continue;
  931. if ((strpos($cont, 'Gift') !== false) && (($pkg['item_length'] > 10) || ($pkg['item_width'] > 7))) continue;
  932. if ((strpos($cont, 'Legal') !== false) && (($pkg['item_length'] > 15) || ($pkg['item_width'] > 9.5))) continue;
  933. if (($pkg['item_length'] > 12.5) || ($pkg['item_width'] > 9.5)) continue; // other envelopes
  934. }
  935. if (($cont == 'SM Flat Rate Box') && (($pkg['item_length'] > 8.625) || ($pkg['item_width'] > 5.375) || ($pkg['item_height'] > 1.625))) continue;
  936. if (($cont == 'MD Flat Rate Box') && (($pkg['item_length'] > 11) || ($pkg['item_width'] > 8.5) || ($pkg['item_height'] > 5.5))) continue;
  937. if (($cont == 'LG Flat Rate Box') && (($pkg['item_length'] > 12) || ($pkg['item_width'] > 12) || ($pkg['item_height'] > 5.5))) continue;
  938. if (($cont == 'Regional Rate Box A') && (($pkg['item_length'] > 10) || ($pkg['item_width'] > 7) || ($pkg['item_height'] > 4.75))) continue;
  939. if (($cont == 'Regional Rate Box B') && (($pkg['item_length'] > 12) || ($pkg['item_width'] > 10.25) || ($pkg['item_height'] > 5))) continue;
  940. $request .= '<Package ID="'. $id . '"><Service>' . $this->type_to_request[$service] . '</Service>';
  941. if (strpos($service, 'First') !== false) $request .= '<FirstClassMailType>' . $fcmt . '</FirstClassMailType>';
  942. $request .= '<ZipOrigination>' . SHIPPING_ORIGIN_ZIP . '</ZipOrigination>' .
  943. '<ZipDestination>' . $this->dest_zip . '</ZipDestination>' .
  944. '<Pounds>' . $shipping_pounds . '</Pounds>' .
  945. '<Ounces>' . $shipping_ounces . '</Ounces>';
  946. if (max($pkg['item_length'], $pkg['item_width'], $pkg['item_height']) > 12) {
  947. $request .= '<Container>RECTANGULAR</Container><Size>LARGE</Size>' .
  948. '<Width>' . $pkg['item_width'] . '</Width>' .
  949. '<Length>' . $pkg['item_length'] . '</Length>'.
  950. '<Height>' . $pkg['item_height'] . '</Height>';
  951. } else {
  952. $request .= '<Container>' . $cont . '</Container><Size>REGULAR</Size>';
  953. }
  954. if (MODULE_SHIPPING_USPS_DMSTC_INSURANCE_OPTION == 'True') $request .= '<Value>' . number_format($pkgvalue, 2, '.', '') . '</Value>';
  955. $request .= $sservice;
  956. if ((strpos($service, 'First') !== false) || (strpos($service, 'Parcel') !== false)) $request .= '<Machinable>true</Machinable>';
  957. $request .= '</Package>';
  958. $id++;
  959. } // end foreach $group
  960. $request .= '</RateV4Request>';
  961. $request = 'API=RateV4&XML=' . urlencode($request);
  962. $usps_server = 'production.shippingapis.com';
  963. $api_dll = 'shippingAPI.dll';
  964. $body = '';
  965. if (!class_exists('httpClient')) {
  966. include('rc2a/catalog/includes/classes/http_client.php');
  967. }
  968. $http = new httpClient();
  969. if ($http->Connect($usps_server, 80)) {
  970. $http->addHeader('Host', $usps_server);
  971. $http->addHeader('User-Agent', 'osCommerce');
  972. $http->addHeader('Connection', 'Close');
  973. if ($http->Get('/' . $api_dll . '?' . $request)) $body = $http->getBody();
  974. $http->Disconnect();
  975. } else {
  976. $body = '<Error><Number></Number><Description>' . MODULE_SHIPPING_USPS_TEXT_CONNECTION_ERROR . '</Description></Error>';
  977. }
  978. $doc = XML_unserialize($body);
  979. if (isset($doc['Error'])) return array('error' => $doc['Error']['Number'] . ' ' . $doc['Error']['Description']);
  980. if (isset($doc['RateV4Response']['Package']['Postage'])) { // single mail service response
  981. $tmp = $this->_decode_domestic_response($doc['RateV4Response']['Package']['Postage'], $pkgvalue, $nonuspsinsurancecost, $pkg['item_price']);
  982. if (!empty($tmp)) $quotes[$tmp['id']] = $tmp;
  983. } elseif (isset($doc['RateV4Response']['Package'][0])) { // multiple mailing services returned
  984. foreach ($doc['RateV4Response']['Package'] as $mailsvc) {
  985. if (isset($mailsvc['Postage'])) {
  986. $tmp = $this->_decode_domestic_response($mailsvc['Postage'], $pkgvalue, $nonuspsinsurancecost, $pkg['item_price']);
  987. if (!empty($tmp)) $quotes[$tmp['id']] = $tmp;
  988. }
  989. }
  990. }
  991. } // end foreach $request_group
  992. return $quotes;
  993. }
  994. // END DOMESTIC REQUEST
  995.  
  996. // START USPS DECODE DOMESTIC RESPONSE
  997. function _decode_domestic_response($service, $pkgvalue, $nonuspsinsurancecost, $opval) {
  998. if (!isset($service['MailService'])) return array();
  999. $qid = str_replace(array('<sup>', '</sup>', '&reg;', '&trade;'), '', htmlspecialchars_decode($service['MailService']));
  1000. $retailrate = $service['Rate'];
  1001. $onlinerate = $service['CommercialRate'];
  1002. $retailavailable = ($retailrate > 0);
  1003. $onlineavailable = ($onlinerate > 0);
  1004. $insset = $confset = false;
  1005. $retailinsfor = $onlineinsfor = 0;
  1006. $retailcname = $onlinecname = '';
  1007. $confname = '';
  1008. if (is_array($service['SpecialServices']['SpecialService'])) {
  1009. foreach ($service['SpecialServices']['SpecialService'] as $special) {
  1010. if (strpos($special['ServiceName'], 'Insurance') !== false) {
  1011. $insavailable = ($special['Available'] == 'true');
  1012. $insavailableonline = ($special['AvailableOnline'] == 'true');
  1013. $insrateretail = $special['Price'];
  1014. $insrateonline = $special['PriceOnline'];
  1015. $insset = true;
  1016. } elseif (strpos($special['ServiceName'], 'Confirmation') !== false) {
  1017. $confname = htmlspecialchars_decode($special['ServiceName']);
  1018. $confavailable = ($special['Available'] == 'true');
  1019. $confavailableonline = ($special['AvailableOnline'] == 'true');
  1020. $confrateretail = $special['Price'];
  1021. $confrateonline = $special['PriceOnline'];
  1022. $confset = true;
  1023. }
  1024. }
  1025. }
  1026. if (MODULE_SHIPPING_USPS_DMSTC_INSURANCE_OPTION == 'True') $onlineavailable = ($onlineavailable && ($pkgvalue <= MODULE_SHIPPING_USPS_DMSTC_INSURANCE_MAX_ONLINE)); // can't use online if package value exceeds maximum available online
  1027. if ((MODULE_SHIPPING_USPS_DMSTC_INSURANCE_OPTION == 'True') && (MODULE_SHIPPING_USPS_NON_USPS_INSURE == 'True') && $insset) {
  1028. if ($insavailable && $retailavailable) { // retail rate & retail insurance available
  1029. if ($insrateretail > $nonuspsinsurancecost) {
  1030. $retailrate += $insrateretail;
  1031. $retailinsfor = $pkgvalue;
  1032. } else {
  1033. $retailrate += $nonuspsinsurancecost;
  1034. $retailinsfor = $opval;
  1035. }
  1036. } elseif ($retailavailable) { // only retail rate available
  1037. $retailrate += $nonuspsinsurancecost;
  1038. $retailinsfor = $opval;
  1039. }
  1040. if ($insavailableonline && $onlineavailable) { // online rate & online insurance available
  1041. if ($insrateonline > $nonuspsinsurancecost) {
  1042. $onlinerate += $insrateonline;
  1043. $onlineinsfor = $pkgvalue;
  1044. } else {
  1045. $onlinerate += $nonuspsinsurancecost;
  1046. $onlineinsfor = $opval;
  1047. }
  1048. } elseif ($onlineavailable) { // only online rate available
  1049. $onlinerate += $nonuspsinsurancecost;
  1050. $onlineinsfor = $opval;
  1051. }
  1052. } elseif (MODULE_SHIPPING_USPS_NON_USPS_INSURE == 'True') {
  1053. if ($retailavailable) { // retail rate available
  1054. $retailrate += $nonuspsinsurancecost;
  1055. $retailinsfor = $opval;
  1056. }
  1057. if ($onlineavailable) { // online rate available
  1058. $onlinerate += $nonuspsinsurancecost;
  1059. $onlineinsfor = $opval;
  1060. }
  1061. } elseif ((MODULE_SHIPPING_USPS_DMSTC_INSURANCE_OPTION == 'True') && $insset) {
  1062. if ($insavailable && $retailavailable) { // retail rate & retail insurance available
  1063. $retailrate += $insrateretail;
  1064. $retailinsfor = $pkgvalue;
  1065. }
  1066. if ($insavailableonline && $onlineavailable) { // online rate & online insurance available
  1067. $onlinerate += $insrateonline;
  1068. $onlineinsfor = $pkgvalue;
  1069. }
  1070. }
  1071. if (((MODULE_SHIPPING_USPS_DMST_SIG_CONF == 'True') || (MODULE_SHIPPING_USPS_DMST_DEL_CONF == 'True')) && $confset) { // using confirmation and it was set
  1072. if ($confavailable && $retailavailable) {
  1073. $retailcname .= $confname;
  1074. $retailrate += $confrateretail;
  1075. }
  1076. if ($confavailableonline && $onlineavailable) {
  1077. $onlinecname .= $confname;
  1078. $onlinerate += $confrateonline;
  1079. }
  1080. }
  1081. return array('id' => $qid,
  1082. 'retailavail' => $retailavailable,
  1083. 'onlineavail' => $onlineavailable,
  1084. 'retailrate' => $retailrate,
  1085. 'onlinerate' => $onlinerate,
  1086. 'retailconf' => $retailcname,
  1087. 'onlineconf' => $onlinecname,
  1088. 'retailinsval' => $retailinsfor,
  1089. 'onlineinsval' => $onlineinsfor);
  1090. }
  1091. // END DOMESTIC RESPONSE
  1092.  
  1093. // START GET DOMESTIC TRANSIT TIME
  1094. function _getDmstcTransitTimes() {
  1095. $transitreq = 'USERID="' . MODULE_SHIPPING_USPS_USERID . '">' .
  1096. '<OriginZip>' . SHIPPING_ORIGIN_ZIP . '</OriginZip>' .
  1097. '<DestinationZip>' . $this->dest_zip . '</DestinationZip>';
  1098. $transitpriorityreq = 'API=PriorityMail&XML=' . urlencode( '<PriorityMailRequest ' . $transitreq . '</PriorityMailRequest>');
  1099. $transitexpressreq = 'API=ExpressMailCommitment&XML=' . urlencode( '<ExpressMailCommitmentRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '"><OriginZIP>' . SHIPPING_ORIGIN_ZIP . '</OriginZIP><DestinationZIP>' . $this->dest_zip . '</DestinationZIP><Date></Date></ExpressMailCommitmentRequest>');
  1100. $transitparcelreq = 'API=StandardB&XML=' . urlencode( '<StandardBRequest ' . $transitreq . '</StandardBRequest>');
  1101. $usps_server = 'production.shippingapis.com';
  1102. $api_dll = 'shippingAPI.dll';
  1103. $body = '';
  1104. if (!class_exists('httpClient')) {
  1105. include('includes/classes/http_client.php');
  1106. }
  1107. $http = new httpClient();
  1108. if ($http->Connect($usps_server, 80)) {
  1109. $http->addHeader('Host', $usps_server);
  1110. $http->addHeader('User-Agent', 'osCommerce');
  1111. $http->addHeader('Connection', 'Close');
  1112. if ($http->Get('/' . $api_dll . '?' . $transitpriorityreq)) $transpriorityresp = $http->getBody();
  1113. if ($http->Get('/' . $api_dll . '?' . $transitexpressreq)) $transexpressresp = $http->getBody();
  1114. if ($http->Get('/' . $api_dll . '?' . $transitparcelreq)) $transparcelresp = $http->getBody();
  1115. // mail('user@localhost.com','USPS Transit Response',$transresp[$key],'From: <user@localhost.com>');
  1116. $http->Disconnect();
  1117. } else {
  1118. return false;
  1119. }
  1120. $prioritytime = '';
  1121. $expresstime = '';
  1122. $parceltime = '';
  1123. $doc = XML_unserialize($transpriorityresp);
  1124. if (isset($doc['PriorityMailResponse']['Days'])) {
  1125. $prioritytime = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . $doc['PriorityMailResponse']['Days'] . ' ' . ($doc['PriorityMailResponse']['Days'] == 1 ? MODULE_SHIPPING_USPS_TEXT_DAY : MODULE_SHIPPING_USPS_TEXT_DAYS);
  1126. }
  1127. $doc = XML_unserialize($transparcelresp);
  1128. if (isset($doc['StandardBResponse']['Days'])) {
  1129. $parceltime = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . $doc['StandardBResponse']['Days'] . ' ' . ($doc['StandardBResponse']['Days'] == 1 ? MODULE_SHIPPING_USPS_TEXT_DAY : MODULE_SHIPPING_USPS_TEXT_DAYS);
  1130. }
  1131. $doc = XML_unserialize($transexpressresp);
  1132. if (isset($doc['ExpressMailCommitmentResponse']['Commitment'])) {
  1133. if (isset($doc['ExpressMailCommitmentResponse']['Commitment']['CommitmentName'])) { // single date
  1134. $sequence = str_replace(array('Next' , 'Days', 'Day'), array('1', MODULE_SHIPPING_USPS_TEXT_DAYS, MODULE_SHIPPING_USPS_TEXT_DAY), $doc['ExpressMailCommitmentResponse']['Commitment']['CommitmentName']);
  1135. } else { // multiple dates returned, choose longest time
  1136. $seqlist = array();
  1137. foreach ($doc['ExpressMailCommitmentResponse']['Commitment'] as $commit) {
  1138. if (isset($commit['CommitmentName'])) $seqlist[] = str_replace(array('Next' , 'Days', 'Day'), array('1', MODULE_SHIPPING_USPS_TEXT_DAYS, MODULE_SHIPPING_USPS_TEXT_DAY), $commit['CommitmentName']);
  1139. }
  1140. rsort($seqlist);
  1141. $sequence = $seqlist[0];
  1142. }
  1143. $expresstime = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . $sequence;
  1144. }
  1145. return array('express' => $expresstime, 'priority' => $prioritytime, 'parcel' => $parceltime);
  1146. }
  1147. // END GET TRANSIT TIME
  1148.  
  1149. // START USPS INTERNATIONAL REQUEST
  1150. function _getIntlQuote($pkg, $method) {
  1151. global $order;
  1152. if (tep_not_null($method) && in_array($method, $this->intl_available)) {
  1153. $request_types = array($method);
  1154. } else {
  1155. $request_types = $this->intl_available;
  1156. }
  1157. $this->intl_request_types = $request_types;
  1158. $shipping_weight = ($pkg['item_weight'] < 0.0625 ? 0.0625 : $pkg['item_weight']);
  1159. $shipping_pounds = floor($shipping_weight);
  1160. $shipping_ounces = ceil((16 * ($shipping_weight - $shipping_pounds)) * 100) / 100; // rounded to two decimal digits
  1161. $nonuspsinsurancecost = 0;
  1162. if (MODULE_SHIPPING_USPS_NON_USPS_INSURE == 'True') {
  1163. if ($pkg['item_price'] <= 50) {
  1164. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS1;
  1165. } else if ($pkg['item_price'] <= 100) {
  1166. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS2;
  1167. } else if ($pkg['item_price'] <= 200) {
  1168. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS3;
  1169. } else if ($pkg['item_price'] <= 300) {
  1170. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS4;
  1171. } else {
  1172. $nonuspsinsurancecost = MODULE_SHIPPING_USPS_INS4 + ((ceil($pkg['item_price']/100) - 3) * MODULE_SHIPPING_USPS_INS5);
  1173. }
  1174. }
  1175. if (empty($this->intl_maxinsure) || (MODULE_SHIPPING_USPS_INTL_INSURANCE_OPTION == 'False')) {
  1176. $checkvalues = array($pkg['item_price']);
  1177. } else {
  1178. $checkvalues = array();
  1179. foreach ($this->intl_maxinsure as $maxins) {
  1180. if ($pkg['item_price'] <= $maxins) {
  1181. $checkvalues[] = $pkg['item_price'];
  1182. break; // stop once we find package value is less than allowed maximum
  1183. } else {
  1184. $checkvalues[] = $maxins;
  1185. }
  1186. }
  1187. }
  1188. rsort($checkvalues); // values must be checked in reverse order
  1189. $request = '<IntlRateV2Request USERID="' . MODULE_SHIPPING_USPS_USERID . '">' .
  1190. '<Revision>2</Revision>';
  1191. foreach ($checkvalues as $pkgvalue) {
  1192. $request .= '<Package ID="' . intval($pkgvalue) . '">' .
  1193. '<Pounds>' . $shipping_pounds . '</Pounds>' .
  1194. '<Ounces>' . $shipping_ounces . '</Ounces>' .
  1195. '<Machinable>True</Machinable>' .
  1196. '<MailType>All</MailType>' .
  1197. '<GXG>' .
  1198. '<POBoxFlag>N</POBoxFlag>' .
  1199. '<GiftFlag>N</GiftFlag>' .
  1200. '</GXG>' .
  1201. '<ValueOfContents>' . number_format($pkgvalue, 2, '.', '') . '</ValueOfContents>' .
  1202. '<Country>' . $this->countries[$order->delivery['country']['iso_code_2']] . '</Country>' .
  1203. '<Container>RECTANGULAR</Container>' .
  1204. '<Size>' . ((max($pkg['item_length'], $pkg['item_width'], $pkg['item_height']) > 12) ? ' LARGE' : 'REGULAR') . '</Size>' .
  1205. '<Width>' . $pkg['item_width'] . '</Width>' .
  1206. '<Length>' . $pkg['item_length'] . '</Length>' .
  1207. '<Height>' . $pkg['item_height'] . '</Height>' .
  1208. '<Girth>' . ($pkg['item_height'] + $pkg['item_height'] + $pkg['item_width'] + $pkg['item_width']) . '</Girth>' .
  1209. '<OriginZip>' . SHIPPING_ORIGIN_ZIP . '</OriginZip>' .
  1210. '<CommercialFlag>Y</CommercialFlag>' .
  1211. '<ExtraServices>' .
  1212. '<ExtraService>1</ExtraService>' .
  1213. '</ExtraServices>' .
  1214. '</Package>';
  1215. }
  1216. $request .= '</IntlRateV2Request>';
  1217. $request = 'API=IntlRateV2&XML=' . urlencode($request);
  1218. $usps_server = 'production.shippingapis.com';
  1219. $api_dll = 'shippingAPI.dll';
  1220. $body = '';
  1221. if (!class_exists('httpClient')) {
  1222. include('rc2a/catalog/includes/classes/http_client.php');
  1223. }
  1224. $http = new httpClient();
  1225. if ($http->Connect($usps_server, 80)) {
  1226. $http->addHeader('Host', $usps_server);
  1227. $http->addHeader('User-Agent', 'osCommerce');
  1228. $http->addHeader('Connection', 'Close');
  1229. if ($http->Get('/' . $api_dll . '?' . $request)) $body = $http->getBody();
  1230. $http->Disconnect();
  1231. } else {
  1232. $body = '<Error><Number></Number><Description>' . MODULE_SHIPPING_USPS_TEXT_CONNECTION_ERROR . '</Description></Error>';
  1233. }
  1234. $doc = XML_unserialize($body);
  1235. $quotes = array();
  1236. if (isset($doc['Error'])) return array('error' => $doc['Error']['Number'] . ' ' . $doc['Error']['Description']);
  1237. if (isset($doc['IntlRateV2Response']['Package']['Error'])) return array('error' => $doc['IntlRateV2Response']['Package']['Error']['Number'] . ' ' . $doc['IntlRateV2Response']['Package']['Error']['Description']);
  1238. if (isset($doc['IntlRateV2Response']['Package'][0]['Error'])) return array('error' => $doc['IntlRateV2Response']['Package']['Error'][0]['Number'] . ' ' . $doc['IntlRateV2Response']['Package'][0]['Error']['Description']);
  1239. if (isset($doc['IntlRateV2Response']['Package']['Service']['SvcDescription'])) { // single mail service response
  1240. $tmp = $this->_decode_intl_response($doc['RateV4Response']['Package']['Service'], $nonuspsinsurancecost, $pkg['item_price']);
  1241. if (!empty($tmp)) $quotes[$tmp['id']] = $tmp;
  1242. } elseif (isset($doc['IntlRateV2Response']['Package']['Service'][0])) { // multiple mailing services returned
  1243. foreach ($doc['IntlRateV2Response']['Package']['Service'] as $mailsvc) {
  1244. if (isset($mailsvc['SvcDescription'])) {
  1245. $tmp = $this->_decode_intl_response($mailsvc, $nonuspsinsurancecost, $pkg['item_price']);
  1246. if (!empty($tmp)) $quotes[$tmp['id']] = $tmp;
  1247. }
  1248. }
  1249. } elseif (isset($doc['IntlRateV2Response']['Package'][0]['Service'])) { // multiple packages requested for insurance purposes
  1250. foreach ($doc['IntlRateV2Response']['Package'] as $package) {
  1251. if (isset($package['Service']['SvcDescription'])) { // single mail service response for package
  1252. $tmp = $this->_decode_intl_response($package['Service'], $nonuspsinsurancecost, $pkg['item_price']);
  1253. if (!empty($tmp)) if (!isset($quotes[$tmp['id']])) $quotes[$tmp['id']] = $tmp; // save only first valid response
  1254. } elseif (isset($package['Service'][0])) { // multiple mailing services returned for package
  1255. foreach ($package['Service'] as $mailsvc) {
  1256. if (isset($mailsvc['SvcDescription'])) {
  1257. $tmp = $this->_decode_intl_response($mailsvc, $nonuspsinsurancecost, $pkg['item_price']);
  1258. if (!empty($tmp)) if (!isset($quotes[$tmp['id']])) $quotes[$tmp['id']] = $tmp; // save only first valid response
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. return $quotes;
  1265. }
  1266. // END INTERNATIONAL REQUEST
  1267.  
  1268. // START DECODE INTERNATIONAL RESPONSE
  1269. function _decode_intl_response($service, $nonuspsinsurancecost, $opval) {
  1270. if (!isset($service['SvcDescription'])) return array();
  1271. $insured_for = 0;
  1272. $insurance_cost = 0;
  1273. if (MODULE_SHIPPING_USPS_INTL_INSURANCE_OPTION == 'True') {
  1274. if (isset($service['InsComment'])) {
  1275. if ($service['InsComment'] == 'INSURED VALUE') return array(); // skip if invalid insured value when doing USPS insurance
  1276. } else {
  1277. if (isset($service['ExtraServices']['ExtraService'])) {
  1278. if (isset($service['ExtraServices']['ExtraService'][0])) { // multiple extras returned
  1279. foreach ($service['ExtraServices']['ExtraService'] as $extra) {
  1280. if (($extra['ServiceName'] == 'Insurance') && ($extra['Available'] == 'True')) {
  1281. $insurance_cost = $extra['Price'];
  1282. $insured_for = $service['ValueOfContents'];
  1283. }
  1284. }
  1285. } elseif (isset($service['ExtraServices']['ExtraService']['ServiceName'])) { //single extra returned
  1286. if (($service['ExtraServices']['ExtraService']['ServiceName'] == 'Insurance') && ($service['ExtraServices']['ExtraService']['Available'] == 'True')) {
  1287. $insurance_cost = $service['ExtraServices']['ExtraService']['Price'];
  1288. $insured_for = $service['ValueOfContents'];
  1289. }
  1290. }
  1291. }
  1292. }
  1293. }
  1294. $qid = str_replace(array('<sup>', '</sup>', '&reg;', '&trade;'), '', htmlspecialchars_decode($service['SvcDescription']));
  1295. if (!in_array($qid, $this->intl_request_types)) return array(); // not an allowed international method
  1296. $retailrate = $service['Postage'];
  1297. $onlinerate = $service['CommercialPostage'];
  1298. $retailavailable = ($retailrate > 0);
  1299. $onlineavailable = ($onlinerate > 0);
  1300. if (MODULE_SHIPPING_USPS_INTL_INSURANCE_OPTION == 'True') $onlineavailable = $onlineavailable && ($insured_for <= MODULE_SHIPPING_USPS_INTL_INSURANCE_MAX_ONLINE); // can't use online for USPS international insurance over maximum
  1301. if ((MODULE_SHIPPING_USPS_INTL_INSURANCE_OPTION == 'True') && (MODULE_SHIPPING_USPS_NON_USPS_INSURE == 'True') && ($insurance_cost > 0)) {
  1302. if (($insurance_cost > 0) && $retailavailable) { // retail rate & insurance available
  1303. if ($insurance_cost > $nonuspsinsurancecost) {
  1304. $retailrate += $insurance_cost;
  1305. $retailinsfor = $insured_for;
  1306. } else {
  1307. $retailrate += $nonuspsinsurancecost;
  1308. $retailinsfor = $opval;
  1309. }
  1310. } elseif ($retailavailable) { // only retail rate available
  1311. $retailrate += $nonuspsinsurancecost;
  1312. $retailinsfor = $opval;
  1313. }
  1314. if (($insurance_cost > 0) && $onlineavailable) { // online rate & online insurance available
  1315. if ($insurance_cost > $nonuspsinsurancecost) {
  1316. $onlinerate += $insurance_cost;
  1317. $onlineinsfor = $insured_for;
  1318. } else {
  1319. $onlinerate += $nonuspsinsurancecost;
  1320. $onlineinsfor = $opval;
  1321. }
  1322. } elseif ($onlineavailable) { // only online rate available
  1323. $onlinerate += $nonuspsinsurancecost;
  1324. $onlineinsfor = $opval;
  1325. }
  1326. } elseif (MODULE_SHIPPING_USPS_NON_USPS_INSURE == 'True') {
  1327. if ($retailavailable) { // retail rate available
  1328. $retailrate += $nonuspsinsurancecost;
  1329. $retailinsfor = $opval;
  1330. }
  1331. if ($onlineavailable) { // online rate available
  1332. $onlinerate += $nonuspsinsurancecost;
  1333. $onlineinsfor = $opval;
  1334. }
  1335. } elseif ((MODULE_SHIPPING_USPS_INTL_INSURANCE_OPTION == 'True') && ($insurance_cost > 0)) {
  1336. if (($insurance_cost > 0) && $retailavailable) { // retail rate & retail insurance available
  1337. $retailrate += $insurance_cost;
  1338. $retailinsfor = $insured_for;
  1339. }
  1340. if (($insurance_cost > 0) && $onlineavailable) { // online rate & online insurance available
  1341. $onlinerate += $insurance_cost;
  1342. $onlineinsfor = $insured_for;
  1343. }
  1344. }
  1345. $transtime = str_replace(array('weeks', 'days', 'day', 'business', 'Varies by country'), array(MODULE_SHIPPING_USPS_TEXT_WEEKS, MODULE_SHIPPING_USPS_TEXT_DAYS, MODULE_SHIPPING_USPS_TEXT_DAY, MODULE_SHIPPING_USPS_TEXT_BUSINESS, MODULE_SHIPPING_USPS_TEXT_VARIES), $service['SvcCommitments']);
  1346. return array('id' => $qid,
  1347. 'retailavail' => $retailavailable,
  1348. 'onlineavail' => $onlineavailable,
  1349. 'retailrate' => $retailrate,
  1350. 'onlinerate' => $onlinerate,
  1351. 'retailinsval' => $retailinsfor,
  1352. 'onlineinsval' => $onlineinsfor,
  1353. 'transtime' => $transtime);
  1354. }
  1355. // END INTERNATIONAL RESPONSE
  1356.  
  1357. }
  1358. // ENDS USPS CLASS
  1359.  
  1360. // Required configuration functions
  1361. // USPS Methods. Added by Greg Deeth
  1362.  
  1363. // Alias function for Store configuration values in the Administration Tool
  1364. function usps_cfg_select_multioption($select_array, $key_value, $key = '') {
  1365. for ($i=0; $i<sizeof($select_array); $i++) {
  1366. $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
  1367. $string .= '<br><input type="checkbox" name="' . $name . '" value="' . $select_array[$i] . '"';
  1368. $key_values = explode( ", ", $key_value);
  1369. if ( in_array($select_array[$i], $key_values) ) $string .= ' CHECKED';
  1370. $string .= '> ' . $select_array[$i];
  1371. }
  1372. $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
  1373. return $string;
  1374. }
  1375.  
  1376. // Alias function for Store configuration values in the Administration Tool.
  1377. function usps_cfg_multiinput_list($select_array, $key_value, $key = '') {
  1378. $key_values = explode( ", ", $key_value);
  1379.  
  1380. for ($i=0; $i<sizeof($select_array); $i++) {
  1381. $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
  1382. $string .= '<br><input type="text" name="' . $name . '" value="' . $key_values[$i] . '" size="7"> ' . $select_array[$i];
  1383. }
  1384. $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
  1385. return $string;
  1386. }
  1387.  
  1388. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement