Easily Add Payments to your Django Site

Here at Eldarion, we launch a lot of sites—both for ourselves as well as for our clients. A large portion of these sites require some form of collecting payments. This used to be a painful and hard problem. Not anymore.

django-stripe-payments

We have integrated websites with a lot of different providers, but none have been as friendly to the developer as Stripe. They have an easy to learn API and do a lot of the heavy lifting that typical subscription-based sites need.

In typical Eldarion fashion we applied the rule of three to code we had written for Stripe integrations on three different sites.

On the first pass, we focus on getting something working for the specific case. As part of the second pass, we generally will copy over bits and use any differences that are needed to inform some abstractions. By the time we need some functionality for a third case, we abstract out the code into a package that can be integrated as a stand alone external app.

today we have open sourced django-stripe-payments and it is now available on Crate. Let's give you a quick overview of how to get going with it.

Installation

pip install django-stripe-payments

Configuration

Go to Stripe and get an account. You can worry about your banking details later. All you need to get going is a set of keys. After creating a username and password, click under Your Accounts and then Account Settings. Click on API Keys on the modal and copy and paste the Test Secret Key and Test Publishable Key. We will add these to your settings.py:

STRIPE_SECRET_KEY = os.environ.get(
    "STRIPE_SECRET_KEY",
    "<the secret test key>"
)
STRIPE_PUBLIC_KEY = os.environ.get(
    "STRIPE_PUBLIC_KEY",
    "<the publishable test key>"
)

Next, we need to add the app to your INSTALLED_APPS:

INSTALLED_APPS = [
    "payments"
]

You will also want to add the context processor:

TEMPLATE_CONTEXT_PROCESSORS = [
    "payments.context_processors.payments_settings"
]

Finally, configure your plans. Something like this:

PAYMENTS_PLANS = {
    "monthly": {
        "stripe_plan_id": "pro-monthly",
        "name": "Web App Pro ($25/month)",
        "description": "The monthly subscription plan to WebApp",
        "price": 25,
        "interval": "month"
    },
    "yearly": {
        "stripe_plan_id": "pro-yearly",
        "name": "Web App Pro ($199/year)",
        "description": "The annual subscription plan to WebApp",
        "price": 199,
        "interval": "year"
    }
}

Usage

First thing you should do when you start, is to seed the Stripe account with your plans:

python manage.py init_plans

If you already have users, you should go ahead and create customer records for them. This isn't mandatory as you might want to have your site not create a customer until they became a paying customer. You can create customers however that don't have a plan or have entered a card. I typically just setup a signal to listen for User creates and create a Customer at the same time. You can run the following command to create a customer for every user and link them together:

python manage.py init_customers

From here, a lot of the integration depends on what you want to accomplish in your site. There is an example of middleware you can add to your site to control access if you user doesn't have an active subscription.

A typical installation would hook up the middleware and a list of white-listed urls that someone who wasn't a subscriber could see, then add in the payments.urls, add links to the payments urls in an account subnav somewhere, wire up the user create signal, init the plans and customers, and launch.

Summary

We hope you find this useful for you subscription-based site needs. We love feedback. We love pull requests even more. If you are not a developer but rather someone looking for someone to build a subscription-based site for you, then get in touch as we'd love to to see how we can help.

comments powered by Disqus