### Setup and Run Tests Source: https://dj-stripe.dev/docs/latest/installation Prepare the PostgreSQL database and install tox for running the project's tests. ```bash createdb djstripe pip install tox tox ``` -------------------------------- ### Install dj-stripe with pip Source: https://dj-stripe.dev/docs/latest/installation Use pip to install the dj-stripe package. ```bash pip install dj-stripe ``` -------------------------------- ### Install dj-stripe with Poetry Source: https://dj-stripe.dev/docs/latest/installation Use Poetry to add dj-stripe as a project dependency. This is the recommended installation method. ```bash poetry add dj-stripe ``` -------------------------------- ### Handle Stripe Webhooks with Django Signals Source: https://dj-stripe.dev Implement custom logic for Stripe webhook events using Django's native signals. This example shows how to handle new customer subscriptions. ```python from django.dispatch import receiver from djstripe import webhooks @webhooks.handler("customer.subscription.created") def handle_new_subscription(event): # Automatically synced to your database subscription = event.data["object"] # Your custom business logic here send_welcome_email(subscription.customer.email) ``` -------------------------------- ### Run Django Migrations and Sync Models Source: https://dj-stripe.dev/docs/latest/installation Apply database migrations and synchronize dj-stripe models with Stripe data. Ensure Stripe API keys are already added to the database. ```bash python manage.py migrate python manage.py djstripe_sync_models ``` -------------------------------- ### Configure Stripe API Keys and Mode Source: https://dj-stripe.dev/docs/latest/installation Set your Stripe live and test secret keys and configure the operating mode. Ensure STRIPE_LIVE_MODE is a boolean. ```python STRIPE_LIVE_SECRET_KEY = os.environ.get("STRIPE_LIVE_SECRET_KEY", "{your secret key}") STRIPE_TEST_SECRET_KEY = os.environ.get("STRIPE_TEST_SECRET_KEY", "{your secret key}") STRIPE_LIVE_MODE = False # Change to True in production DJSTRIPE_FOREIGN_KEY_TO_FIELD = "id" ``` -------------------------------- ### Add dj-stripe to INSTALLED_APPS Source: https://dj-stripe.dev/docs/latest/installation Include 'djstripe' in your Django project's INSTALLED_APPS setting to enable the app. ```python INSTALLED_APPS = ( ... "djstripe", ... ) ``` -------------------------------- ### Query Stripe Data with Django ORM Source: https://dj-stripe.dev Use Django's ORM to query Stripe customer and subscription data. Ensure dj-stripe models are imported. ```python from djstripe.models import Customer, Subscription customer = Customer.objects.get(email="user@example.com") active_subs = customer.subscriptions.filter(status="active") ``` -------------------------------- ### Include dj-stripe URLs Source: https://dj-stripe.dev/docs/latest/installation Add dj-stripe's URL patterns to your project's urls.py to handle Stripe-related requests. ```python path("stripe/", include("djstripe.urls", namespace="djstripe")), ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.