Canada Post Shipping API Integration with ColdFusion

Canada Post Shipping API Integration using ColdFusion
Today, we will discuss about how to integrate Canada Post Shipping API using ColdFusion. If you have searched for Canada Post API code, you might have already noticed that there is no SDK, open source code or a tag readily available. So, we want to help you in doing it by yourself in simple steps. Here is the code sample we give here can be extended to call other Canada Post API methods.
CanadaPost.cfm

<!--- Step1: Initialize all the variables (Canada Post account details, shipping location, parcel dimensions, etc) --->

<cfscript>
apiKey = "xxxxxxxxxxxxxxxx : xxxxxxxxxxxxxxxxxxx";
username = ListFirst(apiKey,':');
password = ListLast(apiKey,':');
customerNumber = "xxxxxxx";
serviceURL = "https://soa-gw.canadapost.ca/rs/ship/price";
originPostalCode = "xxxxxx";
postalCode = "xxxxxx";
countryCode = "CA";
weight = "1";
length = "1";
width = "1";
height = "1";
</cfscript>

<!--- Step2: Construct xml to hit the api and get shipping options and prices --->

<cfsavecontent variable="xmlRequest">
<?xml version="1.0" encoding="utf-8"?>
<mailing-scenario xmlns="http://www.canadapost.ca/ws/ship/rate">
<customer-number>0008114585</customer-number>
<parcel-characteristics>
<dimensions>
<length>#NumberFormat(length, ".9")#</length>
<width>#NumberFormat(width, ".9")#</width>
<height>#NumberFormat(height, ".9")#</height>
</dimensions>
<weight>#NumberFormat(weight, ".9")#</weight>
</parcel-characteristics>
<origin-postal-code>#originPostalCode#</origin-postal-code>
<destination>
<cfif countryCode EQ 'CA'>
<domestic>
<postal-code>#postalCode#</postal-code>
</domestic>
<cfelseif countryCode EQ 'us'>
<united-states>
<zip-code>#postalCode#</zip-code>
</united-states>
<cfelse>
<international>
<country-code>#countryCode#</country-code>
</international>
</cfif>
</destination>
</mailing-scenario>
</cfsavecontent>


<!--- Step3: Post request to Canada Post API --->

<cfhttp
url="#serviceURL#"
method="post"
result="httpResponse"
username="#username#" password="#password#">
<cfhttpparam
type="header"
name="accept-encoding"
value="no-compression"
/>
<cfhttpparam
type="xml"
value="#trim(xmlRequest)#"
/>
<cfhttpparam type="header" name="Content-type" value="application/vnd.cpc.ship.rate+xml">
</cfhttp>

<!--- Step4: Parse the XML response from the API and display the results --->

<cfscript>
shipQuotes = xmlparse(httpResponse.Filecontent);
quoteDetail = shipQuotes["price-quotes"]["price-quote"];
quotesNum = ArrayLen(thisquote)
</cfscript>

<table border="1">
<tr>
<th>Service Code</th>
<th>Servie Name</th>
<th>Service Fee</th>
</tr>
<cfoutput>
<cfloop from="1" to="#quotesNum#" index="i">
<tr>
<td>#quoteDetail[i]["service-code"].XmlText#</td>
<td>#quoteDetail[i]["service-name"].XmlText#</td>
<td>#quoteDetail[i]["price-details"].due.XmlText#</td>
</tr>
</cfloop>
</cfoutput>
</table>

That’s it! You are all set to use CanadaPost API on your ColdFusion website / application.

If you have any questions or problems, please contact us.

References: Canada Post Getting Started Guide

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>