Jump to content
bad_lemming

[Contribution] - USPS Methods

Recommended Posts

I'm working right now to gather all addiitons to any files that have anything to do with USPS or "Greg Deeth" since his name is in many of the changes and file if not al of them :)

 

I've fixed my problem with the help of a friend. Turns out I had more custom stuff in the module file then I had thought. I needed to manually insert the code.

 

Thanks for all the help. Everyone is being great in getting this solved.

 

Mike

Share this post


Link to post
Share on other sites

I'm working right now to gather all addiitons to any files that have anything to do with USPS or "Greg Deeth" since his name is in many of the changes and file if not al of them :)

Sounds great! Glad to hear you've got something workable for the time being.

Share this post


Link to post
Share on other sites

I've fixed my problem with the help of a friend. Turns out I had more custom stuff in the module file then I had thought. I needed to manually insert the code.

 

Thanks for all the help. Everyone is being great in getting this solved.

 

Mike

 

Mike, you were not using the files that were supplied by fullucscents?? Can you be more specific as to what you changed??


Debbie D
Franklin County, VA "Moonshine Capitol of the World"
osCmax Mobile Template oscmaxtemplates.com

Share this post


Link to post
Share on other sites

originally installed (into oscMAX - everything here is in the 2.0.25 version straight from oscMAX except I can't find the sql reference noted at the very bottom here):

 

USPS Methods 4.3.2 Fix international services 8 Jan 2010

 

This version had insurance options & weight thresholds and you could set the handling for international seperate from domestic

 

So I am using the new 5.0.1 files with the latest fix file and these modifications below were in and working for a long time for me, but I continue to have issues with domestic rates not being pulled and/or blank white pages on domestic depending on the settings in the admin

 

So right now the only difference I can see if the sql.. I'm open to suggestions if I should give it a swing or not.. I can do it on my dev cart if needed..

 

=========================

 

catalog\admin\includes\functions\general.php

this change is exact from the 4.3.2 readme

 

FIND:

////
// Alias function for module configuration keys
 function tep_mod_select_option($select_array, $key_name, $key_value) {
   reset($select_array);
   while (list($key, $value) = each($select_array)) {
     if (is_int($key)) $key = $value;
     $string .= '<br><input type="radio" name="configuration[' . $key_name . ']" value="' . $key . '"';
     if ($key_value == $key) $string .= ' CHECKED';
     $string .= '> ' . $value;
   }

   return $string;
 }

 

 

ADD AFTER:

		// USPS Methods - credit to Greg Deeth
	  function tep_cfg_select_multioption($select_array, $key_value, $key = '') {
	    for ($i=0; $i<sizeof($select_array); $i++) {
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="checkbox" name="' . $name . '" value="' . $select_array[$i] . '"';
	      $key_values = explode( ", ", $key_value);
	      if ( in_array($select_array[$i], $key_values) ) $string .= ' CHECKED';
	      $string .= '> ' . $select_array[$i];
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    return $string;
	  }
	  function tep_cfg_multiinput_list($select_array, $key_value, $key = '') {
	    $key_values = explode( ", ", $key_value);

	    for ($i=0; $i<sizeof($select_array); $i++) {
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="text" name="' . $name . '" value="' . $key_values[$i] . '"> ' . $select_array[$i];
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    return $string;
	  }
	  function tep_cfg_multiinput_duallist_oz($select_array, $key_value, $key = '') {
	    $key_values = explode( ", ", $key_value);
	    $string .= '<center>';
	    for ($i=0; $i<sizeof($select_array); $i++) {
		$current_key_value = current($key_values);
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>oz</i>';
		$string .= ' <b><</b> ' . $select_array[$i] . ' <u><b><</b></u>';
		next($key_values);
		$current_key_value = current($key_values);
		$string .= '<input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>oz</i>';
		next($key_values);
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    $string .= '</center>';
	    return $string;
	  }
	  function tep_cfg_multiinput_duallist_lb($select_array, $key_value, $key = '') {
	    $key_values = explode( ", ", $key_value);
	    $string .= '<center>';
	    for ($i=0; $i<sizeof($select_array); $i++) {
		$current_key_value = current($key_values);
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>lbs</i>';
		$string .= ' <b><</b> ' . $select_array[$i] . ' <u><b><</b></u>';
		next($key_values);
		$current_key_value = current($key_values);
		$string .= '<input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>lbs</i>';
		next($key_values);
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    $string .= '</center>';
	    return $string;
	  }

=====================

catalog\includes\functions\general.php

this change is exact from the 4.3.2 readme

 

 

FIND:

 

// Wrapper function for round()
 function tep_round($number, $precision) {
   if (strpos($number, '.') && (strlen(substr($number, strpos($number, '.')+1)) > $precision)) {
     $number = substr($number, 0, strpos($number, '.') + 1 + $precision + 1);

     if (substr($number, -1) >= 5) {
       if ($precision > 1) {
         $number = substr($number, 0, -1) + ('0.' . str_repeat(0, $precision-1) . '1');
       } elseif ($precision == 1) {
         $number = substr($number, 0, -1) + 0.1;
       } else {
         $number = substr($number, 0, -1) + 1;
       }
     } else {
       $number = substr($number, 0, -1);
     }
   }

   return $number;
 }

ADD AFTER:

 

////
// Round up function for non whole numbers by GREG DEETH
// The value for the precision variable determines how many digits after the decimal and rounds the last digit up to the next value
// Precision = 0 -> xx.xxxx = x+
// Precision = 1 -> xx.xxxx = xx.+
// Precision = 2 -> xx.xxxx = xx.x+
 function tep_round_up($number, $precision) {
$number_whole = '';
$num_left_dec = 0;
$num_right_dec = 0;
$num_digits = strlen($number);
$number_out = '';
$i = 0;
while ($i + 1 <= strlen($number))
{
	$current_digit = substr($number, $i, ($i + 1) - $num_digits);
	if ($current_digit == '.') {
		$i = $num_digits + 1;
		$num_left_dec = strlen($number_whole);
		$num_right_dec = ($num_left_dec + 1) - $num_digits;
	} else {
		$number_whole = $number_whole . $current_digit;
		$i = $i + 1;
	}
}
if ($num_digits > 3 && $precision < ($num_digits - $num_left_dec - 1) && $precision >= 0) {
	$i = $precision;
	$addable = 1;
	while ($i > 0) {
		$addable = $addable * .1;
		$i = $i - 1;
	} 
	$number_out = substr($number, 0, $num_right_dec + $precision) + $addable;
} else {
	$number_out = $number;
}
return $number_out;
 }

==============================

 

Addiitonally this mod states if you have version 2.9 or earlier you need to:

 

catalog/admin/modules.php

 

*****************************************

Find This code somewhere around line 43:

*****************************************

 

if (tep_not_null($action)) {
   switch ($action) {
     case 'save':
       while (list($key, $value) = each($HTTP_POST_VARS['configuration'])) {

***********************************************

INSERT THE FOLLOWING CODE AFTER THE ABOVE LINE:

***********************************************

 

		  if( is_array( $value ) ){
         $value = implode( ", ", $value);
	  $value = ereg_replace (", --none--", "", $value);
	    }

=================================

 

Lastly the readme states:

if upgrading from a version prior to 2.9.2 with tarheit's fix for upper case USPS names,

run the SQL

 

 

UPDATE configuration
SET configuration_value = UPPER(configuration_value),
   set_function='tep_cfg_select_multioption(array(''GLOBAL EXPRESS'', ''GLOBAL EXPRESS NON-DOC RECT'', ''GLOBAL EXPRESS NON-DOC NON-RECT'', ''EXPRESS MAIL INT'', ''EXPRESS MAIL INT FLAT RATE ENV'', ''PRIORITY MAIL INT'', ''PRIORITY MAIL INT FLAT RATE ENV'', ''PRIORITY MAIL INT FLAT RATE BOX'', ''FIRST-CLASS MAIL INT''), '
WHERE configuration_key='MODULE_SHIPPING_USPS_TYPES_INTL';
UPDATE configuration
SET configuration_value = UPPER(configuration_value),
   set_function='tep_cfg_select_multioption(array(''EXPRESS'', ''PRIORITY'', ''FIRST CLASS'', ''PARCEL'', ''BPM'', ''LIBRARY'', ''MEDIA''), '
WHERE configuration_key='MODULE_SHIPPING_USPS_TYPES';


Debbie D
Franklin County, VA "Moonshine Capitol of the World"
osCmax Mobile Template oscmaxtemplates.com

Share this post


Link to post
Share on other sites

and hopefully one tidy bit of info for you coders out there.. I turned on the reporting in my dev cart, when I requested the international address rates, the page came up fine with all rates and I got a really nice whole big report in my email.. I reformatted the report mails here so it's easier to read

 

Long story short, checking DOMESTIC express & priority only give no errors, grab rates but do not deliver back to browser properly producing white blank pages. checking/unchecking any other DOMESTIC rate combination produce white pages, and error on the last checked rate

 

switched to domestic address (with all the domestic rate boxes UNchecked)I was presented with the shipping page (not "white blank"), and a statement from USPS that an error had ocurred. the report I got this:

<?xml version="1.0"?>
<Error><Number>-2147219085</Number><Source>Rate_Respond.;SOLServerRates.RateV3_Respond</Source><Description>Invalid XML Element cannot be empty according to the DTD/Schema.
line= 0 pos= 1196</Description><HelpFile></HelpFile><HelpContext>1000440</HelpContext></Error>

 

So it looks like thats correct, no domestic rates checked, sending empty schema, bleh no rates to report.. OK I got that :)

 

now I went back in and checked just express & priority - got "white blank" page - now the report says:

<?xml version="1.0"?>
<RateV3Response>
<Package ID="0"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="3"><MailService>Express Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>15.25</Rate></Postage></Package>
<Package ID="1"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="1"><MailService>Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>5.10</Rate></Postage></Package></RateV3Response>

 

 

One more test, rechecked all but media mail on the domestic boxes and refreshed the "blank" domestic shipping address

response:

<?xml version="1.0"?>
<RateV3Response>
<Package ID="0"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="3"><MailService>Express Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>15.25</Rate></Postage></Package>
<Package ID="1"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="1"><MailService>Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>5.10</Rate></Postage></Package>
<Package ID="2"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="4"><MailService>Parcel Post&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>4.90</Rate></Postage></Package>
<Package ID="3"><Error><Number>-2147219432</Number><Source>DomesticRatesV3;clsRateV3.CreateSinglePostage-CreateSinglePostage;RateEngineV3.ProcessRequest</Source><Description>Service not available.</Description><HelpFile></HelpFile><HelpContext>1000440</HelpContext></Error></Package></RateV3Response>

 

here it looks like even though again I have a white page, all rates were returned except ID3 which I belive is going to be media rate

 

back in one more time, check media mail (now ALL are checked) refresh the still "blank white" page - response:

<?xml version="1.0"?>
<RateV3Response>
<Package ID="0"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="3"><MailService>Express Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>15.25</Rate></Postage></Package>
<Package ID="1"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="1"><MailService>Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>5.10</Rate></Postage></Package>
<Package ID="2"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="4"><MailService>Parcel Post&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>4.90</Rate></Postage></Package>
<Package ID="3"><ZipOrigination>11779</ZipOrigination><ZipDestination>11779</ZipDestination><Pounds>1</Pounds><Ounces>0</Ounces><Container></Container><Size>REGULAR</Size><Zone>1</Zone><Postage CLASSID="6"><MailService>Media Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService><Rate>2.38</Rate></Postage></Package>
<Package ID="4"><Error><Number>-2147219432</Number><Source>DomesticRatesV3;clsRateV3.CreateSinglePostage-CreateSinglePostage;RateEngineV3.ProcessRequest</Source><Description>Service not available.</Description><HelpFile></HelpFile><HelpContext>1000440</HelpContext></Error></Package></RateV3Response>

 

now the error is at ID4 which has to be first class..

 

So it is responding with rates, just it hangs coming back to the browser..

 

progress in finding the answer.. it's right there.. I just cant reach it!


Debbie D
Franklin County, VA "Moonshine Capitol of the World"
osCmax Mobile Template oscmaxtemplates.com

Share this post


Link to post
Share on other sites

Concerning the htmlspecialchars_decode function malfunction reported by others.

 

This function also failed for me in the "USPS Methods 5.0.1 - bug fix." version.

 

I did some research and found that the function is not supported by all versions of PHP. My host is using PHP version 5.2.13, which apparently doesn't support it.

 

I fixed it by replacing the following code:

 

$service = htmlspecialchars_decode($regs[1]);

 

with this code:

 

if ( !function_exists('htmlspecialchars_decode') )

{

function htmlspecialchars_decode($text)

{

return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));

}

}

$service = htmlspecialchars_decode($regs[1]);

 

This code came from a user contribution on the page http://php.net/manual/en/function.htmlspecialchars-decode.php

 

It works! No other changes were made other than copying the edited contribution file to the server.

Share this post


Link to post
Share on other sites

Mike, you were not using the files that were supplied by fullucscents?? Can you be more specific as to what you changed??

I was using the 5.0.1 files. The problem was that I had done a few insurance mods I'd forgot about. So replacing the files was causing errors (blank screens) with my other mods.

 

I basically took fullucscents mods from the files and pasted them into my files.

 

Mike

Share this post


Link to post
Share on other sites

I've installed the 5.0.1 contribution. But I'm having a problem in the admin panel. The shipping method checkboxes aren't working. When I update the module I just get 'Array' under the shipping methods. Anyone else getting this?

Share this post


Link to post
Share on other sites

I waded thru the fixes and have functional carts Except there is no First Class Package option available.... which effectively shuts down the entire store. Anyone else need First Class Mail Package all the time instead of First Class Mail Letter?

Share this post


Link to post
Share on other sites

I waded thru the fixes and have functional carts Except there is no First Class Package option available.... which effectively shuts down the entire store. Anyone else need First Class Mail Package all the time instead of First Class Mail Letter?

 

It shows as letter for items under 3.5 ounces. The module isn't set up to set the thresholds from the admin panel. They are set in the script.

 

Find the following line and change 3.5 to 0. I tested on my site with a 2 ounce item and it works.

 

if($this->pounds == 0 && $this->ounces <= 3.5){

 

change to:

 

if($this->pounds == 0 && $this->ounces <= 0){

 

I didn't write the script and I don't particularly like it. I just modified what I could to get it working until something better comes along. I'm sure there is an entire snippet of code that can be removed, but just changing the weight threshold works just as well.

 

I would really like to see a working update to 4.3.2 as this was the module I was using too and I did like that one. I tried, but couldn't get it working.

Share this post


Link to post
Share on other sites

I would really like to see a working update to 4.3.2 as this was the module I was using too and I did like that one. I tried, but couldn't get it working.

 

darn shame dear.. I like that version also..

 

No one else is experiencing issues with domestic rates still??? I still do not have domestic working..


Debbie D
Franklin County, VA "Moonshine Capitol of the World"
osCmax Mobile Template oscmaxtemplates.com

Share this post


Link to post
Share on other sites

I waded thru the fixes and have functional carts Except there is no First Class Package option available.... which effectively shuts down the entire store. Anyone else need First Class Mail Package all the time instead of First Class Mail Letter?

 

If I'm understanding you correctly, you're like me who doesn't use First Class Letter and only uses First Class Parcel? If this is so, in catalog/includes/modules/shipping/usps.php FIND:

 

$this->FirstClassMailType = 'LETTER';   //OPTIONS: 'LETTER, 'FLAT', 'PARCEL

 

And change LETTER to PARCEL.

 

Edit: Now if anyone can figure out why I can't get First Class International, I'd like that. All the other internationals from Priority above work for me. Domestic is fine too. Thanks.

Edited by a.forever

Share this post


Link to post
Share on other sites

Me too.

 

I would really like to see a working update to 4.3.2 as this was the module I was using too and I did like that one. I tried, but couldn't get it working.

Share this post


Link to post
Share on other sites

Concerning the htmlspecialchars_decode function malfunction reported by others.

 

This function also failed for me in the "USPS Methods 5.0.1 - bug fix." version.

 

I did some research and found that the function is not supported by all versions of PHP. My host is using PHP version 5.2.13, which apparently doesn't support it.

 

I fixed it by replacing the following code:

 

$service = htmlspecialchars_decode($regs[1]);

 

with this code:

 

if ( !function_exists('htmlspecialchars_decode') )

{

function htmlspecialchars_decode($text)

{

return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));

}

}

$service = htmlspecialchars_decode($regs[1]);

 

This code came from a user contribution on the page http://php.net/manual/en/function.htmlspecialchars-decode.php

 

It works! No other changes were made other than copying the edited contribution file to the server.

 

Many thanks! I will try the USPS Methods 5.0.1 again and see if that fixes it. I believe I was all the way there except for this error. Although, First Class Mail International was still not showing up, but maybe I hadn't checked the right thing in the configuration panel.

 

Will let you know later after I've tried this. Again - thanks!

Share this post


Link to post
Share on other sites

I would really like to see a working update to 4.3.2 as this was the module I was using too and I did like that one. I tried, but couldn't get it working.

 

I offered up what worked for me for method 4-3-2 over here

 

It works for me after several days testing. But please read my signature line comments, I am not an expert in this stuff.


I am not a professional webmaster or PHP coder by background or training but I will try to help as best I can.

I remember what it was like when I first started with osC. It can be overwhelming.

However, I strongly recommend considering hiring a professional for extensive site modifications, site cleaning, etc.

There are several good pros here on osCommerce. Look around, you'll figure out who they are.

Share this post


Link to post
Share on other sites

So I installed v5.0.1 with all the bug fixes and everything onto another host and found that it works 100% perfectly. No clue why. :blush:

Share this post


Link to post
Share on other sites

originally installed (into oscMAX - everything here is in the 2.0.25 version straight from oscMAX except I can't find the sql reference noted at the very bottom here):

 

USPS Methods 4.3.2 Fix international services 8 Jan 2010

 

This version had insurance options & weight thresholds and you could set the handling for international seperate from domestic

 

So I am using the new 5.0.1 files with the latest fix file and these modifications below were in and working for a long time for me, but I continue to have issues with domestic rates not being pulled and/or blank white pages on domestic depending on the settings in the admin

 

So right now the only difference I can see if the sql.. I'm open to suggestions if I should give it a swing or not.. I can do it on my dev cart if needed..

 

=========================

 

catalog\admin\includes\functions\general.php

this change is exact from the 4.3.2 readme

 

FIND:

////
// Alias function for module configuration keys
 function tep_mod_select_option($select_array, $key_name, $key_value) {
   reset($select_array);
   while (list($key, $value) = each($select_array)) {
     if (is_int($key)) $key = $value;
     $string .= '<br><input type="radio" name="configuration[' . $key_name . ']" value="' . $key . '"';
     if ($key_value == $key) $string .= ' CHECKED';
     $string .= '> ' . $value;
   }

   return $string;
 }

 

 

ADD AFTER:

		// USPS Methods - credit to Greg Deeth
	  function tep_cfg_select_multioption($select_array, $key_value, $key = '') {
	    for ($i=0; $i<sizeof($select_array); $i++) {
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="checkbox" name="' . $name . '" value="' . $select_array[$i] . '"';
	      $key_values = explode( ", ", $key_value);
	      if ( in_array($select_array[$i], $key_values) ) $string .= ' CHECKED';
	      $string .= '> ' . $select_array[$i];
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    return $string;
	  }
	  function tep_cfg_multiinput_list($select_array, $key_value, $key = '') {
	    $key_values = explode( ", ", $key_value);

	    for ($i=0; $i<sizeof($select_array); $i++) {
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="text" name="' . $name . '" value="' . $key_values[$i] . '"> ' . $select_array[$i];
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    return $string;
	  }
	  function tep_cfg_multiinput_duallist_oz($select_array, $key_value, $key = '') {
	    $key_values = explode( ", ", $key_value);
	    $string .= '<center>';
	    for ($i=0; $i<sizeof($select_array); $i++) {
		$current_key_value = current($key_values);
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>oz</i>';
		$string .= ' <b><</b> ' . $select_array[$i] . ' <u><b><</b></u>';
		next($key_values);
		$current_key_value = current($key_values);
		$string .= '<input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>oz</i>';
		next($key_values);
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    $string .= '</center>';
	    return $string;
	  }
	  function tep_cfg_multiinput_duallist_lb($select_array, $key_value, $key = '') {
	    $key_values = explode( ", ", $key_value);
	    $string .= '<center>';
	    for ($i=0; $i<sizeof($select_array); $i++) {
		$current_key_value = current($key_values);
	      $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
	      $string .= '<br><input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>lbs</i>';
		$string .= ' <b><</b> ' . $select_array[$i] . ' <u><b><</b></u>';
		next($key_values);
		$current_key_value = current($key_values);
		$string .= '<input type="text" name="' . $name . '" size="3" value="' . $current_key_value . '"><i>lbs</i>';
		next($key_values);
	    }
	    $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
	    $string .= '</center>';
	    return $string;
	  }

=====================

catalog\includes\functions\general.php

this change is exact from the 4.3.2 readme

 

 

FIND:

 

// Wrapper function for round()
 function tep_round($number, $precision) {
   if (strpos($number, '.') && (strlen(substr($number, strpos($number, '.')+1)) > $precision)) {
     $number = substr($number, 0, strpos($number, '.') + 1 + $precision + 1);

     if (substr($number, -1) >= 5) {
       if ($precision > 1) {
         $number = substr($number, 0, -1) + ('0.' . str_repeat(0, $precision-1) . '1');
       } elseif ($precision == 1) {
         $number = substr($number, 0, -1) + 0.1;
       } else {
         $number = substr($number, 0, -1) + 1;
       }
     } else {
       $number = substr($number, 0, -1);
     }
   }

   return $number;
 }

ADD AFTER:

 

////
// Round up function for non whole numbers by GREG DEETH
// The value for the precision variable determines how many digits after the decimal and rounds the last digit up to the next value
// Precision = 0 -> xx.xxxx = x+
// Precision = 1 -> xx.xxxx = xx.+
// Precision = 2 -> xx.xxxx = xx.x+
 function tep_round_up($number, $precision) {
$number_whole = '';
$num_left_dec = 0;
$num_right_dec = 0;
$num_digits = strlen($number);
$number_out = '';
$i = 0;
while ($i + 1 <= strlen($number))
{
	$current_digit = substr($number, $i, ($i + 1) - $num_digits);
	if ($current_digit == '.') {
		$i = $num_digits + 1;
		$num_left_dec = strlen($number_whole);
		$num_right_dec = ($num_left_dec + 1) - $num_digits;
	} else {
		$number_whole = $number_whole . $current_digit;
		$i = $i + 1;
	}
}
if ($num_digits > 3 && $precision < ($num_digits - $num_left_dec - 1) && $precision >= 0) {
	$i = $precision;
	$addable = 1;
	while ($i > 0) {
		$addable = $addable * .1;
		$i = $i - 1;
	} 
	$number_out = substr($number, 0, $num_right_dec + $precision) + $addable;
} else {
	$number_out = $number;
}
return $number_out;
 }

==============================

 

Addiitonally this mod states if you have version 2.9 or earlier you need to:

 

catalog/admin/modules.php

 

*****************************************

Find This code somewhere around line 43:

*****************************************

 

if (tep_not_null($action)) {
   switch ($action) {
     case 'save':
       while (list($key, $value) = each($HTTP_POST_VARS['configuration'])) {

***********************************************

INSERT THE FOLLOWING CODE AFTER THE ABOVE LINE:

***********************************************

 

		  if( is_array( $value ) ){
         $value = implode( ", ", $value);
	  $value = ereg_replace (", --none--", "", $value);
	    }

=================================

 

Lastly the readme states:

if upgrading from a version prior to 2.9.2 with tarheit's fix for upper case USPS names,

run the SQL

 

 

UPDATE configuration
SET configuration_value = UPPER(configuration_value),
   set_function='tep_cfg_select_multioption(array(''GLOBAL EXPRESS'', ''GLOBAL EXPRESS NON-DOC RECT'', ''GLOBAL EXPRESS NON-DOC NON-RECT'', ''EXPRESS MAIL INT'', ''EXPRESS MAIL INT FLAT RATE ENV'', ''PRIORITY MAIL INT'', ''PRIORITY MAIL INT FLAT RATE ENV'', ''PRIORITY MAIL INT FLAT RATE BOX'', ''FIRST-CLASS MAIL INT''), '
WHERE configuration_key='MODULE_SHIPPING_USPS_TYPES_INTL';
UPDATE configuration
SET configuration_value = UPPER(configuration_value),
   set_function='tep_cfg_select_multioption(array(''EXPRESS'', ''PRIORITY'', ''FIRST CLASS'', ''PARCEL'', ''BPM'', ''LIBRARY'', ''MEDIA''), '
WHERE configuration_key='MODULE_SHIPPING_USPS_TYPES';

 

I just read through your post. I have no answer as to the blank page issue. BUT, the return response to #3 in the first one is first class - you didn't have media checked. No response is because first class does not apply to parcels over 13 ounces and you had a 16 oz input.

 

Same with #4. All came back except first class because of the weight threshold. Sorry it doesn't help with blank pages, but it should help explain the failure to respond to the shipping method.

Share this post


Link to post
Share on other sites

I offered up what worked for me for method 4-3-2 over here

 

It works for me after several days testing. But please read my signature line comments, I am not an expert in this stuff.

 

In your testing, did you verify that you can choose from the different shipping methods and that they will APPLY at checkout. Do the shipping methods revert during the final stages of checkout? Just showing up is one thing. That was easy. Getting them to work through the full checkout process has been a completely different nightmare. I'll play with it and see what I get.

Share this post


Link to post
Share on other sites

If I'm understanding you correctly, you're like me who doesn't use First Class Letter and only uses First Class Parcel? If this is so, in catalog/includes/modules/shipping/usps.php FIND:

 

$this->FirstClassMailType = 'LETTER';   //OPTIONS: 'LETTER, 'FLAT', 'PARCEL

 

And change LETTER to PARCEL.

 

Edit: Now if anyone can figure out why I can't get First Class International, I'd like that. All the other internationals from Priority above work for me. Domestic is fine too. Thanks.

 

What was your test weight? If it was over 64 ounces, then that would be one reason 1st class international wouldn't work. Just a thought.

Share this post


Link to post
Share on other sites

I'm using a bit older version, (4.3.0 ALPHA) and never upgraded because everything worked for me (international and flat rate boxes). The changes I made to fix the issue are:

 

after function usps(){

// Support for php without htmlspecialchars_decode()
if ( !function_exists('htmlspecialchars_decode') )
{
 function htmlspecialchars_decode($text)
 {
    return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  } 
}

 

Replace:

  $service = ereg('<MailService>(.*)</MailService>', $response[$i], $regs);
  $service = $regs[1];

 

with:

  $service = ereg('<MailService>(.*)</MailService>', $response[$i], $regs);
  $service = htmlspecialchars_decode($regs[1]);
  $service = preg_replace('/\<sup\>\&reg;\<\/sup\>/', '', $service);

 

Replace:

  $service = ereg('<SvcDescription>(.*)</SvcDescription>', $services[$i], $regs);
  $service = $regs[1];

 

with:

  $service = ereg('<SvcDescription>(.*)</SvcDescription>', $services[$i], $regs);
  $service = htmlspecialchars_decode($regs[1]);
  $service = preg_replace('/\<sup\>\&reg;\<\/sup\>/', '', $service);

 

 

You can do a preg_replace to '<sup>®</sup>' but this will cause issues elsewhere in the code where it checks to see if you are allowing the given shipping method. If you do leave the '<sup>®</sup>' in, you'll have to make sure you strip it before doing these checks.

 

-Tim

Share this post


Link to post
Share on other sites

Why don't the recent versions allow the customer to choose domestic priority flat-rate boxes and envelopes?

 

I have 5.0.1 installed with almost everything working. The check-boxes don't show up in the module settings, so I just commented out the options I don't want. I am unable to get first-class international to display.

Share this post


Link to post
Share on other sites

Those having blank white page issues, please see:

http://forums.oscommerce.com/topic/369371-usps-methods-4-3-2-2011-jan-2-issue/page__view__findpost__p__1556789

I have it working now

 

loveleaf: you need to look back a few pages for the change that needs to be made to the database

 

tarheit: I will try your changes in my dev cart and see if that works also, I much preferred that version!

 

fulluvscents: It didnt matter what I had set.. I changed wieghts also and stil issues this new one is WAY different than yours!! I dont understand most of the differences, but it worked for me..


Debbie D
Franklin County, VA "Moonshine Capitol of the World"
osCmax Mobile Template oscmaxtemplates.com

Share this post


Link to post
Share on other sites

Tim, I made your changes to the 4.3.0 ALPHA and oops.. something is wrong :)

 

Internation spitting error

An error occured with the USPS shipping calculations.

If you prefer to use USPS as your shipping method, please contact the store owner

 

Then domestic looks like:

 

usps_oops.jpg

 

Any possability of you sending me your file??


Debbie D
Franklin County, VA "Moonshine Capitol of the World"
osCmax Mobile Template oscmaxtemplates.com

Share this post


Link to post
Share on other sites

I got the flat rate options working. But, I'm not uploading it to contributions yet because I am working on getting this merged with 4.3.2 and want to get that done (or kill myself trying) before I upload again. I also removed the redundant international options to lighten the load. Since ALL flat rate envelopes are the same cost, there is really no sense in keeping more than 1 option - flat rate envelope.

 

here's the new code for usps.php with flat rate options:

 

<?php
/*
$Id: usps.php 3.0 2008-03-01 04:59:07Z user $
 ++++ Original contribution by Brad Waite and Fritz Clapp ++++
 ++++ incorporating USPS revisions to service names ++++
 Copyright 2008 osCommerce
 Released under the GNU General Public License
*/
//LAST UPDATED: May 28th, 2008 by Greg Deeth
//Modified by Greg Deeth April 30, 2008 to use API v.3.0
//Modified by Greg Deeth May 12, 2008 for API Change
//Please refer to http://www.usps.com/webtools/_pdf/Rate-Calculators-v1-2.pdf for more information on RateV3 syntax.
 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);
// removed per alternative //               $this->testing = 0;
     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;
       }
     }

//Modified Jan 4 2011 by Fulluv Scents to include only standard domestic shipping options
     $this->types = array('Express Mail' => 'Express Mail',
                          'Express Mail Flat Rate Envelope' => 'Express Mail Flat Rate Envelope',
                          'Priority Mail' => 'Priority Mail',
                          'Priority Mail Flat Rate Envelope' => 'Priority Mail Flat Rate Envelope',
                          'Priority Mail Small Flat Rate Box' => 'Priority Mail Small Flat Rate Box',
                          'Priority Mail Medium Flat Rate Box' => 'Priority Mail Medium Flat Rate Box',
                          'Priority Mail Large Flat Rate Box' => 'Priority Mail Large Flat Rate Box',
                          'Parcel Post' => 'Parcel Post',
                          'First-Class Mail' => 'First-Class Mail',
                          'Media Mail' => 'Media Mail');


//Modified Jan 4 2011 by Fulluv Scents to remove redundant international options
//******************************************************************* */
//Added by Greg Deeth on May 12th, 2008
//INTERNATIONAL MAIL OPTIONS
//Change the values to the option you would like
//FIRST CLASS MAIL INTERNATIONAL OPTION:
//        $this->FirstClassIntType = 'Letters';                           //OPTIONS: 'Letters', 'Large Envelope', 'Package'
//PRIORITY FLAT-RATE BOX INTERNATIONAL OPTION:
//        $this->PriorityFlatRateBoxType = 'Flat-Rate Box';               //OPTIONS: 'Flat-Rate Box', 'Large Flat-Rate Box'
//****************************************************************** */

//Modified Jan 3 2011 by Fulluv Scents to update International Options
   $this->intl_types = array(
       'Global Express' => 'Global Express Guaranteed (GXG)**',
       'Global Express Non-Doc Rect' => 'Global Express Guaranteed Non-Document Rectangular',
       'Global Express Non-Doc Non-Rect' => 'Global Express Guaranteed Non-Document Non-Rectangular',
       'USPS GXG Envelopes' => 'USPS GXG Envelopes**',
       'Express Mail Int' => 'Express Mail International',
       'Express Mail Int Flat Rate Env' => 'Express Mail International Flat Rate Envelope',
       'Priority Mail International' => 'Priority Mail International',
       'Priority Mail Int Flat Rate Lrg Box' => 'Priority Mail International Large Flat Rate Box',
       'Priority Mail Int Flat Rate Med Box' => 'Priority Mail International Medium Flat Rate Box',
       'Priority Mail Int Flat Rate Small Box' => 'Priority Mail International Small Flat Rate Box**',
       'Priority Mail Int Flat Rate Env' => 'Priority Mail International Flat Rate Envelope**',
       'First-Class Mail Int Lrg Env' => 'First-Class Mail International Large Envelope**',
       'First-Class Mail Int Package' => 'First-Class Mail International Package**',
       'First-Class Mail Int Letter' => 'First-Class Mail International Letter**'
       );


     $this->countries = $this->country_list();
   }
// class methods
   function quote($method = '') {
     global $order, $shipping_weight, $shipping_num_boxes, $transittime;
     if ( tep_not_null($method) && (isset($this->types[$method]) || in_array($method, $this->intl_types)) ) {
       $this->_setService($method);
     }
// usps doesnt accept zero weight
// Modified by Greg Deeth on May 27th 2008
     $shipping_weight = ($shipping_weight < 0.0625 ? 0.0625 : $shipping_weight);
     $shipping_pounds = floor ($shipping_weight);
     $shipping_ounces = (16 * ($shipping_weight - floor($shipping_weight)));
     $this->_setWeight($shipping_pounds, $shipping_ounces);
// Added by Kevin Chen (kkchen@uci.edu); Fixes the Parcel Post Bug July 1, 2004
// Refer to http://www.usps.com/webtools/htm/Domestic-Rates.htm documentation
// Thanks Ryan
// End Kevin Chen July 1, 2004

     if (in_array('Display weight', explode(', ', MODULE_SHIPPING_USPS_OPTIONS)))
       if (!function_exists('round_up')) { function round_up($valueIn, $places=0) {
       if ($places < 0) { $places = 0; }
       $mult = pow(10, $places);
       return (ceil($valueIn * $mult) / $mult);
         }
//      if (in_array('Display weight', explode(', ', MODULE_SHIPPING_USPS_OPTIONS))) {
//        function round_up($valueIn, $places=0) {
//              if ($places < 0) { $places = 0; }
//              $mult = pow(10, $places);
//              return (ceil($valueIn * $mult) / $mult);
//        }
       $shiptitle = ' (' . $shipping_num_boxes . ' x ' . $shipping_weight . 'lbs)' . ' (' . round_up($shipping_pounds, 4) . 'lbs, ' . round_up($shipping_ounces, 4) . 'oz)';
     } else {
       $shiptitle = '';
     }
     $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 . $shiptitle);
         $methods = array();
         $size = sizeof($uspsQuote);
         for ($i=0; $i<$size; $i++) {
           list($type, $cost) = each($uspsQuote[$i]);
           $title = ((isset($this->types[$type])) ? $this->types[$type] : $type);
           if(in_array('Display transit time', explode(', ', MODULE_SHIPPING_USPS_OPTIONS)))    $title .= $transittime[$type];
           $methods[] = array('id' => $type,
                              'title' => $title,
                              '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())");
//Modified Jan 4 2011 by Fulluv Scents to include only standard domestic shipping options
     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_TYPES', 'Express Mail, Express Mail Flat Rate Envelope, Priority Mail, Priority Mail Flat Rate Envelope, Priority Mail Small Flat Rate Box, Priority Mail Medium Flat Rate Box, Priority Mail Large Flat Rate Box, Parcel Post, First-Class Mail, Media Mail', 'Select the domestic services to be offered:', '6', '0', 'tep_cfg_select_multioption(array(\'Express Mail\', \'Express Mail Flat Rate Envelope\', \'Priority Mail\', \'Priority Mail Flat Rate Envelope\', \'Priority Mail Small Flat Rate Box\', \'Priority Mail Medium Flat Rate Box\', \'Priority Mail Large Flat Rate Box\',  \'Parcel Post\', \'First-Class Mail\',\'Media Mail\'), ', now())");
//Modified Jan 3 2011 by Fulluv Scents to update International Options
     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 ('Int\'l Shipping Methods', 'MODULE_SHIPPING_USPS_TYPES_INTL',
     'Global Express, Global Express Non-Doc Rect, Global Express Non-Doc Non-Rect, USPS GXG Envelopes, Express Mail Int, Express Mail Int Flat Rate Env, Priority Mail International, Priority Mail Int Flat Rate Env, Priority Mail Int Flat Rate Small Box, Priority Mail Int Flat Rate Med Box, Priority Mail Int Flat Rate Lrg Box, First-Class Mail Int Lrg Env, First-Class Mail Int Package, First-Class Mail Int Letter',
  'Select the international services to be offered:', '6', '0', 'tep_cfg_select_multioption(
     array(\'Global Express\', \'Global Express Non-Doc Rect\', \'Global Express Non-Doc Non-Rect\', \'USPS GXG Envelopes\', \'Express Mail Int\', \'Express Mail Int Flat Rate Env\', \'Priority Mail International\', \'Priority Mail Int Flat Rate Env\', \'Priority Mail Int Flat Rate Small Box\', \'Priority Mail Int Flat Rate Med Box\', \'Priority Mail Int Flat Rate Lrg Box\', \'First-Class Mail Int Lrg Env\', \'First-Class Mail Int Package\', \'First-Class Mail Int Letter\'), ',  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 ('USPS Options', 'MODULE_SHIPPING_USPS_OPTIONS', 'Display weight, Display transit time', 'Select from the following the USPS options.', '6', '0', 'tep_cfg_select_multioption(array(\'Display weight\', \'Display transit time\'), ', 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', 'MODULE_SHIPPING_USPS_OPTIONS', 'MODULE_SHIPPING_USPS_TYPES', 'MODULE_SHIPPING_USPS_TYPES_INTL');
   }
   function round_up($valueIn, $places=0) {
       if ($places < 0) { $places = 0; }
       $mult = pow(10, $places);
       return (ceil($valueIn * $mult) / $mult);
   }
   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, $transittime;
     if(in_array('Display transit time', explode(', ', MODULE_SHIPPING_USPS_OPTIONS))) $transit = TRUE;
//RateRequest changed to RateV3Request by Greg Deeth April 30, 2008
     if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) {
       $request  = '<RateV3Request 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);
       $allowed_types = explode(", ", MODULE_SHIPPING_USPS_TYPES);
       while (list($key, $value) = each($this->types)) {
         if ( !in_array($key, $allowed_types) ) continue;
/********************************************************************** */
//DOMESTIC MAIL OPTIONS
//For Options list, go to page 9 of document: http://www.usps.com/webtools/_pdf/Rate-Calculators-v1-2.pdf
               //$this->size ='Regular'  //Set default value of Regular unless different value is applied below
//FIRST CLASS MAIL OPTIONS
               if ($key == 'First-Class Mail'){
                       //WEIGHT THRESHOLD OPTIONS (LETTER is changed to FLAT automatically by USPS when over 3.5oz)
                       if($this->pounds == 0 && $this->ounces <= 0){
                               //IF WEIGHT MATCHES STATEMENT, THEN:
                               //IF YOU DO/DON'T WANT MACHINABLE THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"
                       $this->machinable = 'true';                     //OPTIONS: 'true', 'false'
                               //IF YOU DO/DON'T WANT CONTAINER THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"                  
                               $this->FirstClassMailType = 'LETTER';   //OPTIONS: 'LETTER, 'FLAT', 'PARCEL'
                       }
               else{
                               //IF WEIGHT DOES NOT MATCH STATEMENT, THEN:
                       //IF YOU DO/DON'T WANT MACHINABLE THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"
                       $this->machinable = 'false';                    //OPTIONS: 'true', 'false'
                               //IF YOU DO/DON'T WANT CONTAINER THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"                  
                               $this->FirstClassMailType = 'FLAT';     //OPTIONS: 'LETTER, 'FLAT', 'PARCEL'
               }
               }
//PRIORITY MAIL OPTIONS
               if ($key == 'Priority Mail'){
                       $this->container = '';                          //OPTIONS: '', 'FLAT RATE BOX', 'FLAT RATE ENVELOPE'
                       $this->size = 'REGULAR';
               }
               if ($key == 'Priority Mail Flat Rate Envelope'){
                       $key = 'Priority Mail'; //DO NOT CHANGE
                       $this->container = 'FLAT RATE ENVELOPE';        //OPTIONS: 'FLAT RATE ENVELOPE', 'FLAT RATE BOX'
                       $this->size = 'REGULAR';
               }
               if ($key == 'Priority Mail Small Flat Rate Box'){
                       $key = 'Priority Mail'; //DO NOT CHANGE
                       $this->container = 'SM FLAT RATE BOX';             //OPTIONS: 'FLAT RATE BOX', 'FLAT RATE ENVELOPE'
                       $this->size = 'LARGE';
               }
               if ($key == 'Priority Mail Medium Flat Rate Box'){
                       $key = 'Priority Mail'; //DO NOT CHANGE
                       $this->container = 'MD FLAT RATE BOX';             //OPTIONS: 'FLAT RATE BOX', 'FLAT RATE ENVELOPE'
                       $this->size = 'LARGE';
               }
               if ($key == 'Priority Mail Large Flat Rate Box'){
                       $key = 'Priority Mail'; //DO NOT CHANGE
                       $this->container = 'LG FLAT RATE BOX';             //OPTIONS: 'FLAT RATE BOX', 'FLAT RATE ENVELOPE'
                       $this->size = 'LARGE';
               }
//EXPRESS MAIL OPTIONS
               if ($key == 'Express Mail'){
                       $this->container = '';          //OPTIONS: '', 'FLAT RATE ENVELOPE'
                       $this->size = 'REGULAR';                                //OPTIONS: 'REGULAR', 'LARGE'
               }
               if ($key == 'Express Mail Flat Rate Envelope'){
                       $this->container = 'FLAT RATE ENVELOPE';          //OPTIONS: '', 'FLAT RATE ENVELOPE'
                       $this->size = 'REGULAR';                                //OPTIONS: 'REGULAR', 'LARGE'
               }
//PARCEL POST OPTIONS
               if ($key == 'Parcel Post'){
                       //WEIGHT THRESHOLD OPTIONS (DEFAULT MACHINABLE: WEIGHT<70lbs WEIGHT CANNOT EXCEED 70lbs ANYWAY)
                       if($this->pounds <= 70 && $this->ounces <= 0){
                               //IF WEIGHT MATCHES STATEMENT, THEN:
                               //IF YOU DO/DON'T WANT MACHINABLE THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"
                       $this->machinable = 'true';                     //OPTIONS: 'true', 'false'
                               //IF YOU DO/DON'T WANT SIZE THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"                       
                               $this->size = 'REGULAR';                        //OPTIONS: 'REGULAR', 'LARGE', 'OVERSIZE'
                       }
               else{
                               //IF WEIGHT DOES NOT MATCH STATEMENT, THEN:
                       //IF YOU DO/DON'T WANT MACHINABLE THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"
                       $this->machinable = 'true';                     //OPTIONS: 'true', 'false'
                               //IF YOU DO/DON'T WANT SIZE THRESHOLD, REMOVE/ADD COMMENT MARKS AT BEGINNING OF LINE "//"                       
                               $this->size = 'REGULAR';                                //OPTIONS: 'REGULAR', 'LARGE', 'OVERSIZE'
               }
               }
//BPM OPTIONS
               if ($key == 'Bound Printed Matter'){
                       $this->size = 'REGULAR';                                //OPTIONS: 'REGULAR', 'LARGE'
               }
//MEDIA MAIL OPTIONS
               if ($key == 'Media Mail'){
                       $this->size = 'REGULAR';                                //OPTIONS: 'REGULAR, 'LARGE'
               }
//LIBRARY MAIL OPTIONS
/*************************************************************************** */
         $request .= '<Package ID="' . $services_count . '">' .
                     '<Service>' . $key . '</Service>' .
                           '<FirstClassMailType>' . $this->FirstClassMailType . '</FirstClassMailType>' .
                     '<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>';
         if($transit){
           $transitreq  = 'USERID="' . MODULE_SHIPPING_USPS_USERID .
                        '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">' .
                        '<OriginZip>' . STORE_ORIGIN_ZIP . '</OriginZip>' .
                        '<DestinationZip>' . $dest_zip . '</DestinationZip>';
           switch ($key) {
             case 'Express Mail':  $transreq[$key] = 'API=ExpressMail&XML=' .
                              urlencode( '<ExpressMailRequest ' . $transitreq . '</ExpressMailRequest>');
                              break;
             case 'Priority Mail': $transreq[$key] = 'API=PriorityMail&XML=' .
                              urlencode( '<PriorityMailRequest ' . $transitreq . '</PriorityMailRequest>');
                              break;
             case 'Parcel Post':   $transreq[$key] = 'API=StandardB&XML=' .
                              urlencode( '<StandardBRequest ' . $transitreq . '</StandardBRequest>');
                              break;
             default:         $transreq[$key] = '';
                              break;
           }
         }
         $services_count++;
       }
       $request .= '</RateV3Request>'; //'</RateRequest>'; //Changed by Greg Deeth April 30, 2008
       $request = 'API=RateV3&XML=' . urlencode($request);
     } else {
       $request  = '<IntlRateRequest USERID="' . MODULE_SHIPPING_USPS_USERID . '" PASSWORD="' . MODULE_SHIPPING_USPS_PASSWORD . '">' .
                   '<Package ID="0">' .
                   '<Pounds>' . $this->pounds . '</Pounds>' .
                   '<Ounces>' . round ($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'; //'stg-production.shippingapis.com'; // or  stg-secure.shippingapis.com //'production.shippingapis.com';
                          $api_dll = 'shippingapi.dll'; //'shippingapi.dll';
                          break;
       case 'test':
       default:           $usps_server = 'stg-production.shippingapis.com'; //Fixed by Greg Deeth April 30, 2008
                          $api_dll = 'shippingapitest.dll'; //'shippingapi.dll'; //Fixed by Greg Deeth April 30, 2008
                          break;
     }
     $body = '';
     if (!class_exists('httpClient')) {
       include('includes/classes/http_client.php');
     }
     $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();
 mail('user@localhost.com','USPS rate quote response',$body,'From: <user@localhost.com>');
       if ($transit && is_array($transreq) && ($order->delivery['country']['id'] == STORE_COUNTRY)) {
         while (list($key, $value) = each($transreq)) {
           if ($http->Get('/' . $api_dll . '?' . $value)) $transresp[$key] = $http->getBody();
         }
       }
       $http->Disconnect();
     } else {
       return false;
     }

//Modified Jan 4 2011 by Fulluv Scents to remove registered trademarks
$body = str_replace('&lt;sup&gt;&amp;reg;&lt;/sup&gt;', '', $body);
$body = str_replace('&lt;sup&gt;&amp;trade;&lt;/sup&gt;', '', $body);

     $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();
     $rates_sorter = 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], '<Rate>')) {
           $service = ereg('<MailService>(.*)</MailService>', $response[$i], $regs);

//new code
 		$service = htmlspecialchars_decode($regs[1]);
 		$service = preg_replace('/\<sup\>\&reg;\<\/sup\>/', '<sup>®</sup>', $service);
//end new code

                       //$service = $regs[1];

           $postage = ereg('<Rate>(.*)</Rate>', $response[$i], $regs);
           $postage = $regs[1];
               $rates[] = array($service => $postage);
           $rates_sorter[] = $postage;
           if ($transit) {
             switch ($service) {
               case 'Express Mail':     $time = ereg('<MonFriCommitment>(.*)</MonFriCommitment>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 1 - 2 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } else {
                                     $time = 'Tomorrow by ' . $time;
                                   }
                                   break;
               case 'Express Mail Flat Rate Envelope':     $time = ereg('<MonFriCommitment>(.*)</MonFriCommitment>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 1 - 2 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } else {
                                     $time = 'Tomorrow by ' . $time;
                                   }
                                   break;
               case 'Priority Mail':    $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } elseif ($time == '1') {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
                                   } else {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   }
                                   break;
               case 'Priority Mail Flat Rate Envelope':    $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } elseif ($time == '1') {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
                                   } else {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   }
                                   break;
               case 'Priority Mail Small Flat Rate Box':    $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } elseif ($time == '1') {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
                                   } else {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   }
                                   break;
               case 'Priority Mail Medium Flat Rate Box':    $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } elseif ($time == '1') {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
                                   } else {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   }
                                   break;
               case 'Priority Mail Large Flat Rate Box':    $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } elseif ($time == '1') {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
                                   } else {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   }
                                   break;
               case 'Parcel Post':      $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
                                   $time = $tregs[1];
                                   if ($time == '' || $time == 'No Data') {
                                     $time = 'Estimated 2 - 9 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   } elseif ($time == '1') {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
                                   } else {
                                     $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   }
                                   break;
//Modified Jan 4 2011 by Fulluv Scents to show shipping time for all first class methods
               case 'First-Class Mail Letter': $time = 'Estimated 1 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   break;
               					default:            $time = '';
                                   break;
               case 'First-Class Mail Large Envelope': $time = 'Estimated 1 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   break;
               					default:            $time = '';
                                   break;
               case 'First-Class Mail Package': $time = 'Estimated 1 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   break;
               					default:            $time = '';
                                   break;
               case 'Media Mail':              $time = 'Estimated 2 - 9 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   break;
               					default:            $time = '';
                                   break;
               case 'Bound Printed Matter':                    $time = 'Estimated 2 - 9 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                   break;
               					default:            $time = '';
                                   break;
             }
             if ($time != '') $transittime[$service] = ': ' . $time . '';
           }
         }
       }
     } else {
       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);
       } else {
         $body = $response[0];
         $services = array();
         while (true) {
           if ($start = strpos($body, '<Service ID=')) {
             $body = substr($body, $start);
             $end = strpos($body, '</Service>');
             $services[] = substr($body, 0, $end+10);
             $body = substr($body, $end+9);
           } else {
             break;
           }
         }
         $allowed_types = array();
         foreach( explode(", ", MODULE_SHIPPING_USPS_TYPES_INTL) as $value ) $allowed_types[$value] = $this->intl_types[$value];

         $size = sizeof($services);

                for ($i=0, $n=$size; $i<$n; $i++) {
           if (strpos($services[$i], '<Postage>')) {

             $service = ereg('<SvcDescription>(.*)</SvcDescription>', $services[$i], $regs);
                 $service = $regs[1];

// Commented out Jan 4 2011 by Fulluv Scents -- This code did not perform the intended function
//new code
//              $service = str_replace('&lt;sup&gt;&amp;reg;&lt;/sup&gt;', '', $service);
//              $service = str_replace('&lt;sup&gt;&amp;trade;&lt;/sup&gt;', '', $service);
//end new code

                         $postage = ereg('<Postage>(.*)</Postage>', $services[$i], $regs);
             $postage = $regs[1];
             $time = ereg('<SvcCommitments>(.*)</SvcCommitments>', $services[$i], $tregs);
             $time = $tregs[1];
             $time = preg_replace('/Weeks$/', MODULE_SHIPPING_USPS_TEXT_WEEKS, $time);
             $time = preg_replace('/Days$/', MODULE_SHIPPING_USPS_TEXT_DAYS, $time);
             $time = preg_replace('/Day$/', MODULE_SHIPPING_USPS_TEXT_DAY, $time);
             if( !in_array($service, $allowed_types) ) continue;
             if (isset($this->service) && ($service != $this->service) ) {
               continue;
             }
                 $rates[] = array($service => $postage);
             $rates_sorter[] = $postage;
             if ($time != '') $transittime[$service] = ' (' . $time . ')';
           }
         }
       }
     }
       //Sort Rates
       asort($rates_sorter);
       $sorted_rates = array();
       foreach (array_keys($rates_sorter) as $key){
               $sorted_rates[] = $rates[$key];
       }
       return ((sizeof($sorted_rates) > 0) ? $sorted_rates : false);
   }
   function country_list() {
     $list = array('AF' => 'Afghanistan',
                   'AL' => 'Albania',
                   'DZ' => 'Algeria',
                   'AD' => 'Andorra',
                   'AO' => 'Angola',
                   'AI' => 'Anguilla',
                   'AG' => 'Antigua and Barbuda',
                   'AR' => 'Argentina',
                   'AM' => 'Armenia',
                   'AW' => 'Aruba',
                   'AU' => 'Australia',
                   'AT' => 'Austria',
                   'AZ' => 'Azerbaijan',
                   'BS' => 'Bahamas',
                   'BH' => 'Bahrain',
                   'BD' => 'Bangladesh',
                   'BB' => 'Barbados',
                   'BY' => 'Belarus',
                   'BE' => 'Belgium',
                   'BZ' => 'Belize',
                   'BJ' => 'Benin',
                   'BM' => 'Bermuda',
                   'BT' => 'Bhutan',
                   'BO' => 'Bolivia',
                   'BA' => 'Bosnia-Herzegovina',
                   'BW' => 'Botswana',
                   'BR' => 'Brazil',
                   'VG' => 'British Virgin Islands',
                   'BN' => 'Brunei Darussalam',
                   'BG' => 'Bulgaria',
                   'BF' => 'Burkina Faso',
                   'MM' => 'Burma',
                   'BI' => 'Burundi',
                   'KH' => 'Cambodia',
                   'CM' => 'Cameroon',
                   'CA' => 'Canada',
                   'CV' => 'Cape Verde',
                   'KY' => 'Cayman Islands',
                   'CF' => 'Central African Republic',
                   'TD' => 'Chad',
                   'CL' => 'Chile',
                   'CN' => 'China',
                   'CX' => 'Christmas Island (Australia)',
                   'CC' => 'Cocos Island (Australia)',
                   'CO' => 'Colombia',
                   'KM' => 'Comoros',
                   'CG' => 'Congo (Brazzaville),Republic of the',
                   'ZR' => 'Congo, Democratic Republic of the',
                   'CK' => 'Cook Islands (New Zealand)',
                   'CR' => 'Costa Rica',
                   'CI' => 'Cote d\'Ivoire (Ivory Coast)',
                   'HR' => 'Croatia',
                   'CU' => 'Cuba',
                   'CY' => 'Cyprus',
                   'CZ' => 'Czech Republic',
                   'DK' => 'Denmark',
                   'DJ' => 'Djibouti',
                   'DM' => 'Dominica',
                   'DO' => 'Dominican Republic',
                   'TP' => 'East Timor (Indonesia)',
                   'EC' => 'Ecuador',
                   'EG' => 'Egypt',
                   'SV' => 'El Salvador',
                   'GQ' => 'Equatorial Guinea',
                   'ER' => 'Eritrea',
                   'EE' => 'Estonia',
                   'ET' => 'Ethiopia',
                   'FK' => 'Falkland Islands',
                   'FO' => 'Faroe Islands',
                   'FJ' => 'Fiji',
                   'FI' => 'Finland',
                   'FR' => 'France',
                   'GF' => 'French Guiana',
                   'PF' => 'French Polynesia',
                   'GA' => 'Gabon',
                   'GM' => 'Gambia',
                   'GE' => 'Georgia, Republic of',
                   'DE' => 'Germany',
                   'GH' => 'Ghana',
                   'GI' => 'Gibraltar',
                   'GB' => 'Great Britain and Northern Ireland',
                   'GR' => 'Greece',
                   'GL' => 'Greenland',
                   'GD' => 'Grenada',
                   'GP' => 'Guadeloupe',
                   'GT' => 'Guatemala',
                   'GN' => 'Guinea',
                   'GW' => 'Guinea-Bissau',
                   'GY' => 'Guyana',
                   'HT' => 'Haiti',
                   'HN' => 'Honduras',
                   'HK' => 'Hong Kong',
                   'HU' => 'Hungary',
                   'IS' => 'Iceland',
                   'IN' => 'India',
                   'ID' => 'Indonesia',
                   'IR' => 'Iran',
                   'IQ' => 'Iraq',
                   'IE' => 'Ireland',
                   'IL' => 'Israel',
                   'IT' => 'Italy',
                   'JM' => 'Jamaica',
                   'JP' => 'Japan',
                   'JO' => 'Jordan',
                   'KZ' => 'Kazakhstan',
                   'KE' => 'Kenya',
                   'KI' => 'Kiribati',
                   'KW' => 'Kuwait',
                   'KG' => 'Kyrgyzstan',
                   'LA' => 'Laos',
                   'LV' => 'Latvia',
                   'LB' => 'Lebanon',
                   'LS' => 'Lesotho',
                   'LR' => 'Liberia',
                   'LY' => 'Libya',
                   'LI' => 'Liechtenstein',
                   'LT' => 'Lithuania',
                   'LU' => 'Luxembourg',
                   'MO' => 'Macao',
                   'MK' => 'Macedonia, Republic of',
                   'MG' => 'Madagascar',
                   'MW' => 'Malawi',
                   'MY' => 'Malaysia',
                   'MV' => 'Maldives',
                   'ML' => 'Mali',
                   'MT' => 'Malta',
                   'MQ' => 'Martinique',
                   'MR' => 'Mauritania',
                   'MU' => 'Mauritius',
                   'YT' => 'Mayotte (France)',
                   'MX' => 'Mexico',
                   'MD' => 'Moldova',
                   'MC' => 'Monaco (France)',
                   'MN' => 'Mongolia',
                   'MS' => 'Montserrat',
                   'MA' => 'Morocco',
                   'MZ' => 'Mozambique',
                   'NA' => 'Namibia',
                   'NR' => 'Nauru',
                   'NP' => 'Nepal',
                   'NL' => 'Netherlands',
                   'AN' => 'Netherlands Antilles',
                   'NC' => 'New Caledonia',
                   'NZ' => 'New Zealand',
                   'NI' => 'Nicaragua',
                   'NE' => 'Niger',
                   'NG' => 'Nigeria',
                   'KP' => 'North Korea (Korea, Democratic People\'s Republic of)',
                   'NO' => 'Norway',
                   'OM' => 'Oman',
                   'PK' => 'Pakistan',
                   'PA' => 'Panama',
                   'PG' => 'Papua New Guinea',
                   'PY' => 'Paraguay',
                   'PE' => 'Peru',
                   'PH' => 'Philippines',
                   'PN' => 'Pitcairn Island',
                   'PL' => 'Poland',
                   'PT' => 'Portugal',
                   'QA' => 'Qatar',
                   'RE' => 'Reunion',
                   'RO' => 'Romania',
                   'RU' => 'Russia',
                   'RW' => 'Rwanda',
                   'SH' => 'Saint Helena',
                   'KN' => 'Saint Kitts (St. Christopher and Nevis)',
                   'LC' => 'Saint Lucia',
                   'PM' => 'Saint Pierre and Miquelon',
                   'VC' => 'Saint Vincent and the Grenadines',
                   'SM' => 'San Marino',
                   'ST' => 'Sao Tome and Principe',
                   'SA' => 'Saudi Arabia',
                   'SN' => 'Senegal',
                   'YU' => 'Serbia-Montenegro',
                   'SC' => 'Seychelles',
                   'SL' => 'Sierra Leone',
                   'SG' => 'Singapore',
                   'SK' => 'Slovak Republic',
                   'SI' => 'Slovenia',
                   'SB' => 'Solomon Islands',
                   'SO' => 'Somalia',
                   'ZA' => 'South Africa',
                   'GS' => 'South Georgia (Falkland Islands)',
                   'KR' => 'South Korea (Korea, Republic of)',
                   'ES' => 'Spain',
                   'LK' => 'Sri Lanka',
                   'SD' => 'Sudan',
                   'SR' => 'Suriname',
                   'SZ' => 'Swaziland',
                   'SE' => 'Sweden',
                   'CH' => 'Switzerland',
                   'SY' => 'Syrian Arab Republic',
                   'TW' => 'Taiwan',
                   'TJ' => 'Tajikistan',
                   'TZ' => 'Tanzania',
                   'TH' => 'Thailand',
                   'TG' => 'Togo',
                   'TK' => 'Tokelau (Union) Group (Western Samoa)',
                   'TO' => 'Tonga',
                   'TT' => 'Trinidad and Tobago',
                   'TN' => 'Tunisia',
                   'TR' => 'Turkey',
                   'TM' => 'Turkmenistan',
                   'TC' => 'Turks and Caicos Islands',
                   'TV' => 'Tuvalu',
                   'UG' => 'Uganda',
                   'UA' => 'Ukraine',
                   'AE' => 'United Arab Emirates',
                   'UY' => 'Uruguay',
                   'UZ' => 'Uzbekistan',
                   'VU' => 'Vanuatu',
                   'VA' => 'Vatican City',
                   'VE' => 'Venezuela',
                   'VN' => 'Vietnam',
                   'WF' => 'Wallis and Futuna Islands',
                   'WS' => 'Western Samoa',
                   'YE' => 'Yemen',
                   'ZM' => 'Zambia',
                   'ZW' => 'Zimbabwe');
     return $list;
   }
 }
