osCommerce V2.3 Canada Post Shipping Service (REST) Module

Canada Post changed its previous Sell Online services with the new REST or SOAP services. Searching the web or in the community contributions, cannot find anything based on this new services. So I decided to write my own module.

REST or SOAP?

Canada Post support two services: REST and SOAP, so which one is the right one? The demo website (it is a demonstration site for a template based on osCommerce V2.3, do not place any order and pay with your credit card!) I am testing on is hosted on IPOWER, I was having problem with the SOAP service, since by default, the SOAP module is not enabled! The REST service is based on XML and is available almost on any hosting services, so I turned to the REST service for Canada Post.

What do you need?

In order to use the Canada Post service, you will need to have a customer number and API Keys from them. Please refer to here for more details.

Dimension support

The shipping rate for Canada Post service is based on parcel weight, it also support the dimensions (length/height/width) to get more accurate rate. So both are considered in my implementation. This module also supports packing, for example, pack several small items into one box for shipping. The packing support is based on one of the community contribution for upsxml, so the credits goes to the original contributor, although I have modified it to work better with osCommerce V2.3.

The setup screen for Canada Post shipping module

setup screen shot Canada Post shipping module for osCommerce

Now here is the shopping cart check out screen using Canada Post Service

This Canada Post shipping module supports shipping to Canada, US and other countries (internationally). There is one thing I do not like about this Canada Post service is, if I have more than one parcel to send, I have to call Canada Post service twice to get the rates separately. Unlike UPS, you could define all the items in one request. But anyway, it works this way and I am happy it is been done.

About these ads

