Decorators

Event Handling Decorators

More documentation coming on these soon. For now, see our implementations in djstripe.event_handlers

Specific Event(s) Handler

Decorator that registers a function as a webhook handler.

Functions can be registered for event types (e.g. 'customer') or fully qualified event sub-types (e.g. 'customer.subscription.deleted').

If an event type is specified, the handler will receive callbacks for ALL webhook events of that type. For example, if 'customer' is specified, the handler will receive events for 'customer.subscription.created', 'customer.subscription.updated', etc.

:param event_types: The event type(s) that should be handled. :type event_types: str.

Source code in djstripe/webhooks.py
def handler(*event_types):
    """
    Decorator that registers a function as a webhook handler.

    Functions can be registered for event types (e.g. 'customer') or
    fully qualified event sub-types (e.g. 'customer.subscription.deleted').

    If an event type is specified, the handler will receive callbacks for
    ALL webhook events of that type.  For example, if 'customer' is specified,
    the handler will receive events for 'customer.subscription.created',
    'customer.subscription.updated', etc.

    :param event_types: The event type(s) that should be handled.
    :type event_types: str.
    """

    def decorator(func):
        for event_type in event_types:
            registrations[event_type].append(func)
        return func

    return decorator

All Events Handler

Decorator that registers a function as a webhook handler for ALL webhook events.

Handles all webhooks regardless of event type or sub-type.

Source code in djstripe/webhooks.py
def handler_all(func=None):
    """
    Decorator that registers a function as a webhook handler for ALL webhook events.

    Handles all webhooks regardless of event type or sub-type.
    """
    if not func:
        return functools.partial(handler_all)

    registrations_global.append(func)

    return func