Polar webhooks

Fetch current subscription state when Polar webhooks arrive out of order

When a Polar subscription.updated arrives after subscription.revoked, fetch the subscription from Polar before writing access. Process the fetch and write in a single sequence for each subscription ID so a slow worker cannot put an older result back.

Payment and webhook repair commits
Event order or state
3 of 388
Event order and state count

Apply the fix

  1. Queue the verified delivery

    Verify the signature with the Polar SDK. For a Standard Webhooks endpoint, store webhook-id under a unique key scoped to the destination and acknowledge after queueing durably. Use data.id as the subscription key for the worker. It stays the same across different events for that subscription.

  2. Reconcile the current subscription

    For subscription.updated, subscription.canceled and subscription.revoked, fetch GET /v1/subscriptions/{id} in the owning organization and environment. Serialize the fetch through the commit by subscription ID. Retry temporary API or database failures.

  3. Keep cancellation and revocation distinct

    Read cancel_at_period_end and ends_at together with the current subscription status. A scheduled cancellation can leave access intact through the paid term. When Polar revokes the subscription, end access for that subscription ID.

Verify the change

Run npx viberaven check from your repo root. The webhook check starts in VibeRaven 1.15. To run the current release explicitly, run npx viberaven@1.20.1 check. Review payment_webhook_order warnings in the handler. In a test environment, deliver a revocation followed by an older update for the same subscription. Check that stored access still reflects the current Polar subscription.

Research and sources

We labeled 3 of 388 payment and webhook repair commits as event order or state. The sample counts repairs in Git history across payment providers, so the denominator is repair commits across providers.