PayPal IPN problem...

G

Greg

I have code (below) that was working a year ago that I'm trying to use again
and it's now not working. The response is always "invalid". Anyone know
why?

Code:

Function GetPayPal() As Boolean
Dim myRequest As HttpWebRequest =
CType(HttpWebRequest.Create("https://www.paypal.com/cgi-bin/webscr"),
HttpWebRequest)
Dim strToSend As String
myRequest.AllowAutoRedirect = False
myRequest.Method = "POST"
myRequest.ContentType = "application/x-www-form-urlencoded"

strToSend = Request.Form.ToString()
'Create the string to post back to PayPal system to validate
strToSend &= "&cmd=_notify-validate"

'Create post stream
Dim RequestStream As Stream = myRequest.GetRequestStream()
Dim SomeBytes() As Byte = Encoding.UTF8.GetBytes(strToSend)

RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
RequestStream.Close()

'Send request and get response
Dim myResponse As HttpWebResponse = CType(myRequest.GetResponse(),
HttpWebResponse)

If myResponse.StatusCode = HttpStatusCode.OK Then
'Obtain a 'Stream' object associated with the response object.
Dim ReceiveStream As Stream = myResponse.GetResponseStream()
Dim encode As Encoding =
System.Text.Encoding.GetEncoding("utf-8")

'Pipe the stream to a higher level stream reader with the
required encoding format.
Dim readStream As StreamReader = New StreamReader(ReceiveStream,
encode)

'Read result
Dim Result As String = readStream.ReadLine()

'For testing purposes
Response.Write(Result)
Response.End()
End If
End Function
 
V

vMike

Here is a snip from what I use. It may help you.


Dim stringPost, stringResult As String
Dim mgWebRequest As HttpWebRequest
Dim mgWebResponse As HttpWebResponse
Dim mgStreamWriter As StreamWriter
Dim mgStreamReader As StreamReader

stringPost = Request.Form.tostring()
mgWebRequest
=CType(WebRequest.Create(ConfigurationSettings.AppSettings("paypalURL")),Htt
pWebRequest)
'This test to see if request should be verified
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()
else ...

In this case stringResult contains the paypal response string for further
back end processing to database
 
G

Greg

vMike said:
Here is a snip from what I use. It may help you.

Strange. With your code too the stringResult = "Invalid". It must not be a
problem with the code, but something else.

Do you know if anything special needs to be done with the URL besides
specifying a return URL?

Thanks.
 
V

vMike

The paypal ipn system is a bit difficult to troubleshoot because you must
have an actual purchase taking place. I don't know of any other way to test
it. Are you testing it by making a purchase. (You can make a purchase and
then refund it.) INVALID is one of the responses they send if the data you
are sending doesn't match their transaction.
 
V

vMike

From the Paypal IPN manual

13 Copyright 2004 PayPal, Inc. All rights reserved

Notification Validation

To ensure that a payment has been made into your PayPal account, you must

verify that the email address used as your receiver_email has been
registered and

confirmed in your PayPal account.

Once your server has received the Instant Payment Notification, you will
need to

confirm it by constructing an HTTP POST to PayPal. Your POST should be sent
to

https://www.paypal.com/cgi-bin/webscr. This post-back of the IPN data to a
secure

PayPal URL (i.e., https://) prevents 'spoofing,' so you can be sure that the
IPN came

from PayPal.

Note: It is possible to implement IPN without SSL (i.e., http://), but then
the IPN

data that is received and posted back is not secure.

You must post all of the form variable you received exactly as you received
them. You

will also need to append a variable named cmd with the value
_notify-validate

(e.g., cmd=_notify-validate) to the POST string.

PayPal will respond to the post with a single word, "VERIFIED" or "INVALID,"
in the

body of the response.

Instant Payment Notification Manual

When you receive a VERIFIED response, perform the following checks:

1. Check that the payment_status is Completed.

2. If the payment_status is Completed, check the txn_id against the previous

PayPal transaction you have processed to ensure it is not a duplicate.

3. After you have checked the payment_status and txn_id, make sure the

receiver_email is an email address registered in your PayPal account.

4. Check that the price, mc_gross, and currency, mc_currency, are correct
for the

item, item_name or item_number.

5. (Optional) To be 100% certain of the authenticity of this tranasaction,
you can

check PayPal's digital certificate if you are using SSL:

/C=US/ST=California/L=Palo Alto/O=PayPal, Inc./OU=Information Systems/

CN=www.paypal.com

Once you have completed the above checks, you may update your database based
on

the information provided.

If you receive an INVALID response, you should investigate. In some cases,
this

response is caused by an IPN error, possibly from a change in the IPN
format. To

determine if it is an IPN error, first examine your code. If you need
further assistance,

go to http://www.paypal.com/wf/, click the Seller Tools topic, and then
click Instant

Payment Notification (IPN).
 

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