Write code in the "Codebehind" of the web form to add an item to the Basket by consuming the XWebCheckOut Web Service
Before we begin talking about code, we must lay out the sequence of events that take place during the checkout process. All
websites that list products/services for sale feature a product/service page which contains the standard Add To Cart link.
Because in Step 2 of this guide we created a page named "order.aspx", the
Add To Cart link should reference the "order.aspx" page with the product id/sku of that particular product/service as a
querystring parameter. For example:
order.aspx?pid=item238
When the website visitor clicks the link, our code must check to see if an Order ID has already been created for this
particular visitor. We do this by checking whether or not a cookie with the Order ID has already been set. If it has not,
we set one.
Dim strOrderID As String
If Request.Cookies("OID") Is Nothing Then
strOrderID = Replace(Guid.NewGuid.ToString, "-", "")
Dim objCookie As New HttpCookie("OID", strOrderID)
Response.Cookies.Add(objCookie)
Else
strOrderID = Request.Cookies("OID").Value
End If
Next, we use the LoadOrder SOAP method of the XWebCheckOut Web Service to retrieve the order record's details and store
them into an XML DOM. Based on the documentation for the LoadOrder SOAP method, if the record has not already been created,
the method still returns the <Order>
root node; therefore, no extra code is required to account for new versus already existent records.
Since the web service requires SOAP Headers for authentication purposes, we must first create a new
instance of the class representing the SOAP Header and then populate the SOAP Header values:
Dim objAuth As New XWebCheckOut.AuthHeader
objAuth.LoginName = "<my_login_name>"
objAuth.Password = "<my_password>"
NOTE***: Instead of hard-coding the LoginName and Password values, you may want to
dynamically read them from the web.config file (LOGIN_NAME and PASSWORD must be added to the
<AppSettings>
section of the web.config file):
objAuth.LoginName = System.Configuration.ConfigurationSettings.AppSettings("LOGIN_NAME")
objAuth.Password = System.Configuration.ConfigurationSettings.AppSettings("PASSWORD")
Next, we need to create a new instance of the proxy class and assign the SOAP Header object to the member
variable of the proxy class representing the SOAP Header:
Dim objOrder As New XWebCheckOut.XWebCheckOut
objOrder.AuthHeaderValue = objAuth
We are now ready to call the method on the proxy class that communicates with the Web Service SOAP Method and returns the
Order record we requested. Since the method returns the Order record in the form of an XML string, we load an XML DOM with it:
Dim orderDOM As New System.Xml.XmlDocument
orderDOM.LoadXml(objOrder.LoadOrder(strOrderID))
Since different websites have differently behaving Baskets, the XWebCheckOut web service provides great flexibility by
storing the contents of a Basket as one 5000 character field. For this guide, we will store the contents as XML, consisting
of a list of items the visitor has added to his/her basket as well as the order's total.
Dim basketDOM As New System.Xml.XmlDocument
If orderDOM.DocumentElement.SelectSingleNode("Basket") Is Nothing Then
basketDOM.AppendChild(basketDOM.CreateElement("Basket"))
Dim itemsNode As System.Xml.XmlNode =
basketDOM.DocumentElement.AppendChild(basketDOM.CreateElement("Items"))
Dim itemNode As System.Xml.XmlNode =
itemsNode.AppendChild(basketDOM.CreateElement("Item"))
Dim priceNode As System.Xml.XmlNode = basketDOM.CreateElement("Price")
priceNode.InnerText = price_of_the_product i.e. 4.99
itemNode.AppendChild(priceNode)
Else
basketDOM.LoadXml(orderDOM.DocumentElement.
SelectSingleNode("Basket/Contents").InnerText)
If basketDOM.DocumentElement.SelectSingleNode("Items/Item[@Item_ID =
'" & product_id/sku i.e. item238 & "']") Is Nothing Then
Else
End If
End If
Dim xpathNav As System.Xml.XPath.XPathNavigator =
basketDOM.CreateNavigator
Dim xpathExpression As System.Xml.XPath.XPathExpression =
xpathNav.Compile("sum(Basket/Items/Item/Price)")
Dim totalNode As System.Xml.XmlNode = basketDOM.CreateElement("Total")
totalNode.InnerText = xpathNav.Evaluate(xpathExpression)
basketDOM.DocumentElement.AppendChild(totalNode)
For our last step, we consume the ProcessOrder SOAP method to store the Basket's contents.
orderDOM = New System.Xml.XmlDocument
orderDOM.AppendChild(orderDOM.CreateElement("Order"))
Dim oidAttribute As System.Xml.XmlAttribute =
orderDOM.CreateAttribute("Order_ID")
oidAttribute.InnerText = strOrderID
orderDOM.DocumentElement.Attributes.Append(oidAttribute)
Dim basketNode As System.Xml.XmlNode =
orderDOM.DocumentElement.AppendChild(orderDOM.CreateElement("Basket"))
Dim contentsNode As System.Xml.XmlNode =
orderDOM.CreateElement("Contents")
contentsNode.InnerText = basketDOM.DocumentElement.OuterXML
basketNode.AppendChild(contentsNode)
Dim strResult As String =
objOrder.ProcessOrder(orderDOM.DocumentElement.OuterXml)
If strResult <> "" Then Throw New Exception(strResult)
|