Pos Malaysia Shipping Widget for your website

February 26th, 2010 | by Sean |

(If you’re in Malaysia, the latency to spider.my’s server in the USA is going to make this appear awful. As long as my local Internet bomoh is smiling at me, you might be able to see a Malaysian copy at http://pos.spider.my/ – just don’t depend on it being available.)

I’ve never written a widget before! This one hasn’t turned out bad – try changing the parcel weight and destination. See? It works! And you can have it*1 on your website too! All you need to do is to add this little bit of HTML:

<div id=”pos.spider.box”><script type=”text/javascript” src=”http://spider.my/static/js/posmalaysia4.js”></script></div>

…and next time you look at your webpage, it’ll have Pos Malaysia shipping rates on it, courtesy of spider.my and a very dodgy browser-security workaround to allow cross-domain … actually it isn’t really AJAX, it’s more like Asynchronous JavaScript And More JavaScript, but you’ll have to wait for the HowTo post to see how I do it (or just view my page source).

At the start of the week I said I’d demonstrate a how a Pos Malaysia API (Application Programmer’s Interface) should work. As a recap, it was to have two major parts: a simple, reliable, fast technique for obtaining a single quote in a format that can be readily used by any application, and a technique for providing a complete set of rating tables for applications that need higher performance or reliability, but also want easy access to updated rates.

It has actually been a very solid week’s work, but it’s now all done. On Monday, I demonstrated what the problem is. Pos Malaysia’s online shipping quotation page is OK*2 for humans to look at, but is too ‘fat’ and arbitrarily formatted for efficient and reliable use in e-commerce. I hinted at an AJAX replacement by caching quotes on my ‘old-technique’ page.

On Tuesday I started ‘ripping’ Pos Malaysia’s shipping rates. The whole problem is the difficulty of access to their rates, so having my own set would make the rest of the demo much easier. To avoid upsetting the Pos Malaysia sysadmin, I wrote a set of persistent classes for the rating data on spider.my, so that I would only have to make the hundreds of requests needed to rip the data just once.

On Wednesday I tidied up the server-side access to the ripped rates and provided a reliable AJAX interface which accepts a weight and a destination and returns quotes from all qualifying methods. I currently only have Pos Laju Document and Parcel rates, so if you ask for a weight over 1kg, you get only 1 quote, 2 quotes for anything lighter (now more quotes since Pos Parcel rates added). At this stage, part 1 of the job is finished: there’s now a simple technique for Pos Malaysia’s customers and partners to instantly acquire a single quote.

The method for complete rating data updates was quite straightforward to provide once the server-side persistence was finished. On Thursday I provided a rates download facility which can give full rating data for all countries and all methods, all methods for one country, or all countries for a single method. I have an e-commerce project ‘on a back burner’ at the moment which will use stored shipping rates to simplify and speed up its checkout. With Pos Malaysia’s current online facility that’s practically impossible. If Pos provided this kind of API it would be straightforward.

As a demonstration piece, I started writing a ‘widget’ that can be used on anybody’s blog or website on Thursday night. I’ve never written much in the way of javascript before this week, so I’ve been spending a lot of time at w3schools.com! I thought I’d finished the widget last night, but discovered the “Same Origin Policy” which prevents javascript on a page from one website loading data from another website. There seem to be a lot of very complicated solutions for this problem, but this afternoon I stumbled on a simple one.

The widget is not very pretty, but I’ve got no intention of providing shipping rates from spider.my in the long run. I want Pos Malaysia to allow me to set up this API (or something similar – there are already improvements I have in mind!) on their pos.com.my domain – possibly as api.pos.com.my – so that everyone who uses Pos’ services in their e-businesses can benefit. If Pos likes the widget idea, then I think a little bit of effort to make it more ‘beautiful’ will be good for Pos, as they’ll have their branding on other people’s websites.

If Pos wants my help to get this up and running on their server, I will do it FOR FREE. The benefit to me in having a reliable quotation system in place at Pos will be huge for my own projects. If other businesses in Malaysia also benefit, and if Pos Malaysia win some prizes for competent IT, then I’d like to think I might get some free cups of teh tarik, or some wang ehsan or something. I’ll let you know how I get on with Pos next week.

*1 – Yes, you can put it on your blog or website. It’s not very pretty, but if you’d like to try it out I would be very pleased that you did. Let me know what you think of it functionally. If any Malaysians with design skills fancy demonstrating just how beautiful a Pos Malaysia widget could be, give it your best shot and if it looks nice, I’ll adopt your design, and give you all the credit (but no cash, soz. You could try asking Pos, I guess). One last thing – this is very experimental, so it might stop working from time to time (might, but might also keep going forever), and watch out for the rates: they don’t include Pos’ silly random surcharges.

*2 – OK… but it needs some basic maintenance! There are very many quotes which claim to allow shipments up to 999kg. It looks like Pos has never got round to setting the maximum values for all their countries. Also the country names – some of them are oddly styled like ‘Ukraine (Kiev)’ and ‘Serbia & Montenegro’. The latter hasn’t existed since 2006, according to Wikipedia! The technique Pos uses to request a quote from their server doesn’t actually work with names like these (try getting a quote for shipping to ‘Serbia & Montenegro’ from Pos’ website). The names form the URL for the quote, but contain invalid characters for URLs, so some countries return errors instead of quotes!


Share and Enjoy:
  • Digg
  • del.icio.us
  • Fark
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • email
  • Facebook
  • Furl
  • Mixx
  • Print
  • Sphinn
  1. 2 Trackback(s)

  2. Feb 28, 2010: Cross-domain javascript widget, no JSON, no AJAX
  3. Mar 1, 2010: NEW: Pos Malaysia Parcel rates

Post a Comment