Aug 21 2008

Getting Shipping Rates from USPS

Published by at 11:34 am under Flex

I have been working on a Flash/Flex shopping cart which uses AMFPHP to connect with OSCommerce and you can manage your cart using OScommerce. 

 

The client wanted to get shipping prices based on the weight and it had to be USPS. It turned out to be a really easy integration, I have used Flex but it would be the same using Flash as well. 

 

The only problem is when you hit the testing server of USPS they expect the xml to be exactly the same as they specify in the examples and also Rate3 API’s don’t work well. Shown below is the simple Flex code to get response from the testing server,

 

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”sendService()”>

    <mx:Script>

        <![CDATA[

            import mx.controls.Alert;

            private function errorMsg(info:Object):void{

                var data:Object = info;

            }

            private function gotBack(event:Object):void{

                var data:Object = event;

                mx.controls.Alert.show(event.result.Error.Description);

            }

            private function sendService():void{

                var xmlData:String = ‘<RateV2Request USERID=”xxxxxx”><Package                                                                         ID=”0″><Service>PRIORITY</Service>’ + 

                     ‘<ZipOrigination>10022</ZipOrigination><ZipDestination>20008</ZipDestination>’                                     + ‘<Pounds>10</Pounds><Ounces>5</Ounces><Container>Flat Rate Box</Container><Size>REGULAR</Size></Package></RateV2Request>’;

                var requestObj:Object = new Object();

                requestObj.XML = new XML(xmlData).toXMLString();

                requestObj.API = “RateV2”;

                upsService.request = requestObj;

                upsService.send();

            }

]]>

</mx:Script>

<mx:HTTPService id=”upsService” url=”http://testing.shippingapis.com/ShippingAPITest.dll” result=”gotBack(event)”

  fault=”errorMsg(event)” resultFormat=”e4x” />

</mx:Application>

 

As you can see there is not much to it, you just have to use a simple httpservice and the parameters required is the XML and the API. You can request upto 25 rates and the details can be found on the usps site out here.

 

USPS will return an XML even if there is an error and for production you have to move to a different server but the basic things still remain the same. Now only if they would have a crossdomain.xml on their servers, have put in a request for that but i doubt they would be willing to have it. *sigh*

One response so far

One Response to “Getting Shipping Rates from USPS”

  1. Timon 27 Dec 2015 at 2:43 pm

    I’ve received this scam two days in a row. The first time I did click on the amntchteat, but there was nothing intelligible there and my antivirus software scolded me and killed it. Note that the syntax is very poor (for example procedure and conditions of parcels keeping and Thank you for attention ), suggesting foreign origin.

Trackback URI | Comments RSS

Leave a Reply