?>

Share this post


Link to post
Share on other sites

Okay, well, that wasn't so bad. It hasn't been tested for everything imaginable, BUT, I know that all domestic and international options are working.

 

I removed some internationals. If all flat rate envelopes are the same price, then there is not need to list them all. It's just one flat rate envelope to the customer. Seller can ship in his choice.

 

4.3.2 has been merged with 5.0.1

 

Handling is working but only applies to domestic options. Insurance is working for US and Canada. I don't have other test countries set up, so I haven't checked any others. If you run into bugs, please post the bug and the fix. I'd like to get a bug free fully functional module and get it uploaded to contributions all in one shot.

 

Here's the code for the usps.php file.

 

<?php
/*
$Id: usps.php 5.0 2008-03-01 04:59:07Z user $

+++++ Original contribution by Brad Waite and Fritz Clapp ++++
++++ Revisions and Modifications made by Greg Deeth, 2008 ++++
++++++++++++ Insurance module by Kevin Shelton +++++++++++++++

Copyright 2008 osCommerce
Released under the GNU General Public License
*/
//VERSION: 5.1.0 ALPHA
//LAST UPDATED: January 6th, 2011 by Fulluv Scents
//Please refer to http://www.usps.com/webtools/ for more information on API syntax.


 class usps {
   var $code, $title, $description, $icon, $enabled, $countries;
   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->handling = explode( ", ", MODULE_SHIPPING_USPS_HANDLING_DOMESTIC);

     $this->types = array('Express Mail' => 'Express Mail',
                          'Express Mail Flat Rate Envelope' => 'Express Mail Flat Rate Envelope',
                          'Priority Mail' => 'Priority Mail',
                          'Priority Mail Flat Rate Envelope' => 'Priority Mail Flat Rate Envelope',
                          'Priority Mail Small Flat Rate Box' => 'Priority Mail Small Flat Rate Box',
                          'Priority Mail Medium Flat Rate Box' => 'Priority Mail Medium Flat Rate Box',
                          'Priority Mail Large Flat Rate Box' => 'Priority Mail Large Flat Rate Box',
                          'Parcel Post' => 'Parcel Post',
                          'First-Class Mail' => 'First-Class Mail',
                          'Media Mail' => 'Media Mail');
   $this->intl_types = array(
       'Global Express' => 'Global Express Guaranteed (GXG)**',
       'Global Express Non-Doc Rect' => 'Global Express Guaranteed Non-Document Rectangular',
       'Global Express Non-Doc Non-Rect' => 'Global Express Guaranteed Non-Document Non-Rectangular',
       'USPS GXG Envelopes' => 'USPS GXG Envelopes**',
       'Express Mail Int' => 'Express Mail International',
       'Express Mail Int Flat Rate Env' => 'Express Mail International Flat Rate Envelope',
       'Priority Mail International' => 'Priority Mail International',
       'Priority Mail Int Flat Rate Lrg Box' => 'Priority Mail International Large Flat Rate Box',
       'Priority Mail Int Flat Rate Med Box' => 'Priority Mail International Medium Flat Rate Box',
       'Priority Mail Int Flat Rate Small Box' => 'Priority Mail International Small Flat Rate Box**',
       'Priority Mail Int Flat Rate Env' => 'Priority Mail International Flat Rate Envelope**',
       'First-Class Mail Int Lrg Env' => 'First-Class Mail International Large Envelope**',
       'First-Class Mail Int Package' => 'First-Class Mail International Package**',
       'First-Class Mail Int Letter' => 'First-Class Mail International Letter**'
       );

						      $this->countries = $this->country_list();

       $this->countryinsure = $this->country_maxinsure();
						    }

   function quote($method = '') {
     global $order, $shipping_weight, $shipping_num_boxes, $transittime;

     if ( tep_not_null($method) && (isset($this->types[$method]) || in_array($method, $this->intl_types)) ) {

       $this->_setService($method);
     }

       if (MODULE_SHIPPING_USPS_INSURE_TAX == 'True') {
                 $costperpkg = $order->info['total'] / $shipping_num_boxes;
       }
       else {
                 $costperpkg = $order->info['subtotal'] / $shipping_num_boxes;
       }

       // retrieve the maximum allowed insurance for the destination country and if the package value exceeds it then set package value to the maximum allowed
       $maxins = $this->countryinsure[$order->delivery['country']['iso_code_2']];
       if ($costperpkg > $maxins) $costperpkg = $maxins;
       // if insurance not allowed for destination or insurance is turned off add nothing to shipping cost
       if (($maxins == 0) || (MODULE_SHIPPING_USPS_INSURE == 'False')) {
                 $insurance = 0;
       }
       else if (($order->delivery['country']['iso_code_2'] == 'US') || ($order->delivery['country']['iso_code_2'] == 'CA')){
               if ($costperpkg<=50) {
                       $insurance=MODULE_SHIPPING_USPS_INS1;
               }
               else if ($costperpkg<=100) {
                       $insurance=MODULE_SHIPPING_USPS_INS2;
               }
               else if ($costperpkg<=200) {
                       $insurance=MODULE_SHIPPING_USPS_INS3;
               }
               else if ($costperpkg<=300) {
                       $insurance=MODULE_SHIPPING_USPS_INS4;
               }
               else {
                       $insurance = MODULE_SHIPPING_USPS_INS4 + ((ceil($costperpkg/100) -3) * MODULE_SHIPPING_USPS_INS5);
               }
         }
         else {
               if ($costperpkg<=50) {
                           $insurance=MODULE_SHIPPING_USPS_INS6;
               }
               else if ($costperpkg<=100) {
                           $insurance=MODULE_SHIPPING_USPS_INS7;
               }
                 else if ($costperpkg<=200) {
                           $insurance=MODULE_SHIPPING_USPS_INS8;
               }
                 else if ($costperpkg<=300) {
                           $insurance=MODULE_SHIPPING_USPS_INS9;
               }
               else {
                           $insurance = MODULE_SHIPPING_USPS_INS9 + ((ceil($costperpkg/100) - 3) * MODULE_SHIPPING_USPS_INS10);
               }
       }

     if ($shipping_weight <= 0) {$shipping_weight = 0;}
     $shipping_weight = ($shipping_weight < 0.0625 ? 0.0625 : $shipping_weight);
     $shipping_pounds = floor ($shipping_weight);
     $shipping_ounces = tep_round_up((16 * ($shipping_weight - floor($shipping_weight))), 2);
     $this->_setWeight($shipping_pounds, $shipping_ounces, $shipping_weight);

     if (in_array('Display weight', explode(', ', MODULE_SHIPPING_USPS_OPTIONS))) {
                 $shiptitle = ' (' . $shipping_num_boxes . ' x ' . tep_round_up($shipping_weight, 2) . 'lbs)' . ' (' . $shipping_pounds . 'lbs, ' . $shipping_ounces . 'oz)';
     } else {
       $shiptitle = '';
     }
     $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 . $shiptitle);
         $methods = array();
         $size = sizeof($uspsQuote);
         for ($i=0; $i<$size; $i++) {
           list($type, $cost) = each($uspsQuote[$i]);
           $title = ((isset($this->types[$type])) ? $this->types[$type] : $type);
           if(in_array('Display transit time', explode(', ', MODULE_SHIPPING_USPS_OPTIONS)))    $title .= $transittime[$type];

                       if (MODULE_SHIPPING_DMSTC_INSURANCE_OPTION == 'Force Insurance') {
                               $methods[] = array('id' => $type,
                               'title' => $title,
                               'cost' => ($cost + $insurance + $handling_cost[0]) * $shipping_num_boxes);
                       } else {
               $methods[] = array('id' => $type,
                      			   'title' => $title,
                                  'cost' => ($cost + $handling_cost[0]) * $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, 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())");
     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 ('USPS Options', 'MODULE_SHIPPING_USPS_OPTIONS', 'Display weight, Display transit time', 'Select from the following the USPS options.', '6', '0', 'tep_cfg_select_multioption(array(\'Display weight\', \'Display transit time\'), ', 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, set_function, date_added) values ('Enter the USPS User ID', 'MODULE_SHIPPING_USPS_USERID', 'NONE, 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', 'tep_cfg_multiinput_list(array(\'ID\', \'Password\'), ', 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 ('Domestic Handling Fees', 'MODULE_SHIPPING_USPS_HANDLING_DOMESTIC', '0, 0, 0, 0, 0, 0, 0, 0, 0, 0', 'Add a different handling fee for each shipping type.', '6', '0', 'tep_cfg_multiinput_list(array(\'Express Mail\', \'Express Mail Flat Rate Envelope\', \'Priority Mail\', \'Priority Mail Flat Rate Envelope\', \'Priority Mail Small Flat Rate Box\', \'Priority Mail Medium Flat Rate Box\', \'Priority Mail Large Flat Rate Box\', \'First-Class Mail\', \'Parcel Post\',\'Media Mail\'), ', 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 ('Domestic Shipping Methods', 'MODULE_SHIPPING_USPS_TYPES', 'Express Mail, Express Mail Flat Rate Envelope, Priority Mail, Priority Mail Flat Rate Envelope, Priority Mail Small Flat Rate Box, Priority Mail Medium Flat Rate Box, Priority Mail Large Flat Rate Box, Parcel Post, First-Class Mail, Media Mail', 'Select the domestic services to be offered:', '6', '0', 'tep_cfg_select_multioption(array(\'Express Mail\', \'Express Mail Flat Rate Envelope\', \'Priority Mail\', \'Priority Mail Flat Rate Envelope\', \'Priority Mail Small Flat Rate Box\', \'Priority Mail Medium Flat Rate Box\', \'Priority Mail Large Flat Rate Box\',  \'Parcel Post\', \'First-Class Mail\',\'Media Mail\'), ', 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 ('Int\'l Shipping Methods', 'MODULE_SHIPPING_USPS_TYPES_INTL',
     'Global Express, Global Express Non-Doc Rect, Global Express Non-Doc Non-Rect, USPS GXG Envelopes, Express Mail Int, Express Mail Int Flat Rate Env, Priority Mail International, Priority Mail Int Flat Rate Env, Priority Mail Int Flat Rate Small Box, Priority Mail Int Flat Rate Med Box, Priority Mail Int Flat Rate Lrg Box, First-Class Mail Int Lrg Env, First-Class Mail Int Package, First-Class Mail Int Letter',
  'Select the international services to be offered:', '6', '0', 'tep_cfg_select_multioption(
     array(\'Global Express\', \'Global Express Non-Doc Rect\', \'Global Express Non-Doc Non-Rect\', \'USPS GXG Envelopes\', \'Express Mail Int\', \'Express Mail Int Flat Rate Env\', \'Priority Mail International\', \'Priority Mail Int Flat Rate Env\', \'Priority Mail Int Flat Rate Small Box\', \'Priority Mail Int Flat Rate Med Box\', \'Priority Mail Int Flat Rate Lrg Box\', \'First-Class Mail Int Lrg Env\', \'First-Class Mail Int Package\', \'First-Class Mail Int Letter\'), ',  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 ('Domstic First-Class Threshold', 'MODULE_SHIPPING_CONFIG_DMSTC_FIRSTCLASS_THRESHOLD', '0, 3.5, 3.5, 10, 10, 13, 0, 13', 'If the total weight is between 0oz and 3.5oz, Letter is used. Over 3.5oz, Flat is used, etc. ONLY ONE TYPE WILL SHOW IN CART<br><br><u>Maximums:</u><br>Letters <u><</u> 3.5oz<br>Flats (large envelopes) <u><</u> 13oz<br>Parcels (packages) <u><</u> 13oz</i>', '6', '0', 'tep_cfg_multiinput_duallist_oz(array(\'Letter\', \'Flat\', \'Parcel\', \'MACHINABLE True\'), ', 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 ('Domstic Priority Mail Threshold', 'MODULE_SHIPPING_CONFIG_DMSTC_PRIORITY_THRESHOLD', '0, 70, 0, 70, 0, 70, 0, 70, 0, 70', 'If the total weight is between 0lb and 2lbs, Standard is used. Between 1lb and 3lbs, Flat Rate is used, etc.  CAN BE SHOWN SIMULTANEOUSLY<br><br><i>All Priority types have a weight limit of 70lbs.</i>', '6', '0', 'tep_cfg_multiinput_duallist_lb(array(\'Flat Rate Envelope\', \'Sm Flat Rate Box\', \'Md Flat Rate Box\', \'Lg Flat Rate Box\', \'Standard Priority\'), ', 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 ('Domstic Other Mail Threshold', 'MODULE_SHIPPING_CONFIG_DMSTC_OTHER_THRESHOLD', '0, 70, 0, 70, 0, 30, 30, 50, 50, 70, 0, 70', 'ParcelPst Reg, Lrg, OvrSz cannot be used simultaneously. All others are controlled separately.<br><br><i>USPS limit is 70lbs</i>', '6', '0', 'tep_cfg_multiinput_duallist_lb(array(\'Express FltRt Env\', \'Express Standard\', \'Parcel Pst Reg\', \'Parcel Pst Lrg\', \'Parcel Pst OvrSz\', \'Media Mail\'), ', 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 ('Domestic Insurance Options', 'MODULE_SHIPPING_DMSTC_INSURANCE_OPTION', 'None', 'Select how you want to offer USPS Domestic Insurance.', '6', '0', 'tep_cfg_select_option(array(\'None\', \'Buyers Option Does not work yet\', \'Force Insurance\'), ', 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 ('International Insurance Options', 'MODULE_SHIPPING_INTL_INSURANCE_OPTION', 'None', 'Select how you want to offer USPS International Insurance.  International Insurance is calculated automatically by USPS for certain mailing types.', '6', '0', 'tep_cfg_select_option(array(\'None\', \'Buyers Option Does not work yet\', \'Force Insurance\'), ', now())");
     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 ('US/Canada $.01-$50.00', 'MODULE_SHIPPING_USPS_INS1', '1.75', 'US/Canada insurance for totals $.01-$50.00', '6', '0', 'currencies->format', now())");
     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 ('US/Canada $50.01-$100.00', 'MODULE_SHIPPING_USPS_INS2', '2.25', 'US/Canada insurance for totals $50.01-$100', '6', '0', 'currencies->format', now())");
     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 ('US/Canada $100.01-$200.00', 'MODULE_SHIPPING_USPS_INS3', '2.75', 'US/Canada insurance for totals $100.01-$200', '6', '0', 'currencies->format', now())");
     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 ('US/Canada $200.01-$300.00', 'MODULE_SHIPPING_USPS_INS4', '4.70', 'US/Canada insurance for totals $200.01-$300', '6', '0', 'currencies->format', now())");
     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 ('US/Canada per $100 over $300', 'MODULE_SHIPPING_USPS_INS5', '1.00', 'US/Canada insurance for every $100 over $300 (add)', '6', '0', 'currencies->format', 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 ('Insure Tax', 'MODULE_SHIPPING_USPS_INSURE_TAX', 'True', 'Insure tax on packages shipped by USPS?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', 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_HANDLING_DOMESTIC', 'MODULE_SHIPPING_USPS_SERVER', 'MODULE_SHIPPING_USPS_TAX_CLASS', 'MODULE_SHIPPING_USPS_ZONE', 'MODULE_SHIPPING_USPS_SORT_ORDER', 'MODULE_SHIPPING_USPS_OPTIONS', 'MODULE_SHIPPING_DMSTC_INSURANCE_OPTION', 'MODULE_SHIPPING_INTL_INSURANCE_OPTION', 'MODULE_SHIPPING_USPS_TYPES', 'MODULE_SHIPPING_CONFIG_DMSTC_FIRSTCLASS_THRESHOLD', 'MODULE_SHIPPING_CONFIG_DMSTC_PRIORITY_THRESHOLD', 'MODULE_SHIPPING_CONFIG_DMSTC_OTHER_THRESHOLD', 'MODULE_SHIPPING_USPS_TYPES_INTL', 'MODULE_SHIPPING_USPS_INS1', 'MODULE_SHIPPING_USPS_INS2', 'MODULE_SHIPPING_USPS_INS3','MODULE_SHIPPING_USPS_INS4', 'MODULE_SHIPPING_USPS_INS5', /*'MODULE_SHIPPING_USPS_INS6', 'MODULE_SHIPPING_USPS_INS7', 'MODULE_SHIPPING_USPS_INS8', 'MODULE_SHIPPING_USPS_INS9', 'MODULE_SHIPPING_USPS_INS10', 'MODULE_SHIPPING_USPS_INSURE', */'MODULE_SHIPPING_USPS_INSURE_TAX');
   }

   function _setService($service) {
     $this->service = $service;
   }

   function _setWeight($pounds, $ounces=0, $weight) {
     $this->pounds = $pounds;
     $this->ounces = $ounces;
       $this->weight = $weight;
   }

   function _setMachinable($machinable) {
     $this->machinable = $machinable;
   }
   function _getQuote() {
     global $order, $transittime;
     $transit = (in_array('Display transit time', explode(', ', MODULE_SHIPPING_USPS_OPTIONS)));

$Authentication = explode( ", ", MODULE_SHIPPING_USPS_USERID);  //0=>USERID, 1=>PASSWORD
     if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY) {
       $request  = '<RateV3Request USERID="' . $Authentication[0] . '" PASSWORD="' . $Authentication[1] . '">';
       $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);
       $allowed_types = explode(", ", MODULE_SHIPPING_USPS_TYPES);
       while (list($key, $value) = each($this->types)) {
         if ( !in_array($key, $allowed_types) ) continue;

			if ($key == 'First-Class Mail'){
$FCT = explode(", ", MODULE_SHIPPING_CONFIG_DMSTC_FIRSTCLASS_THRESHOLD);
                       if($this->pounds == 0) {
                               if($FCT[0] < $this->ounces && $this->ounces <= $FCT[1]) {
                                          $this->FirstClassMailType = 'LETTER';
                               } else if($FCT[2] < $this->ounces && $this->ounces <= $FCT[3]) {
                                     $this->FirstClassMailType = 'FLAT';
                                  } else if($FCT[4] < $this->ounces && $this->ounces <= $FCT[5]) {
                                       $this->FirstClassMailType = 'PARCEL';
                               } else {
                                       $key = 'none';
                               }
                               if($FCT[6] < $this->ounces && $this->ounces <= $FCT[7]) {
                                       $this->machinable = 'true';
                               } else {
                                       $this->machinable = 'false';
                               }
                       }
               }
$PT = explode(", ", MODULE_SHIPPING_CONFIG_DMSTC_PRIORITY_THRESHOLD);
               if ($key == 'Priority Mail'){
                       if ($PT[4] < $this->weight && $this->weight <= $PT[5]) {
                               $this->container = '';
                               $this->size = 'REGULAR';
                       } else {
                               $key = 'none';
                       }
               }
               if ($key == 'Priority Mail Flat Rate Envelope'){
                       if ($PT[0] < $this->weight && $this->weight <= $PT[1]) {
                               $key = 'Priority'; //DO NOT CHANGE
                               $this->container = 'FLAT RATE ENVELOPE';
                               $this->size = '';
                       } else {
                               $key = 'none';
                       }
               }
               if ($key == 'Priority Mail Small Flat Rate Box'){
                       if ($PT[2] < $this->weight && $this->weight <= $PT[3]) {
                               $key = 'Priority'; //DO NOT CHANGE
                               $this->container = 'SM FLAT RATE BOX';
                               $this->size = '';
                       } else {
                               $key = 'none';
                       }
               }
               if ($key == 'Priority Mail Medium Flat Rate Box'){
                       if ($PT[2] < $this->weight && $this->weight <= $PT[3]) {
                               $key = 'Priority'; //DO NOT CHANGE
                               $this->container = 'MD FLAT RATE BOX';
                               $this->size = '';
                       } else {
                               $key = 'none';
                       }
               }
               if ($key == 'Priority Mail Large Flat Rate Box'){
                       if ($PT[2] < $this->weight && $this->weight <= $PT[3]) {
                               $key = 'Priority'; //DO NOT CHANGE
                               $this->container = 'LG FLAT RATE BOX';
                               $this->size = '';
                       } else {
                               $key = 'none';
                       }
               }
		 $AOT = explode( ", ", MODULE_SHIPPING_CONFIG_DMSTC_OTHER_THRESHOLD);

               if ($key == 'Express Mail'){
                       if ($PT[4] < $this->weight && $this->weight <= $PT[5]) {
                               $this->container = '';
                               $this->size = 'REGULAR';
                       } else {
                               $key = 'none';
                       }
               }
               if ($key == 'Express Mail Flat Rate Envelope'){
                       if ($PT[0] < $this->weight && $this->weight <= $PT[1]) {
                               $key = 'Express'; //DO NOT CHANGE
                               $this->container = 'FLAT RATE ENVELOPE';
                               $this->size = '';
                       } else {
                               $key = 'none';
                       }
               }
               if ($key == 'Parcel Post'){
                       //WEIGHT THRESHOLD OPTIONS (DEFAULT MACHINABLE: WEIGHT<70lbs WEIGHT CANNOT EXCEED 70lbs ANYWAY)
                       if ($AOT[4] < $this->weight && $this->weight <= $AOT[5]){
                             $this->machinable = 'true';        //OPTIONS: 'true', 'false'
                               $this->size = 'REGULAR';
                       } elseif ($AOT[6] < $this->weight && $this->weight <= $AOT[7]){
                             $this->machinable = 'true';        //OPTIONS: 'true', 'false'
                               $this->size = 'LARGE';
                     } elseif ($AOT[8] < $this->weight && $this->weight <= $AOT[9]){
                             $this->machinable = 'true';        //OPTIONS: 'true', 'false'
                               $this->size = 'OVERSIZE';
                       } else {
                               $key = 'none';
                       }
               }
               if ($key == 'Media Mail'){
                       if ($AOT[12] < $this->weight && $this->weight <= $AOT[13]){
                             $this->size = 'REGULAR';        //OPTIONS: 'REGULAR, 'LARGE'
                       } else {
                               $key = 'none';
                       }
               }

         $request .= '<Package ID="' . $services_count . '">' .
                     '<Service>' . $key . '</Service>' .
                     '<FirstClassMailType>' . $this->FirstClassMailType . '</FirstClassMailType>' .
                     '<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>';

         if($transit){

           $transitreq  = 'USERID="' . $Authentication[0] .
                        '" PASSWORD="' . $Authentication[1] . '">' .
                        '<OriginZip>' . SHIPPING_ORIGIN_ZIP . '</OriginZip>' .
                        '<DestinationZip>' . $dest_zip . '</DestinationZip>';

           switch ($key) {
             case 'Express Mail':  $transreq[$key] = 'API=ExpressMail&XML=' .
                              urlencode( '<ExpressMailRequest ' . $transitreq . '</ExpressMailRequest>');
            if ($this->container == 'FLAT RATE ENVELOPE') {
                   $key = 'Express Mail Flat Rate Envelope';
            }
                              break;
             case 'Priority Mail': $transreq[$key] = 'API=PriorityMail&XML=' .
                              urlencode( '<PriorityMailRequest ' . $transitreq . '</PriorityMailRequest>');
            if ($this->container == 'FLAT RATE ENVELOPE') {
                   $key = 'Priority Mail Flat Rate Envelope';
            }
            elseif ($this->container == 'SM FLAT RATE BOX') {
                   $key = 'Priority Mail Small Flat Rate Box';
            }
            elseif ($this->container == 'MD FLAT RATE BOX') {
                   $key = 'Priority Mail Medium Flat Rate Box';
            }
            elseif ($this->container == 'LG FLAT RATE BOX') {
                   $key = 'Priority Mail Large Flat Rate Box';
            }
                              break;
             case 'Parcel Post':   $transreq[$key] = 'API=StandardB&XML=' .
                              urlencode( '<StandardBRequest ' . $transitreq . '</StandardBRequest>');
                              break;
             default:         $transreq[$key] = '';
                              break;
           }
         }
         $services_count++;
       }
       $request .= '</RateV3Request>'; //'</RateRequest>'; //Changed by Greg Deeth April 30, 2008
       $request = 'API=RateV3&XML=' . urlencode($request);
     } else {
       $request  = '<IntlRateRequest USERID="' . $Authentication[0] . '" PASSWORD="' . $Authentication[1] . '">' .
						                    '<Package ID="0">' .
						                    '<Pounds>' . $this->pounds . '</Pounds>' .
                   '<Ounces>' . $this->ounces . '</Ounces>' .
                   '<MailType>Package</MailType>' .
                   '<ValueOfContents>' . $order->info['total'] . '</ValueOfContents>' .
						                    '<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'; //'stg-production.shippingapis.com'; // or  stg-secure.shippingapis.com //'production.shippingapis.com';
                          $api_dll = 'shippingapi.dll'; //'shippingapi.dll';
                          break;
       case 'test':
       default:           $usps_server = 'testing.shippingapis.com';
                          $api_dll = 'ShippingAPITest.dll';
                          break;
      }
      $body = '';
      if (!class_exists('httpClient')) {
        include('includes/classes/http_client.php');
      }
      $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();
 //mail('user@localhost.com','USPS rate quote response',$body,'From: <user@localhost.com>');
       if ($transit && is_array($transreq) && ($order->delivery['country']['id'] == STORE_COUNTRY)) {
         while (list($key, $value) = each($transreq)) {
           if ($http->Get('/' . $api_dll . '?' . $value)) $transresp[$key] = $http->getBody();
 //mail('user@localhost.com','USPS rate quote response',$transresp[$key],'From: <user@localhost.com>');
         }
       }
       $http->Disconnect();
     } else {
       return false;
     }
$body = str_replace('&lt;sup&gt;&amp;reg;&lt;/sup&gt;', '', $body);
$body = str_replace('&lt;sup&gt;&amp;trade;&lt;/sup&gt;', '', $body);
     $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();
     $rates_sorter = 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], '<Rate>')) {
           $service = ereg('<MailService>(.*)</MailService>', $response[$i], $regs);
  		$service = htmlspecialchars_decode($regs[1]);
  		$service = preg_replace('/\<sup\>\&reg;\<\/sup\>/', '<sup>®</sup>', $service);
           $service = $regs[1];
           $postage = ereg('<Rate>(.*)</Rate>', $response[$i], $regs);
           $postage = $regs[1];
       $insurance = ereg('<Insurance>(.*)</Insurance>', $response[$i], $regs);
       $insurance = $regs[1];
              switch ($service) {
             case 'Express Mail':
               $time = ereg('<MonFriCommitment>(.*)</MonFriCommitment>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 2 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } else {
                 $time = MODULE_SHIPPING_USPS_TEXT_TOMORROW . $time;
               }
                                                   $postage = $postage + $this->handling[0];
               break;
             case 'Express Mail Flat Rate Envelope':
                     $time = ereg('<MonFriCommitment>(.*)</MonFriCommitment>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 2 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } else {
                 $time = MODULE_SHIPPING_USPS_TEXT_TOMORROW . $time;
               }
                                             $postage = $postage + $this->handling[0];
               break;
             case 'Priority Mail':
               $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } elseif ($time == '1') {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
               } else {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               }
                                             $postage = $postage + $this->handling[1];
               break;
                   case 'Priority Mail Flat Rate Envelope':
                     $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } elseif ($time == '1') {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
               } else {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               }
                                             $postage = $postage + $this->handling[2];
               break;
                          case 'Priority Mail Small Flat Rate Box':
                            $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } elseif ($time == '1') {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
               } else {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               }
                                                    $postage = $postage + $this->handling[3];
               break;
                          case 'Priority Mail Medium Flat Rate Box':
                            $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } elseif ($time == '1') {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
               } else {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               }
                                                    $postage = $postage + $this->handling[3];
               break;
                          case 'Priority Mail Large Flat Rate Box':
                            $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 3 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } elseif ($time == '1') {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
               } else {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               }
                                                    $postage = $postage + $this->handling[3];
               break;
             case 'Parcel Post':
               $time = ereg('<Days>(.*)</Days>', $transresp[$service], $tregs);
               $time = $tregs[1];
               if ($time == '' || $time == 'No Data') {
                 $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 2 - 9 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               } elseif ($time == '1') {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAY;
               } else {
                 $time .= ' ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
               }
                                             $postage = $postage + $this->handling[5];
               break;
             case 'First-Class Mail':
               $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                             $postage = $postage + $this->handling[4];
               break;
//add first class options
             case 'First-Class Mail Letter':
               $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                             $postage = $postage + $this->handling[4];
               break;
             case 'First-Class Mail Large Envelope':
               $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                             $postage = $postage + $this->handling[4];
               break;
             case 'First-Class Mail Package':
               $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 1 - 5 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                             $postage = $postage + $this->handling[4];
               break;
                               case 'Media Mail':
                                       $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 2 - 9 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                             $postage = $postage + $this->handling[7];
               break;
                               case 'Bound Printed Matter':
                                       $time = MODULE_SHIPPING_USPS_TEXT_ESTIMATED . ' 2 - 9 ' . MODULE_SHIPPING_USPS_TEXT_DAYS;
                                             $postage = $postage + $this->handling[6];
               break;
             default: 
               $time = '';
               break;
             }
                       $rates[] = array($service => $postage);
           $rates_sorter[] = $postage;
           if ($transit) {
             if ($time != '') $transittime[$service] = ' (' . $time . ') ';
           }
         }
       }
     } else {
       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);
       } else {
         $body = $response[0];
         $services = array();
         while (true) {
           if ($start = strpos($body, '<Service ID=')) {
             $body = substr($body, $start);
             $end = strpos($body, '</Service>');
             $services[] = substr($body, 0, $end+10);
             $body = substr($body, $end+9);
           } else {
             break;
           }
         }
         $allowed_types = array();
         foreach( explode(", ", MODULE_SHIPPING_USPS_TYPES_INTL) as $value ) $allowed_types[$value] = $this->intl_types[$value];
 $size = sizeof($services);
 for ($i=0, $n=$size; $i<$n; $i++) {
   if (strpos($services[$i], '<Postage>')) {
     $service = ereg('<SvcDescription>(.*)</SvcDescription>', $services[$i], $regs);
         $service = $regs[1];                    
     $postage = ereg('<Postage>(.*)</Postage>', $services[$i], $regs);
     $postage = $regs[1];
     $time = ereg('<SvcCommitments>(.*)</SvcCommitments>', $services[$i], $tregs);
     $time = $tregs[1];
     $time = preg_replace('/Weeks$/', MODULE_SHIPPING_USPS_TEXT_WEEKS, $time);
     $time = preg_replace('/Days$/', MODULE_SHIPPING_USPS_TEXT_DAYS, $time);
     $time = preg_replace('/Day$/', MODULE_SHIPPING_USPS_TEXT_DAY, $time);
$time = ' (' . $time . ') ';
             if( !in_array($service, $allowed_types) ) continue;
             if (isset($this->service) && ($service != $this->service) ) {
               continue;
             }
                 if (strpos($services[$i], '<Insurance>')) {
                         $iinsurance = ereg('<Insurance>(.*)</Insurance>', $services[$i], $regs);
       $iinsurance = $regs[1];
                         if (MODULE_SHIPPING_INTL_INSURANCE_OPTION == 'Force Insurance') {
                                 $postage = $postage + $iinsurance;
                                 if ($iinsurance > 0) {
                                         $time = ($time . ' ~~' . MODULE_SHIPPING_USPS_TEXT_INSURED . ' $' . $order->info['total'] . '~~');
                                 }
                         }
                 }
                 $postage = $postage + $handling_cost[$i];
                 $rates[] = array($service => $postage);
             $rates_sorter[] = $postage;
             if ($time != '') $transittime[$service] = $time;
           }
         }
       }
     }
       print_r($handling_cost);
       asort($rates_sorter);
       $sorted_rates = array();
       foreach (array_keys($rates_sorter) as $key){
               $sorted_rates[] = $rates[$key];
       }
       return ((sizeof($sorted_rates) > 0) ? $sorted_rates : false);
   }
   function country_list() {
     $list = array('AF' => 'Afghanistan',
                   'AL' => 'Albania',
                   'DZ' => 'Algeria',
                   'AD' => 'Andorra',
                   'AO' => 'Angola',
                   'AI' => 'Anguilla',
                   'AG' => 'Antigua and Barbuda',
                   'AR' => 'Argentina',
                   'AM' => 'Armenia',
                   'AW' => 'Aruba',
                   'AU' => 'Australia',
                   'AT' => 'Austria',
                   'AZ' => 'Azerbaijan',
                   'BS' => 'Bahamas',
                   'BH' => 'Bahrain',
                   'BD' => 'Bangladesh',
                   'BB' => 'Barbados',
                   'BY' => 'Belarus',
                   'BE' => 'Belgium',
                   'BZ' => 'Belize',
                   'BJ' => 'Benin',
                   'BM' => 'Bermuda',
                   'BT' => 'Bhutan',
                   'BO' => 'Bolivia',
                   'BA' => 'Bosnia-Herzegovina',
                   'BW' => 'Botswana',
                   'BR' => 'Brazil',
                   'VG' => 'British Virgin Islands',
                   'BN' => 'Brunei Darussalam',
                   'BG' => 'Bulgaria',
                   'BF' => 'Burkina Faso',
                   'MM' => 'Burma',
                   'BI' => 'Burundi',
                   'KH' => 'Cambodia',
                   'CM' => 'Cameroon',
                   'CA' => 'Canada',
                   'CV' => 'Cape Verde',
                   'KY' => 'Cayman Islands',
                   'CF' => 'Central African Republic',
                   'TD' => 'Chad',
                   'CL' => 'Chile',
                   'CN' => 'China',
                   'CX' => 'Christmas Island (Australia)',
                   'CC' => 'Cocos Island (Australia)',
                   'CO' => 'Colombia',
                   'KM' => 'Comoros',
                   'CG' => 'Congo (Brazzaville),Republic of the',
                   'ZR' => 'Congo, Democratic Republic of the',
                   'CK' => 'Cook Islands (New Zealand)',
                   'CR' => 'Costa Rica',
                   'CI' => 'Cote d\'Ivoire (Ivory Coast)',
                   'HR' => 'Croatia',
                   'CU' => 'Cuba',
                   'CY' => 'Cyprus',
                   'CZ' => 'Czech Republic',
                   'DK' => 'Denmark',
                   'DJ' => 'Djibouti',
                   'DM' => 'Dominica',
                   'DO' => 'Dominican Republic',
                   'TP' => 'East Timor (Indonesia)',
                   'EC' => 'Ecuador',
                   'EG' => 'Egypt',
                   'SV' => 'El Salvador',
                   'GQ' => 'Equatorial Guinea',
                   'ER' => 'Eritrea',
                   'EE' => 'Estonia',
                   'ET' => 'Ethiopia',
                   'FK' => 'Falkland Islands',
                   'FO' => 'Faroe Islands',
                   'FJ' => 'Fiji',
                   'FI' => 'Finland',
                   'FR' => 'France',
                   'GF' => 'French Guiana',
                   'PF' => 'French Polynesia',
                   'GA' => 'Gabon',
                   'GM' => 'Gambia',
                   'GE' => 'Georgia, Republic of',
                   'DE' => 'Germany',
                   'GH' => 'Ghana',
                   'GI' => 'Gibraltar',
                   'GB' => 'Great Britain and Northern Ireland',
                   'GR' => 'Greece',
                   'GL' => 'Greenland',
                   'GD' => 'Grenada',
                   'GP' => 'Guadeloupe',
                   'GT' => 'Guatemala',
                   'GN' => 'Guinea',
                   'GW' => 'Guinea-Bissau',
                   'GY' => 'Guyana',
                   'HT' => 'Haiti',
                   'HN' => 'Honduras',
                   'HK' => 'Hong Kong',
                   'HU' => 'Hungary',
                   'IS' => 'Iceland',
                   'IN' => 'India',
                   'ID' => 'Indonesia',
                   'IR' => 'Iran',
                   'IQ' => 'Iraq',
                   'IE' => 'Ireland',
                   'IL' => 'Israel',
                   'IT' => 'Italy',
                   'JM' => 'Jamaica',
                   'JP' => 'Japan',
                   'JO' => 'Jordan',
                   'KZ' => 'Kazakhstan',
                   'KE' => 'Kenya',
                   'KI' => 'Kiribati',
                   'KW' => 'Kuwait',
                   'KG' => 'Kyrgyzstan',
                   'LA' => 'Laos',
                   'LV' => 'Latvia',
                   'LB' => 'Lebanon',
                   'LS' => 'Lesotho',
                   'LR' => 'Liberia',
                   'LY' => 'Libya',
                   'LI' => 'Liechtenstein',
                   'LT' => 'Lithuania',
                   'LU' => 'Luxembourg',
                   'MO' => 'Macao',
                   'MK' => 'Macedonia, Republic of',
                   'MG' => 'Madagascar',
                   'MW' => 'Malawi',
                   'MY' => 'Malaysia',
                   'MV' => 'Maldives',
                   'ML' => 'Mali',
                   'MT' => 'Malta',
                   'MQ' => 'Martinique',
                   'MR' => 'Mauritania',
                   'MU' => 'Mauritius',
                   'YT' => 'Mayotte (France)',
                   'MX' => 'Mexico',
                   'MD' => 'Moldova',
                   'MC' => 'Monaco (France)',
                   'MN' => 'Mongolia',
                   'MS' => 'Montserrat',
                   'MA' => 'Morocco',
                   'MZ' => 'Mozambique',
                   'NA' => 'Namibia',
                   'NR' => 'Nauru',
                   'NP' => 'Nepal',
                   'NL' => 'Netherlands',
                   'AN' => 'Netherlands Antilles',
                   'NC' => 'New Caledonia',
                   'NZ' => 'New Zealand',
                   'NI' => 'Nicaragua',
                   'NE' => 'Niger',
                   'NG' => 'Nigeria',
                   'KP' => 'North Korea (Korea, Democratic People\'s Republic of)',
                   'NO' => 'Norway',
                   'OM' => 'Oman',
                   'PK' => 'Pakistan',
                   'PA' => 'Panama',
                   'PG' => 'Papua New Guinea',
                   'PY' => 'Paraguay',
                   'PE' => 'Peru',
                   'PH' => 'Philippines',
                   'PN' => 'Pitcairn Island',
                   'PL' => 'Poland',
                   'PT' => 'Portugal',
                   'QA' => 'Qatar',
                   'RE' => 'Reunion',
                   'RO' => 'Romania',
                   'RU' => 'Russia',
                   'RW' => 'Rwanda',
                   'SH' => 'Saint Helena',
                   'KN' => 'Saint Kitts (St. Christopher and Nevis)',
                   'LC' => 'Saint Lucia',
                   'PM' => 'Saint Pierre and Miquelon',
                   'VC' => 'Saint Vincent and the Grenadines',
                   'SM' => 'San Marino',
                   'ST' => 'Sao Tome and Principe',
                   'SA' => 'Saudi Arabia',
                   'SN' => 'Senegal',
                   'YU' => 'Serbia-Montenegro',
                   'SC' => 'Seychelles',
                   'SL' => 'Sierra Leone',
                   'SG' => 'Singapore',
                   'SK' => 'Slovak Republic',
                   'SI' => 'Slovenia',
                   'SB' => 'Solomon Islands',
                   'SO' => 'Somalia',
                   'ZA' => 'South Africa',
                   'GS' => 'South Georgia (Falkland Islands)',
                   'KR' => 'South Korea (Korea, Republic of)',
                   'ES' => 'Spain',
                   'LK' => 'Sri Lanka',
                   'SD' => 'Sudan',
                   'SR' => 'Suriname',
                   'SZ' => 'Swaziland',
                   'SE' => 'Sweden',
                   'CH' => 'Switzerland',
                   'SY' => 'Syrian Arab Republic',
                   'TW' => 'Taiwan',
                   'TJ' => 'Tajikistan',
                   'TZ' => 'Tanzania',
                   'TH' => 'Thailand',
                   'TG' => 'Togo',
                   'TK' => 'Tokelau (Union) Group (Western Samoa)',
                   'TO' => 'Tonga',
                   'TT' => 'Trinidad and Tobago',
                   'TN' => 'Tunisia',
                   'TR' => 'Turkey',
                   'TM' => 'Turkmenistan',
                   'TC' => 'Turks and Caicos Islands',
                   'TV' => 'Tuvalu',
                   'UG' => 'Uganda',
                   'UA' => 'Ukraine',
                   'AE' => 'United Arab Emirates',
                   'UY' => 'Uruguay',
                   'UZ' => 'Uzbekistan',
                   'VU' => 'Vanuatu',
                   'VA' => 'Vatican City',
                   'VE' => 'Venezuela',
                   'VN' => 'Vietnam',
                   'WF' => 'Wallis and Futuna Islands',
                   'WS' => 'Western Samoa',
                   'YE' => 'Yemen',
                   'ZM' => 'Zambia',
                   'ZW' => 'Zimbabwe');
     return $list;
   }
