Stripe Webhook
The StripeWebhook
class is a powerful tool for processing Stripe webhooks in your Django application. It provides a simple and flexible way to handle incoming webhook requests, allowing you to focus on implementing your business logic.
Using the StripeWebhook
To use the StripeWebhook
class, you'll need to create a new class that inherits from it. This new class will define the logic for processing incoming webhook requests.
Here's an example of how you might use the StripeWebhook
class to implement a new webhook:
Example Webhook
from django_stripe.actions import StripeWebhook
class MyWebhook(StripeWebhook):
def process_webhook(self, event_data):
# Process the incoming webhook request here
# For example, you might update a database record or send a notification
print("Received webhook event:", event_data)
In this example, the MyWebhook
class inherits from StripeWebhook
and defines a process_webhook
method. This method will be called whenever an incoming webhook request is received.
Automatic Registration
One of the benefits of using the StripeWebhook
class is that you don't need to register the webhook manually. The class will automatically register the webhook with Stripe when it's instantiated.
This means that you can focus on implementing your business logic, without worrying about the underlying registration process.
Example Use Case
Here's an example of how you might use the StripeWebhook
class to implement a webhook that updates a database record when a customer's subscription is updated:
Example Use Case
from django_stripe.actions import StripeWebhook
from django_stripe.models import StripeCustomer
class SubscriptionUpdatedWebhook(StripeWebhook):
def process_webhook(self, event_data):
customer_id = event_data["data"]["object"]["customer"]
customer = StripeCustomer.objects.get(stripe_id=customer_id)
customer.subscription_status = event_data["data"]["object"]["status"]
customer.save()
In this example, the SubscriptionUpdatedWebhook
class inherits from StripeWebhook
and defines a process_webhook
method. This method updates a Customer
record in the database when a subscription is updated.
Tips and Best Practices
- Make sure to handle any exceptions that might occur during the processing of incoming webhook requests.
- Use the
process_webhook
method to perform any necessary business logic, such as updating database records or sending notifications. - Use the
event_data
parameter to access the incoming webhook request data. - Use the
stripe
library to interact with the Stripe API, if necessary.
By following these tips and best practices, you can use the StripeWebhook
class to implement powerful and flexible webhooks in your Django application.
Implementing Webhook API
To implement a webhook API, you need to create a view that handles incoming webhook requests and processes the event data using the StripeWebhook
class.
Here is an example of how to implement a webhook API using Django:
Implement Webhook API using Django
from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
from django_stripe.actions import StripeWebhook
@require_http_methods(["POST"])
def stripe_webhook(request):
event_data = request.json()
StripeWebhook.process_webhook(event_data)
return HttpResponse("Webhook processed successfully")
This example creates a view that handles incoming webhook requests, processes the event data using the StripeWebhook
class, and returns a success response.
You can also use a library like Django Rest Framework to implement a webhook API. Here is an example:
Implement Webhook API using DRF
from rest_framework.views import APIView
from rest_framework.response import Response
from django_stripe.actions import StripeWebhook
class StripeWebhookView(APIView):
def post(self, request):
event_data = request.data
StripeWebhook.process_webhook(event_data)
return Response("Webhook processed successfully")
This example creates a view that handles incoming webhook requests, processes the event data using the StripeWebhook
class, and returns a success response.
Registering Webhook API
To register the webhook API, you need to add a URL pattern to your Django project's URL configuration. Here is an example:
Example
from django.urls import path
from .views import stripe_webhook
urlpatterns = [
path("stripe/webhook/", stripe_webhook, name="stripe_webhook"),
]
This example adds a URL pattern for the stripe_webhook
view.
You can also use a library like Django Rest Framework to register the webhook API. Here is an example:
Example
from rest_framework.routers import DefaultRouter
from .views import StripeWebhookView
router = DefaultRouter()
router.register("stripe/webhook", StripeWebhookView, basename="stripe_webhook")
urlpatterns = router.urls
This example registers the StripeWebhookView
view using the DefaultRouter
class.
Included Webhook Events
The django-stripe
library provides a set of built-in webhook events that it listens for by default. All these webhook events are located in the django_stripe.webhooks
module.
The following table lists all the webhook events that are included in django-stripe
library by default.
Webhook Event | Class | Description |
---|---|---|
customer.created | CustomerCreatedWebhook |
Occurs whenever a customer is created |
customer.updated | CustomerUpdatedWebhook |
Occurs whenever any property of a customer changes |
customer.deleted | CustomerDeletedWebhook |
Occurs whenever a customer is deleted |
customer.subscription.created | SubscriptionCreatedWebhook |
Occurs whenever a customer with no subscription is signed up for a plan. |
customer.subscription.updated | SubscriptionUpdatedWebhook |
Occurs whenever a subscription changes. Examples would include switching from one plan to another, or switching status from trial to active. |
customer.subscription.deleted | SubscriptionDeletedWebhook |
Occurs whenever a customer ends their subscription. |
customer.subscription.trial_will_end | SubscriptionTrialWillEndWebhook |
Occurs whenever a customer's trial period is about to end. |
product.created | ProductCreatedWebhook |
Occurs whenever a product is created. |
product.updated | ProductUpdatedWebhook |
Occurs whenever any property of a product changes. |
product.deleted | ProductDeletedWebhook |
Occurs whenever a product is deleted. |
price.created | PriceCreatedWebhook |
Occurs whenever a price is created. |
price.updated | PriceUpdatedWebhook |
Occurs whenever any property of a price changes. |
price.deleted | PriceDeletedWebhook |
Occurs whenever a price is deleted. |
coupon.created | CouponCreatedWebhook |
Occurs whenever a coupon is created. |
coupon.updated | CouponUpdatedWebhook |
Occurs whenever any property of a coupon changes. |
coupon.deleted | CouponDeletedWebhook |
Occurs whenever a coupon is deleted. |