Skip to content

PayPal IPN Python Code

by Paul Kenjora on October 2nd, 2008

PayPal has Instant Payment Notification (IPN) libraries with examples for Perl, Java, and even Ruby, but look as hard as I may none for Python. Then again I spent 2 years at PayPal as a senior developer, C++ and Java are the dominant languages. Django and Rails are not even on the radar (well I exaggerate a few people have vision). Point is I couldn’t find any clean and simple IPN code for my Django projects.

Yes, I know, I know…. There are other posts with source code for IPN. For example Google Code has an example. So does Django Snippets. Both are great examples but I don’t need a shopping cart or Google checkout, I just need PayPal IPN.

I decided to roll my own, after all I spent 2 years looking at the source code how tough could it be? Not tough at all actually, it just takes a while to dig through the documentation. Below is the source code for PayPal IPN. Its tested, and deployed on several projects.

The above code is the bare minimum required to correctly process an IPN request. Everything else you can setup through the PayPal sandbox. They even have a testing tool just for IPN within the sandbox.

Looking back on the code, I guess that PayPal does not have a Python/Django library or examples because its so simple. Once you have the IPN code above everything else is fairly straight forward. Assembling the purchase request, cart or single item, is not too difficult. You can do it using a simple HTML form. Encryption on the payment button is nice but IPN is even more secure so I would not spend too much time on the encrypt.

From → PayPal, Tutorial

  • Sam
    Hi

    I am a complete beginner at this HTTP stuff, so I was wondering a probably really stupid question - how exactly can i implement this function?! What I mean is how do i receive Paypal's response to my payment and pass it to this function?

    Thanks very much
    Sam
  • Sam
    Sorry, should have mentioned I'm trying to use paypal without Django, so kinda need this code but to implement on my own site...
    Thanks for the help!
    Sam
  • Jesper Taxbøl
    First of all good work! :)

    I am just trying to integrate this snippet in a small django app, and I am having trouble making it work. This might be because im working through a shell, and only have passive means to detect how long the script succeeds. So debugging is sort of a pain.

    But it seems to me that something might have changed or there is an error in the code. Can anybody verify that it is still working?

    I suspect the same thing Brian Neal suspects, that the line 13 is wrong. I tried assuming that the payment_status is found in the POST data. This brings me down to the first urlencode, which then fails on me.

    This might be because I have nordic characters in my name, which pops up in the data. (http://stackoverflow.com/questions/787935/python-interface-to-paypal-urllib-urlencode-non-ascii-characters-failing) But im not sure.

    Any ideas?

    Sorry for being such a noob :)
  • You can set the encoding for your script to be UTF-8. I suggest using decode(string, 'utf-8') on the response data.
  • raniologi
    Thank you very much, great post.
  • Thank you for the code, I have been trying to learn about this. You have helped me a lot.
  • That's awesome i wanted this code badly and i got here.

    Can you give me shopping cart script for my website i am doing online portal through that website i will sell my some products like books and some electrical products.

    Thanks
  • Sweet, I'm going to recommend this option for my office.
  • Brian Neal
    Thanks for posting this.

    I didn't think you could index a request object like you are doing in line 13?
  • Guest
    Was a good read. I'll be back for more.
  • Was a good read. I'll be back for more.
  • Guest
    I hope we all learn something new each and everyday. It is what helps to improve who we are.
  • cprail
    Thank you very much. Simple, but a little help is always welcomed!
  • This is super handy for me -- thanks. Just wanted to let you know your RSS XML seems to be broken.
  • We've implemented something pretty similar in Satchmo. You can see the view we use for paypal IPN support - http://www.satchmoproject.com/trac/browser/satchmo/trunk/satchmo/payment/modules/paypal/views.py

    Like you said, though, the API is really pretty simple but this might help people understand how to link into other parts of their app.
  • Michael Warkentin
    Thanks for this! Perfect timing for me! :)
  • quite useful code!
    thanks for sharing.
blog comments powered by Disqus