Asked by:
Problems in my WebRequest/WebResponse

Question
-
So I was struggling with making head or tail out of the PayPal documentation and always felt that something was not right with my Webrequest. So I stripped all the code back to basic and simply submitted the request via HTTP and the PLUS side is that I now get a response back from the PayPal sandbox server where
ACK=Success and TOKEN=Valid-token-value-here
there are some other variables returned too, such as CORRELATIONID and TIMESTAMP.
And hence so I tried some of the webrequest samples and I simply get a blank screen instead of being redirected to Paypal for the (sandbox) customer to complete payment. So if anyone can post their WebRequest method that would be great. Here is the code I used for my webrequest, I'm sure its wrong but cannot pinpoint where it is going wrong.
Thank you
'------------------------------------------------------------------------ '-> [Function] SetExpressCheckout '------------------------------------------------------------------------ Function SetExpressCheckout(ByRef pRtnReturnUrl As String) As Boolean ' Dim _sxForwarding As String = Nothing Dim _sxNameValCol As System.Collections.Specialized.NameValueCollection = Nothing Dim _sxCounter As Integer = Nothing ' '-> Init _sxForwarding = "" SetExpressCheckout = False _pgHost = "https://api-3t.sandbox.paypal.com/nvp" '-> Key/Value Collection Params _sxNameValCol = New System.Collections.Specialized.NameValueCollection() _sxNameValCol.Add("METHOD", "SetExpressCheckout") _sxNameValCol.Add("USER", "username-email-address-here") _sxNameValCol.Add("PWD", "password-here") _sxNameValCol.Add("SIGNATURE", "signature-here") _sxNameValCol.Add("PAYMENTREQUEST_0_AMT", _pgBasket.BasketTotalIncDelivery / 100) _sxNameValCol.Add("BUTTONSOURCE", "PP-ECWizard") _sxNameValCol.Add("RETURNURL", _pgReturnURL) _sxNameValCol.Add("CANCELURL", _pgCancelURL) _sxNameValCol.Add("REQCONFIRMSHIPPING", "0") _sxNameValCol.Add("NOSHIPPING", "2") _sxNameValCol.Add("LOCALECODE", "EN") _sxNameValCol.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale") _sxNameValCol.Add("PAYMENTREQUEST_0_CURRENCYCODE", "GBP") _sxNameValCol.Add("PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID", "seller-email-here") _sxNameValCol.Add("VERSION", "93.0") '-> UrlEncode For _sxCounter = 0 To _sxNameValCol.Count - 1 If _sxCounter = 0 Then _sxForwarding = _sxForwarding & "?" & _sxNameValCol.Keys(_sxCounter) & "=" & Server.UrlEncode(_sxNameValCol(_sxCounter)) Else _sxForwarding = _sxForwarding & "&" & _sxNameValCol.Keys(_sxCounter) & "=" & Server.UrlEncode(_sxNameValCol(_sxCounter)) End If Next ' SUBMITTING ABOVE QUERYSTRING GENERATES VALID RESPONSE FROM PAYPAL ' USING THE LINE BELOW 'Response.Redirect(_pgHost & _sxForwarding) 'SetExpressCheckout = True '====================================================================== ' MY WEBREQUEST EFFORT '====================================================================== '-> Locals Dim objRequest As System.Net.WebRequest = DirectCast(System.Net.WebRequest.Create(_pgHost), System.Net.WebRequest) Dim MyByteArray As Byte() = Nothing Dim MyResult As String = Nothing ' '-> WebRequest objRequest.Timeout = 6000 objRequest.Method = "POST" MyByteArray = Encoding.UTF8.GetBytes(_sxForwarding) objRequest.ContentType = "application/x-www-form-urlencoded" objRequest.ContentLength = MyByteArray.Length Try Using MyWriter As New System.IO.StreamWriter(objRequest.GetRequestStream()) MyWriter.Write(_sxForwarding) End Using Try '-> WebResponse MyResult = "" Dim objResponse As System.Net.HttpWebResponse = DirectCast(objRequest.GetResponse(), System.Net.HttpWebResponse) Using MyReader As New System.IO.StreamReader(objResponse.GetResponseStream()) MyResult = MyReader.ReadToEnd() End Using SetExpressCheckout = True pRtnReturnUrl = MyResult objResponse = Nothing Catch ex As Exception '-> Error pRtnReturnUrl = MyResult '("APIError.Aspx?ErrorCode=" & pRtnReturnUrl("L_ERRORCODE0") & "&Desc=" & pRtnReturnUrl("L_SHORTMESSAGE0") & "&Desc2=" & pRtnReturnUrl("L_LONGMESSAGE0")) SetExpressCheckout = False End Try Catch e As Exception '-> Error pRtnReturnUrl = MyResult '("APIError.Aspx?ErrorCode=" & pRtnReturnUrl("L_ERRORCODE0") & "&Desc=" & pRtnReturnUrl("L_SHORTMESSAGE0") & "&Desc2=" & pRtnReturnUrl("L_LONGMESSAGE0")) SetExpressCheckout = False End Try objRequest = Nothing MyByteArray = Nothing MyResult = Nothing ' End Function
DocZaf
- Edited by DocZaf Saturday, July 4, 2015 1:58 PM
- Edited by KareninstructorMVP Monday, July 6, 2015 12:49 PM Removed question out of code block
Saturday, July 4, 2015 1:49 PM
All replies
-
I think that PayPal has an API that you can use.
If I'm right, that would make this much easier. You might want to look into it.
***** EDIT *****
I was right, here it is.
Still lost in code, just at a little higher level.
:-)- Edited by Frank L. Smith Saturday, July 4, 2015 3:42 PM
Saturday, July 4, 2015 3:40 PM -
There is a detailed example below if you want to use or compare with yours:
http://www.codeproject.com/Articles/19184/Use-of-the-PayPal-payment-system-in-ASP-NET
Fouad Roumieh
Monday, July 6, 2015 10:00 AM