Stripe Payment Options

There are currently three ways to accept one-time payments with Stripe:

  1. Charges API (legacy)
  2. Stripe Checkout (the focus of this tutorial)
  3. Payment Intents API (often coupled with Stripe Elements)

Which one should you use?

  1. Use Checkout if you want to get up and running fast. If you’re familiar with the old modal version of Checkout, this is the way to go. It provides a ton of features out-of-the-box, supports multiple languages, and includes an easy path to implementing recurring payments. Most importantly, Checkout manages the entire payment process for you, so you can begin accepting payments without even having to add a single form!
  2. Use the Payment Intents API If you want a more custom experience for your end users.

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.

Project Setup

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)$ startproject djangostripe .

Feel free to swap out virtualenv and Pip for Poetry or Pipenv.

Next, create a new app called payments:

(env)$ python startapp payments

Now add the new app to the INSTALLED_APPS configuration in

## djangostripe/


    ## Local
    'payments.apps.PaymentsConfig', ## new

Update the project-level file with the payments app:

## djangostripe/

from django.contrib import admin
from django.urls import path, include ## new

urlpatterns = [
    path('', include('payments.urls')), ## new

Create a file within the new app, too:

(env)$ touch payments/

Then populate it as follows:

## payments/

from django.urls import path

from . import views

urlpatterns = [
    path('', views.HomePageView.as_view(), name='home'),

Now add a file:

## payments/

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>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Django + Stripe Checkout</title>
    <link rel="stylesheet" href="">
    <script defer src=""></script>
  <section class="section">
    <div class="container">
      <button class="button is-primary" id="submitBtn">Purchase!</button>

Make sure to update the file so Django knows to look for a “templates” folder:

## djangostripe/

        '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 migrate
(env)$ python runserver

That’s it! Check out http://localhost:8000/ and you’ll see the homepage:


Add Stripe

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:

Stripe Developers

Then from the dropdown list click on “API keys”:

Stripe Developers Key

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 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/

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

Stripe Account Name

#django #stripe #web-development

Django Stripe Tutorial
27.80 GEEK