Here is the file you asked for. I have to post in 3 posts. 1 of 3
<?php
/**
* USPS Module for Zen Cart v1.3.x - v1.6
* RateV3 Updates to: January 27, 2013 Version G for CURL
*
* @package shippingMethod
* @copyright Copyright 2003-2013 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license <http://www.zen-cart.com/license/2_0.txt> GNU Public License V2.0
* @version $Id: usps.php 18348G 2013-01-27 01:27:00Z ajeh $
*/
// bof: functions contributed by Marco B
//Quote sorting functions
if (!function_exists('usps_sort_Alphanumeric')) {
function usps_sort_Alphanumeric ($a, $b) {
return strcmp($a['title'],$b['title']);
}
}
if (!function_exists('usps_sort_Price')) {
function usps_sort_Price ($a, $b) {
$c=(float)$a['cost'];
$d=(float)$b['cost'];
if ($c==$d) return 0;
return ($c>$d?1:-1);
}
}
// eof: functions contributed by Marco B
/**
* USPS Shipping Module class
*
*/
class usps extends base {
/**
* Declare shipping module alias code
*
* @var string
*/
var $code;
/**
* Shipping module display name
*
* @var string
*/
var $title;
/**
* Shipping module display description
*
* @var string
*/
var $description;
/**
* Shipping module icon filename/path
*
* @var string
*/
var $icon;
/**
* Shipping module status
*
* @var boolean
*/
var $enabled;
/**
* Shipping module list of supported countries (unique to USPS/UPS)
*
* @var array
*/
var $countries;
/**
* Constructor
*
* @return usps
*/
// use USPS translations for US shops
var $usps_countries;
function usps() {
global $order, $db, $template, $current_page_base;
$this->code = 'usps';
$this->title = MODULE_SHIPPING_USPS_TEXT_TITLE;
if (MODULE_SHIPPING_USPS_STATUS == 'True' && MODULE_SHIPPING_USPS_DEBUG_MODE != 'Off' && IS_ADMIN_FLAG) {
$this->title .= '<span class="alert"> (Debug is ON: ' . MODULE_SHIPPING_USPS_DEBUG_MODE . ')</span>';
}
if (MODULE_SHIPPING_USPS_STATUS == 'True' && MODULE_SHIPPING_USPS_SERVER != 'production' && IS_ADMIN_FLAG) {
$this->title .= '<span class="alert"> (USPS Server set to: ' . MODULE_SHIPPING_USPS_SERVER . ')</span>';
}
$this->description = MODULE_SHIPPING_USPS_TEXT_DESCRIPTION;
$this->sort_order = MODULE_SHIPPING_USPS_SORT_ORDER;
$this->icon = $template->get_template_dir('shipping_usps.gif', DIR_WS_TEMPLATE, $current_page_base,'images/icons'). '/' . 'shipping_usps.gif';
$this->tax_class = MODULE_SHIPPING_USPS_TAX_CLASS;
$this->tax_basis = MODULE_SHIPPING_USPS_TAX_BASIS;
// disable only when entire cart is free shipping
// if (true || zen_get_shipping_enabled($this->code)) {
$this->enabled = ((MODULE_SHIPPING_USPS_STATUS == 'True') ? true : false);
// }
if ($this->enabled) {
// check if all keys are in configuration table and correct version
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_USPS_VERSION'");
$check_version = $check_query->fields['configuration_value'];
if (IS_ADMIN_FLAG) {
$chk_sql = $db->Execute("select * from " . TABLE_CONFIGURATION . " where configuration_key like 'MODULE\_SHIPPING\_USPS\_%' ");
$chk_keys = $this->keys();
if (($check_version != '2013-01-27') || (sizeof($chk_keys) != $chk_sql->RecordCount())) {
$this->title = $this->title . '<span class="alert">' . ' - Missing Keys you should reinstall!' . '</span>';
}
}
}
global $db, $order, $cart;
$valid_zone_id = 4;
$check_flag = false;
$check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . $valid_zone_id . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
while (!$check->EOF) {
if ($check->fields['zone_id'] < 1) {
$check_flag = true;
break;
} elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
$check_flag = true;
break;
}
$check->MoveNext();
}
if ($check_flag == true) {
$this->enabled = false;
} else {
$this->enabled = true;
}
// echo 'USPS shipping class delivery: ' . 'checkflag: ' . $check_flag . '<br> shipping: ' . $order->delivery['country']['id'] . ' state: ' . $order->delivery['state'] . ' zone: ' . $order->delivery['zone']['id'];
if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_USPS_ZONE > 0) ) {
$check_flag = false;
$check = $db->Execute("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->EOF) {
if ($check->fields['zone_id'] < 1) {
$check_flag = true;
break;
} elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
$check_flag = true;
break;
}
$check->MoveNext();
}
if ($check_flag == false) {
$this->enabled = false;
}
}
// update and new shipping names 01-27-2013
$this->types = array(
'EXPRESS' => 'Express Mail', // ID="0"
'FIRST CLASS' => 'First-Class Mail', // ID="1"
'PRIORITY' => 'Priority Mail', // ID="2"
'PARCEL' => 'Standard Post', // ID="3" changed from Parcel Post 01-27-2013
'MEDIA' => 'Media Mail', // ID="4"
'LIBRARY' => 'Library' // ID="5"
);
// update and add new shipping names 01-02-2011
// 4 6 7 12 1 10 [17] 2 11 9 16 [24] [25] 8 [22] [23] [18] [20] 14 15 13
// update and new shipping names 01-22-2012
// 4 6 7 12 1 [26] 10 17 2 11 9 16 24 25 8 22 23 18 20 [19] [15] 14 13
// update and new shipping names 01-27-2013
// 4 6 7 12 1 26 10 17 [27] 2 11 9 16 24 25 8 22 23 18 20 19 [15] 14 13
$this->intl_types = array(
'Global Express' => 'Global Express Guaranteed (GXG)**', // ID="4"
'Global Express Non-Doc Rect' => 'Global Express Guaranteed Non-Document Rectangular', // ID="6"
'Global Express Non-Doc Non-Rect' => 'Global Express Guaranteed Non-Document Non-Rectangular', // ID="7"
'USPS GXG Envelopes' => 'USPS GXG Envelopes**', // ID="12"
'Express Mail Int' => 'Express Mail International', // ID="1"
'Express Mail Int Flat Rate Box' => 'Express Mail International Flat Rate Boxes', // ID="26"
'Express Mail Int Flat Rate Env' => 'Express Mail International Flat Rate Envelope', // ID="10"
'Express Mail Int Legal' => 'Express Mail International Legal Flat Rate Envelope', // ID="17"
'Express Mail Int Padded Flat Rate Env' => 'Express Mail International Padded Flat Rate Envelope', // ID="27"
'Priority Mail International' => 'Priority Mail International', // ID="2"
'Priority Mail Int Flat Rate Lrg Box' => 'Priority Mail International Large Flat Rate Box', // ID="11"
'Priority Mail Int Flat Rate Med Box' => 'Priority Mail International Medium Flat Rate Box', // ID="9"
'Priority Mail Int Flat Rate Small Box' => 'Priority Mail International Small Flat Rate Box**', // ID="16"
'Priority Mail Int DVD' => 'Priority Mail International DVD Flat Rate Box**', // ID="24"
'Priority Mail Int Lrg Video' => 'Priority Mail International Large Video Flat Rate Box**', // ID="25"
'Priority Mail Int Flat Rate Env' => 'Priority Mail International Flat Rate Envelope**', // ID="8"
'Priority Mail Int Legal Flat Rate Env' => 'Priority Mail International Legal Flat Rate Envelope**', // ID="22"
'Priority Mail Int Padded Flat Rate Env' => 'Priority Mail International Padded Flat Rate Envelope**', // ID="23"
'Priority Mail Int Gift Card Flat Rate Env' => 'Priority Mail International Gift Card Flat Rate Envelope**', // ID=18
'Priority Mail Int Small Flat Rate Env' => 'Priority Mail International Small Flat Rate Envelope**', // ID="20"
'Priority Mail Int Window Flat Rate Env' => 'Priority Mail International Window Flat Rate Envelope**', // ID=19
'First Class Mail Int Package' => 'First-Class Package International Service**', // ID="15" Changed Mail International Parcel to Package International Service
'First Class Mail Int Lrg Env' => 'First-Class Mail International Large Envelope**', // ID="14"
'First Class Mail Int Letter' => 'First-Class Mail International Letter**' // ID="13"
);
$this->countries = $this->country_list();
// use USPS translations for US shops
$this->usps_countries = $this->usps_translation();
}
/**
* Get quote from shipping provider's API:
*
* @param string $method
* @return array of quotation results
*/
function quote($method = '') {
// BOF: UPS USPS
global $order, $shipping_weight, $shipping_num_boxes, $transittime;
if ( zen_not_null($method) && (isset($this->types[$method]) || in_array($method, $this->intl_types)) ) {
$this->_setService($method);
}
// usps doesnt accept zero weight send 1 ounce (0.0625) minimum
$usps_shipping_weight = ($shipping_weight <= 0.0 ? 0.0625 : $shipping_weight);
$shipping_pounds = floor ($usps_shipping_weight);
$shipping_ounces = (16 * ($usps_shipping_weight - floor($usps_shipping_weight)));
// usps currently cannot handle more than 5 digits on international
// change to 2 if International rates fail based on Tare Settings
$shipping_ounces = zen_round($shipping_ounces, MODULE_SHIPPING_USPS_DECIMALS);
// weight must be less than 35lbs and greater than 6 ounces or it is not machinable
switch(true) {
case ($shipping_pounds == 0 and $shipping_ounces < 6):
// override admin choice too light
$is_machinable = 'False';
break;
case ($usps_shipping_weight > 35):
// override admin choice too heavy
$is_machinable = 'False';
break;
default:
// admin choice on what to use
$is_machinable = MODULE_SHIPPING_USPS_MACHINABLE;
}
$this->_setMachinable($is_machinable);
$this->_setContainer('None');
$this->_setSize('REGULAR');
$this->_setFirstClassType('FLAT');
$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 {
// BOF: UPS USPS
if (in_array('Display weight', explode(', ', MODULE_SHIPPING_USPS_OPTIONS))) {
switch (SHIPPING_BOX_WEIGHT_DISPLAY) {
case (0):
$show_box_weight = '';
break;
case (1):
$show_box_weight = ' (' . $shipping_num_boxes . ' ' . TEXT_SHIPPING_BOXES . ')';
break;
case (2):
$show_box_weight = ' (' . number_format($usps_shipping_weight * $shipping_num_boxes,2) . TEXT_SHIPPING_WEIGHT . ')';
break;
default:
$show_box_weight = ' (' . $shipping_num_boxes . ' x ' . number_format($usps_shipping_weight,2) . TEXT_SHIPPING_WEIGHT . ')';
break;
}
}
// EOF: UPS USPS
// BOF: UPS USPS
$this->quotes = array('id' => $this->code,
'module' => $this->title . $show_box_weight);
// EOF: UPS USPS
// set handling fee
if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY || (SHIPPING_ORIGIN_COUNTRY == '223' && $this->usps_countries == 'US')) {
// national
$usps_handling_fee = MODULE_SHIPPING_USPS_HANDLING;
} else {
// international
$usps_handling_fee = MODULE_SHIPPING_USPS_HANDLING_INT;
}
$methods = array();
$size = sizeof($uspsQuote);
for ($i=0; $i<$size; $i++) {
list($type, $cost) = each($uspsQuote[$i]);
// BOF: UPS USPS
$title = ((isset($this->types[$type])) ? $this->types[$type] : $type);
if(in_array('Display transit time', explode(', ', MODULE_SHIPPING_USPS_OPTIONS))) $title .= $transittime[$type];
// strip the ** from the titles
$title = str_replace('**', '', $title);
$cost = preg_replace('/[^0-9.]/', '', $cost);
// add $this->usps_countries to title to test actual country
$methods[] = array('id' => $type,
'title' => $title,
'cost' => ($cost * $shipping_num_boxes) + (MODULE_SHIPPING_USPS_HANDLING_METHOD == 'Box' ? $usps_handling_fee * $shipping_num_boxes : $usps_handling_fee) );
// bof: sort by contributed by Marco B
// Sort the options
if (MODULE_SHIPPING_USPS_QUOTE_SORT != 'Unsorted') {
usort($methods,'usps_sort_'.MODULE_SHIPPING_USPS_QUOTE_SORT);
}
// eof: sort by contributed by Marco B
}
$this->quotes['methods'] = $methods;
if ($this->tax_class > 0) {
$this->quotes['tax'] = zen_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
}
}
} elseif ($uspsQuote == -1) {
$this->quotes = array('module' => $this->title,
'error' => MODULE_SHIPPING_USPS_TEXT_SERVER_ERROR . (MODULE_SHIPPING_USPS_SERVER=='test' ? MODULE_SHIPPING_USPS_TEXT_TEST_MODE_NOTICE : ''));
} else {
$this->quotes = array('module' => $this->title,
'error' => MODULE_SHIPPING_USPS_TEXT_ERROR . (MODULE_SHIPPING_USPS_SERVER=='test' ? MODULE_SHIPPING_USPS_TEXT_TEST_MODE_NOTICE : ''));
}
if (zen_not_null($this->icon)) $this->quotes['icon'] = zen_image($this->icon, $this->title);
return $this->quotes;
}
/**
* check status of module
*
* @return boolean
*/
function check() {
global $db;
if (!isset($this->_check)) {
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_SHIPPING_USPS_STATUS'");
$this->_check = $check_query->RecordCount();
}
return $this->_check;
}
/**
* Install this module
*
*/
function install() {
global $db;
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('USPS Version Date', 'MODULE_SHIPPING_USPS_VERSION', '2013-01-27', 'You have installed:', '6', '0', 'zen_cfg_select_option(array(\'2013-01-27\'), ', now())");
$db->Execute("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', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Enter the USPS Web Tools User ID', 'MODULE_SHIPPING_USPS_USERID', 'NONE', 'Enter the USPS USERID assigned to you for Rate Quotes/ShippingAPI.', '6', '0', now())");
$db->Execute("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', 'zen_cfg_select_option(array(\'test\', \'production\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('All Packages are Machinable', 'MODULE_SHIPPING_USPS_MACHINABLE', 'False', 'Are all products shipped machinable based on C700 Package Services 2.0 Nonmachinable PARCEL POST USPS Rules and Regulations?<br /><br /><strong>Note: Nonmachinable packages will usually result in a higher Parcel Post Rate Charge.<br /><br />Packages 35lbs or more, or less than 6 ounces (.375), will be overridden and set to False</strong>', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee - US', 'MODULE_SHIPPING_USPS_HANDLING', '0', 'National Handling fee for this shipping method.', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee - International', 'MODULE_SHIPPING_USPS_HANDLING_INT', '0', 'International Handling fee for this shipping method.', '6', '0', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Handling Per Order or Per Box', 'MODULE_SHIPPING_USPS_HANDLING_METHOD', 'Box', 'Do you want to charge Handling Fee Per Order or Per Box?', '6', '0', 'zen_cfg_select_option(array(\'Order\', \'Box\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Quote Sort Order', 'MODULE_SHIPPING_USPS_QUOTE_SORT', 'Unsorted', 'Sorts the returned quotes using the service name Alphanumerically or by Price. Unsorted will give the order provided by USPS.', '6', '0', 'zen_cfg_select_option(array(\'Unsorted\',\'Alphanumeric\', \'Price\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Decimal Settings', 'MODULE_SHIPPING_USPS_DECIMALS', '3', 'Decimal Setting can be 1, 2 or 3. Sometimes International requires 2 decimals, based on Tare Rates or Product weights. Do you want to use 1, 2 or 3 decimals?', '6', '0', 'zen_cfg_select_option(array(\'1\', \'2\', \'3\'), ', now())");
$db->Execute("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', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Tax Basis', 'MODULE_SHIPPING_USPS_TAX_BASIS', 'Shipping', 'On what basis is Shipping Tax calculated. Options are<br />Shipping - Based on customers Shipping Address<br />Billing Based on customers Billing address<br />Store - Based on Store address if Billing/Shipping Zone equals Store zone', '6', '0', 'zen_cfg_select_option(array(\'Shipping\', \'Billing\', \'Store\'), ', now())");
$db->Execute("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', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
$db->Execute("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())");