Accepting and processing PayPal IPN messages (including using App Engine)
PayPal's https://www.paypal.com/ipn is ridiculously easy to consume. You can tell PayPal to POST every single transaction on your account to a URL somewhere, then set up a handler at that URL which processes those transactions in some way. Security is ensured by POSTing the incoming data back to PayPal for confirmation that the transaction is legitimate.
These classes are probably over-engineered, but they were a fun experiment in creating class-based generic views.
This snippet helps me to understand how IPN works, and eventually I am able to implement my own IPN handler (Django 1.6 on Python 2.6). Thank you.
One thing I am not sure is that how to make sure the cmd=_notify_validate is put in the beginning of the parameter list (as requested by PayPal) without the use of an OrderedDict. So I force it to the beginning by doing something similar to the following:
'cmd=_notify_validate&' + urllib.urlencode(data)
I ran into Unicode errors when processing orders from some countries (Encode/Decode errors)..
I fixed it by replacing args.update(data) in the verify method with the following:
for k, v in data.items():
args[k] = v.encode('utf-8')
Seemed to solve my issues..
Great snippet. Much cleaner than the paypalipn app I created.
One thing though, I think you should make the Endpoint class inherit 'object' at it's base..
So that when over writing methods like __init__ you can call super() without any problems.
For instance, I wanted to change the obj.verify_url when calling the class for testing purposes..
def __init__(self, *args, **kwargs):
is_test = kwargs.pop('is_test', False)
super(PaypalIPN, self).__init__(*args, **kwargs)
self.verify_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'
Without object as the base inheritance this raises a TypeError.
PayPal have an IPN simulation tool for testing, which you can use to send example requests to your endpoint URL:
(You'll need to sign up for a PayPal developer account to use it)
Annoyingly, it doesn't look like it's possible to get that tool to send UTF8 rather than windows-1252.