django-stripe-lite


Namedjango-stripe-lite JSON
Version 0.6.0 PyPI version JSON
download
home_pagehttps://github.com/yunojuno/django-stripe-lite
SummaryA library to aid Django integration with Stripe.
upload_time2023-11-15 12:17:33
maintainerYunoJuno
docs_urlNone
authorYunoJuno
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # 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.8+ & Django 3.2+.

## Installation & Usage

```bash
pip install django-stripe-lite
```

**Include the app in your INSTALLED_APPS setting:**

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

**Include the URLs in your URL conf:**

```python
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:**

```python
STRIPE_WEBHOOK_SECRET = "whsec_0DoBceBjS0jjm7aQj459FXiFSluJEBxt"
```

**Run the migrations:**

```bash
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](https://docs.djangoproject.com/en/3.0/ref/applications/#django.apps.AppConfig.ready)).

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.

```python
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:

```bash
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:

```bash
$ 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.

```bash
$ tox
```

#### CI

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

## Publish to PyPi

Update versions, then:

```bash
poetry build
poetry publish
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yunojuno/django-stripe-lite",
    "name": "django-stripe-lite",
    "maintainer": "YunoJuno",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "code@yunojuno.com",
    "keywords": "",
    "author": "YunoJuno",
    "author_email": "code@yunojuno.com",
    "download_url": "https://files.pythonhosted.org/packages/2f/37/89e3430e931db9662cc3e09125d07df45b67eb9c5151b6026c8cf062ded8/django_stripe_lite-0.6.0.tar.gz",
    "platform": null,
    "description": "# django-stripe-lite\n\nProvides a light-touch Django integration with Stripe.\n\nWe handle Stripe webhook security & persisting all events, while allowing your project to take care\nof the business logic.\n\nRequires Python 3.8+ & Django 3.2+.\n\n## Installation & Usage\n\n```bash\npip install django-stripe-lite\n```\n\n**Include the app in your INSTALLED_APPS setting:**\n\n```python\nINSTALLED_APPS = (\n    ...,\n    \"django_stripe\",\n)\n```\n\n**Include the URLs in your URL conf:**\n\n```python\nfrom django.urls import include, path\n\nurlpatterns = [\n    # Assuming we're at the root, this will make the webhook\n    # available at /stripe/webhook/\n    path(\"stripe/\", include(\"django_stripe.urls\", namespace=\"stripe\"))\n]\n```\n\n**Set the required settings in your settings file:**\n\n```python\nSTRIPE_WEBHOOK_SECRET = \"whsec_0DoBceBjS0jjm7aQj459FXiFSluJEBxt\"\n```\n\n**Run the migrations:**\n\n```bash\npython manage.py migrate django_stripe\n```\n\n**Set up your event handlers:**\n\nEvent handlers are simply functions in your code base, which are wrapped with a decorator which\nsignifies that they wish to handle a particular event type (or multiple) when it is received via the\nwebhook.\n\nAll event handlers must be imported at application startup, otherwise the decorator wil not be able\nto register them against the event type. An easy way to ensure this in your local project is to\ntrigger the import in one of your Django Apps `apps.py::AppConfig::ready()` method\n([see the docs](https://docs.djangoproject.com/en/3.0/ref/applications/#django.apps.AppConfig.ready)).\n\nWhen a webhook event is received, all processing of it is wrapped in a transaction such that a\nsingle event handler failure will result in an HTTP 500 returned from the endpoint and the\ntransaction will be rolled back resulting in no database changes for that request. This means that\nthe `WebhookEvent` is not persisted unless:\n\n-   it was received successfully and there were no active handlers registered for the event type,\n    or:\n-   it was received successfully and processed successfully by _all_ active handlers registered\n    against the event type.\n\n```python\nfrom django_stripe.models import WebhookEvent\nfrom django_stripe.webhooks import stripe_webhook_handler\n\n# Single event handler\n@stripe_webhook_handler(\"customer.subscription.deleted\")\ndef delete_customer_subscription(event: WebhookEvent) -> Any:\n    # event.data (dict, Stripe Event object.data field, the object which triggered the webhook event)\n    # event.event_type (str, the full event type name e.g customer.subscription.deleted)\n    # event.mode (textchoices, LIVE or TEST)\n    # event.stripe_created_at (datetime, when Stripe created the event)\n    # event.db_created_at (datetime, when the database initially saved the event)\n    # event.db_last_updated_at (datetime, when the database last saved the event)\n    # event.stripe_id (str, Stripe Event ID)\n    # event.api_version (str, Stripe API version)\n    # event.request_id (str, the Stripe ID of the instigating request, if available)\n    # event.request_idempotency_key (str, the idempotency key of the instigating request, if available)\n    # event.is_processed (bool, whether the event was processed by a handler successfully)\n    # event.headers (dict, the headers of the webhook request)\n    # event.remote_ip (str, Remote IP of the webhook request)\n    pass\n\n# Multiple event handler\n@stripe_webhook_handler(\n    \"customer.subscription.created\",\n    \"customer.subscription.deleted\",\n    \"customer.subscription.updated\",\n)\ndef customer_handler(event: WebhookEvent) -> Any:\n    # See notes above for event structure.\n    pass\n```\n\nThat's it, you should be able to start receiving webhook events with the Stripe CLI test client.\nThen once you're ready, setup the production webhook via the Stripe dashboard.\n\n## Development\n\nCheck out the repo, then get the deps:\n\n```bash\npoetry install\n```\n\n## Tests\n\n#### Running tests\n\nThe tests themselves use `pytest` as the test runner. If you have installed the `poetry` evironment,\nyou can run them:\n\n```bash\n$ poetry run pytest\n```\n\nThe CI suite is controlled by `tox`, which contains a set of environments that will format (`fmt`),\nlint, and test against all supported Python + Django version combinations.\n\n```bash\n$ tox\n```\n\n#### CI\n\nCI is handled by GitHub Actions. See the Actions tab on Github & the `.github/workflows` folder.\n\n## Publish to PyPi\n\nUpdate versions, then:\n\n```bash\npoetry build\npoetry publish\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A library to aid Django integration with Stripe.",
    "version": "0.6.0",
    "project_urls": {
        "Documentation": "https://github.com/yunojuno/django-stripe-lite",
        "Homepage": "https://github.com/yunojuno/django-stripe-lite",
        "Repository": "https://github.com/yunojuno/django-stripe-lite"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "136a18d4d8ec2eb82faf458fb289e1c8cc9df4cd823633626223fd025cdf41a8",
                "md5": "94f009c95bcbcc77a6a0179d5fabfa03",
                "sha256": "00ae9909c8811e75981d573795dfa48d0dce4fd4379979c1835ee7c02fde2959"
            },
            "downloads": -1,
            "filename": "django_stripe_lite-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "94f009c95bcbcc77a6a0179d5fabfa03",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 12498,
            "upload_time": "2023-11-15T12:17:25",
            "upload_time_iso_8601": "2023-11-15T12:17:25.210793Z",
            "url": "https://files.pythonhosted.org/packages/13/6a/18d4d8ec2eb82faf458fb289e1c8cc9df4cd823633626223fd025cdf41a8/django_stripe_lite-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2f3789e3430e931db9662cc3e09125d07df45b67eb9c5151b6026c8cf062ded8",
                "md5": "6099eed54a9ed92477acfacea66399eb",
                "sha256": "d457a4299e1695f9a91d9f1603010ecc69813e06f75e36d55fc5623961ad3a75"
            },
            "downloads": -1,
            "filename": "django_stripe_lite-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6099eed54a9ed92477acfacea66399eb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 10148,
            "upload_time": "2023-11-15T12:17:33",
            "upload_time_iso_8601": "2023-11-15T12:17:33.851475Z",
            "url": "https://files.pythonhosted.org/packages/2f/37/89e3430e931db9662cc3e09125d07df45b67eb9c5151b6026c8cf062ded8/django_stripe_lite-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-15 12:17:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yunojuno",
    "github_project": "django-stripe-lite",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "django-stripe-lite"
}
        
Elapsed time: 0.15214s