user278808
user278808

Reputation:

Getting INVALID response from PayPal's Sandbox IPN

I am trying to implement a simple online payment system using PayPal, however I have tried everything I know and am still getting an INVALID response.

I know it's nothing too simple, because I get a VERIFIED response when using the IPN simulator. I have tried putting the items into a dict first, I have tried fixing the encoding, and still nothing. PayPal says the reasons for an INVALID response could be:

The following is the snippet concerned:

f = cgi.FieldStorage()
newparams = 'cmd=_notify-validate'
for key in f.keys():
    val = f[key].value
    newparams += '&' + urlencode({key: val.encode('utf-8')})

req = urllib2.Request(PP_URL, newparams)
req.add_header("Content-type", "application/x-www-form-urlencoded")
http = urllib2.urlopen(req)
ret = http.read()
fi.write(ret + '\n')
if ret == 'VERIFIED':
    #*do stuff*

Upvotes: 1

Views: 2038

Answers (4)

None
None

Reputation: 1

Make sure you are posting to the sandbox and not the live..

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

Upvotes: -1

user535010
user535010

Reputation: 419

in django

import httplib2
import urllib

h = httplib2.Http()

params = urllib.urlencode(request.POST, True)

response, content = h.request("http://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate&%s" % params)

Upvotes: 1

Richard Clark
Richard Clark

Reputation: 147

The order is critical. You must verify in the same order that Paypal specify. The simplest way to achieve this is to use the exact order they were provided in:

def paypal_verify():
    """ Returns false if the current request cannot be verified by paypal """
    # Create verify param string from current query string
    verify_string = "cmd=_notify_validate&" + cherrypy.request.query_string
    req = urllib2.Request("http://www.paypal.com/cgi-bin/webscr", verify_string)
    response = urllib2.urlopen(req)
    result = response.read()
    if response == "VERIFIED":
        # All good
        return True
    # Fail
    return False

If you're not using cherrypy, some other mechanism should be similarly available to get the query string as provided by Paypal.

Upvotes: 2

Justin Peel
Justin Peel

Reputation: 47082

It is hard to judge from what you've given me. Here are a couple of guesses/suggestions:

  1. You may need to encode in utf-8 the key as well as the val.
  2. If it is picky about the order of the keys, you should log the keys as you put them into newparams. Since you are using a dict for f, the order may not be what you are expecting it to be.

Upvotes: 0

Related Questions