Stripe Subscription Payment Options

Stripe Subscription Payment Options

Stripe Subscription Payment Options. This tutorial looks at how to handle subscription payments with Django and Stripe.

There are multiple ways to implement and handle Stripe subscriptions, but the two most well-known are:

  1. Fixed-price subscriptions
  2. Future payments

In both cases, you can either use Stripe Checkout (which is a Stripe-hosted checkout page) or Stripe Elements (which are a set of custom UI components used to build payment forms). Use Stripe Checkout if you don't mind redirecting your users to a Stripe-hosted page and want Stripe to handle most of the payment process for you (e.g., customer and payment intent creation, etc.), otherwise use Stripe Elements.

The fixed-price approach is much easier to set up, but you don't have full control over the billing cycles and payments. By using this approach Stripe will automatically start charging your customers every billing cycle after a successful checkout.

Fixed-price steps:

  1. Redirect the user to Stripe Checkout (with mode=subscription)
  2. Create a webhook that listens for checkout.session.completed
  3. After webhook is called, save relevant data to your database

The future payments approach is harder to set up, but this approach give you full control over the subscriptions. You collect customer details and payment information in advance and charge your customers at a future date. This approach also allows you to sync billing cycles so that you can charge all of your customers on the same day.

Future payments steps:

  1. Redirect the user to Stripe Checkout (with mode=setup) to collect payment information
  2. Create a webhook that listens for checkout.session.completed
  3. After webhook is called, save relevant data to your database
  4. From there, you can charge the payment method at a future date using the Payment Intents API

In this tutorial, we'll use the fixed-price approach with Stripe Checkout.

Billing Cycles

Before jumping in, it's worth noting that Stripe doesn't have a default billing frequency. Every Stripe subscription's billing date is determined by the following two factors:

  1. billing cycle anchor (timestamp of subscription's creation)
  2. recurring interval (daily, monthly, yearly, etc.)

For example, a customer with a monthly subscription set to cycle on the 2nd of the month will always be billed on the 2nd.

If a month doesn’t have the anchor day, the subscription will be billed on the last day of the month. For example, a subscription starting on January 31 bills on February 28 (or February 29 in a leap year), then March 31, April 30, and so on.

To learn more about billing cycles, refer to the Setting the subscription billing cycle date page from the Stripe documentation.

Project Setup

Let's start off by creating a new directory for our project. Inside the directory we'll create and activate a new virtual environment, install Django, and create a new Django project using django-admin:

$ mkdir django-stripe-subscriptions && cd django-stripe-subscriptions
$ python3.8 -m venv env
$ source env/bin/activate

(env)$ pip install django
(env)$ django-admin.py startproject djangostripe .

After that, create a new app called subscriptions:

(env)$ python manage.py startapp subscriptions

Register the app in djangostripe/settings.py under INSTALLED_APPS:

## djangostripe/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'subscriptions.apps.SubscriptionsConfig', ## new
]

Create a new view called home, which will serve as our main index page:

## subscriptions/views.py

def home(request):
    return render(request, 'home.html')

django

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Main Pros and Cons of Django As A Web Framework for Python Developers

India's best Institute for Django Online Training Course & Certification. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

django-allauth Django REST Framework |teratail

Djangoアプリを、当初は Django REST Framework (DRF) を使わずに作成しました。(認証パッケージは、 django-allauthを使用)アプリリリース後に、DRFも追加で導入して、DRFで作成したAPIでのデ

Django CRUD Application — PostgreSQL

In this blog, let’s see what is CRUD and how to perform CRUD with Django. Also, visit my previous blogs if you have any problem with connecting Django and Databases. In this blog, I am performing CRUD functionality with PostgreSQL.

Django REST Framework Course for Beginners

In this article we are going to talk about Django REST Framework Course for Beginners, also for more information you can check the complete video for th