Using Webhooks with Subscriptions

    Learn to use webhooks to receive notifications of subscription activity.

    Stripe sends notifications to your app using webhooks. Although webhooks can be used for many purposes, they are especially important when using subscriptions, where most activity occurs asynchronously.

    To use webhooks with your subscriptions:

    1. Follow the general webhooks guide to create your webhook endpoint and configure it in your Stripe account.
    2. Test your webhook endpoint to confirm that it's working.

    After taking those basic steps, read about common uses for webhooks with subscriptions. You should also note how webhooks affect when invoices are finalized.

    Common uses of webhooks with subscriptions

    As subscriptions are an automatically recurring process, most subscription activity is triggered by Stripe: the generation of invoices, attempted payment on invoices, and cancellation of subscriptions due to failed payment. For each of these events—and more—you’ll likely want to take steps on your site.

    Three common uses of webhooks with subscriptions include:

    Handling payment failures

    Webhook notifications provide a reliable way to be notified of payment failures on subscription invoices. A payment failure can be a temporary problem—the card issuer declined this charge but may allow the automatic retry—or indicative of a permanent blocker, such as not having a usable payment source.

    We’ve written up a recipe that walks through accepting and processing payment failure notifications so you can automatically email your customer when it happens.

    Tracking active subscriptions

    Subscriptions require more coordination between your site and Stripe than one-time payments do. In the subscription model, a customer’s continued access to your product or service is determined by the success or failure of automatic, recurring payments. Take, for example, a site where a subscription provides access to the site’s content.

    On the site, you’d store each user’s login credentials (e.g., email address and password, in an appropriately secure, encrypted format). You’d also store a value representing the customer’s access expiration date (i.e., the customer has paid for access until this point).

    If a customer subscribes to a monthly plan, you would initially store this timestamp value (it may be called current_period_end), adding perhaps a day or two for leeway. When the customer logs in, you’d verify the login credentials and check the current_period_end timestamp to confirm that it’s still in the future, and therefore an active account.

    When the subscription is renewed (i.e., when Stripe bills the customer and they are charged again) your site is notified via webhooks of the additional payment:

    1. A few days prior to renewal, your site receives an invoice.upcoming event at the webhook endpoint. You can listen for this event to add extra invoice items to a subscription draft invoice.
    2. Your site receives an invoice.payment_succeeded event.
    3. Your webhook endpoint finds the customer for whom payment was just made.
    4. Your webhook endpoint updates the customer’s current_period_end timestamp in your database to the appropriate date in the future (plus a day or two for leeway).

    Catching subscription state changes

    Although most subscriptions exist in an active state, state changes are common and are something you’ll want to be prepared to address. Just a few examples of changes and potential reactions include:

    • When a subscription is about to move from trialing to active, you’ll receive a customer.subscription.trial_will_end event. Upon receiving this event notification, your webhook script might ensure there’s a payment source on the customer so they can be billed, and optionally notify the customer that a charge is forthcoming.
    • When a subscription changes to past_due, your webhook script could email you about the problem so you can reach out to the customer, or the script could email the customer directly, asking them to update their payment details.
    • When a subscription changes to canceled or unpaid, your webhook script should ensure the customer is no longer receiving your products or services.

    Webhooks and finalizing invoices

    Registering a webhook endpoint has an important effect on invoices, predominantly those created for subscriptions. When automatic collection is enabled, Stripe automatically finalizes and begins automatic collection of the invoice.

    If a webhook is disabled, Stripe waits an hour after receiving a successful response to the invoice.created event before attempting payment. If delivering that webhook doesn’t succeed within 72 hours of it being sent, Stripe attempts to finalize and send the invoice.

    In case you want to treat one-off invoices differently than subscription invoices, check the subscription property in the webhook body. This indicates if the invoice was created for a subscription (or not).

    If your webhook endpoint does not respond properly, Stripe continues retrying the webhook notification every hour for up to 72 hours. During that time, no payment will be attempted unless a successful response is received. We’ll also notify you via email that the webhook is failing.

    This behavior applies to all webhook endpoints defined on your account, including cases where a Connect application or other third-party service is having trouble handling incoming webhooks.

    Next steps

    Congrats! You’ve learned how to use webhooks with subscriptions. Some documentation you might want to read next:

    Questions?

    We're always happy to help with code or other questions you might have! Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    Was this page helpful? Yes No

    Send

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.