Retry windows and rate limits are from Stripe's and PayPal's docs, checked on 14 November 2026. Providers change them without an API version bump.
An API call is pull: your server asks the provider a question and gets an answer now. A webhook is push: the provider sends an HTTP POST to a URL you registered when something happens, whether or not you were asking. A WebSocket is a long-lived two-way connection between a client and a server, designed for browser apps that would otherwise open lots of HTTP connections or long-poll (RFC 6455).
So the real difference is who starts the conversation, and who's left holding the problem when it fails.
API polling vs webhooks vs WebSockets
| API polling | Webhooks | WebSockets | |
|---|---|---|---|
| Direction | You pull | Provider pushes to you | Both ways, over one open connection |
| Who starts it | You, on a timer | The provider, when something happens | Usually the browser, then either side sends |
| How fresh | As fresh as your interval | Seconds, usually | Immediate |
| What it costs | A request per object per interval, mostly answering "no change" | One request per event | An open connection per client |
| What breaks | You burn rate limit and still learn late | Duplicates, out-of-order delivery, missed deliveries while you're down | Dropped connections; you reconnect and catch up |
| In payments, use it for | Reading the current state, and the backstop sweep | Learning that something changed | Telling the customer's browser it's paid |
Why polling alone fails
Arithmetic. Say you have 1,000 payments in a pending state and you check each one every 5 seconds: 1,000 / 5 = 200 requests a second. Stripe's global live-mode limit is 100 requests a second per account (Stripe). You're over the limit before you've charged anyone, and a payment that settles one second after your last check still waits four more. Some events have no one to poll for at all: a dispute, a refund issued from the dashboard, a subscription renewal at 2 AM.
Why webhooks alone fail
Webhooks are delivered at least once, in no particular order, and only for as long as the provider keeps retrying. Stripe retries live events for up to three days and doesn't guarantee order (Stripe); PayPal retries up to 25 times over 3 days and then marks the delivery Failed (PayPal). An endpoint that was down for a long weekend, or a handler that returned 200 and then crashed, loses events silently. And an event is a snapshot from when it was created, which may not be the state now.
Stripe's own checkout guide says both halves out loud. You can't fulfil from the success page alone, because a customer can pay and lose their connection before it loads, so a webhook is required. And when the webhook arrives, the guide has you retrieve the Checkout Session from the API and check payment_status before fulfilling (Stripe). Its webhook docs say the same about order: "You can also use the API to retrieve any missing objects" (Stripe).
The pattern: hint, truth, backstop
- Webhook as the hint. Verify the signature on the raw bytes, return 2xx fast, and treat the event as "something changed on object X".
- API read as the truth. Fetch X and write what the API says now. That one move makes duplicates and out-of-order delivery harmless, because the handler doesn't care which event woke it up.
- A sweep as the backstop. On a schedule, ask the provider what you might have missed. Stripe's Events API lists events back 30 days, and
delivery_success=falsereturns the ones still pending or that failed every delivery attempt (Stripe).
In Stripe terms, the same handler serves both paths:
import time
import stripe
HANDLED = ["payment_intent.succeeded", "payment_intent.payment_failed",
"payment_intent.canceled"]
def on_event(event):
"""Shared by the webhook endpoint and the sweep.
The event is a hint. The API read is the truth. Because this writes
whatever Stripe says now, running it twice, or out of order, is harmless.
"""
if event.type not in HANDLED:
return
# A stripe.StripeError here should reach the endpoint as a 500, so
# Stripe retries; in the sweep, the next run picks the event up again.
intent = stripe.PaymentIntent.retrieve(event.data.object.id)
save_payment_status(intent.id, intent.status) # UPDATE ... SET status
def sweep(hours=6):
"""The backstop: events Stripe hasn't managed to deliver. Run hourly."""
since = int(time.time()) - hours * 3600
events = stripe.Event.list(
delivery_success=False, # pending or failed deliveries
types=HANDLED,
created={"gte": since},
limit=100,
)
for event in events.auto_paging_iter():
on_event(event)
The sweep will sometimes pick up an event the webhook delivers a minute later. That's fine: the handler writes current state, so the second run changes nothing. If your handler does something that isn't naturally repeatable, like sending an email, dedupe on the event ID first (webhook idempotency).
Webhook vs WebSocket
They aren't competing for the same job. Payment providers report events to your server as webhooks; chapter 39 compares Stripe, PayPal, Paddle, Adyen and Square, and all five deliver over HTTP with retries. A WebSocket (or server-sent events) is for the last hop, from your server to the customer's browser: the webhook updates your database, and the open connection tells the waiting checkout page "paid" without it polling you. Don't hang order fulfilment on that socket. If the tab is closed, the payment still happened.
Fetch-current-state, sequence numbers and per-object queues, and what each one costs. From chapter 39.
Ordering Strategies
You have three ways to survive out-of-order delivery, and they cost different things. The cheapest to reason about is fetch-current-state: treat the webhook as a hint that something changed, call the provider's API for the object, and act on what comes back. Chapter 9 shows this for Stripe. It costs one API call per event, and Stripe's docs single out the start of the month, when every subscription renews at once, as the spike that overwhelms endpoints.
Sequence numbers are what the code below implements, and they only work if the provider gives you real ones. Adyen does for some webhook types. Stripe doesn't, and its created field has one-second resolution, so two events can share it. Paddle gives you occurred_at, a timestamp rather than a counter; the docs' advice is to store it on the row and skip any event older than the one you've already applied. That's a last-writer-wins guard rather than ordering, but for a subscription status field it's usually all you need.
The failure mode of sequence tracking is the gap. If the third event never arrives, the fourth sits in the future queue until the timeout, one hour in the code, and is then abandoned. Abandoned to what is your call; the code doesn't say, and the sensible fallback is to fetch current state for that resource and move on. The if not sequence branch means Stripe, Paddle, and Square events skip the mechanism entirely and go straight to the handler. For those providers, ordering has to come from fetch-current-state inside the handler itself.
The third strategy is per-object queues, and it solves a different problem. Route every event for one customer or subscription through the same worker, or take a lock on the resource ID, so two handlers never race on the same row. That fixes concurrency, not order; the events still arrive however they arrive. Combine it with fetch-current-state and you get the boring outcome: one writer per resource, always writing the latest truth. PayPal's docs don't say whether events arrive in order, which is its own kind of answer, so give PayPal this combination.
The minimal handler: verify first, then act on results that arrive after your request returned. From chapter 4.
3. Handle Async Webhooks
Some payment results arrive later via webhook:
import stripe
@app.route('/webhook', methods=['POST'])
def handle_webhook():
payload = request.data
sig_header = request.headers.get('Stripe-Signature')
webhook_secret = os.environ.get('STRIPE_WEBHOOK_SECRET')
# CRITICAL: Always verify the webhook signature
try:
event = stripe.Webhook.construct_event(
payload, sig_header, webhook_secret
)
except ValueError:
# Invalid payload
return 'Invalid payload', 400
except stripe.SignatureVerificationError:
# Invalid signature - possible attack
return 'Invalid signature', 400
# Now safe to process the verified event
if event.type == 'payment_intent.succeeded':
# Payment worked but response didn't make it back
mark_order_paid(event.data.object.metadata.order_id)
elif event.type == 'payment_intent.payment_failed':
# Payment failed after initial response
handle_late_failure(event.data.object)
return 'OK', 200
Webhooks are how you hear about payment results that land after your original request has already come back. Always verify the signature before processing, and make the handler idempotent, since you might get the same webhook more than once.
Questions
What is the difference between a webhook and an API?
An API call is pull: your server asks the provider and gets an answer now. A webhook is push: the provider sends an HTTP POST to a URL you registered when something happens. In payments you need both: the webhook tells you something changed, and an API read tells you the current state.
What is the difference between a webhook and a WebSocket?
A webhook is a one-off HTTP POST from a provider to your server when an event happens. A WebSocket is a long-lived two-way connection, usually between a browser and your server. Payment providers report events by webhook; a WebSocket is useful for telling the customer's browser the payment went through.
Why not just poll the payment API?
Because it doesn't scale and still learns late. Checking 1,000 pending payments every 5 seconds is 200 requests a second, twice Stripe's global live-mode limit of 100. And some events, like disputes or dashboard refunds, have nothing to poll for.
Can I rely on webhooks alone?
No. Webhooks arrive at least once, out of order, and only while the provider keeps retrying: up to three days for Stripe, and up to 25 attempts over 3 days for PayPal. Fetch the object from the API when an event arrives, and run a scheduled sweep for events that were never delivered.
How do I find webhook events I missed on Stripe?
List events from the Events API, which goes back 30 days, with delivery_success set to false. That returns events still pending or that failed every delivery attempt. Run them through the same handler your endpoint uses.