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