Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use ASP to Get shipping rates from UPS.

ASP with API's

Use ASP to Get shipping rates from UPS.

by  schase  Posted    (Edited  )
This will get the rates from UPS and put them into a dropdown box. You do need to sign up with UPS and get an access key and probably a developers key.

http://www.ups.com/content/us/en/bussol/offering/technology/automated_shipping/online_tools.html

Of course change Access number, User ID & Password with your own. Please note I put any dims into a separate page. In this I have also included a method to pull a specific dollar amount for handling charges (strChosen) substitute the sql below for it and jut put strChosen=1, 2, whatever value you want.

Define both elements below.

strCompanyPostCode = whatever zip code you are sending from
orderWeight = the total weight of the package.

Good luck :)

Code:
<% 'create XML to send to UPS to gather rates
strXML= "<?xml version='1.0'?><AccessRequest xml:lang='en-US'><AccessLicenseNumber>xxxxxxxxxxxxxxxx</AccessLicenseNumber><UserId>xxxxxxxxxx</UserId><Password>xxxxxxxxxxxx</Password></AccessRequest>"
strXML=strXML & "<?xml version='1.0'?><RatingServiceSelectionRequest xml:lang='en-US'><Request><TransactionReference><CustomerContext>Rating and Service</CustomerContext><XpciVersion>1.0001</XpciVersion></TransactionReference><RequestAction>Rate</RequestAction><RequestOption>shop</RequestOption></Request>"
strXML=strXML & "<PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address><PostalCode>" & strCompanyPostCode & "</PostalCode></Address></Shipper>"
strXML=strXML & "<ShipTo><Address><PostalCode>" & (request("txtShipPostCode")) & "</PostalCode></Address></ShipTo>"
strXML=strXML & "<Service><Code>11</Code></Service>"
strXML=strXML & "<Package><PackagingType><Code>02</Code><Description>Package</Description></PackagingType><Description>Rate Shopping</Description>"
strXML=strXML & "<PackageWeight><Weight>" & orderWeight & "</Weight></PackageWeight></Package>"
strXML=strXML & "<ShipmentServiceOptions/></Shipment></RatingServiceSelectionRequest>" 
Set strXMLhttp = Server.CreateObject("MSXML2.ServerXMLHTTP") 
'send it
strXMLhttp.Open "POST","https://www.ups.com/ups.app/xml/Rate?",false 
strXMLhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
strXMLhttp.send strXML
'pull any shipping surcharges from the database that we set up. 
BDSQL="Select fldShipping from tblShipping where fldVisible=1;"
set BDTemp=BDConn.execute(BDSQL)
IF BDTemp.EOF THEN
strChosen=0
else
strChosen=BDTemp("fldShipping")
END IF
'the response from UPS
strXMLResponse = strXMLhttp.responseText 
Set mydoc=Server.CreateObject("Microsoft.xmlDOM") 
mydoc.loadxml(strXMLResponse) 
'Create A Nodelist of All The RatedShipments 
Set NodeList = mydoc.documentElement.selectNodes("RatedShipment") 
For strCount = 0 To NodeList.length - 1 
'Service/Code 
'put into session value any codes we choose to use - being Next day, 2nd day, 3 day and ground.
Select case NodeList.Item(strCount).selectSingleNode("Service/Code").Text
 case "01"
session("svUPS3")="UPS Next Day Air"
session("svUPSCost3")=(cdbl(NodeList.Item(strCount).selectSingleNode("TotalCharges/MonetaryValue").Text)+cdbl(strChosen)) 
session("svUPSOrder3")=3
strCount2=strCount2+1
 case "02"
session("svUPS2")="UPS 2nd Day Air"
session("svUPSCost2")=(cdbl(NodeList.Item(strCount).selectSingleNode("TotalCharges/MonetaryValue").Text)+cdbl(strChosen))
session("svUPSOrder2")=2 
strCount2=strCount2+1
 case "12"
session("svUPS1")="UPS 3 Day Select"
session("svUPSCost1")=(cdbl(NodeList.Item(strCount).selectSingleNode("TotalCharges/MonetaryValue").Text)+cdbl(strChosen))
session("svUPSOrder1")=1
strCount2=strCount2+1
 case "03"
session("svUPS0")="UPS Ground"
session("svUPSCost0")=(cdbl(NodeList.Item(strCount).selectSingleNode("TotalCharges/MonetaryValue").Text )+cdbl(strChosen))
session("svUPSOrder0")=0
strCount2=strCount2+1
END SELECT
NEXT
session("svCount")=clng(strCount2)
'Create a select table from the response xml 
response.Write("<select name=""txtShipping"" class=""searchfield"">")  & vbcrlf
IF (request("txtShipping"))="0" or (request("txtShipping"))="" or (request("txtshipping"))="&nbsp;" or isNULL((request("txtshipping"))) THEN 
response.write "<Option value=""0"" selected><-- Select Shipping --></option>"  & vbcrlf
ELSE
response.write "<Option value=""0""><-- Select Shipping --></option>"  & vbcrlf
END IF
'TotalCharges/MonetaryValue
For strCount=0 to (clng(session("svCount"))-1) 
IF (request("txtShipping"))= Session("svUPS"&strCount) THEN
response.write("<option value=""" & Session("svUPS"&strCount) & """ selected>") 
ELSE
response.write("<option value=""" & Session("svUPS"&strCount) & """>") 
END IF
response.write Session("svUPS"&strCount) & " - " & formatcurrency((Session("svUPSCost"&strCount)),2)
response.write("</option>") & vbcrlf 
Next 
response.Write("</select>") & vbcrlf
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top