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 FedEx

ASP with API's

Use ASP to get shipping rates from FedEx

by  schase  Posted    (Edited  )
This one they make you jump through some hoops. First you have to register for the API http://www.fedex.com/us/solutions/wis/

You can get some of the documentation also, mainly the XML Transactions Guide and the ship manager direct. Inside the XML Transaction Guide we will be using the FDXSubscriptionRequest & the FDXRateAvailableServices.

Now you have to set up the subscription request to test on their testing platform. And email them to be put onto the testing platform. Doing the subscription request will give you your meter number. Copy all of the code below and change the elements to fit your use. then run the page.

Code:
<html>
<body>
<%
xmltext="<?xml version='1.0' encoding='UTF-8' ?>"
xmltext=xmltext & "<FDXSubscriptionRequest xmlns:api='http://www.fedex.com/fsmapi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='FDXSubscriptionRequest.xsd'>"
xmltext=xmltext & "<RequestHeader>"
xmltext=xmltext & " <CustomerTransactionIdentifier>String</CustomerTransactionIdentifier>"
xmltext=xmltext & " <AccountNumber>*********</AccountNumber>"
xmltext=xmltext & "</RequestHeader>"
xmltext=xmltext & "<Contact>"
xmltext=xmltext & " <PersonName>contact name</PersonName>"
xmltext=xmltext & " <CompanyName>companyname</CompanyName>"
xmltext=xmltext & " <Department>department name</Department>"
xmltext=xmltext & " <PhoneNumber>main number</PhoneNumber>"
xmltext=xmltext & " <FaxNumber>faxnumber</FaxNumber>"
xmltext=xmltext & " <E-MailAddress>you@yourdomain.com</E-MailAddress>"
xmltext=xmltext & "</Contact>"
xmltext=xmltext & "<Address>"
xmltext=xmltext & " <Line1>your address</Line1>"
xmltext=xmltext & " <Line2>if any more address</Line2>"
xmltext=xmltext & " <City>your city</City>"
xmltext=xmltext & " <StateOrProvinceCode>2 digit state</StateOrProvinceCode>"
xmltext=xmltext & " <PostalCode>zip</PostalCode>"
xmltext=xmltext & " <CountryCode>US</CountryCode>"
xmltext=xmltext & "</Address>"
xmltext=xmltext & "</FDXSubscriptionRequest>"
Set strXMLhttp = Server.CreateObject("MSXML2.ServerXMLHTTP") 
strXMLhttp.Open "POST","https://gateway.fedex.com:443/GatewayDC",false 
strXMLhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
strXMLhttp.send xmltext
strResponseMessage = strXMLhttp.responseText
intHTTPStatusCode = strXMLhttp.status
response.write strResponseMessage & "!<BR>"
response.write intHTTPStatusCode & "!!<BR>"
response.write strHTTPStatusText & "!!!<BR>"
responsexml = strResponseMessage
Set mydoc=Server.CreateObject("Microsoft.xmlDOM") 
mydoc.loadxml(responsexml) 
for each x in mydoc.documentElement.childNodes
   if x.NodeName = "to" then name=x.text
next
response.write(name)
%>

right click on the resulting page and look for the number between the meter tag - there is your testing environment meter number.

Now construct your asp to test pulling rates. here is mine - look at the UPS and USPS if you have questions on how it may fit together.

define the following elements.

orderweight - the weight of the shipment
strCompanyPostCode
strCompanyState

Obviously put in your own Account number and meter number. Notice there are two sections to it. One is for Express and the second for ground. In my example I put the results into sessions, then filter the sessions out to display in a specific order. As I will not offer all of FedEx's services I will only select certain ones. After all of that, I will put them into a drop down - this example only puts them into option selections.

