Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Recommended Posts

Posted

Hi

I have a problem with the Canada Post shipping modulel, this morning it stopped working and I get this error:

 

Cannot reach Canada Post Server. You may refresh this page (Press F5 on your keyboard) to try again.

 

I contacted Canada Post to see if they had problem with the server , thay said everything is working over there, thay asked me to check the exact error message I get from the server.

How can I modify the Canada Post script to see this error?

 

Thanks

Frank

 

Here is the script:

 

 

<?php
/*
 $Id: canadapost.php,v 3.6

 Before use this class, you should open a Canada  
 Post Eparcel Account, and change the CPCIP to 
 your ID. Visit www.canadapost.ca for detail. 
  
 XML connection method with Canada Post. 

 OSC 2.2 MS2 version only.
  
 Copyright (c) 2002,2003 Kelvin Zhang ([email protected])
 Modified by Kenneth Wang ([email protected]), 2002.11.12
 3.6 for OSC 2.2 MS2 with LXWXH added by Tom St.Croix ([email protected]).
 All thanks to Kelvin and Kenneth and many others.

 Released under the GNU General Public License
*/
 class canadapost {
   var $code, $title, $descrption, $icon, $enabled, $types, $boxcount;

// class constructor
   function canadapost() {
     $this->code = 'canadapost';
     $this->title = MODULE_SHIPPING_CANADAPOST_TEXT_TITLE;
     $this->description = MODULE_SHIPPING_CANADAPOST_TEXT_DESCRIPTION;
     $this->icon = DIR_WS_ICONS . 'shipping_canadapost.gif';
     $this->comments = '';
     $this->enabled = ((MODULE_SHIPPING_CANADAPOST_STATUS == 'True') ? true : false);
     $this->server = MODULE_SHIPPING_CANADAPOST_SERVERIP;
     $this->port = MODULE_SHIPPING_CANADAPOST_SERVERPOST;
     $this->language = MODULE_SHIPPING_CANADAPOST_LANGUAGE;
     $this->CPCID = MODULE_SHIPPING_CANADAPOST_CPCID;
     $this->turnaround_time = MODULE_SHIPPING_CANADAPOST_TIME;
     $this->sort_order = MODULE_SHIPPING_CANADAPOST_SORT_ORDER;
     $this->items_qty = 0;
     $this->items_price = 0;
     $this->tax_class = MODULE_SHIPPING_CANADAPOST_TAX_CLASS;
     $this->cp_oniline_handling = ((MODULE_SHIPPING_CANADAPOST_CP_HANDLING == 'True') ? true : false);

     if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_CANADAPOST_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_CANADAPOST_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;
       }
     }
   }


