webRequest object and PayPal

G

Guest

Hi,

I'm trying to use PayPal and its Instant Payment Notification. In short,
when a payment is made, PayPal send a post to my server and I post it back to
PayPal.

I'm using WebRequest to do this. I receive the PayPal post, but I can't post
it back. It's always giving me a Time out.

Here's my code:

// Setup Web request
objRequest = System.Net.WebRequest.Create(url);
objRequest.Timeout = timeoutSeconds * 1000;
objRequest.ContentType="application/x-www-form-urlencoded";
objRequest.Method = "POST";
// Create a copy of the http post received
// to post it back to paypal
NameValueCollection form = new NameValueCollection(Request.Form);
string hdrs = "";
foreach ( string var in form )
{
if (form[var] != null)
objRequest.Headers.Add(var,form[var]);
}
// Adds the Paypal validation field to the request
objRequest.Headers.Add("cmd","_notify-validate");

// Retrieve data from request
objResponse = objRequest.GetResponse();

....Then, nothing happens here and I receive a timeout webException.

I'm using this in other class where it's working really well. Also, I made a
HTML form to test the post from my server and it's working too. The PayPal
url exists and I don't understand why I can't reach it via webRequest.

Any idea?

Thanks,

Stephane
 
V

vMike

Stephane said:
Hi,

I'm trying to use PayPal and its Instant Payment Notification. In short,
when a payment is made, PayPal send a post to my server and I post it back to
PayPal.

I'm using WebRequest to do this. I receive the PayPal post, but I can't post
it back. It's always giving me a Time out.

Here's my code:

// Setup Web request
objRequest = System.Net.WebRequest.Create(url);
objRequest.Timeout = timeoutSeconds * 1000;
objRequest.ContentType="application/x-www-form-urlencoded";
objRequest.Method = "POST";
// Create a copy of the http post received
// to post it back to paypal
NameValueCollection form = new NameValueCollection(Request.Form);
string hdrs = "";
foreach ( string var in form )
{
if (form[var] != null)
objRequest.Headers.Add(var,form[var]);
}
// Adds the Paypal validation field to the request
objRequest.Headers.Add("cmd","_notify-validate");

// Retrieve data from request
objResponse = objRequest.GetResponse();

...Then, nothing happens here and I receive a timeout webException.

I'm using this in other class where it's working really well. Also, I made a
HTML form to test the post from my server and it's working too. The PayPal
url exists and I don't understand why I can't reach it via webRequest.

Any idea?

Thanks,

Stephane
When paypal post to your post page you only need to send back the date they
posted with the appended cmd info

Try something like this. Careful of screen wrap and you will need to add
your code to handle the response you get
I have use setting in my web config. you will have to replace those values
with your own values.


Dim stringPost, stringResult As String

Sub Page_Load(sender as object, e as EventArgs)
Dim mgWebRequest As HttpWebRequest
Dim mgWebResponse As HttpWebResponse
Dim mgStreamWriter As StreamWriter
Dim mgStreamReader As StreamReader
stringPost = Request.Form.tostring()
' Send Data to paypal with append
mgWebRequest
=CType(WebRequest.Create(ConfigurationSettings.AppSettings("paypalURL")),Htt
pWebRequest)

if request.form.get("receiver_email") =
ConfigurationSettings.AppSettings("paypalrec") then
mgWebRequest.Method = "POST"
mgWebRequest.ContentLength = stringPost.Length + 21 'length plus
21 because &cmd=_notify-validate is 21 chars long
mgWebRequest.ContentType = "application/x-www-form-urlencoded"
mgStreamWriter = Nothing
mgStreamWriter = New StreamWriter(mgWebRequest.GetRequestStream())
stringPost = stringPost + "&cmd=_notify-validate"
mgStreamWriter.Write(stringPost)
mgStreamWriter.Close()
mgWebResponse = CType(mgWebRequest.GetResponse(),HttpWebResponse)
mgStreamReader = New StreamReader(mgWebResponse.GetResponseStream())
stringResult = mgStreamReader.ReadToEnd()
mgStreamReader.Close()
if instr(1,stringResult,"VERIFIED") > 0 then
if request.form.get("payment_status") = "Completed" then
'code to write to data base
else
'code to handle other responses
end if
else if instr(1,stringResult, "INVALID") > 0 then
'code to handle the INVALID response
else
'code to handle unknown response
End if
else
'code to handle unrequest paypal posts. do really need anything here
end if
end sub
 
