Skip to main content

django-stripe-lite

Provides a light-touch Django integration with Stripe.

We handle Stripe webhook security & persisting all events, while allowing your project to take care of the business logic.

Requires Python 3.12+ and Django 5.2-6.0.

Installation & Usage

pip install django-stripe-lite

Include the app in your INSTALLED_APPS setting:

INSTALLED_APPS = (
    ...,
    "django_stripe",
)

Include the URLs in your URL conf:

from django.urls import include, path

urlpatterns = [
    # Assuming we're at the root, this will make the webhook
    # available at /stripe/webhook/
    path("stripe/", include("django_stripe.urls", namespace="stripe"))
]

Set the required settings in your settings file:

STRIPE_WEBHOOK_SECRET = "whsec_0DoBceBjS0jjm7aQj459FXiFSluJEBxt"

Run the migrations:

python manage.py migrate django_stripe

Set up your event handlers:

Event handlers are simply functions in your code base, which are wrapped with a decorator which signifies that they wish to handle a particular event type (or multiple) when it is received via the webhook.

All event handlers must be imported at application startup, otherwise the decorator wil not be able to register them against the event type. An easy way to ensure this in your local project is to trigger the import in one of your Django Apps apps.py::AppConfig::ready() method (see the docs).

When a webhook event is received, all processing of it is wrapped in a transaction such that a single event handler failure will result in an HTTP 500 returned from the endpoint and the transaction will be rolled back resulting in no database changes for that request. This means that the WebhookEvent is not persisted unless:

  • it was received successfully and there were no active handlers registered for the event type, or:
  • it was received successfully and processed successfully by all active handlers registered against the event type.
from django_stripe.models import WebhookEvent
from django_stripe.webhooks import stripe_webhook_handler

# Single event handler
@stripe_webhook_handler("customer.subscription.deleted")
def delete_customer_subscription(event: WebhookEvent) -> Any:
    # event.data (dict, Stripe Event object.data field, the object which triggered the webhook event)
    # event.event_type (str, the full event type name e.g customer.subscription.deleted)
    # event.mode (textchoices, LIVE or TEST)
    # event.stripe_created_at (datetime, when Stripe created the event)
    # event.db_created_at (datetime, when the database initially saved the event)
    # event.db_last_updated_at (datetime, when the database last saved the event)
    # event.stripe_id (str, Stripe Event ID)
    # event.api_version (str, Stripe API version)
    # event.request_id (str, the Stripe ID of the instigating request, if available)
    # event.request_idempotency_key (str, the idempotency key of the instigating request, if available)
    # event.is_processed (bool, whether the event was processed by a handler successfully)
    # event.headers (dict, the headers of the webhook request)
    # event.remote_ip (str, Remote IP of the webhook request)
    pass

# Multiple event handler
@stripe_webhook_handler(
    "customer.subscription.created",
    "customer.subscription.deleted",
    "customer.subscription.updated",
)
def customer_handler(event: WebhookEvent) -> Any:
    # See notes above for event structure.
    pass

That's it, you should be able to start receiving webhook events with the Stripe CLI test client. Then once you're ready, setup the production webhook via the Stripe dashboard.

Development

Check out the repo, then get the deps:

poetry install

Tests

Running tests

The tests themselves use pytest as the test runner. If you have installed the poetry evironment, you can run them:

$ poetry run pytest

The CI suite is controlled by tox, which contains a set of environments that will format (fmt), lint, and test against all supported Python + Django version combinations.

$ tox

CI

CI is handled by GitHub Actions. See the Actions tab on Github & the .github/workflows folder.

Publish to PyPi

Update versions, then:

poetry build
poetry publish

Metadata

Release files for django-stripe-lite 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-stripe-lite 0.8.0
File Size Uploaded
django_stripe_lite-0.8.0.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-stripe-lite 0.8.0
File Interpreter ABI Platform
django_stripe_lite-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / django_stripe_lite-0.8.0.tar.gz

Download URL django_stripe_lite-0.8.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ad9c3909252250ad0f29053ac5da9d0ce91375d81e7309498cab1a1de5142d1c
BLAKE2b-256 checksum
How to use checksums
cf1d888b7e9be540ee1ee39558249f1fd059c5580cc1c8764b29d286e7342549
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / django_stripe_lite-0.8.0-py3-none-any.whl

Download URL django_stripe_lite-0.8.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ff0fba137697aa82c3f1056d3e08c108a892267cd997527cff7c48a0275b93e
BLAKE2b-256 checksum
How to use checksums
30f211f44faeb523a1e4bd97636524d0a86251a498c1df236a8b14aa3c0b3e93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page