// class methods
   function quote($method = '') {
     global $HTTP_POST_VARS, $order, $shipping_weight, $shipping_num_boxes, $total_weight, $boxcount, $handling_cp, $cart;
   // will round to 2 9.112 becomes 1.11 thus a product can be 0.1 of a KG
     $shipping_weight = round($shipping_weight, 2);

  
     $country_name = tep_get_countries(STORE_COUNTRY, true);
     $this->_canadapostOrigin(SHIPPING_ORIGIN_ZIP, $country_name['countries_iso_code_2']);
     $this->_canadapostDest($order->delivery['city'], $order->delivery['state'], $order->delivery['country']['iso_code_2'], $order->delivery['postcode']);

     $products_array = $cart->get_products();
     for ($i=0; $i<count($products_array); $i++) 
       $this->_addItem ($products_array[$i][quantity], $products_array[$i][final_price], $products_array[$i][weight], $products_array[$i][length], $products_array[$i][width], $products_array[$i][height], $products_array[$i][name], $products_array[$i][ready_to_ship], $products_array[$i][dim_type], $products_array[$i][weight_type]);
    
     $canadapostQuote = $this->_canadapostGetQuote();
     if ( (is_array($canadapostQuote)) && (sizeof($canadapostQuote) > 0) ) {
       $this->quotes = array('id' => $this->code,
                             'module' => $this->title . ' (' . $this->boxCount . MODULE_SHIPPING_PACKAGING_RESULTS . ')');
       $methods = array();
       for ($i=0; $i<sizeof($canadapostQuote); $i++) {
         list($type, $cost) = each($canadapostQuote[$i]);

if ( $this->cp_oniline_handling == true) {
  if ( $method == '' || $method == $type ) {
           $methods[] = array('id' => $type,
                            'title' => $type,
        'cost' => $cost + $this->handling_cp);
}
 } else {
  if ( $method == '' || $method == $type ) {
           $methods[] = array('id' => $type,
                            'title' => $type,
                           'cost' => (SHIPPING_HANDLING + $cost));
	 }
 }
}
     if ($this->tax_class > 0) {
       $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
     }
       $this->quotes['methods'] = $methods;
     } else {
    	 if ( $canadapostQuote != false ) {
          $errmsg = $canadapostQuote;
    	 } else {
          $errmsg = 'An unknown error occured with the canadapost shipping calculations.';
}
    	 $errmsg .= '<br>If you prefer to use canadapost as your shipping method, please contact the '.STORE_NAME.' via <a href="mailto:'.STORE_OWNER_EMAIL_ADDRESS.'"><u>Email</U></a>.';
       $this->quotes = array('module' => $this->title,
                             'error' => $errmsg);
                            
     }


     if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
     if (tep_not_null($this->comments)) $this->quotes['comments'] = $this->comments;

     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_CANADAPOST_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 CanadaPost Shipping', 'MODULE_SHIPPING_CANADAPOST_STATUS', 'True', 'Do you want to offer Canada Post 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 CanadaPost Server IP', 'MODULE_SHIPPING_CANADAPOST_SERVERIP', '206.191.4.228', 'ip address of canada post server.(if you have your own CP Account and CP has moved your account to the Production Server use this setting. Production IP: 216.191.36.73 port 3000)', '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 CanadaPost Server Port', 'MODULE_SHIPPING_CANADAPOST_SERVERPOST', '30000', 'service port of canadapast server.', '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 Selected Language-optional', 'MODULE_SHIPPING_CANADAPOST_LANGUAGE', 'en', 'canada post supports two languages en-english fr-french.', '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 Your CanadaPost Customer ID', 'MODULE_SHIPPING_CANADAPOST_CPCID', 'CPC_DEMO_XML', 'Canada Post Customer ID Merchant Identification assigned by Canada Post.', '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 Turn Around Time(optional)', 'MODULE_SHIPPING_CANADAPOST_TIME', '0', 'Turn Around Time -hours.', '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_CANADAPOST_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_CANADAPOST_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, set_function, date_added) values ('Use CP Handling Charge System', 'MODULE_SHIPPING_CANADAPOST_CP_HANDLING', 'False', 'Use the Canada Post shipping and handling charge system opposed to this mods handling charge feature?', '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 ('Handling Charge per box', 'SHIPPING_HANDLING', '0', 'Handling Charge is only used if the CP Handling System is set to false', '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 ('Sort Order', 'MODULE_SHIPPING_CANADAPOST_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
   }

   function remove() {
     $keys = '';
     $keys_array = $this->keys();
     for ($i=0; $i<sizeof($keys_array); $i++) {
       $keys .= "'" . $keys_array[$i] . "',";
     }
     $keys = substr($keys, 0, -1);

     tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
   }

   function keys() {
     return array('MODULE_SHIPPING_CANADAPOST_STATUS', 'MODULE_SHIPPING_CANADAPOST_SERVERIP', 'MODULE_SHIPPING_CANADAPOST_SERVERPOST', 'MODULE_SHIPPING_CANADAPOST_LANGUAGE', 'MODULE_SHIPPING_CANADAPOST_CPCID', 'MODULE_SHIPPING_CANADAPOST_TIME', 'MODULE_SHIPPING_CANADAPOST_TAX_CLASS', 'MODULE_SHIPPING_CANADAPOST_ZONE', 'MODULE_SHIPPING_CANADAPOST_CP_HANDLING', 'SHIPPING_HANDLING', 'MODULE_SHIPPING_CANADAPOST_SORT_ORDER');
  }


   function _canadapostOrigin($postal, $country){
     $this->_canadapostOriginPostalCode = str_replace(' ', '', $postal);
     $this->_canadapostOriginCountryCode = $country;
   }


   function _canadapostDest($dest_city,$dest_province,$dest_country,$dest_zip){
     $this->dest_city = $dest_city;
     $this->dest_province = $dest_province;
     $this->dest_country = $dest_country;
     $this->dest_zip = str_replace(' ', '', $dest_zip);
   }


   /*
     Add items to parcel.
   */
   
   function _addItem ($quantity, $rate, $weight, $length, $width, $height, $description, $ready_to_ship, $dim_type, $weight_type) {
     $index = $this->items_qty;
     $this->item_quantity[$index] = (string)$quantity;
     $this->item_weight[$index] = ( $weight ? (string)$weight : '0' );
     $this->item_length[$index] = ( $length ? (string)$length : '0' );
     $this->item_width[$index] = ( $width ? (string)$width : '0' );
     $this->item_height[$index] = ( $height ? (string)$height : '0' );
     $this->item_description[$index] = $description;
     $this->item_ready_to_ship[$index] = (string)$ready_to_ship;
     $this->item_dim_type[$index] = (string)$dim_type;
     $this->item_weight_type[$index] = (string)$weight_type;
     $this->items_qty ++;
     $this->items_price += $quantity * $rate;
   }


   /* 
     using HTTP/POST send message to canada post server
   */
   function _sendToHost($host,$port,$method,$path,$data,$useragent=0) {
// Supply a default method of GET if the one passed was empty
if (empty($method))
    $method = 'GET';
$method = strtoupper($method);
if ($method == 'GET')
    $path .= '?' . $data;
$buf = "";
// try to connect to Canada Post server, for 2 second
$fp = @fsockopen($host, $port, $errno, $errstr, 2);

if ( $fp ) {
  fputs($fp, "$method $path HTTP/1.1\n");
  fputs($fp, "Host: $host\n");
  fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
  fputs($fp, "Content-length: " . strlen($data) . "\n");
  if ($useragent)
 fputs($fp, "User-Agent: BetterThanNature.com\n");
  fputs($fp, "Connection: close\n\n");
  if ($method == 'POST')
 fputs($fp, $data);

  while (!feof($fp))
 $buf .= fgets($fp,128);
  fclose($fp);
} else {
  $buf = '<?xml version="1.0" ?><eparcel><error><statusMessage>Cannot reach Canada Post Server. You may refresh this page (Press F5 on your keyboard) to try again.</statusMessage></error></eparcel>'; 
}

return $buf;
   }

   function _canadapostGetQuote() {
$strXML = "<?xml version=\"1.0\" ?>";

// set package configuration.
$strXML .= "<eparcel>\n";
$strXML .= "        <language>" . $this->language . "</language>\n";
$strXML .= "        <ratesAndServicesRequest>\n";
$strXML .= "                <merchantCPCID>" . $this->CPCID . "</merchantCPCID>\n";
$strXML .= "                <fromPostalCode>" . $this->_canadapostOriginPostalCode . "</fromPostalCode>\n";
$strXML .= "                <turnAroundTime>" . $this->turnaround_time . "</turnAroundTime>\n";
$strXML .= "                <itemsPrice>" . (string)$this->items_price . "</itemsPrice>\n";
/*
if ($this->item_dim_type[$i] == 'in') { 
 $strXML .= "                <length>" . ($this->item_length[$i] * (25/10)) . "</length>\n";
 $strXML .= "                <width>" . ($this->item_width[$i] * (25/10)) . "</width>\n";
 $strXML .= "                <height>" . ($this->item_height[$i] * (25/10)) . "</height>\n";
} else {
 $strXML .= "                <length>" . $this->item_length[$i] . "</length>\n";
 $strXML .= "                <width>" . $this->item_width[$i] . "</width>\n";
 $strXML .= "                <height>" . $this->item_height[$i] . "</height>\n";
}
*/

// add items information.
$strXML .= "            <lineItems>\n";
for ($i=0; $i < $this->items_qty; $i++) {
 $strXML .= "     <item>\n";
 $strXML .= "                <quantity>" . $this->item_quantity[$i] . "</quantity>\n";
if ($this->item_weight_type[$i] == 'lbs') {
 $strXML .= "                <weight>" . ($this->item_weight[$i] * (453597/1000000)) . "</weight>\n";
} else {
 $strXML .= "                <weight>" . $this->item_weight[$i] . "</weight>\n";
}
if ($this->item_dim_type[$i] == 'in') { 
 $strXML .= "                <length>" . ($this->item_length[$i] * (254/100)) . "</length>\n";
} else {
 $strXML .= "                <length>" . $this->item_length[$i] . "</length>\n";
}
if ($this->item_dim_type[$i] == 'in') { 
 $strXML .= "                <width>" . ($this->item_width[$i] * (254/100)) . "</width>\n";
} else {
 $strXML .= "                <width>" . $this->item_width[$i] . "</width>\n";
}
if ($this->item_dim_type[$i] == 'in') { 
 $strXML .= "                <height>" . ($this->item_height[$i] * (254/100)) . "</height>\n";
} else {
 $strXML .= "                <height>" . $this->item_height[$i] . "</height>\n";
}
 $strXML .= "                <description>" . $this->item_description[$i] . "</description>\n";
 if ($this->item_ready_to_ship[$i] == '1') { $strXML .= "                <readyToShip/>\n"; }
 $strXML .= "     </item>\n";
}
$strXML .= "           </lineItems>\n";

// add destination information.
$strXML .= "               <city>" . $this->dest_city . "</city>\n";
$strXML .= "               <provOrState>" . $this->dest_province . "</provOrState>\n";
$strXML .= "               <country>" . $this->dest_country . "</country>\n";
$strXML .= "               <postalCode>" . $this->dest_zip . "</postalCode>\n";
$strXML .= "        </ratesAndServicesRequest>\n";
$strXML .= "</eparcel>\n";

// print $strXML;
if ($resultXML = $this->_sendToHost($this->server,$this->port,'POST','',$strXML)) {
 return $this->_parserResult($resultXML);
} else {
    return false;
}
   }	


   /*
     Parser XML message returned by canada post server.
   */
   function _parserResult($resultXML) {
  	 $statusMessage = substr($resultXML, strpos($resultXML, "<statusMessage>")+strlen("<statusMessage>"), strpos($resultXML, "</statusMessage>")-strlen("<statusMessage>")-strpos($resultXML, "<statusMessage>"));
  	 //print "message = $statusMessage";
$cphandling = substr($resultXML, strpos($resultXML, "<handling>")+strlen("<handling>"), strpos($resultXML, "</handling>")-strlen("<handling>")-strpos($resultXML, "<handling>"));
$this->handling_cp = $cphandling;
  	 if ($statusMessage == 'OK') {
     $strProduct = substr($resultXML, strpos($resultXML, "<product id=")+strlen("<product id=>"), strpos($resultXML, "</product>")-strlen("<product id=>")-strpos($resultXML, "<product id="));
     $index = 0;
     $aryProducts = false;
     while (strpos($resultXML, "</product>")) {
	 $cpnumberofboxes = substr_count($resultXML, "<expediterWeight");
	 $this->boxCount = $cpnumberofboxes;
    	 $name = substr($resultXML, strpos($resultXML, "<name>")+strlen("<name>"), strpos($resultXML, "</name>")-strlen("<name>")-strpos($resultXML, "<name>"));
    	 $rate = substr($resultXML, strpos($resultXML, "<rate>")+strlen("<rate>"), strpos($resultXML, "</rate>")-strlen("<rate>")-strpos($resultXML, "<rate>"));
    	 $shippingDate = substr($resultXML, strpos($resultXML, "<shippingDate>")+strlen("<shippingDate>"), strpos($resultXML, "</shippingDate>")-strlen("<shippingDate>")-strpos($resultXML, "<shippingDate>"));
    	 $deliveryDate = substr($resultXML, strpos($resultXML, "<deliveryDate>")+strlen("<deliveryDate>"), strpos($resultXML, "</deliveryDate>")-strlen("<deliveryDate>")-strpos($resultXML, "<deliveryDate>"));
    	 $deliveryDayOfWeek = substr($resultXML, strpos($resultXML, "<deliveryDayOfWeek>")+strlen("<deliveryDayOfWeek>"), strpos($resultXML, "</deliveryDayOfWeek>")-strlen("<deliveryDayOfWeek>")-strpos($resultXML, "<deliveryDayOfWeek>"));
    	 $nextDayAM = substr($resultXML, strpos($resultXML, "<nextDayAM>")+strlen("<nextDayAM>"), strpos($resultXML, "</nextDayAM>")-strlen("<nextDayAM>")-strpos($resultXML, "<nextDayAM>"));
    	 $packingID = substr($resultXML, strpos($resultXML, "<packingID>")+strlen("<packingID>"), strpos($resultXML, "</packingID>")-strlen("<packingID>")-strpos($resultXML, "<packingID>"));
    	 $aryProducts[$index] = array($name . ', ' . $deliveryDate => $rate);
    	 $index++;
    	 $resultXML = substr($resultXML, strpos($resultXML, "</product>") + strlen("</product>"));
     }
     return $aryProducts;
  	 } else {
     if (strpos($resultXML, "<error>")) return $statusMessage;
     else return false;
  	 }
   }
 }
?>

Posted

rather than going thru all that, i would ask the host what upgrades may have gone on over the weekend, also to have them restart the server.

Posted
rather than going thru all that, i would ask the host what upgrades may have gone on over the weekend, also to have them restart the server.

Ok thanks

Posted

oK i restared my server and no luck, I am sure it is Canada Post the problem due to strike, is there a way to see the exact error of the script?

 

Thanks

Frank

Posted

I'd have to agree with you that it has something do to with the strike. I know that when I go to their tracking site, it says that they are having problems & to check back later.... Could be all related!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...