Customers 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 into 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 creating a webhook endpoint in the 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>")
</br>STRIPE_LIVE_SECRET_KEY = os.environ.get("STRIPE_LIVE_SECRET_KEY", "<your secret key>")
</br>STRIPE_TEST_PUBLIC_KEY = os.environ.get("STRIPE_TEST_PUBLIC_KEY", "<your publishable key>")
</br>STRIPE_TEST_SECRET_KEY = os.environ.get("STRIPE_TEST_SECRET_KEY", "<your secret key>")
</br>DJSTRIPE_WEBHOOK_SECRET = "whsec_xxx"

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 three days of a free trial.

We run the 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</br></br>python djstripe_init_customers</br></br>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 on the Developer > Webhooks page.

Screen Shot —

Let’s create an endpoint for customer deleted event:

<strong class="ke ki">from</strong> <strong class="ke ki">djstripe</strong></br><strong class="ke ki">import</strong> webhooksfrom <strong class="ke ki">django.core.mail </strong>import <strong class="ke ki">send_mail@webhooks.handler</strong>("customer.deleted")</br>
<strong class="ke ki">def</strong> customer_deleted_event_listener(event, **kwargs):
    </br>'Subscription Deleted',
    </br>'See ya! 👋',
    </br>fail_silently=<strong class="ke ki">False</strong>,

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 the Stripe Checkout method, when implementing a SaaS application API, we should create a Stripe session which has some customer information about the 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 the “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 excellent documentation. I suggest that you read through it before using Stripe.


Author: Berat Nakip

Date Published: Jul 2, 2020