📄 usps.php
字号:
<?php
/*
$Id: usps.php,v 1.1.1.1 2004/10/09 14:29:26 cvsadmin Exp $
osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com
Copyright (c) 2003 osCommerce
Released under the GNU General Public License
*/
class usps {
var $code, $title, $description, $icon, $enabled, $countries;
// class constructor
function usps() {
global $order;
$this->code = 'usps';
$this->title = MODULE_SHIPPING_USPS_TEXT_TITLE;
$this->description = MODULE_SHIPPING_USPS_TEXT_DESCRIPTION;
$this->sort_order = MODULE_SHIPPING_USPS_SORT_ORDER;
$this->icon = DIR_WS_ICONS . 'shipping_usps.gif';
$this->tax_class = MODULE_SHIPPING_USPS_TAX_CLASS;
$this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPS_ZONE > 0) ) {
$check_flag = false;
$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");
while ($check = tep_db_fetch_array($check_query)) {
if ($check['zone_id'] < 1) {
$check_flag = true;
break;
} elseif ($check['zone_id'] == $order->delivery['zone_id']) {
$check_flag = true;
break;
}
}
if ($check_flag == false) {
$this->enabled = false;
}
}
$this->types = array('Express' => 'Express Mail',
'First Class' => 'First-Class Mail',
'Priority' => 'Priority Mail',
'Parcel' => 'Parcel Post');
$this->intl_types = array('GXG Document' => 'Global Express Guaranteed Document Service',
'GXG Non-Document' => 'Global Express Guaranteed Non-Document Service',
'Express' => 'Global Express Mail (EMS)',
'Priority Lg' => 'Global Priority Mail - Flat-rate Envelope (large)',
'Priority Sm' => 'Global Priority Mail - Flat-rate Envelope (small)',
'Priority Var' => 'Global Priority Mail - Variable Weight Envelope (single)',
'Airmail Letter' => 'Airmail Letter Post',
'Airmail Parcel' => 'Airmail Parcel Post',
'Surface Letter' => 'Economy (Surface) Letter Post',
'Surface Post' => 'Economy (Surface) Parcel Post');
$this->countries = $this->country_list();
}
// class methods
function quote($method = '') {
global $order, $shipping_weight, $shipping_num_boxes;
if ( tep_not_null($method) && (isset($this->types[$method]) || in_array($method, $this->intl_types)) ) {
$this->_setService($method);
}
$this->_setMachinable('False');
$this->_setContainer('None');
$this->_setSize('REGULAR');
// usps doesnt accept zero weight
$shipping_weight = ($shipping_weight < 0.1 ? 0.1 : $shipping_weight);
$shipping_pounds = floor ($shipping_weight);
$shipping_ounces = round(16 * ($shipping_weight - floor($shipping_weight)));
$this->_setWeight($shipping_pounds, $shipping_ounces);
$uspsQuote = $this->_getQuote();
if (is_array($uspsQuote)) {
if (isset($uspsQuote['error'])) {
$this->quotes = array('module' => $this->title,
'error' => $uspsQuote['error']);
} else {
$this->quotes = array('id' => $this->code,
'module' => $this->title . ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'lbs)');
$methods = array();
$size = sizeof($uspsQuote);
for ($i=0; $i<$size; $i++) {
list($type, $cost) = each($uspsQuote[$i]);
$methods[] = array('id' => $type,
'title' => ((isset($this->types[$type])) ? $this->types[$type] : $type),
'cost' => ($cost + MODULE_SHIPPING_USPS_HANDLING) * $shipping_num_boxes);
}
$this->quotes['methods'] = $methods;
if ($this->tax_class > 0) {
$this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
}
}
} else {
$this->quotes = array('module' => $this->title,
'error' => MODULE_SHIPPING_USPS_TEXT_ERROR);
}
if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
return $this->quotes;
}
function check() {
if (!isset($this->_check)) {
$check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_USPS_STATUS'");
$this->_check = tep_db_num_rows($check_query);
}
return $this->_check;
}
function install() {
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())");
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())");
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())");
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())");
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())");
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())");
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())");
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())");
}
function remove() {
tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
}
function keys() {
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');
}
function _setService($service) {
$this->service = $service;
}
function _setWeight($pounds, $ounces=0) {
$this->pounds = $pounds;
$this->ounces = $ounces;
}
function _setContainer($container) {
$this->container = $container;
}
function _setSize($size) {
$this->size = $size;
}
function _setMachinable($machinable) {
$this->machinable = $machinable;
}
function _getQuote() {
global $order;
if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) {
$request = '<RateRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">';
$services_count = 0;
if (isset($this->service)) {
$this->types = array($this->service => $this->types[$this->service]);
}
$dest_zip = str_replace(' ', '', $order->delivery['postcode']);
if ($order->delivery['country']['iso_code_2'] == 'US') $dest_zip = substr($dest_zip, 0, 5);
reset($this->types);
while (list($key, $value) = each($this->types)) {
$request .= '<Package ID="' . $services_count . '">' .
'<Service>' . $key . '</Service>' .
'<ZipOrigination>' . SHIPPING_ORIGIN_ZIP . '</ZipOrigination>' .
'<ZipDestination>' . $dest_zip . '</ZipDestination>' .
'<Pounds>' . $this->pounds . '</Pounds>' .
'<Ounces>' . $this->ounces . '</Ounces>' .
'<Container>' . $this->container . '</Container>' .
'<Size>' . $this->size . '</Size>' .
'<Machinable>' . $this->machinable . '</Machinable>' .
'</Package>';
$services_count++;
}
$request .= '</RateRequest>';
$request = 'API=Rate&XML=' . urlencode($request);
} else {
$request = '<IntlRateRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">' .
'<Package ID="0">' .
'<Pounds>' . $this->pounds . '</Pounds>' .
'<Ounces>' . $this->ounces . '</Ounces>' .
'<MailType>Package</MailType>' .
'<Country>' . $this->countries[$order->delivery['country']['iso_code_2']] . '</Country>' .
'</Package>' .
'</IntlRateRequest>';
$request = 'API=IntlRate&XML=' . urlencode($request);
}
switch (MODULE_SHIPPING_USPS_SERVER) {
case 'production': $usps_server = 'production.shippingapis.com';
$api_dll = 'shippingapi.dll';
break;
case 'test':
default: $usps_server = 'testing.shippingapis.com';
$api_dll = 'ShippingAPITest.dll';
break;
}
$body = '';
$http = new httpClient();
if ($http->Connect($usps_server, 80)) {
$http->addHeader('Host', $usps_server);
$http->addHeader('User-Agent', 'osCommerce');
$http->addHeader('Connection', 'Close');
if ($http->Get('/' . $api_dll . '?' . $request)) $body = $http->getBody();
$http->Disconnect();
} else {
return false;
}
$response = array();
while (true) {
if ($start = strpos($body, '<Package ID=')) {
$body = substr($body, $start);
$end = strpos($body, '</Package>');
$response[] = substr($body, 0, $end+10);
$body = substr($body, $end+9);
} else {
break;
}
}
$rates = array();
if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) {
if (sizeof($response) == '1') {
if (ereg('<Error>', $response[0])) {
$number = ereg('<Number>(.*)</Number>', $response[0], $regs);
$number = $regs[1];
$description = ereg('<Description>(.*)</Description>', $response[0], $regs);
$description = $regs[1];
return array('error' => $number . ' - ' . $description);
}
}
$n = sizeof($response);
for ($i=0; $i<$n; $i++) {
if (strpos($response[$i], '<Postage>')) {
$service = ereg('<Service>(.*)</Service>', $response[$i], $regs);
$service = $regs[1];
$postage = ereg('<Postage>(.*)</Postage>', $response[$i], $regs);
$postage = $regs[1];
$rates[] = array($service => $postage);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -