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

8 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.

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 )

Connecting to %s