pythonpaypalpaypal-ipnurlencodeserver-side-scripting

Why does paypal-ipn still return INVALID to my POST?


i have build a simple paypal-ipn listener: it listens for paypal sending me a http POST, reads its data out and sends it back to paypal, which should answer VALID if the data received matches the data send.

i am using the Paypal IPN-Simulator to send the IPNs.

i basically copied the code from here and i get back INVALID all the time. Looking at the rawinput data from stdin i get the feel I am missing something about the encoding here, but i am too new to all that stuff to figure it out.

#!/usr/bin/python3
import sys
import cgi
import os; import json; import collections;import urllib; from urllib import parse

PAYPAL_URL = "https://sandbox.paypal.com/cgi-bin/webscr"

inputraw =sys.stdin.read()

f = open('test','w')

print("content-type: text/html\r\n\r\n")

formData ="cmd=_notify-validate&"+parse.unquote(inputraw)

f.write(formData+"\n")

req = urllib.request.Request(PAYPAL_URL, formData.encode())

req.add_header("Content-type", "application/x-www-form-urlencoded")
response = urllib.request.urlopen(req)
status = str(response.read())

f.write(status); f.write("\n")

if (not status == "b'VERIFIED'"):
        f.write("OH NO")
else:
        f.write("OH YES")

f.write("\n")
f.close()

i also did not get how to send a pure HTTP 200 Response and used the print("content-type: text/html\r\n\r\n") line just because it worked.


Solution

  • i found it via the answer PayPal IPN always return INVALID even when I follow PayPal sample code to the word?

    turns out: the ipn-simulator does not (or does not always?) give VALID responses but the sandbox accounts do.