// Set up list of maximum allowed insurance values  
   function country_maxinsure() {
     $list = array('AF' => 0,
                   'AL' => 0,
                   'DZ' => 2185,
                   'AD' => 5000,
                   'AO' => 0,
                   'AI' => 415,
                   'AG' => 60,
                   'AR' => 5000,
                   'AM' => 1350,
                   'AW' => 830,
                   'AU' => 3370,
                   'AT' => 5000,
                   'AZ' => 5000,
                   'BS' => 2795,
                   'BH' => 0,
                   'BD' => 5000,
                   'BB' => 220,
                   'BY' => 1323,
                   'BE' => 5000,
                   'BZ' => 1600,
                   'BJ' => 170,
                   'BM' => 440,
                   'BT' => 440,
                   'BO' => 0,
                   'BA' => 5000,
                   'BW' => 145,
                   'BR' => 5000,
                   'VG' => 165,
                   'BN' => 4405,
                   'BG' => 1030,
                   'BF' => 530,
                   'MM' => 4045,
                   'BI' => 790,
                   'KH' => 0,
                   'CM' => 5000,
                   'CA' => 675,
                   'CV' => 0,
                   'KY' => 0,
                   'CF' => 4405,
                   'TD' => 440,
                   'CL' => 0,
                   'CN' => 1130,
                   'CX' => 3370,
                   'CC' => 3370,
                   'CO' => 0,
                   'KM' => 690,
                   'CG' => 1685,
                   'ZR' => 0,
                   'CK' => 980,
                   'CR' => 0,
                   'CI' => 5000,
                   'HR' => 5000,
                   'CU' => 0,
                   'CY' => 5000,
                   'CZ' => 5000,
                   'DK' => 5000,
                   'DJ' => 880,
                   'DM' => 0,
                   'DO' => 0,
                   'TP' => 0,
                   'EC' => 0,
                   'EG' => 1685,
                   'SV' => 0,
                   'GQ' => 0,
                   'ER' => 0,
                   'EE' => 2020,
                   'ET' => 1000,
                   'FK' => 510,
                   'FO' => 5000,
                   'FJ' => 600,
                   'FI' => 5000,
                   'FR' => 5000,
                   'GF' => 5000,
                   'PF' => 1015,
                   'GA' => 485,
                   'GM' => 2575,
                   'GE' => 1350,
                   'DE' => 5000,
                   'GH' => 5000,
                   'GI' => 5000,
                   'GB' => 857,
                   'GR' => 5000,
                   'GL' => 5000,
                   'GD' => 350,
                   'GP' => 5000,
                   'GT' => 0,
                   'GN' => 875,
                   'GW' => 21,
                   'GY' => 10,
                   'HT' => 0,
                   'HN' => 0,
                   'HK' => 5000,
                   'HU' => 5000,
                   'IS' => 5000,
                   'IN' => 2265,
                   'ID' => 0,
                   'IR' => 0,
                   'IQ' => 0,
                   'IE' => 5000,
                   'IL' => 0,
                   'IT' => 5000,
                   'JM' => 0,
                   'JP' => 5000,
                   'JO' => 0,
                   'KZ' => 5000,
                   'KE' => 815,
                   'KI' => 0,
                   'KW' => 1765,
                   'KG' => 1350,
                   'LA' => 0,
                   'LV' => 1350,
                   'LB' => 440,
                   'LS' => 440,
                   'LR' => 440,
                   'LY' => 0,
                   'LI' => 5000,
                   'LT' => 5000,
                   'LU' => 5000,
                   'MO' => 4262,
                   'MK' => 2200,
                   'MG' => 675,
                   'MW' => 50,
                   'MY' => 1320,
                   'MV' => 0,
                   'ML' => 950,
                   'MT' => 5000,
                   'MQ' => 5000,
                   'MR' => 635,
                   'MU' => 270,
                   'YT' => 5000,
                   'MX' => 0,
                   'MD' => 1350,
                   'MC' => 5000,
                   'MN' => 440,
                   'MS' => 2200,
                   'MA' => 5000,
                   'MZ' => 0,
                   'NA' => 4405,
                   'NR' => 220,
                   'NP' => 0,
                   'NL' => 5000,
                   'AN' => 830,
                   'NC' => 1615,
                   'NZ' => 980,
                   'NI' => 440,
                   'NE' => 810,
                   'NG' => 205,
                   'KP' => 0,
                   'NO' => 0,
                   'OM' => 575,
                   'PK' => 270,
                   'PA' => 0,
                   'PG' => 445,
                   'PY' => 0,
                   'PE' => 0,
                   'PH' => 270,
                   'PN' => 0,
                   'PL' => 1350,
                   'PT' => 5000,
                   'QA' => 2515,
                   'RE' => 5000,
                   'RO' => 5000,
                   'RU' => 5000,
                   'RW' => 0,
                   'SH' => 170,
                   'KN' => 210,
                   'LC' => 400,
                   'PM' => 5000,
                   'VC' => 130,
                   'SM' => 5000,
                   'ST' => 440,
                   'SA' => 0,
                   'SN' => 865,
                   'YU' => 5000,
                   'SC' => 0,
                   'SL' => 0,
                   'SG' => 4580,
                   'SK' => 5000,
                   'SI' => 4400,
                   'SB' => 0,
                   'SO' => 440,
                   'ZA' => 1760,
                   'GS' => 510,
                   'KR' => 5000,
                   'ES' => 5000,
                   'LK' => 35,
                   'SD' => 0,
                   'SR' => 535,
                   'SZ' => 560,
                   'SE' => 5000,
                   'CH' => 5000,
                   'SY' => 3080,
                   'TW' => 1350,
                   'TJ' => 1350,
                   'TZ' => 230,
                   'TH' => 1350,
                   'TG' => 2190,
                   'TK' => 295,
                   'TO' => 515,
                   'TT' => 930,
                   'TN' => 2200,
                   'TR' => 880,
                   'TM' => 675,
                   'TC' => 0,
                   'TV' => 4715,
                   'UG' => 0,
                   'UA' => 5000,
                   'AE' => 5000,
                   'UY' => 0,
                   'UZ' => 5000,
                   'VU' => 0,
                   'VA' => 5000,
                   'VE' => 0,
                   'VN' => 0,
                   'WF' => 1615,
                   'WS' => 295,
                   'YE' => 0,
                   'ZM' => 540,
                   'ZW' => 600,
                   'US' => 5000);
     return $list;
   }
 }
?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×