Thought I would write a quick one about something useful I've been wanting to do for sometime now and finally figured out. It is yet another one of those things that can be quite helpful when applied correctly to solve a problem. Our problem seemed simple enough, as the hard ones often do! We were building an e-commerce solution for a client of ours and got to the portion of the application where we had built the shopping cart, but needed to figure out the shipping rate for the entire order. Our client wanted to use the United States Postal Service's (USPS) API for calculating shipping rate. As we began to read the USPS API we quickly realized that the generated request had to be in XML. Personally, you can fit all of my XML knowledge into a very small container - that's how much XML experience I've had. I do however know a fair amount about HTML and creating HTTP POST requests. As we
read through the USPS API, which is about 92 pages (ouch), we discovered basically all we needed to do was build the HTTP Post request, send it, wait for the response, and parse it. Try searching on Notes.net for XML parser and lotusscript and you get a ton of articles mentioning how "nice it would be" to have such functionality. Luckily we found an article by Paul Ray that we modified to solve our problem.
Prerequisites:
This script must be running on the Win32 platform to work, so if you're still reading this - you're in luck. This example describes creating a new HTTP Post request using calls to the MSXML parser that is installed with Internet Explorer 5. We are running Lotus Domino 5.08 on a Windows 2000 Advanced Server platform with Internet Explorer 5 installed and the script worked like a charm.
The Basics:
In our situation, we needed to calculate the shipping rate before we opened the document in "checkout" mode. The Lotusscript agent is called in the WebQueryOpen event to give us the ability to modify field values before the document is opened. Here is the basic structure of the agent:
Sub Initialize
%REM
This routine will create a new HTTP POST request, send values, and retrieve the response from the server. This is done via the XMLHTTP object of the MSXML parser.
%END REM
Dim s As New NotesSession
Dim doc As NotesDocument
Dim objHttp As Variant
Dim response As String
Dim request As String
Dim beginTag As String
Dim endTag As String
Dim rightHalf As String
Dim leftHalf As String
On Error Goto ErrHandler
Set db = s.CurrentDatabase
Set doc = s.DocumentContext
query_string = doc.getitemvalue("Query_String")(0)
If Instr(query_string, "&ZC=") Then
destZipCode = Strright(query_string,"&ZC=")
' --- instantiate an MSXML XMLHTTP object
set objHttp = CreateObject("Microsoft.XMLHTTP")
' --- open a new POST request and set the Content-type header
url = |Http://production.shippingapis.com/ShippingAPI.dll|
req = |API=Rate&XML=<RateRequest USERID="5555555555"
PASSWORD="777777777777"><Package ID="TestID">
<Service>Priority</Service><ZipOrigination>55555
</ZipOrigination><ZipDestination>|+destZipCode+|
</ZipDestination><Pounds>5</Pounds><Ounces>10
</Ounces><Container>None</Container><Size>REGULAR
</Size><Machinable></Machinable></Package>
</RateRequest>|
objHttp.open "POST", url, False, "", ""
objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHttp.send(req)
response = objHttp.responseText
beginTag = "<Postage>"
endTag = "</Postage>"
rightHalf = RightBack(response,beginTag)
LeftHalf = myLeft(rightHalf,endTag)
doc.Shipping = Ccur(LeftHalf)
Call doc.Save(True, True)
Cleanup:
Set objHttp=Nothing
newHREF = "/" + db.FilePath + "/0/" + doc.UniversalID + "?OpenDocument"
Print |[| + newHREF + |]|
End If
Exit Sub
ErrHandler:
' --- runtime error occurred
Msgbox Error$, 48, "Runtime Error"
Resume Cleanup
End Sub
Function RightBack ( sourceString As String, searchString As String ) As String
For i% = Len(sourceString) To 1 Step -1
sourceStringBack$=sourceStringBack$ & Mid(sourceString, i%, 1)
Next
For i% = Len(searchString) To 1 Step -1
searchStringBack$=searchStringBack$ & Mid(searchString, i%, 1)
Next
pos% = Instr ( sourceStringBack$, searchStringBack$ )
If pos% > 0 Then pos% = pos% -1
result$ = Left ( sourceStringBack$, pos% )
For i% = Len(result$) To 1 Step -1
turn$=turn$ & Mid(result$, i%, 1)
Next
RightBack=turn$
End Function
Function myLeft ( sourceString As String, searchString As String ) As String
pos% = Instr ( sourceString, searchString )
If pos% > 0 Then pos% = pos% -1
myLeft = Left ( sourceString, pos% )
End Function
Copyright © 2000 - 2024 Jake Howlett of Rockall Design ltd. This article was printed from codestore.net