Django and Stripe subscriptions — Part 3

Nuno Bispo
Sep 4, 2020 · 3 min read
Django + Stripe

In case you missed Part 2, you can find it here:

In the previous part of this tutorial series, we perform the following:

  • Stripe configuration, products and coupons
  • Views and html templates for checkout
  • Configured the static javascript files
  • Collected a test payment

On this Part 3 we will learn how to manage subscriptions and how to update our customer data in case something changes in Stripe.

1) Our settings page

If you remember from the previous articles, we had a settings page that only logged in members could access.

We will now expand that page so in case a user has a membership he can view the membership information and cancel if necessary.

Let’s start by changing our view to get the Stripe membership information:

cloudhomelab/membership/views.py (settings view)

In this view we have a check for a POST request or a GET request:

  • The POST request is used after the user pressed the cancel button on the page to cancel the membership. For that we get the Stripe subscription and set the flag to cancel at the period end.
  • GET request we are just getting the information.

The corresponding html template to show this view is as follows:

cloudhomelab/membership/templates/registration/settings.html

And, of course, we need to add our new url to the ‘urls.py’ file:

cloudhomelab/cloudhomelab/urls.py

2) Get updates from Stripe

We now have the capability of a user cancelling their subscription from our website, but what if there is a problem with the Stripe subscription?

For instance a credit card might expire and the subscription will not get renew. In those cases we want to update that info from our side and also suspend the membership for that user.

There are several ways of achieving this, for this tutorial I will create an update page that the Django superuser can access to perform the updates.

Let’s create view for the update page:

cloudhome/membership/views.py (view updateaccounts)

The decorator ‘user_passes_test’ allows us to define an inner function to perform the check if the logged user is a Django superuser. If that is the case then he can access the page.

The view handler gets all of our Customers from the database, their corresponding Stripe subscriptions and then according to the status, activate or deactivate the membership on our site. At the end we just return a simple response, so we don’t need an html template to render this view (perfectly fine for a backoffice page)

3) Conclusion

On this last part of our tutorial, we performed the following:

  • Created a page where our user can cancel their membership
  • Retrieve updates from Stripe for the subscriptions status

This concludes our 3 part tutorial, hope that now with this basic knowledge you are prepared to expand and create your own subscription based Django website with Stripe.

You can check out my GitHub at https://github.com/nunombispo

Or check my website at https://bispo-mobile.net

For low cost Nextcloud hosting, check out: https://cloudhomelab.com (50% discount with promo code 50OFF)

Until the next article…

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Nuno Bispo

Written by

Outsystems Professional Web Developer for over 10 years and also as a DevOps engineer. Trying to share my knowledge and experience of all things IT.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store