ASPDesigns.com Just another WordPress weblog

6Sep/09

Calling the UPS ratefinder API in Classic ASP

Last week I started this blog with some quick and dirty code for calling the rate finder API from the USPS. As with most API's, Classic ASP samples are hard to come by these days. So here is a sample of the UPS code. Remember, you have to have a web tools account with UPS which you can get here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<%
 
 
ship_to_zip=33323
weight=5
package_length=6
package_width=6
package_height=6
residential=true
 
 
xml_request= "<?xml version='1.0'?>"
xml_request = xml_request & "<accessRequest xml:lang='en-US'>"
xml_request = xml_request & "<accessLicenseNumber>YOUR_ACCESS_LICENSE_NUMBER</accessLicenseNumber>"
xml_request = xml_request & "<userId>YOUR_USER_NAME</userId>"
xml_request = xml_request & "<password>YOUR_PASSWORD</password>"
xml_request = xml_request & "</accessRequest>"
 
xml_request = xml_request & "<?xml version='1.0'?>"
xml_request = xml_request & "<ratingServiceSelectionRequest xml:lang='en-US'>"
xml_request = xml_request & "<request>"
xml_request = xml_request & "<transactionReference>"
xml_request = xml_request & "<customerContext>Rating and Service</customerContext>"
xml_request = xml_request & "<xpciVersion>1.0001</xpciVersion>"
xml_request = xml_request & "</transactionReference>"
xml_request = xml_request & "<requestAction>Rate</requestAction>"
xml_request = xml_request & "<requestOption>Rate</requestOption>"
xml_request = xml_request & "</request>"
xml_request = xml_request & "<pickupType>"
xml_request = xml_request & "<code>01</code>"
xml_request = xml_request & "</pickupType>"
xml_request = xml_request & "<shipment>"
xml_request = xml_request & "<shipper>"
xml_request = xml_request & "<shipperNumber>YOUR_SHIPPER_NUMBER</shipperNumber>"
xml_request = xml_request & "<address>"
xml_request = xml_request & "<postalCode>YOUR_POSTAL_CODE</postalCode>"
xml_request = xml_request & "</address>"
xml_request = xml_request & "</shipper>"
xml_request = xml_request & "<shipTo>"
xml_request = xml_request & "<address>"
xml_request = xml_request & "<postalCode>" & ship_to_zip & "</postalCode>"
if (residential) then
xml_request = xml_request & "<residentialAddressIndicator/>"
end if
xml_request = xml_request & "</address>"
xml_request = xml_request & "</shipTo>"
xml_request = xml_request & "<service>"
xml_request = xml_request & "<code>" & ups_code & "</code>"
xml_request = xml_request & "</service>"
xml_request = xml_request & "<package>"
xml_request = xml_request & "<packagingType>"
xml_request = xml_request & "<code>02</code>"
xml_request = xml_request & "<description>Package</description>"
xml_request = xml_request & "</packagingType>"
xml_request = xml_request & "<description>Rate Shopping</description>"
xml_request = xml_request & "<packageWeight>"
xml_request = xml_request & "<weight>" & weight & "</weight>"
xml_request = xml_request & "</packageWeight>"
xml_request = xml_request & "<dimensions>"
xml_request = xml_request & "<length>" & package_length & "</length>"
xml_request = xml_request & "<width>" & package_width & "</width>"
xml_request = xml_request & "<height>" & package_height & "</height>"
xml_request = xml_request & "</dimensions>"
xml_request = xml_request & "</package>"
xml_request = xml_request & "<shipmentServiceOptions/>"
xml_request = xml_request & "</shipment>"
xml_request = xml_request & "</ratingServiceSelectionRequest>"
 
'uncomment to see response in case of error
'response.write xml_request

URLToRSS = "https://wwwcie.ups.com/ups.app/xml/Rate"
 
Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
' resolve, connect, send, receive - in milliseconds
xmlHttp.setTimeouts 10000, 60000, 10000, 10000
xmlHttp.Open "Post", URLToRSS, false
 
xmlHttp.Send(xml_request)
set RSSXML = xmlHttp.ResponseXML
 
Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
xmlDOM.async = false
xmlDOM.setProperty "SelectionLanguage", "XPath"
xmlDOM.Load(RSSXML)
 
if xmlDOM.parseError.errorCode <> 0 then
 
response.write "DOM Not Loaded"
 
end if
 
'check for error
xmlQuery = "*/Error"
 
