type:
integration
system:
ASP.Net, VB.Net
name:
ASP.NET VB.NET API + Datafeed Code
description:
Functions to interact with the FoxyCart API and XML Datafeed
tag:
xml, api
date:
2009-07-24
developer:
http://forum.foxycart.com/comments.php?DiscussionID=1575

ASP.NET & VB.NET API and XML Datafeed E-Commerce Integration

Please note: The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

Description

Functions to interact with the FoxyCart API and XML Datafeeds. While this code doesn't actually do anything, it is a very useful foundation upon which to build an advanced FoxyCart + .NET e-commerce system.

Code

See if the Customer is in Foxy Cart:

Function VerifyIfInFoxyCart(ByVal Email As String, ByVal password As String) As String
Try
Dim inputs As New System.Collections.Specialized.NameValueCollection
inputs.Add("api_token", AppSettings("FoxyCartTokenID"))
inputs.Add("api_action", "customer_get")
inputs.Add("customer_email", Email)
inputs.Add("customer_password", password)
 
Dim str As String = PostTo("https://wellness-works.foxycart.com/api", inputs)
 
If str.IndexOf("SUCCESS") >= 0 Then
'Get CustomerID
'get rid of everyting before <foxycart> and after </FoxyCart>
str = str.Substring(str.IndexOf("<?xml version"))
str = str.Substring(0, str.IndexOf("</foxydata>") + 11)
 
Dim doc As New System.Xml.XmlDocument()
doc.LoadXml(str)
Dim element As System.Xml.XmlNode = doc.SelectSingleNode("/foxydata/customer_id")
 
Return element.InnerText
Else
Return Nothing
End If
 
Catch ex As Exception
Throw ex
End Try
End Function
 
Add Customer to FoxyCart (via API):
 
'Add or update the foxcart customer
Function AddCustomerToFoxyCart(ByVal Email As String, ByVal password As String) As String
Try
Dim inputs As New System.Collections.Specialized.NameValueCollection
inputs.Add("api_token", AppSettings("FoxyCartTokenID"))
inputs.Add("api_action", "customer_save")
inputs.Add("customer_email", Email)
inputs.Add("customer_password", password)
 
Dim str As String = PostTo("https://wellness-works.foxycart.com/api", inputs)
 
If str.IndexOf("SUCCESS") >= 0 Then
'Get CustomerID
'get rid of everyting before <foxycart> and after </FoxyCart>
str = str.Substring(str.IndexOf("<?xml version"))
str = str.Substring(0, str.IndexOf("</foxydata>") + 11)
 
Dim doc As New System.Xml.XmlDocument()
doc.LoadXml(str)
Dim element As System.Xml.XmlNode = doc.SelectSingleNode("/foxydata/customer_id")
 
Return element.InnerText
Else
Return Nothing
End If
 
Catch ex As Exception
Throw ex
End Try
 
End Function

Method to forward user to checkout:

Sub ForwardToCheckout(ByVal password As String, ByVal customerID As String)
Try
 
Dim ts As Int32 = Int32.Parse(Request.QueryString("timestamp")) + (60 * 60) 'add 60 minutes to timeout
 
Dim AuthKey As String = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(customerID + "|" + ts.ToString() + "|" + AppSettings("FoxyCartTokenID"), "SHA1") 'System.Web.Configuration.FormsAuthPasswordFormat.SHA1
 
Response.Redirect("https://wellness-works.foxycart.com/checkout?fc_auth_token=" + AuthKey.ToLower() + "&fc_customer_id=" + customerID + "&timestamp=" + ts.ToString() + "&fcsid=" + Request.QueryString("fcsid"))
 
Catch ex As Exception
Throw New Exception("ForwardToCheckout: " + password + " | " + customerID.ToString() + " - " + ex.Message)
End Try
End Sub

Receiving page - This recieves all transactions after they have been completed at Foxy Cart

Dim buffer As Byte() = New Byte(len) {}
Request.InputStream.Read(buffer, 0, len)
Dim FoxyFeed As String = Request.Form("FoxyData")
Dim ff_bytes As Byte() = Encoding.ASCII.GetBytes(FoxyFeed)
Dim foxy_feed2 As String = HttpUtility.UrlDecode(ff_bytes, Encoding.GetEncoding(0)) ' Server.UrlDecode(FoxyFeed)
 
line = foxy_feed2 'System.Text.Encoding.UTF8.GetString(buffer)
 
mydata = New WplData(ConnectionStrings("ConnectionString").ConnectionString)
myLMSData = New WplData(System.Configuration.ConfigurationManager.ConnectionStrings("LMSConnectionString").ConnectionString)
 
Dim lms As New LMSInterface
 
Try
 
'Line is now in XML and decrypted
line = lms.RC4(line, AppSettings("FoxyCartTokenID"))
 
Catch ex As Exception
Throw New Exception("RC4 decrypting error: " + ex.Message)
End Try

Decryption Function:

Function RC4(ByRef pStrMessage As String, ByRef pStrKey As String) As String
 
Dim lBytAsciiAry(255)
Dim lBytKeyAry(255)
Dim lLngIndex As Int32
Dim lBytJump
Dim lBytTemp
Dim lBytY
Dim lLngT
Dim lLngX
Dim lLngKeyLength As Int32
 
' Validate data
If Len(pStrKey) = 0 Then Return Nothing
If Len(pStrMessage) = 0 Then Return Nothing
 
' transfer repeated key to array
lLngKeyLength = pStrKey.Length
For lLngIndex = 0 To 255
lBytKeyAry(lLngIndex) = Asc(Mid(pStrKey, ((lLngIndex) Mod (lLngKeyLength)) + 1, 1))
Next
 
' Initialize S
For lLngIndex = 0 To 255
lBytAsciiAry(lLngIndex) = lLngIndex
Next
 
' Switch values of S arround based off of index and Key value
lBytJump = 0
For lLngIndex = 0 To 255
 
' Figure index to switch
lBytJump = (lBytJump + lBytAsciiAry(lLngIndex) + lBytKeyAry(lLngIndex)) Mod 256
 
' Do the switch
lBytTemp = lBytAsciiAry(lLngIndex)
lBytAsciiAry(lLngIndex) = lBytAsciiAry(lBytJump)
lBytAsciiAry(lBytJump) = lBytTemp
 
Next
 
lLngIndex = 0
lBytJump = 0
Dim TempRC4 As String = ""
For lLngX = 1 To Len(pStrMessage)
lLngIndex = (lLngIndex + 1) Mod 256 ' wrap index
lBytJump = (lBytJump + lBytAsciiAry(lLngIndex)) Mod 256 ' wrap J+S()
 
' Add/Wrap those two
lLngT = (lBytAsciiAry(lLngIndex) + lBytAsciiAry(lBytJump)) Mod 256
 
' Switcheroo
lBytTemp = lBytAsciiAry(lLngIndex)
lBytAsciiAry(lLngIndex) = lBytAsciiAry(lBytJump)
lBytAsciiAry(lBytJump) = lBytTemp
 
lBytY = lBytAsciiAry(lLngT)
 
' Character Encryption ...
TempRC4 += Chr(Asc(Mid(pStrMessage, lLngX, 1)) Xor lBytY)
Next
Return TempRC4
End Function

Site Tools