26 thoughts on “osCommerce V2.3 Canada Post Shipping Service (REST) Module

  1. Turns out that there are sample codes on the Canada Post web site mentioned above – look in the CP side-bar for “code samples REST | SOAP” when you are logged in the developers section. Now implementing them may be tricky, so any suggestions would be great!
    Thanks again!

    • the core (request for Canada Post rating service) is based on the sample code and the document, the sample code from Canada Post is quite simple, it does not have dimension, insurance and shipping to US or other countries. Below are the code to call to Canada Post REST rating service and the reqeust building function.

      The call to Canada Post REST rating service

       function _post($xmlRequest) {
              $url = $this->protocol."://".$this->host.":".$this->port.$this->path;
      
      		$curl = curl_init($url); // Create REST Request
      		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
      		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
      		curl_setopt($curl, CURLOPT_CAINFO, realpath(dirname($argv[0])) . '/includes/modules/shipping/canadapost/cacert.pem'); // Signer Certificate in PEM format
      		curl_setopt($curl, CURLOPT_POST, true);
      		curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlRequest);
      		curl_setopt($curl, CURLOPT_TIMEOUT, (int)$this->timeout);
      		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      		curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
      		curl_setopt($curl, CURLOPT_USERPWD, $this->access_username . ':' . $this->access_password);
      		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/vnd.cpc.ship.rate+xml', 'Accept: application/vnd.cpc.ship.rate+xml'));
      		$curl_response = curl_exec($curl); // Execute REST Request
      	
      		// send email if enabled in the admin section
      		if (curl_errno($curl) && $this->email_errors) {
      			$error_from_curl = sprintf('Error [%d]: %s', curl_errno($curl), curl_error($curl));
      			error_log("Error from cURL: " . $error_from_curl . " experienced by customer with id " . $_SESSION['customer_id'] . " on " . date('Y-m-d H:i:s'), 1, STORE_OWNER_EMAIL_ADDRESS);
      		}
      		
      		curl_close ($curl);
      ....
      }
      

      Building the rating request

      
      function _GetRequestXml($index)
        {
      	...	  
      	$xmlRequest ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
      	<mailing-scenario xmlns=\"http://www.canadapost.ca/ws/ship/rate\">
      	  <customer-number>$this->access_customer_number</customer-number>
      	  <quote-type>$this->quotetype</quote-type>";
      	//if there is a contract  
      	if($this->contract_id) {
      	   $xmlRequest .= "<contract-id>$this->contract_id</contract-id>";	
      	}  
      	//if set to insure the packages 
      	if (($this->use_insurance) && ($price>0)) {
      		$xmlRequest .= "<options><option>
      			  <option-code>COV</option-code>
      			  <option-amount>$price</option-amount>
      			</option>
      		</options>";
      	}
      	$xmlRequest .= "  <parcel-characteristics>
      		<weight>$weight</weight>";
      	//add dimension support	
      	if (($this->dimensions_support>0) && ($width>0)	 && ($height>0)	 && ($length>0)) {
      	   $xmlRequest .= "<dimensions><length>$length</length>
      	   	<width>$width</width>
      		<height>$height</height>
      	   </dimensions>";
      	}
      	$xmlRequest .= "</parcel-characteristics>
      	  <origin-postal-code>$this->_canadaPostOriginPostalCode</origin-postal-code>
      	  <destination>";
      	  if($this->_canadaPostDestCountryCode=='CA') {
      		$xmlRequest .="<domestic>
      		  <postal-code>$this->_canadaPostDestPostalCode</postal-code>
      		</domestic>";
      	  } else if($this->_canadaPostDestCountryCode=='US') {
      		$xmlRequest .="<united-states>
      		  <zip-code>$this->_canadaPostDestPostalCode</zip-code>
      		</united-states>";
      	  } else {
      		$xmlRequest .="<international>
      		  <country-code>$this->_canadaPostDestCountryCode</country-code>
      		</international>";
      	  }
      	$xmlRequest .="  </destination>
      	</mailing-scenario>";
      	//echo $xmlRequest;
      	return $xmlRequest;
        } //end of function _GetRequestXml
      
  2. Is this module you describe about available somewhere for download? I’m desperately trying to find a solution to implement Canada Post Shipping in osCommerce but can’t seem to find anything that works with the new web services. Thanks for any help you can Provide

  3. Do you happen to know if the Code you developed for use with Canada Post and OS COmmerce will work with Tomatocart? I’m not a programmer by any stretch (more of a cutter and paster) and am looking for a working implementation we can work with.

  4. Hi Sean. I would love to get a hold of your module code, if you would be at all willing to share it with me. I’ve looked at the Add-Ons library on the OS Commerce site, and haven’t been able to find anything current that is working for Canada Post. If your module is fully functional in OSC 2.3, I’d be more than willing to shoot you over some money via PayPal for the share. Let me know. Thanks in advance!

  5. So, have you developed a module for download then? I am looking for one, and am about to scratch build one, but I assume by your screen shot of the settings you have already made one. Is it available?

    Thanks,
    Chad

  6. Has anyone tried porting this to ZenCart? I understand the changes are minimal to make it work.
    I’d be interested in paying (not a lot, we’re a small company, but something!) to port it over. The only ZenCart module currently still uses sellonline :(

  7. Sean, how did you get this working with zencart 1.39? The categories.php and the modules.php files on my current site have many mods already so I was going to do the line by line editing as suggested in your Canada Post readme.txt, but the lines of code you say to look for in the original files don’t exist! I did a content comparison and for all intents and purposes, the code is, for the most part, completely different. I did all the file subs and overwrites with your files and did the sql inserts and when I open the Modules/Shipping in the admin section, the screen is blank–as in totally white blank? I looked through the code for zencart 1.39 versions a to h and the same problem. So how did you get it to work?

    Dave

    • This module is for osCommerce only and it will not work for ZenCart, and as Canada Post have updated their web service to newer version, the module here would not work for the new service. I have modified the module for the new Canada Post web service, but so far it has not released to the public. As for Zencart (V1.39), I have recently completed the Canada Post shipping module, but as said, it would be a separate module and by no means the module stated in this post would work for Zencart.
      Sean

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s