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 settings.py:

PAYPAL_RECEIVER_EMAIL = "yourbusinessaccount@example.com"

INSTALLED_APPS = ( ...,
  'paypal.standard.ipn',
  ...)

Now, you’ll have to add an entry in your urls.py 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 views.py. 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": "http://example.com/something/hard/to/guess/",
    "return_url": "http://example.com/thanks/",
    "cancel_return": "http://example.com/cancel/",
    "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 paypal.standard.forms.py 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="mailto:you@example.com">email us</a>.
	</p>

	<A HREF="https://www.paypal.com/cgi-bin/webscr?cmd=_subscr-find&alias={{ encoded_email }}" _fcksavedurl="https://www.paypal.com/cgi-bin/webscr?cmd=_subscr-find&alias={{ encoded_email }}"><IMG BORDER="0" SRC="https://www.paypal.com/en_US/i/btn/btn_unsubscribe_LG.gif"></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="mailto:you@example.com">email us</a>.
		</p>

	{{ 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!

About Robert Graham

I am Robert Graham. In late 2009 I launched my first attempt at a SaaS webapp and business. In 2012 I wrote a book about cold calling for startups. I write code, do marketing, outsource, and live through the ups and downs. You can read about my adventures here. Google Authorship
No comments yet.

Leave a Reply