Django-Paypal standard subscriptions integration

Want to have a site with PayPal integration using Django with no fuss? That is just django-paypal’s game. I’ll show you how to integrate with Website Payments Standard in a jiffy.

pip install django-paypal

You are using pip, right? If not:

apt-get install python-setuptools
easy_install pip

Whew! Thats better. Let’s fix up your



Now, you’ll have to add an entry in your for the Instant Payment Notifications (IPNs) from PayPal that let you know when people have paid:

urlpatterns = patterns('',
  url(r'^/something/hard/to/guess/, include('paypal.standard.ipn.urls')),

We need a view in That is easy too:

from django.conf import settings
def subscribe(request):
    # What you want the button to do.
    paypal_dict = {
    "cmd": "_xclick-subscriptions",
	"business": settings.PAYPAL_RECEIVER_EMAIL,
	"a1": "0",	                   # trial price
	"p1": 1,	                   # trial duration, duration of unit defaults to month
	"a3": "29.95",                     # yearly price
	"p3": 1,                           # duration of each unit (depends on unit)
	"t3": "Y",                         # duration unit ("M for Month")
    "src": "1",                        # make payments recur
	"sra": "1",                        # reattempt payment on payment error
	"no_note": "1",                    # remove extra notes (optional)
    "item_name": "PayPal Shows This To The User",
    "invoice": "101",
    "notify_url": "",
    "return_url": "",
    "cancel_return": "",
    "custom": request.user.username,   # put whatever data you need to track things here

    # Create the instance.
    form = PayPalPaymentsForm(initial=paypal_dict, button_type="subscribe")

    context = {"form": form, "encoded_email" : urlquote(settings.PAYPAL_RECEIVER_EMAIL), "paid" : request.user.get_profile().paid}

The reason I’ve included the encoded_email in the view is related to the unsubscribe button we’ll setup in a minute. The button in that view will be a subscription. You can easily modify it to be a buy now button. See the README for django-paypal. If you browse the code on github and checkout you can see all the possible options for buttons.

Now we can create the template and the (optional) unsubscribe button:

{% extends "base.html" %}

{% block content %}

{% if paid %}
<h2>Thank you! You are currently subscribed.</h2>
<p id="page-intro">You can cancel anytime online using your PayPal account or from this page.
	If you have any questions or problems please <a href="">email us</a>.

	<A HREF="{{ encoded_email }}" _fcksavedurl="{{ encoded_email }}"><IMG BORDER="0" SRC=""></A>
{% else %}
	<!-- Page Head -->
	<h2>Please subscribe to Example using PayPal.</h2>
	<p id="page-intro">You can cancel anytime online using your PayPal account or on this page.
		If you have any questions or problems please <a href="">email us</a>.

	{{ form.render }}
{% endif %}
{% endblock %}

Keying off of if the user has paid or not means we can use this template for subscribing or unsubscribing. Now go get paid!

8 thoughts on “Django-Paypal standard subscriptions integration”

  1. Great tutorial. I am just wondering, after setting the URL for the IPN, where do I write the code for what I want to be done once we get the notification?

    1. You’ll need a route/URLConf that catches the notification when it is POSTed to the URL. The code in the Django view for that route will be executed when you receive the IPN and you will do what you need to with the IPN information from PayPal there.

  2. Well it be great if you finish covering up the rest of the tutorial. I’m starting my own business and In my country electronic payments are tedious and the easiest way I found out to tackle this problem is using paypal. Thanks in advance!

  3. I have done subscription by django paypal.Now my doubt is how do i cancel subscription and If i do that how will the user will get notification

Leave a Reply

Your email address will not be published. Required fields are marked *