Stripe and Starting dj-stripe

Stripe is great, but using a library makes it even better. Dj-stripe is a great django library for working with stripe to get people to subscribe to your service. See how simple it is to start working with dj-stripe.

resources

NOTES: For the recording of this video I fixed a bug in dj-stripe for migrations to work with it for pre-release of Django 1.7. The bug should be fixed around the time Django 1.7 releases.

settings.py

INSTALLED_APPS = (
    'djstripe',
)

STRIPE_PUBLIC_KEY = os.environ.get('STRIPE_PUBLIC_KEY', '')
STRIPE_SECRET_KEY = os.environ.get('STRIPE_SECRET_KEY', '')

DJSTRIPE_PLANS = {
    "monthly": {
        "stripe_plan_id": "basic_plan",
        "name": "Monthly Subscription",
        "description": "Monthly subscription plan",
        "price": 900,
        "currency": "usd",
        "interval": "month"
    }
}
views.py
import stripe

from django.conf import settings
from django.core.urlresolvers import reverse_lazy
from django.views.generic import FormView, TemplateView

from djstripe.models import Customer

from .forms import StripeForm


class StripeMixin(object):
    def get_context_data(self, kwargs):
        context = super(StripeMixin, self).get_context_data(kwargs)
        context['publishable_key'] = settings.STRIPE_PUBLIC_KEY
        return context


class SuccessView(TemplateView):
    template_name = 'store/thank_you.html'


class CustomerMixin(object):
    def get_customer(self):
        try:
            return self.request.user.customer
        except:
            return Customer.create(self.request.user)


class SubscribeView(StripeMixin, CustomerMixin, FormView):
    template_name = 'store/subscribe.html'
    form_class = StripeForm
    success_url = reverse_lazy('thank_you')

    def form_valid(self, form):
        customer = self.get_customer()
        customer.update_card(form.cleaned_data.get('stripe_token', None))
        customer.subscribe('monthly')

        return super(SubscribeView, self).form_valid(form)
comments powered by Disqus