V

vMike

vMike said:

I forgot to mention. The code I provided is the a "listening page" that
paypal will post to. The way I test it is to do the following.
Create a page to post TO the listen page. Then create another page with the
following

<html>

<body>
VERIFIED
</body>
</html>

change the setting it the code I sent to so that the paypaly url is the page
with the Verified in it. Using your posting page, you can post a paypal
tranaction to the listening page. Good luck.

Mike
 
G

Guest

It works. Thanks!!

vMike said:
Stephane said:
Hi,

I'm trying to use PayPal and its Instant Payment Notification. In short,
when a payment is made, PayPal send a post to my server and I post it back to
PayPal.

I'm using WebRequest to do this. I receive the PayPal post, but I can't post
it back. It's always giving me a Time out.

Here's my code:

// Setup Web request
objRequest = System.Net.WebRequest.Create(url);
objRequest.Timeout = timeoutSeconds * 1000;
objRequest.ContentType="application/x-www-form-urlencoded";
objRequest.Method = "POST";
// Create a copy of the http post received
// to post it back to paypal
NameValueCollection form = new NameValueCollection(Request.Form);
string hdrs = "";
foreach ( string var in form )
{
if (form[var] != null)
objRequest.Headers.Add(var,form[var]);
}
// Adds the Paypal validation field to the request
objRequest.Headers.Add("cmd","_notify-validate");

// Retrieve data from request
objResponse = objRequest.GetResponse();

...Then, nothing happens here and I receive a timeout webException.

I'm using this in other class where it's working really well. Also, I made a
HTML form to test the post from my server and it's working too. The PayPal
url exists and I don't understand why I can't reach it via webRequest.

Any idea?

Thanks,

Stephane
When paypal post to your post page you only need to send back the date they
posted with the appended cmd info

Try something like this. Careful of screen wrap and you will need to add
your code to handle the response you get
I have use setting in my web config. you will have to replace those values
with your own values.


Dim stringPost, stringResult As String

Sub Page_Load(sender as object, e as EventArgs)
Dim mgWebRequest As HttpWebRequest
Dim mgWebResponse As HttpWebResponse
Dim mgStreamWriter As StreamWriter
Dim mgStreamReader As StreamReader
stringPost = Request.Form.tostring()
' Send Data to paypal with append
mgWebRequest
=CType(WebRequest.Create(ConfigurationSettings.AppSettings("paypalURL")),Htt
pWebRequest)

if request.form.get("receiver_email") =
ConfigurationSettings.AppSettings("paypalrec") then
mgWebRequest.Method = "POST"
mgWebRequest.ContentLength = stringPost.Length + 21 'length plus
21 because &cmd=_notify-validate is 21 chars long
mgWebRequest.ContentType = "application/x-www-form-urlencoded"
mgStreamWriter = Nothing
mgStreamWriter = New StreamWriter(mgWebRequest.GetRequestStream())
stringPost = stringPost + "&cmd=_notify-validate"
mgStreamWriter.Write(stringPost)
mgStreamWriter.Close()
mgWebResponse = CType(mgWebRequest.GetResponse(),HttpWebResponse)
mgStreamReader = New StreamReader(mgWebResponse.GetResponseStream())
stringResult = mgStreamReader.ReadToEnd()
mgStreamReader.Close()
if instr(1,stringResult,"VERIFIED") > 0 then
if request.form.get("payment_status") = "Completed" then
'code to write to data base
else
'code to handle other responses
end if
else if instr(1,stringResult, "INVALID") > 0 then
'code to handle the INVALID response
else
'code to handle unknown response
End if
else
'code to handle unrequest paypal posts. do really need anything here
end if
end sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top