There are currently three ways to accept one-time payments with Stripe:
Which one should you use?
Although you can still use the Charges API, if you’re new to Stripe do not use it since it does not support the latest banking regulations (like SCA). You will see a high rate of declines. For more, review the Charges vs. Payment Intents APIs page from the official Stripe docs.
Still using the Charges API? If most of your customers are based in the US or Canada you don’t need to migrate just yet. Review the Checkout migration guide guide for more info.
Create a new project directory along with a new Django project called djangostripe
:
$ mkdir django-stripe-checkout && cd django-stripe-checkout
$ python3.8 -m venv env
$ source env/bin/activate
(env)$ pip install django==3.1
(env)$ django-admin.py startproject djangostripe .
Feel free to swap out virtualenv and Pip for Poetry or Pipenv.
Next, create a new app called payments
:
(env)$ python manage.py startapp payments
Now add the new app to the INSTALLED_APPS
configuration in settings.py:
## djangostripe/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
## Local
'payments.apps.PaymentsConfig', ## new
]
Update the project-level urls.py file with the payments
app:
## djangostripe/urls.py
from django.contrib import admin
from django.urls import path, include ## new
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('payments.urls')), ## new
]
Create a urls.py file within the new app, too:
(env)$ touch payments/urls.py
Then populate it as follows:
## payments/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.HomePageView.as_view(), name='home'),
]
Now add a views.py file:
## payments/views.py
from django.views.generic.base import TemplateView
class HomePageView(TemplateView):
template_name = 'home.html'
And create a dedicated “templates” folder and file for our homepage.
(env)$ mkdir templates
(env)$ touch templates/home.html
Then, add the following HTML:
<!-- templates/home.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Django + Stripe Checkout</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.8.2/css/bulma.min.css">
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
</head>
<body>
<section class="section">
<div class="container">
<button class="button is-primary" id="submitBtn">Purchase!</button>
</div>
</section>
</body>
</html>
Make sure to update the settings.py file so Django knows to look for a “templates” folder:
## djangostripe/settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'], ## new
...
Finally run migrate
to sync the database and runserver
to start Django’s local web server.
(env)$ python manage.py migrate
(env)$ python manage.py runserver
That’s it! Check out http://localhost:8000/ and you’ll see the homepage:
Time for Stripe. Start by installing it:
(env)$ pip install stripe==2.50.0
Next, register for a Stipe account (if you haven’t already done so) and navigate to the dashboard. Click on “Developers” in the left sidebar:
Then from the dropdown list click on “API keys”:
Each Stripe account has four API keys: two for testing and two for production. Each pair has a “secret key” and a “publishable key”. Do not reveal the secret key to anyone; the publishable key will be embedded in the JavaScript on the page that anyone can see.
Currently the toggle for “Viewing test data” in the upper right indicates that we’re using the test keys now. That’s what we want.
At the bottom of your settings.py file, add the following two lines including your own test secret and test publishable keys. Make sure to include the ''
characters around the actual keys.
## djangostripe/settings.py
STRIPE_PUBLISHABLE_KEY = '<your test publishable key here>'
STRIPE_SECRET_KEY = '<your test secret key here>'
Finally, you’ll need to specify an “Account name” within your “Account settings” at https://dashboard.stripe.com/settings/account:
#django #stripe #web-development