Integrating with external API, in my opinion, is always a little bit tricky especially when you do that first time with a specific product. You need spent tons of hours reading documentation and it often turns out that the answer is on the stack instead of docs…
In this tutorial, I’ll focus on billing product which means you’ll get knowledge about how to create subscriptions and manage them. I’ll use low code examples of docs - don’t be scared that’s easy!
In our project, we'll use Docker so we have to create two files Dockerfile and docker-compose.yml. But first let’s create base folder structure of our project.
Now back to the main project folder this is django-stripe-integration and let’s create Dockerfile and docker-compose.yml.
In Dockerfile we define the python version which we want to use, next is workdir which means folder in docker container, then copy requirements.tsx to workdir and install assets, last step is copying everything to code workdir. If you want to create project like me check this requirements.
Dockerfile
In Docker-compose.yml we define two services postgres and django - this is very simple file only for test project purpose as you can see in database there is password, that should comes from env file.
Now we have everything to start app so you can hit that command:
After the build you'll have information about migration so you need to check name of your container using docker ps
In my case my container have django-stripe-integration_django_1 name so lets jump into this container.
First of all before we gonna continue our work we need to jump into settings.py.
For keeping good practices of security you must keep your secret variables in .env file, and then you can use that variables using environ like example bellow. By the way here is my .env.example file.
Above we defined basic things for django, but now we need to provide things for our database.
And the last major thing is this what we actually doing in this tutorial are things for Stripe.
Before we gonna start coding let’s create two apps - auth_ex and subscriptions.
After start app you need of course adjust your settings, because in this example we gonna use djoser for authorization - first of all is installed apps.
I think we have everything done with the basic setup so we can switch the info User model, To be honest, you don’t need to do this, because in this article I gonna focus on Stripe integration. As you can see I created UserManaged and User model with a pretty simple structure but the crucial thing is field customer_id in User because when you need to deal with Stripe customer id is the really important thing.
Before you start coding you need to have access to the developer dashboard in Stripe, when you log in grab your API secret key and pass it into the env file.
Now we can continue our work and start with viewSets for subscriptions. First is CreateCheckoutSession this endpoint is for creating URL for checkout session in stripe - when in your frontend app user need to add a payment method and create a subscription.
CustomerPortal endpoint is for managing user subscription, flow is a user from frontend need to just ask API for this, and then redirect.
When in your frontend app you need to display all available plans this is simple endpoint for this.
The last thing is webhooks - actually, this is a crucial thing because in your API you need to detect what happened in stripe API.
Jump into stripe dashboard and create webhook there as on a screen bellow. Remember to grab your secret webhook key and pass to env file.
As you can see you need to provide Endpoint URL and while I was working on local environment I used ngrok for testing webhooks.
Now we can code webhooks in our API, we gonna listen checkout.session.completed action and on this action we'll update our User and customer_id field.
After creating viewSets we need to define urls - check this out.
As I mentioned before dealing with extending API is always tricky, I hope now you will get some knowledge of how to deal with stripe using Python and Django. That project was pretty simple only for testing purposes and gaining knowledge of Python and Stripe. If you want check repo of this project and each files here. Thanks for your time! If you have questions you can ask me about them on my socials YouTube channel or Twitter.