Webhook
Listen for events on your Stripe account so your integration can automatically trigger reactions.
Stripe uses webhooks to notify your application when an event happens in your account. Webhooks are particularly useful for asynchronous events like when a customer’s bank confirms a payment, a customer disputes a charge, a recurring payment succeeds, or when collecting subscription payments.
A webhook enables Stripe to push real-time notifications to your app. Stripe uses HTTPS to send these notifications to your app as a JSON payload. You can then use these notifications to execute actions in your backend systems.
Webhook events
Following are the supported webhook events:
- Customer:
stripe_integrations.webhooks.customerscontains all the customer related webhook event- Create customer:
CustomerCreatedWebhookwill process thecustomer.createdwebhook event. - Update customer:
CustomerUpdatedWebhookwill process thecustomer.updatedwebhook event. - Delete customer:
CustomerDeletedWebhookwill process thecustomer.deletedwebhook event.
- Create customer:
- Card:
stripe_integrations.webhooks.sourcescontains all the card related webhook event- Create card:
CustomerSourceCreatedWebhookwill process thecustomer.source.createdwebhook event. - Update card:
CustomerSourceUpdatedWebhookwill process thecustomer.source.updatedwebhook event. - Delete card:
CustomerSourceDeletedWebhookwill process thecustomer.source.deletedwebhook event.
- Create card:
- Subscription:
stripe_integrations.webhooks.subscriptionscontains all the subscriptions related webhook event- Create subscription:
CustomerSubscriptionCreatedWebhookwill process thecustomer.subscription.created"webhook event. - Update subscription:
CustomerSubscriptionUpdatedWebhookwill process thecustomer.subscription.updated"webhook event. - Subscription trial will end:
CustomerSubscriptionTrialWillEndWebhookwill process thecustomer.subscription.trial_will_end"webhook event. - Delete subscription:
CustomerSubscriptionDeletedWebhookwill process thecustomer.subscription.deleted"webhook event.
- Create subscription:
- Product:
stripe_integrations.webhooks.productscontains all the product related webhook event- Create product:
ProductCreatedWebhookwill process theproduct.createdwebhook event. - Update product:
ProductUpdatedWebhookwill process theproduct.updatedwebhook event. - Delete product:
ProductDeletedWebhookwill process theproduct.deletedwebhook event.
- Create product:
- Price:
stripe_integrations.webhooks.pricescontains all the price related webhook event- Create price:
PriceCreatedWebhookwill process theprice.createdwebhook event. - Update price:
PriceUpdatedWebhookwill process theprice.updatedwebhook event. - Delete price:
PriceDeletedWebhookwill process theprice.deletedwebhook event.
- Create price:
- Coupon:
stripe_integrations.webhooks.couponscontains all the coupon related webhook event- Create coupon:
CouponCreatedWebhookwill process thecoupon.createdwebhook event. - Update coupon:
CouponUpdatedWebhookwill process thecoupon.updatedwebhook event. - Delete coupon:
CouponDeletedWebhookwill process thecoupon.deletedwebhook event.
- Create coupon:
Configure Webhook
Create a view-set that will use StripeWebhook action to process the webhook event.
Create webhook viewset
from django.http import Http404
from rest_framework import viewsets
from rest_framework.permissions import AllowAny
from stripe_integrations.actions import StripeWebhook
from project.base import response
from payment.models import Event
class StripeWebhookViewSet(viewsets.GenericViewSet):
EVENT_MODEL = Event
# Check the webhook signatures
# Ref: https://stripe.com/docs/webhooks/signatures
def create(self, request, *args, **kwargs):
try:
event_data = request.data
StripeWebhook.process_webhook(event_data)
except Http404 as e:
raise e
return response.Ok({"success": True})
Register webhook view-set to an endpoint
from rest_framework.routers import DefaultRouter
from payments.apis import StripeWebhookViewSet
default_router = DefaultRouter(trailing_slash=False)
default_router.register(
"stripe/webhook", StripeWebhookViewSet, basename="stripe-webhook"
)
Custom Webhook Event
To create a custom webhook event for a specific Stripe webhook event, you can inherit BaseWebhook from stripe_integrations.webhooks.base and implement your own webhook event processing logic.
As an example, let's say you want to create a webhook event for the product.created event. You can create a new fileapp/webhook/products.py and define the webhook event class in it.
Example
# Stripe Integrations Stuff
from stripe_integrations.actions import StripeProduct
from stripe_integrations.webhooks.base import BaseWebhook
class ProductCreateWebhook(BaseWebhook):
name = "product.created"
description = "Occurs whenever a new product is created."
def process_webhook(self):
StripeProduct.sync(self.event.message["data"]["object"])
Once you have implemented the webhook event, you should import the webhook file in the __init__.py file of the app. This is required for the webhook event class to be registered.
Example
import importlib
importlib.import_module("app.webhooks.products")
Note
If the class is not registered, then the webhook event won't be processed.