Michael Knight
asked on
How to Post Shopping Cart Data to PayPal from VB.NET
I have a VB.NET application that allows users to add items to a data table shopping cart within the program. When they are reday to checkout, I take them to PayPal to perform the actual payment procedure.
The method I have is VERY crude and primitive. I tried several others ways, including UploadData and such, with no luck. I was never able to open/access the cart with the POSTED contents in the web browser.
I am trying to make certain the user never sees the entire cart item URL string so they cannot be tempted to change the price before paying.
Below is my current "cheat" code. Lets see who can come up with the cleanest "true" solution. I would prefer a true POST in a fashion I can use the "hidden" directives used by PayPal. I have searched the web high and low without finding a good solid free solution.
Dim httpString As String
For Each oCartItem As DataRow In dtShoppingCart.Rows
httpString = "&item_number=" & URLEncode(oCartItem(0)) & _
"&item_name=" & URLEncode(oCartItem(1)) & _
"&amount=" & oCartItem(2) & _
"&quantity=" & oCartItem(3) & _
"&shipping=5.00" & _
"&shipping2=5.00" & _
"&on0=" & URLEncode("Added Info") & _
"&os0=" & URLEncode(oCartItem(4))
System.Diagnostics.Process .Start("https://www.paypal.com/cgi-bin/webscr?cmd=_cart&business=test@company.com&add=1&invoice=VBAPP&undefined_quantity=1¤cy_code=USD" & httpString)
Next
System.Diagnostics.Process .Start("https://www.paypal.com/cgi-bin/webscr?cmd=_cart&business=test@company.com&display=1")
The URLEncode function simply reformats the data to an acceptable URL format. In a true POST method, this should no longer be needed.
The dtShoppingCart data table is defined as follows:
dtShoppingCart = New DataTable("ShoppingCart")
dtShoppingCart.Columns.Add ("Item Number", GetType(String)) ' 0
dtShoppingCart.Columns.Add ("Item Name", GetType(String)) ' 1
dtShoppingCart.Columns.Add ("Price Each", GetType(String)) ' 2
dtShoppingCart.Columns.Add ("Quantity ", GetType(Integer)) ' 3
dtShoppingCart.Columns.Add ("Added Info", GetType(String)) ' 4
dtShoppingCart.PrimaryKey = New DataColumn() {dtShoppingCart.Columns("I tem Number")}
Thanks,
maknight
The method I have is VERY crude and primitive. I tried several others ways, including UploadData and such, with no luck. I was never able to open/access the cart with the POSTED contents in the web browser.
I am trying to make certain the user never sees the entire cart item URL string so they cannot be tempted to change the price before paying.
Below is my current "cheat" code. Lets see who can come up with the cleanest "true" solution. I would prefer a true POST in a fashion I can use the "hidden" directives used by PayPal. I have searched the web high and low without finding a good solid free solution.
Dim httpString As String
For Each oCartItem As DataRow In dtShoppingCart.Rows
httpString = "&item_number=" & URLEncode(oCartItem(0)) & _
"&item_name=" & URLEncode(oCartItem(1)) & _
"&amount=" & oCartItem(2) & _
"&quantity=" & oCartItem(3) & _
"&shipping=5.00" & _
"&shipping2=5.00" & _
"&on0=" & URLEncode("Added Info") & _
"&os0=" & URLEncode(oCartItem(4))
System.Diagnostics.Process
Next
System.Diagnostics.Process
The URLEncode function simply reformats the data to an acceptable URL format. In a true POST method, this should no longer be needed.
The dtShoppingCart data table is defined as follows:
dtShoppingCart = New DataTable("ShoppingCart")
dtShoppingCart.Columns.Add
dtShoppingCart.Columns.Add
dtShoppingCart.Columns.Add
dtShoppingCart.Columns.Add
dtShoppingCart.Columns.Add
dtShoppingCart.PrimaryKey = New DataColumn() {dtShoppingCart.Columns("I
Thanks,
maknight
Try this ...
Use the HTTPWeb Request and response to post and get the data
-------------------------- ---------- ---------- ---------- ---------- ----------
Dim objHTTPRequest As Net.HttpWebRequest
Dim objRequestStream As IO.Stream
Dim objHttpResponse As Net.HttpWebResponse
objHTTPRequest = Net.HttpWebRequest.Create( "https://www.paypal.com/cgi-bin/webscr")
Dim httpString As String
For Each oCartItem As DataRow In dtShoppingCart.Rows
httpString = "cmd=_cart&business=test@c ompany.com &display=1 " & _
"&item_number=" & URLEncode(oCartItem(0)) & _
"&item_name=" & URLEncode(oCartItem(1)) & _
"&amount=" & oCartItem(2) & _
"&quantity=" & oCartItem(3) & _
"&shipping=5.00" & _
"&shipping2=5.00" & _
"&on0=" & URLEncode("Added Info") & _
"&os0=" & URLEncode(oCartItem(4))
Next
Dim ContentData As Byte() = System.Text.Encoding.UTF8. GetBytes(h ttpString)
objRequestStream = objHTTPRequest.GetRequestS tream()
objHTTPRequest.Method = "POST"
objHTTPRequest.ContentType = "application/x-www-form-ur lencoded"
objHTTPRequest.ContentLeng th = ContentData.Length
objRequestStream.Write(Con tentData, 0, ContentData.Length)
objRequestStream.Close()
objHttpResponse = objHTTPRequest.GetResponse ()
Dim objReader As System.IO.StreamReader = New IO.StreamReader(objHttpRes ponse.GetR esponseStr eam())
MsgBox(objReader.ReadToEnd ())
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
Regards
PRakash
Use the HTTPWeb Request and response to post and get the data
--------------------------
Dim objHTTPRequest As Net.HttpWebRequest
Dim objRequestStream As IO.Stream
Dim objHttpResponse As Net.HttpWebResponse
objHTTPRequest = Net.HttpWebRequest.Create(
Dim httpString As String
For Each oCartItem As DataRow In dtShoppingCart.Rows
httpString = "cmd=_cart&business=test@c
"&item_number=" & URLEncode(oCartItem(0)) & _
"&item_name=" & URLEncode(oCartItem(1)) & _
"&amount=" & oCartItem(2) & _
"&quantity=" & oCartItem(3) & _
"&shipping=5.00" & _
"&shipping2=5.00" & _
"&on0=" & URLEncode("Added Info") & _
"&os0=" & URLEncode(oCartItem(4))
Next
Dim ContentData As Byte() = System.Text.Encoding.UTF8.
objRequestStream = objHTTPRequest.GetRequestS
objHTTPRequest.Method = "POST"
objHTTPRequest.ContentType
objHTTPRequest.ContentLeng
objRequestStream.Write(Con
objRequestStream.Close()
objHttpResponse = objHTTPRequest.GetResponse
Dim objReader As System.IO.StreamReader = New IO.StreamReader(objHttpRes
MsgBox(objReader.ReadToEnd
--------------------------
Regards
PRakash
ASKER
Thanks for the quick responses.
I found that it must be done in this order or an exception is generated.
objHTTPRequest.Method = "POST"
objHTTPRequest.ContentType = "application/x-www-form-ur lencoded"
objHTTPRequest.ContentLeng th = ContentData.Length
objRequestStream = objHTTPRequest.GetRequestS tream()
objRequestStream.Write(Con tentData, 0, ContentData.Length)
objHttpResponse = objHTTPRequest.GetResponse ()
Dim objReader As System.IO.StreamReader = New IO.StreamReader(objHttpRes ponse.GetR esponseStr eam())
objRequestStream.Close()
This is basically what I had tried with UploadData (although this is a bit cleaner than what I had), but it is not persistent. I cannot do anything with the cart after the post because it doesn't exist any longer after the response data is returned. If I try to do anything with the returned cart, PayPal just returns that the cart is empty. The returned (response) data shows a properly filled cart, however.
For example, if you create a PayPal cart with the full URL, or through a normal HTML request, you can close your browser, and use "https://www.paypal.com/cgi-bin/webscr?cmd=_cart&business=test@company.com&display=1" to redisplay it until the cart expires after a few days.
How do I maintain persistence? I tried making the stream variable global and keeping it open while I tested it.
maknight
I found that it must be done in this order or an exception is generated.
objHTTPRequest.Method = "POST"
objHTTPRequest.ContentType
objHTTPRequest.ContentLeng
objRequestStream = objHTTPRequest.GetRequestS
objRequestStream.Write(Con
objHttpResponse = objHTTPRequest.GetResponse
Dim objReader As System.IO.StreamReader = New IO.StreamReader(objHttpRes
objRequestStream.Close()
This is basically what I had tried with UploadData (although this is a bit cleaner than what I had), but it is not persistent. I cannot do anything with the cart after the post because it doesn't exist any longer after the response data is returned. If I try to do anything with the returned cart, PayPal just returns that the cart is empty. The returned (response) data shows a properly filled cart, however.
For example, if you create a PayPal cart with the full URL, or through a normal HTML request, you can close your browser, and use "https://www.paypal.com/cgi-bin/webscr?cmd=_cart&business=test@company.com&display=1" to redisplay it until the cart expires after a few days.
How do I maintain persistence? I tried making the stream variable global and keeping it open while I tested it.
maknight
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's what I was trying to figure out! I don't know why that was eluding me so. It was a VERY clean solution. Thank you.
I do have a couple more questions, but they really require a seperate question, I feel, and I may be able to muddle my way through them myself having this much.
Regards,
maknight
I do have a couple more questions, but they really require a seperate question, I feel, and I may be able to muddle my way through them myself having this much.
Regards,
maknight
Thank u maknight..
Regards
prakash
Regards
prakash
Does paypal offer a webservice interface to create a session ahead of time and upload the contents of the cart, then redirect the user to paypal using a unique ID?