Products Solutions Contact TR

Django Stripe Integration using dj-stripe

I had the opportunity to examine dj-stripe while researching which payment libraries are used in Django projects. If you are going to integrate a payment system to your project, you may want to have features such as subscription management or single-unit purchases. Especially subscription management is quite essential and time-consuming subject for SaaS projects. dj-stripe takes care of this.


  • A Django project (Django version>= 2.2)
  • Python version >= 3.6
  • Ready to use Stripe account


pip install dj-stripe

Then we add djstripe to INSTALLED_APPS:


and add to

path("stripe/", include("djstripe.urls", namespace="djstripe"))

Stripe Settings


You can find webhook endpoints Developers > Webhooks  in Stripe Dashboard. After we create a webhook endpoint in Stripe dashboard, we get our webhook secret key which starts with whsec_.

Screen Shot —

API Keys

Screen Shot —

We take our Stripe public and secret keys from Developers > API Keys  then add them to our .env  file. Our file should be like:

STRIPE_LIVE_PUBLIC_KEY = os.environ.get("STRIPE_LIVE_PUBLIC_KEY", "<your publishable key>")

STRIPE_LIVE_SECRET_KEY = os.environ.get("STRIPE_LIVE_SECRET_KEY", "<your secret key>")
STRIPE_TEST_PUBLIC_KEY = os.environ.get("STRIPE_TEST_PUBLIC_KEY", "<your publishable key>")
STRIPE_TEST_SECRET_KEY = os.environ.get("STRIPE_TEST_SECRET_KEY", "<your secret key>")

dj-stripe will use your test keys while STRIPE_LIVE_MODE is False .


Screen Shot —

Name” field is required. I created a recurring plan with 3 days of free trial.

We run below commands to create dj-stripe tables, initialize existing customers on Stripe and migrate created plans from Stripe to our dj-stripe tables:

python migrate

python djstripe_init_customers

python djstripe_sync_plans_from_stripe


You may send an email to the customer when checkout fails, or you may want to remind your customer that the subscription will end in x days. In these cases, you will choose the events you want to listen to on the Stripe dashboard, and you should implement a listener method for each.

We choose our events in the Developer > Webhooks page.

Screen Shot —

Let’s create an endpoint for customer deleted event:

from djstripe
import webhooksfrom django.core.mail import send_mail@webhooks.handler("customer.deleted")
def customer_deleted_event_listener(event, **kwargs):
'Subscription Deleted',
'See ya! 👋',

We can find customer information inside the event variable.


Checkout creates a secure, Stripe-hosted payment page that lets you collect payments quickly. It works across devices and is designed to increase your conversion.

For more information >>

In Stripe Checkout method, when implementing a SaaS application API, we should create a Stripe session which has some customer information about checkout step (e.g., plan name, customer id)

customer = Customer.objects.get(subscriber=request.user)
stripe.api_key = STRIPE_SECRET_KEY
session = stripe.checkout.Session.create(,
'items': [{

data = {
}return JsonResponse(data, status=200)

After creating session_id, we deliver it to our client application.

We should import Stripe.js before implementing checkout steps on the client.

We add the below code line to our .html file:

<script src=""></script>

We initialize a stripe object:

var stripe = Stripe(YOUR_PUBLIC_KEY);

When a customer clicks “Subscribe Now” button, it should run the piece of code below:

sessionId: this.state.session_id
.then(function(result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
var displayError = document.getElementById('error-message');
displayError.textContent = result.error.message;});

When a customer clicks the button, they see the below page:

Screen Shot —

P.S: If you use Stripe test keys, the page is displayed in test mode. You can test your checkout step with test card numbers.

  • Default U.S. card — 4242 4242 4242 4242
  • Authenticate with 3D Secure — 4000 0000 0000 3220


Stripe has lots of features with fantastic documentation. I suggest that you read through it before using Stripe.


Author: Berat Nakip

Date Published: Jul 2, 2020