Code:
<%
strCount=0
'amount to add for a surcharge of shipping and handling
strChosen=1
'add an arbitrary date  I use mySQL here so I needed to put it into mySQL format
strUKDate=DateAdd("d",1,Date())
MYdd = DatePart("d", strUKDate)
MYmm = DatePart("m", strUKDate)
MYyy = DatePart("yyyy", strUKDate)
strUKDate = (MYyy & "-" & MYmm & "-" & MYdd)
'construct request for Express rates
strXML="<?xml version='1.0' encoding='UTF-8' ?>"
strXML=strXML & "<FDXRateAvailableServicesRequest xmlns:api='http://www.fedex.com/fsmapi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='FDXRateAvailableServicesRequest.xsd'>"
strXML=strXML & "<RequestHeader>"
strXML=strXML & "<AccountNumber>999999999</AccountNumber>"
strXML=strXML & "<MeterNumber>9999999</MeterNumber>"
strXML=strXML & "<CarrierCode>FDXE</CarrierCode>"
strXML=strXML & "</RequestHeader>"
strXML=strXML & "<ShipDate>" & strUKDate & "</ShipDate>"
strXML=strXML & "<DropoffType>REGULARPICKUP</DropoffType>"
strXML=strXML & "<Packaging>YOURPACKAGING</Packaging>"
strXML=strXML & "<WeightUnits>LBS</WeightUnits>"
strXML=strXML & "<Weight>" & formatnumber(orderWeight,1) & "</Weight>"
strXML=strXML & "<ListRate>false</ListRate>"
strXML=strXML & "<OriginAddress>"
strXML=strXML & "<StateOrProvinceCode>" & strCompanyState & "</StateOrProvinceCode>"
strXML=strXML & "<PostalCode>" & strCompanyPostCode & "</PostalCode>"
strXML=strXML & "<CountryCode>US</CountryCode>"
strXML=strXML & "</OriginAddress>"
strXML=strXML & "<DestinationAddress>"
strXML=strXML & "<StateOrProvinceCode>" & BDQuotes(request("txtShipState")) & "</StateOrProvinceCode>"
strXML=strXML & "<PostalCode>" & BDQuotes(request("txtShipPostCode")) & "</PostalCode>"
strXML=strXML & "<CountryCode>US</CountryCode>"
strXML=strXML & "</DestinationAddress>"
strXML=strXML & "<Payment>"
strXML=strXML & "<PayorType>SENDER</PayorType>"
strXML=strXML & "</Payment>"
strXML=strXML & "<PackageCount>1</PackageCount>"
strXML=strXML & "</FDXRateAvailableServicesRequest>"
'send it
Set strXMLhttp = Server.CreateObject("MSXML2.ServerXMLHTTP") 
strXMLhttp.Open "POST","https://gatewaybeta.fedex.com:443/GatewayDC",false 
strXMLhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
strXMLhttp.send strXML
strXMLResponse=strXMLhttp.responseText
intHTTPStatusCode = strXMLhttp.status
strHTTPStatusText = strXMLhttp.statusText 
Set mydoc=Server.CreateObject("Microsoft.xmlDOM") 
mydoc.loadxml(strXMLResponse) 
IF mydoc.documentElement.nodeName = "Error" then 'top level error
session("svFEDEXSelect")="##"
ELSE
Set NodeList = mydoc.documentElement.selectNodes("Entry") 
For strCount = 0 To NodeList.length - 1 
Select case NodeList.Item(strCount).selectSingleNode("Service").Text
case "FEDEXEXPRESSSAVER"
Session("svFEDEX1")="FedEx Express Saver (3 day)"
Session("svFEDEXCost1")=(cdbl(NodeList.Item(strCount).selectSingleNode("EstimatedCharges/DiscountedCharges/NetCharge").Text)+cdbl(strChosen))
Session("svFEDEXOrder1")=1
strCount2=strCount2+1
CASE "FEDEX2DAY"
Session("svFEDEX2")="FedEx 2 Day"
Session("svFEDEXCost2")=(cdbl(NodeList.Item(strCount).selectSingleNode("EstimatedCharges/DiscountedCharges/NetCharge").Text)+cdbl(strChosen))
Session("svFEDEXOrder2")=1
strCount2=strCount2+1
CASE "STANDARDOVERNIGHT"
Session("svFEDEX3")="FedEx Standard Overnight"
Session("svFEDEXCost3")=(cdbl(NodeList.Item(strCount).selectSingleNode("EstimatedCharges/DiscountedCharges/NetCharge").Text)+cdbl(strChosen))
Session("svFEDEXOrder3")=1
strCount2=strCount2+1
END SELECT
NEXT
'construct for ground rates
strXML="<?xml version='1.0' encoding='UTF-8' ?>"
strXML=strXML & "<FDXRateAvailableServicesRequest xmlns:api='http://www.fedex.com/fsmapi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='FDXRateAvailableServicesRequest.xsd'>"
strXML=strXML & "<RequestHeader>"
strXML=strXML & "<AccountNumber>999999999</AccountNumber>"
strXML=strXML & "<MeterNumber>9999999</MeterNumber>"
strXML=strXML & "<CarrierCode>FDXG</CarrierCode>"
strXML=strXML & "</RequestHeader>"
strXML=strXML & "<ShipDate>" & strUKDate & "</ShipDate>"
strXML=strXML & "<DropoffType>REGULARPICKUP</DropoffType>"
strXML=strXML & "<Packaging>YOURPACKAGING</Packaging>"
strXML=strXML & "<WeightUnits>LBS</WeightUnits>"
strXML=strXML & "<Weight>" & formatnumber(orderWeight,1) & "</Weight>"
strXML=strXML & "<ListRate>false</ListRate>"
strXML=strXML & "<OriginAddress>"
strXML=strXML & "<StateOrProvinceCode>" & strCompanyState & "</StateOrProvinceCode>"
strXML=strXML & "<PostalCode>" & strCompanyPostCode & "</PostalCode>"
strXML=strXML & "<CountryCode>US</CountryCode>"
strXML=strXML & "</OriginAddress>"
strXML=strXML & "<DestinationAddress>"
strXML=strXML & "<StateOrProvinceCode>" & BDQuotes(request("txtShipState")) & "</StateOrProvinceCode>"
strXML=strXML & "<PostalCode>" & BDQuotes(request("txtShipPostCode")) & "</PostalCode>"
strXML=strXML & "<CountryCode>US</CountryCode>"
strXML=strXML & "</DestinationAddress>"
strXML=strXML & "<Payment>"
strXML=strXML & "<PayorType>SENDER</PayorType>"
strXML=strXML & "</Payment>"
strXML=strXML & "<PackageCount>1</PackageCount>"
strXML=strXML & "</FDXRateAvailableServicesRequest>"
'send it
Set strXMLhttp = Server.CreateObject("MSXML2.ServerXMLHTTP") 
strXMLhttp.Open "POST","https://gatewaybeta.fedex.com:443/GatewayDC",false 
strXMLhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
strXMLhttp.send strXML
strXMLResponse=strXMLhttp.responseText
intHTTPStatusCode = strXMLhttp.status
strHTTPStatusText = strXMLhttp.statusText 
Set mydoc=Server.CreateObject("Microsoft.xmlDOM") 
mydoc.loadxml(strXMLResponse) 
IF mydoc.documentElement.nodeName = "Error" then 'top level error
session("svFEDEXSelect")="##"
ELSE
Set NodeList = mydoc.documentElement.selectNodes("Entry") 
For strCount = 0 To NodeList.length - 1 
Select case NodeList.Item(strCount).selectSingleNode("Service").Text
case "FEDEXGROUND"
Session("svFEDEX0")="FedEx Ground"
Session("svFEDEXCost0")=(cdbl(NodeList.Item(strCount).selectSingleNode("EstimatedCharges/DiscountedCharges/NetCharge").Text)+cdbl(strChosen))
Session("svFEDEXOrder0")=1
strCount2=strCount2+1
END SELECT
NEXT
session("svFEDEXCount")=clng(strCount2)
IF clng(session("svFEDEXCount"))>0 THEN
session("svFEDEXSelect")=""
ELSE
Session("svFEDEXSelect")="##"
END IF
For strCount=0 to (clng(session("svFEDEXCount"))-1)
IF BDQuotes(request("txtShipping"))= Session("svFEDEX"&strCount) THEN
session("svFEDEXSelect")=session("svFEDEXSelect") & "<option value=""" & Session("svFEDEX"&strCount) & """ selected>"
session("svFEDEXSelect")=session("svFEDEXSelect") &  Session("svFEDEX"&strCount) & " - " & formatcurrency((Session("svFEDEXCost"&strCount)),2)
session("svFEDEXSelect")=session("svFEDEXSelect") & "</option>" & vbcrlf 
Session("svShippingCost")=session("svFEDEXCost"&strCount)
ELSE
session("svFEDEXSelect")=session("svFEDEXSelect") & "<option value=""" & Session("svFEDEX"&strCount) & """>"
session("svFEDEXSelect")=session("svFEDEXSelect") &  Session("svFEDEX"&strCount) & " - " & formatcurrency((Session("svFEDEXCost"&strCount)),2)
session("svFEDEXSelect")=session("svFEDEXSelect") & "</option>" & vbcrlf 
END IF
Session.Contents.Remove "svFEDEX"&strCount
Session.Contents.Remove "svFEDEXCost"&strCount
Session.Contents.Remove "svFEDEXOrder"&strCount 
Next 
END IF
END IF
session.Contents.Remove "svFEDEXCount"
strCount=0
strCount2=0
response.write session("svFEDEXSelect")
%>
<%
BDConn.close()
set BDConn=nothing
Function BDQuotes(strQuotes)
strQuoteOutput=replace(strQuotes,chr(34), "''")
strQuoteOutput=replace(strQuoteOutput,"'","''")
BDQuotes=strQuoteOutput
END FUNCTION
%>

Now, once you have that showing as you like it. You have to call FedEx to request to go through their certification process. Who they then ask you if you will resell it, if it is for corporate use, what elements will you use, etc.

They then email you instructions on how to request to be put on the production server. You write out a letter with your intent, fax it to them. They then email you with the server address to Subscribe to their production server. Run the same 1st code set above to subscribe to the production server (change the server address). That will give you a whole new meter number to put into the 2nd set of code above.

And you are good to go.
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