Guide

Billing webhooks: how to receive them safely

A billing webhook can grant access, ship an order or mark an invoice paid. Treat the endpoint that receives it like a login: prove who is calling, expect repeats, and never act on a signal that only looks like payment.

7 min read · Last checked

Key points

  • Verify the signature on the raw request body before you do anything else.
  • Deliveries are at least once, so the same event will sometimes arrive twice. Deduplicate by event id.
  • Return a 2xx quickly and do the real work from a queue.
  • A successful checkout call is not proof of payment. Fulfil on the paid-invoice event.

Verify every request

Anyone who finds your endpoint URL can post to it. The signature is how you know the call came from your billing system and that nobody changed it on the way. TenaBill signs each delivery with an X-TenaBill-Signature header of the form t=timestamp,v1=signature, an HMAC-SHA256 over the timestamp, a dot and the raw JSON body, using your endpoint’s signing secret.

Two details cause most failures. Compute the signature over the body exactly as received, because a framework that parses and re-serialises JSON changes the bytes. And compare in constant time, then reject a timestamp older than a few minutes so a captured request cannot be replayed later. Stripe’s guidance for its own webhooks is the same: its libraries default to a five-minute tolerance and warn that a tolerance of zero disables the check.

// rawBody is the request body exactly as received; do not re-serialise it.
var evt = TenaBillWebhookVerifier.VerifyAndParse(
    secret,
    request.Headers["X-TenaBill-Signature"],
    request.Headers["X-TenaBill-Event"],
    request.Headers["X-TenaBill-Event-Id"],
    rawBody);

if (evt.AsInvoiceRefunded() is { IsFullRefund: true } refund)
{
    // revoke access for refund.InvoiceId
}

Expect the same event twice

TenaBill delivers webhooks at least once. A network failure after you process an event but before your reply arrives makes the sender try again. The X-TenaBill-Event-Id header carries the same id on every retry of a delivery, so store the ids you have handled and return 2xx without repeating side effects when you see one again.

Signature checks do not protect you from this. The timestamp changes on every attempt, so a retry is a valid, newly signed request.

Answer fast, work later

Senders wait a limited time for a reply and treat a slow one as a failure. Verify, store the event, and return a 2xx at once. Do the real work from a queue. Stripe recommends the same and points out that a burst, such as every subscription renewing at the start of the month, can overwhelm an endpoint that does its work inline.

Do not trust order of arrival

  • Events can arrive out of order. Do not assume a created event comes before a status-changed event.
  • When order matters, treat the event as a signal and read the current state of the invoice or subscription.
  • Ignore fields you do not know. Events only ever gain fields, and ignoring new ones keeps your receiver working.

Fulfil on payment, not on checkout

The most common integration bug is granting access the moment a checkout call returns. That call creates an invoice and a payment session and returns immediately, while the buyer still has to complete the hosted payment step. Until the invoice is paid, the checkout has started but not finished.

Subscribe to invoice.paid and do fulfilment work only in that handler, after verification and once per event id. For refunds, check isFullRefund on invoice.refunded before you revoke access, because a partial refund should not remove it. Use subscription.status_changed to grant or revoke access as the subscription moves between states.

Rotate the secret without dropping events

When you rotate an endpoint’s secret, TenaBill keeps signing with the previous secret for 24 hours by default, and you can choose anything from none to 720 hours. During the overlap the signature header carries a second value, v1_previous, so a receiver that accepts either one keeps working while you deploy the new secret. If the old secret leaked, rotate with no overlap.

Recover from your own downtime

If your endpoint was down or had a bug, redeliver the missed events from the Webhooks page once it works. A redelivery keeps the original event id, so a receiver that skips ids it already handled will not process an event twice. Pair this with a periodic check of invoice status for anything that should have been paid by now.

What to know before you rely on this

  • The verification helper is part of the .NET SDK. In other languages, compute the HMAC-SHA256 yourself over the timestamp, a dot and the raw body.
  • Webhook endpoints are managed in the merchant console, and the signing secret is shown once when you create an endpoint.

Questions people ask

Why does verification fail even though my secret is right?

Usually the body was changed before you checked it. Verify against the raw bytes as received, before any parsing.

Which event should unlock what the customer bought?

invoice.paid. A completed checkout call or a started session only means the buyer has begun paying.

Do I get webhooks for usage limits?

Yes. usage.threshold_reached fires when a subscription uses a configured percentage of a meter’s included units, and usage.cap_reached fires when a spend cap is reached.

Sources

Keep reading

Try it with test cards