set errorNode = xmlDOM.documentElement.selectNodes(xmlQuery)
 
if not errorNode is nothing then
	errorLength = errorNode.length
		if errorLength > 0 then
			rate_error_severity= errorNode.item(0).selectSingleNode("ErrorSeverity").nodeTypedValue
			rate_error_code= errorNode.item(0).selectSingleNode("ErrorCode").nodeTypedValue
			rate_error_description= errorNode.item(0).selectSingleNode("ErrorDescription").nodeTypedValue
		end if
end if
 
if rate_error_severity="Hard" then
 
	total= 0
	rate_error= rate_error_description
	'response.end()

else
 
rate_error=""
 
xmlQuery = "/RatingServiceSelectionResponse/RatedShipment"
set docHeadlines = xmlDOM.documentElement.selectNodes(xmlQuery)
 
total= docHeadlines.item(0).selectSingleNode("TotalCharges").selectSingleNode("MonetaryValue").nodeTypedValue
GuaranteedDaysToDelivery= docHeadlines.item(0).selectSingleNode("GuaranteedDaysToDelivery").nodeTypedValue
ScheduledDeliveryTime= docHeadlines.item(0).selectSingleNode("ScheduledDeliveryTime").nodeTypedValue
 
end if
%>
Tagged as: , , , 2 Comments
2Sep/09

Calling the USPS API via Classic ASP

For years I have been thinking about writing an asp interface for the UPS and USPS rate finder API.  But since I had a com component installed on the one server I used it on, I always put it off and used the component.  As luck would have it, one Sunday a few weeks ago (it's always a Sunday) the component decided to return errors from UPS.  Of course, the component writer is long gone, so it was time to write the interface I had been contemplating for years.  Now mind you, this was on a very busy ecommerce site, so I was in a rush.

Surprisingly, I had some asp code up and running quickly.  Here is the simple code in ASP that calls the USPS API and reads the results.  This can be plugged in easily into any code that needs the USPS rate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<%
'needs the following variables

weight=1
ship_to_zip=33323
package_width=6
package_length=6
'package_height=6

if int(weight) <> weight then
	weight=int(weight) + 1
end if
 
if package_width < 6 then package_width=6
if package_length < 6 then package_length=6
if package_height < 6 then package_height=6
 
xml_request= "<?xml version='1.0'?>"
xml_request = xml_request & "<rateV3Request USERID='YOUR_USERID'>"
 
if usps_service="" then
usps_service="PRIORITY"
end if
 
xml_request = xml_request & "<package ID='1ST'>"
xml_request = xml_request & "<service>" & usps_service & "</service>"
xml_request = xml_request & "<zipOrigination>33323</zipOrigination>"
xml_request = xml_request & "<zipDestination>" & ship_to_zip & "</zipDestination>"
xml_request = xml_request & "<pounds>" & weight & "</pounds>"
xml_request = xml_request & "<ounces>0</ounces>"
xml_request = xml_request & "<container></container>"
xml_request = xml_request & "<size>REGULAR</size>"
xml_request = xml_request & "<width>" & package_width & "</width>"
xml_request = xml_request & "<length>" & package_length & "</length>"
xml_request = xml_request & "<height>" & package_height & "</height>"
'xml_request = xml_request & "<girth>55</girth>"
if usps_service="PARCEL POST" then
xml_request = xml_request & "<machinable>true</machinable>"
end if
xml_request = xml_request & "</package>"
xml_request = xml_request & "</rateV3Request>"
 
URLToRSS = "http://Production.ShippingAPIs.com/ShippingAPI.dll"
 
Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlHttp.Open "Post", URLToRSS, false
xmlHttp.Send("API=RateV3&XML=" & xml_request)
 
'uncomment below to see what USPS API is returning
'Response.write xmlHttp.ResponseTEXT

Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
xmlDOM.async = false
xmlDOM.setProperty "SelectionLanguage", "XPath"
xmlDOM.LoadXML xmlHttp.ResponseTEXT
 
if xmlDOM.parseError.errorCode <> 0 then
 
	response.write "DOM Not Loaded"
 
end if
 
xmlQuery = "/RateV3Response/Package/Postage"
set docHeadlines = xmlDOM.documentElement.selectNodes(xmlQuery)
rate_error=""
total=0
total= docHeadlines.item(0).selectSingleNode("Rate").nodeTypedValue
 
'returns total with rate
%>