Skip to content
Development

Set Up Stripe on Django

Integrating Stripe directly into Django gives you maximum control over your checkout flow. This guide walks you through a professional, "clean" setup using the Stripe CLI to handle local testing and webhooks.

CodingBowl··2 min read
Photo by Annie Spratt on Unsplash

Integrating Stripe directly into Django gives you maximum control over your checkout flow. This guide walks you through a professional, "clean" setup using the Stripe CLI to handle local testing and webhooks.

Phase 1: Local Environment & CLI

1. Install Stripe CLI

On Windows (PowerShell):

`winget install --id Stripe.StripeCLI

`

On macOS (Homebrew):

`brew install stripe/stripe-cli/stripe

`

2. Pair Your Account

Link your local machine to your Stripe Sandbox:

`stripe login

`

3. Install Python Library

Install the official Stripe library in your virtual environment:

`pip install stripe python-dotenv

`

Phase 2: Django Configuration

4. Set Your API Keys

Add these to your .env file from the Stripe Dashboard:

`
STRIPE_PUBLIC_KEY=pk_test_...
STRIPE_SECRET_KEY=sk_test_...

`

5. Import Keys into Settings

Update your settings.py to load the environment variables:

`
import os
STRIPE_PUBLIC_KEY = os.getenv('STRIPE_PUBLIC_KEY')
STRIPE_SECRET_KEY = os.getenv('STRIPE_SECRET_KEY')
STRIPE_WEBHOOK_SECRET = os.getenv('STRIPE_WEBHOOK_SECRET')

`

6. Sync Your Catalog

Run your custom management command to pull Products and Prices from Stripe into your local database:

`python manage.py sync_stripe_catalog

`

Phase 3: Webhook Setup

7. Start the Webhook Listener

Create a secure tunnel from Stripe to your local server:

`stripe listen --forward-to localhost:8000/payments/webhook/

`

8. Update the Webhook Secret

Copy the "Signing Secret" (whsec_...) from the terminal output of the previous step and add it to your .env file.

9. Create the Webhook View

Build a Django view using @csrf_exempt that uses stripe.Webhook.construct_event to verify incoming signals.

Phase 4: Running & Testing

10. Launch the Servers

Keep the 'stripe listen' command running in one terminal, and start your Django server in another:

`python manage.py runserver

`

11. Trigger a Clean Test Event

Test the connection by creating a real, deletable customer object instead of a guest record:

`stripe trigger customer.created

`

12. Final Verification

Check your listener terminal for a "200 OK" response. This confirms your tunnel, secret, and Django view logic are all working together.