Home > Enterprise >  How to catch all stripe webhook events that are not explicitly handled?
How to catch all stripe webhook events that are not explicitly handled?

Time:01-04

There are a lot of different types of Stripe events. Further, they note they can add more at any time. I am using dj-stripe. With my event handlers I have an OK idea of what types of webhooks I should be monitoring for a pretty straightforward Stripe subscription setup. Within the dj-stripe framework is there an easy way to catch unhandled webhooks that I encounter in production? On these I'd like to email myself that an unhandled Stripe webhook event has occurred.

For example, I have the following webhook handlers:

@csrf_exempt
@webhooks.handler("checkout")
def my_handler(event, **kwargs):
    print("handling checkout event...")
    print(event.type)

@csrf_exempt
@webhooks.handler("customer")
def my_customer_handler(event, **kwargs):
    print("handling customer event... in my_customer_handler")
    print(event.type)

@csrf_exempt
@webhooks.handler("charge")
def my_charge_handler(event, **kwargs):
    print("handling charge event... in my_charge_handler")
    print(event.type)

@csrf_exempt
@webhooks.handler("payment_intent")
def my_payment_intent_handler(event, **kwargs):
    print("handling payment_intent event... in my_payment_intent_handler")
    print(event.type)


@csrf_exempt
@webhooks.handler("price", "product")
def my_price_and_product_handler(event, **kwargs):
    print("handling price/product event... in my_price_and_product_handler")
    print(event.type)

Now let's say that some type of invoice webhook comes in. I understand that djstripe will save this event to the djstripe_invoice table (via path('stripe/', include("djstripe.urls", namespace="djstripe")),). But what if I want to catch that it not a webhook type that is currently handled outside of the built-in dj-stripe URLs? Is there any webhook signature I can add to notify me that a webhook event has occurred that I didn't do anything on other than update the DB?

CodePudding user response:

Stripe recommends only subscribing to events necessary for your business, so if you did discover subscribed events you aren't handling a good option would be to unsubscribe from them.

If you want to do some processing for all events, it looks like dj-stripe has a handler_all option (code). You'd likely want to maintain some dictionary of explicit event types you handle and check if the received event is handled before logging your unhandled event.

  •  Tags:  
  • Related