[ Index ] |
|
Code source de osCommerce 2.2ms2-060817 |
1 <?php 2 /* 3 $Id: usps.php,v 1.47 2003/04/08 23:23:42 dgw_ Exp $ 4 5 osCommerce, Open Source E-Commerce Solutions 6 http://www.oscommerce.com 7 8 Copyright (c) 2003 osCommerce 9 10 Released under the GNU General Public License 11 */ 12 13 class usps { 14 var $code, $title, $description, $icon, $enabled, $countries; 15 16 // class constructor 17 function usps() { 18 global $order; 19 20 $this->code = 'usps'; 21 $this->title = MODULE_SHIPPING_USPS_TEXT_TITLE; 22 $this->description = MODULE_SHIPPING_USPS_TEXT_DESCRIPTION; 23 $this->sort_order = MODULE_SHIPPING_USPS_SORT_ORDER; 24 $this->icon = DIR_WS_ICONS . 'shipping_usps.gif'; 25 $this->tax_class = MODULE_SHIPPING_USPS_TAX_CLASS; 26 $this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false); 27 28 if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPS_ZONE > 0) ) { 29 $check_flag = false; 30 $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"); 31 while ($check = tep_db_fetch_array($check_query)) { 32 if ($check['zone_id'] < 1) { 33 $check_flag = true; 34 break; 35 } elseif ($check['zone_id'] == $order->delivery['zone_id']) { 36 $check_flag = true; 37 break; 38 } 39 } 40 41 if ($check_flag == false) { 42 $this->enabled = false; 43 } 44 } 45 46 $this->types = array('Express' => 'Express Mail', 47 'First Class' => 'First-Class Mail', 48 'Priority' => 'Priority Mail', 49 'Parcel' => 'Parcel Post'); 50 51 $this->intl_types = array('GXG Document' => 'Global Express Guaranteed Document Service', 52 'GXG Non-Document' => 'Global Express Guaranteed Non-Document Service', 53 'Express' => 'Global Express Mail (EMS)', 54 'Priority Lg' => 'Global Priority Mail - Flat-rate Envelope (large)', 55 'Priority Sm' => 'Global Priority Mail - Flat-rate Envelope (small)', 56 'Priority Var' => 'Global Priority Mail - Variable Weight Envelope (single)', 57 'Airmail Letter' => 'Airmail Letter Post', 58 'Airmail Parcel' => 'Airmail Parcel Post', 59 'Surface Letter' => 'Economy (Surface) Letter Post', 60 'Surface Post' => 'Economy (Surface) Parcel Post'); 61 62 $this->countries = $this->country_list(); 63 } 64 65 // class methods 66 function quote($method = '') { 67 global $order, $shipping_weight, $shipping_num_boxes; 68 69 if ( tep_not_null($method) && (isset($this->types[$method]) || in_array($method, $this->intl_types)) ) { 70 $this->_setService($method); 71 } 72 73 $this->_setMachinable('False'); 74 $this->_setContainer('None'); 75 $this->_setSize('REGULAR'); 76 77 // usps doesnt accept zero weight 78 $shipping_weight = ($shipping_weight < 0.1 ? 0.1 : $shipping_weight); 79 $shipping_pounds = floor ($shipping_weight); 80 $shipping_ounces = round(16 * ($shipping_weight - floor($shipping_weight))); 81 $this->_setWeight($shipping_pounds, $shipping_ounces); 82 83 $uspsQuote = $this->_getQuote(); 84 85 if (is_array($uspsQuote)) { 86 if (isset($uspsQuote['error'])) { 87 $this->quotes = array('module' => $this->title, 88 'error' => $uspsQuote['error']); 89 } else { 90 $this->quotes = array('id' => $this->code, 91 'module' => $this->title . ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'lbs)'); 92 93 $methods = array(); 94 $size = sizeof($uspsQuote); 95 for ($i=0; $i<$size; $i++) { 96 list($type, $cost) = each($uspsQuote[$i]); 97 98 $methods[] = array('id' => $type, 99 'title' => ((isset($this->types[$type])) ? $this->types[$type] : $type), 100 'cost' => ($cost + MODULE_SHIPPING_USPS_HANDLING) * $shipping_num_boxes); 101 } 102 103 $this->quotes['methods'] = $methods; 104 105 if ($this->tax_class > 0) { 106 $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']); 107 } 108 } 109 } else { 110 $this->quotes = array('module' => $this->title, 111 'error' => MODULE_SHIPPING_USPS_TEXT_ERROR); 112 } 113 114 if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title); 115 116 return $this->quotes; 117 } 118 119 function check() { 120 if (!isset($this->_check)) { 121 $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_USPS_STATUS'"); 122 $this->_check = tep_db_num_rows($check_query); 123 } 124 return $this->_check; 125 } 126 127 function install() { 128 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())"); 129 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.', '6', '0', now())"); 130 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 Password', 'MODULE_SHIPPING_USPS_PASSWORD', 'NONE', 'See USERID, above.', '6', '0', now())"); 131 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 ('Which server to use', 'MODULE_SHIPPING_USPS_SERVER', 'production', 'An account at USPS is needed to use the Production server', '6', '0', 'tep_cfg_select_option(array(\'test\', \'production\'), ', now())"); 132 tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee', 'MODULE_SHIPPING_USPS_HANDLING', '0', 'Handling fee for this shipping method.', '6', '0', now())"); 133 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())"); 134 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())"); 135 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())"); 136 } 137 138 function remove() { 139 tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')"); 140 } 141 142 function keys() { 143 return array('MODULE_SHIPPING_USPS_STATUS', 'MODULE_SHIPPING_USPS_USERID', 'MODULE_SHIPPING_USPS_PASSWORD', 'MODULE_SHIPPING_USPS_SERVER', 'MODULE_SHIPPING_USPS_HANDLING', 'MODULE_SHIPPING_USPS_TAX_CLASS', 'MODULE_SHIPPING_USPS_ZONE', 'MODULE_SHIPPING_USPS_SORT_ORDER'); 144 } 145 146 function _setService($service) { 147 $this->service = $service; 148 } 149 150 function _setWeight($pounds, $ounces=0) { 151 $this->pounds = $pounds; 152 $this->ounces = $ounces; 153 } 154 155 function _setContainer($container) { 156 $this->container = $container; 157 } 158 159 function _setSize($size) { 160 $this->size = $size; 161 } 162 163 function _setMachinable($machinable) { 164 $this->machinable = $machinable; 165 } 166 167 function _getQuote() { 168 global $order; 169 170 if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) { 171 $request = '<RateRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">'; 172 $services_count = 0; 173 174 if (isset($this->service)) { 175 $this->types = array($this->service => $this->types[$this->service]); 176 } 177 178 $dest_zip = str_replace(' ', '', $order->delivery['postcode']); 179 if ($order->delivery['country']['iso_code_2'] == 'US') $dest_zip = substr($dest_zip, 0, 5); 180 181 reset($this->types); 182 while (list($key, $value) = each($this->types)) { 183 $request .= '<Package ID="' . $services_count . '">' . 184 '<Service>' . $key . '</Service>' . 185 '<ZipOrigination>' . SHIPPING_ORIGIN_ZIP . '</ZipOrigination>' . 186 '<ZipDestination>' . $dest_zip . '</ZipDestination>' . 187 '<Pounds>' . $this->pounds . '</Pounds>' . 188 '<Ounces>' . $this->ounces . '</Ounces>' . 189 '<Container>' . $this->container . '</Container>' . 190 '<Size>' . $this->size . '</Size>' . 191 '<Machinable>' . $this->machinable . '</Machinable>' . 192 '</Package>'; 193 $services_count++; 194 } 195 $request .= '</RateRequest>'; 196 197 $request = 'API=Rate&XML=' . urlencode($request); 198 } else { 199 $request = '<IntlRateRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">' . 200 '<Package ID="0">' . 201 '<Pounds>' . $this->pounds . '</Pounds>' . 202 '<Ounces>' . $this->ounces . '</Ounces>' . 203 '<MailType>Package</MailType>' . 204 '<Country>' . $this->countries[$order->delivery['country']['iso_code_2']] . '</Country>' . 205 '</Package>' . 206 '</IntlRateRequest>'; 207 208 $request = 'API=IntlRate&XML=' . urlencode($request); 209 } 210 211 switch (MODULE_SHIPPING_USPS_SERVER) { 212 case 'production': $usps_server = 'production.shippingapis.com'; 213 $api_dll = 'shippingapi.dll'; 214 break; 215 case 'test': 216 default: $usps_server = 'testing.shippingapis.com'; 217 $api_dll = 'ShippingAPITest.dll'; 218 break; 219 } 220 221 $body = ''; 222 223 $http = new httpClient(); 224 if ($http->Connect($usps_server, 80)) { 225 $http->addHeader('Host', $usps_server); 226 $http->addHeader('User-Agent', 'osCommerce'); 227 $http->addHeader('Connection', 'Close'); 228 229 if ($http->Get('/' . $api_dll . '?' . $request)) $body = $http->getBody(); 230 231 $http->Disconnect(); 232 } else { 233 return false; 234 } 235 236 $response = array(); 237 while (true) { 238 if ($start = strpos($body, '<Package ID=')) { 239 $body = substr($body, $start); 240 $end = strpos($body, '</Package>'); 241 $response[] = substr($body, 0, $end+10); 242 $body = substr($body, $end+9); 243 } else { 244 break; 245 } 246 } 247 248 $rates = array(); 249 if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) { 250 if (sizeof($response) == '1') { 251 if (ereg('<Error>', $response[0])) { 252 $number = ereg('<Number>(.*)</Number>', $response[0], $regs); 253 $number = $regs[1]; 254 $description = ereg('<Description>(.*)</Description>', $response[0], $regs); 255 $description = $regs[1]; 256 257 return array('error' => $number . ' - ' . $description); 258 } 259 } 260 261 $n = sizeof($response); 262 for ($i=0; $i<$n; $i++) { 263 if (strpos($response[$i], '<Postage>')) { 264 $service = ereg('<Service>(.*)</Service>', $response[$i], $regs); 265 $service = $regs[1]; 266 $postage = ereg('<Postage>(.*)</Postage>', $response[$i], $regs); 267 $postage = $regs[1]; 268 269 $rates[] = array($service => $postage); 270 } 271 } 272 } else { 273 if (ereg('<Error>', $response[0])) { 274 $number = ereg('<Number>(.*)</Number>', $response[0], $regs); 275 $number = $regs[1]; 276 $description = ereg('<Description>(.*)</Description>', $response[0], $regs); 277 $description = $regs[1]; 278 279 return array('error' => $number . ' - ' . $description); 280 } else { 281 $body = $response[0]; 282 $services = array(); 283 while (true) { 284 if ($start = strpos($body, '<Service ID=')) { 285 $body = substr($body, $start); 286 $end = strpos($body, '</Service>'); 287 $services[] = substr($body, 0, $end+10); 288 $body = substr($body, $end+9); 289 } else { 290 break; 291 } 292 } 293 294 $size = sizeof($services); 295 for ($i=0, $n=$size; $i<$n; $i++) { 296 if (strpos($services[$i], '<Postage>')) { 297 $service = ereg('<SvcDescription>(.*)</SvcDescription>', $services[$i], $regs); 298 $service = $regs[1]; 299 $postage = ereg('<Postage>(.*)</Postage>', $services[$i], $regs); 300 $postage = $regs[1]; 301 302 if (isset($this->service) && ($service != $this->service) ) { 303 continue; 304 } 305 306 $rates[] = array($service => $postage); 307 } 308 } 309 } 310 } 311 312 return ((sizeof($rates) > 0) ? $rates : false); 313 } 314 315 function country_list() { 316 $list = array('AF' => 'Afghanistan', 317 'AL' => 'Albania', 318 'DZ' => 'Algeria', 319 'AD' => 'Andorra', 320 'AO' => 'Angola', 321 'AI' => 'Anguilla', 322 'AG' => 'Antigua and Barbuda', 323 'AR' => 'Argentina', 324 'AM' => 'Armenia', 325 'AW' => 'Aruba', 326 'AU' => 'Australia', 327 'AT' => 'Austria', 328 'AZ' => 'Azerbaijan', 329 'BS' => 'Bahamas', 330 'BH' => 'Bahrain', 331 'BD' => 'Bangladesh', 332 'BB' => 'Barbados', 333 'BY' => 'Belarus', 334 'BE' => 'Belgium', 335 'BZ' => 'Belize', 336 'BJ' => 'Benin', 337 'BM' => 'Bermuda', 338 'BT' => 'Bhutan', 339 'BO' => 'Bolivia', 340 'BA' => 'Bosnia-Herzegovina', 341 'BW' => 'Botswana', 342 'BR' => 'Brazil', 343 'VG' => 'British Virgin Islands', 344 'BN' => 'Brunei Darussalam', 345 'BG' => 'Bulgaria', 346 'BF' => 'Burkina Faso', 347 'MM' => 'Burma', 348 'BI' => 'Burundi', 349 'KH' => 'Cambodia', 350 'CM' => 'Cameroon', 351 'CA' => 'Canada', 352 'CV' => 'Cape Verde', 353 'KY' => 'Cayman Islands', 354 'CF' => 'Central African Republic', 355 'TD' => 'Chad', 356 'CL' => 'Chile', 357 'CN' => 'China', 358 'CX' => 'Christmas Island (Australia)', 359 'CC' => 'Cocos Island (Australia)', 360 'CO' => 'Colombia', 361 'KM' => 'Comoros', 362 'CG' => 'Congo (Brazzaville),Republic of the', 363 'ZR' => 'Congo, Democratic Republic of the', 364 'CK' => 'Cook Islands (New Zealand)', 365 'CR' => 'Costa Rica', 366 'CI' => 'Cote d\'Ivoire (Ivory Coast)', 367 'HR' => 'Croatia', 368 'CU' => 'Cuba', 369 'CY' => 'Cyprus', 370 'CZ' => 'Czech Republic', 371 'DK' => 'Denmark', 372 'DJ' => 'Djibouti', 373 'DM' => 'Dominica', 374 'DO' => 'Dominican Republic', 375 'TP' => 'East Timor (Indonesia)', 376 'EC' => 'Ecuador', 377 'EG' => 'Egypt', 378 'SV' => 'El Salvador', 379 'GQ' => 'Equatorial Guinea', 380 'ER' => 'Eritrea', 381 'EE' => 'Estonia', 382 'ET' => 'Ethiopia', 383 'FK' => 'Falkland Islands', 384 'FO' => 'Faroe Islands', 385 'FJ' => 'Fiji', 386 'FI' => 'Finland', 387 'FR' => 'France', 388 'GF' => 'French Guiana', 389 'PF' => 'French Polynesia', 390 'GA' => 'Gabon', 391 'GM' => 'Gambia', 392 'GE' => 'Georgia, Republic of', 393 'DE' => 'Germany', 394 'GH' => 'Ghana', 395 'GI' => 'Gibraltar', 396 'GB' => 'Great Britain and Northern Ireland', 397 'GR' => 'Greece', 398 'GL' => 'Greenland', 399 'GD' => 'Grenada', 400 'GP' => 'Guadeloupe', 401 'GT' => 'Guatemala', 402 'GN' => 'Guinea', 403 'GW' => 'Guinea-Bissau', 404 'GY' => 'Guyana', 405 'HT' => 'Haiti', 406 'HN' => 'Honduras', 407 'HK' => 'Hong Kong', 408 'HU' => 'Hungary', 409 'IS' => 'Iceland', 410 'IN' => 'India', 411 'ID' => 'Indonesia', 412 'IR' => 'Iran', 413 'IQ' => 'Iraq', 414 'IE' => 'Ireland', 415 'IL' => 'Israel', 416 'IT' => 'Italy', 417 'JM' => 'Jamaica', 418 'JP' => 'Japan', 419 'JO' => 'Jordan', 420 'KZ' => 'Kazakhstan', 421 'KE' => 'Kenya', 422 'KI' => 'Kiribati', 423 'KW' => 'Kuwait', 424 'KG' => 'Kyrgyzstan', 425 'LA' => 'Laos', 426 'LV' => 'Latvia', 427 'LB' => 'Lebanon', 428 'LS' => 'Lesotho', 429 'LR' => 'Liberia', 430 'LY' => 'Libya', 431 'LI' => 'Liechtenstein', 432 'LT' => 'Lithuania', 433 'LU' => 'Luxembourg', 434 'MO' => 'Macao', 435 'MK' => 'Macedonia, Republic of', 436 'MG' => 'Madagascar', 437 'MW' => 'Malawi', 438 'MY' => 'Malaysia', 439 'MV' => 'Maldives', 440 'ML' => 'Mali', 441 'MT' => 'Malta', 442 'MQ' => 'Martinique', 443 'MR' => 'Mauritania', 444 'MU' => 'Mauritius', 445 'YT' => 'Mayotte (France)', 446 'MX' => 'Mexico', 447 'MD' => 'Moldova', 448 'MC' => 'Monaco (France)', 449 'MN' => 'Mongolia', 450 'MS' => 'Montserrat', 451 'MA' => 'Morocco', 452 'MZ' => 'Mozambique', 453 'NA' => 'Namibia', 454 'NR' => 'Nauru', 455 'NP' => 'Nepal', 456 'NL' => 'Netherlands', 457 'AN' => 'Netherlands Antilles', 458 'NC' => 'New Caledonia', 459 'NZ' => 'New Zealand', 460 'NI' => 'Nicaragua', 461 'NE' => 'Niger', 462 'NG' => 'Nigeria', 463 'KP' => 'North Korea (Korea, Democratic People\'s Republic of)', 464 'NO' => 'Norway', 465 'OM' => 'Oman', 466 'PK' => 'Pakistan', 467 'PA' => 'Panama', 468 'PG' => 'Papua New Guinea', 469 'PY' => 'Paraguay', 470 'PE' => 'Peru', 471 'PH' => 'Philippines', 472 'PN' => 'Pitcairn Island', 473 'PL' => 'Poland', 474 'PT' => 'Portugal', 475 'QA' => 'Qatar', 476 'RE' => 'Reunion', 477 'RO' => 'Romania', 478 'RU' => 'Russia', 479 'RW' => 'Rwanda', 480 'SH' => 'Saint Helena', 481 'KN' => 'Saint Kitts (St. Christopher and Nevis)', 482 'LC' => 'Saint Lucia', 483 'PM' => 'Saint Pierre and Miquelon', 484 'VC' => 'Saint Vincent and the Grenadines', 485 'SM' => 'San Marino', 486 'ST' => 'Sao Tome and Principe', 487 'SA' => 'Saudi Arabia', 488 'SN' => 'Senegal', 489 'YU' => 'Serbia-Montenegro', 490 'SC' => 'Seychelles', 491 'SL' => 'Sierra Leone', 492 'SG' => 'Singapore', 493 'SK' => 'Slovak Republic', 494 'SI' => 'Slovenia', 495 'SB' => 'Solomon Islands', 496 'SO' => 'Somalia', 497 'ZA' => 'South Africa', 498 'GS' => 'South Georgia (Falkland Islands)', 499 'KR' => 'South Korea (Korea, Republic of)', 500 'ES' => 'Spain', 501 'LK' => 'Sri Lanka', 502 'SD' => 'Sudan', 503 'SR' => 'Suriname', 504 'SZ' => 'Swaziland', 505 'SE' => 'Sweden', 506 'CH' => 'Switzerland', 507 'SY' => 'Syrian Arab Republic', 508 'TW' => 'Taiwan', 509 'TJ' => 'Tajikistan', 510 'TZ' => 'Tanzania', 511 'TH' => 'Thailand', 512 'TG' => 'Togo', 513 'TK' => 'Tokelau (Union) Group (Western Samoa)', 514 'TO' => 'Tonga', 515 'TT' => 'Trinidad and Tobago', 516 'TN' => 'Tunisia', 517 'TR' => 'Turkey', 518 'TM' => 'Turkmenistan', 519 'TC' => 'Turks and Caicos Islands', 520 'TV' => 'Tuvalu', 521 'UG' => 'Uganda', 522 'UA' => 'Ukraine', 523 'AE' => 'United Arab Emirates', 524 'UY' => 'Uruguay', 525 'UZ' => 'Uzbekistan', 526 'VU' => 'Vanuatu', 527 'VA' => 'Vatican City', 528 'VE' => 'Venezuela', 529 'VN' => 'Vietnam', 530 'WF' => 'Wallis and Futuna Islands', 531 'WS' => 'Western Samoa', 532 'YE' => 'Yemen', 533 'ZM' => 'Zambia', 534 'ZW' => 'Zimbabwe'); 535 536 return $list; 537 } 538 } 539 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 19:48:25 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |