If your app shows the status of something that happens somewhere else (a payment, a shipment, a crypto swap), you usually get updates two ways: the provider sends webhooks, and you poll its API as a backup. Both paths feed the same screen.

That's where a quiet bug lives. Webhooks are delivered at least once, so the same event can show up twice. They can arrive out of order after a retry. And a poll that started a second before a webhook can finish a second after it, carrying older data. If every update simply overwrites the stored status, your user sees "sending", then "confirming" again, then "sending". On a screen that shows their money, that looks like something went wrong.

The fix is small: run every update, from either path, through one function that decides whether it's allowed to change anything.

Don't rank the statuses

The first idea most people have is to give each status a number and only move forward. seen is 1, confirming is 2, exchanging is 3, and so on, and a lower number never replaces a higher one.

This breaks as soon as the flow has a detour. Swap services, payment processors and couriers all have some kind of "on hold" state, and an order can leave it and go back to an earlier step. Say an underpaid deposit goes to hold, the user tops it up, and the order goes back to exchanging. A forward-only ranking will either refuse that move or need a pile of special cases.

The provider already knows the order of events. Use its timestamp (or a sequence number, if the API has one) and keep three rules:

  1. An event id you've already processed changes nothing.
  2. A finished order stays finished.
  3. An update older than what you already show changes nothing.

The reducer

const FINAL = new Set(['done', 'refunded', 'expired']); export function applyUpdate(order, update) { // 1. The same event twice: nothing to do. if (order.seenIds.has(update.id)) return order; const seenIds = new Set(order.seenIds).add(update.id); // 2. A finished order stays finished. if (FINAL.has(order.status)) return { ...order, seenIds }; // 3. Older than what we already show: keep the id, ignore the status. const at = Date.parse(update.updatedAt); if (order.updatedAt && at <= Date.parse(order.updatedAt)) { return { ...order, seenIds }; } return { ...order, status: update.status, reason: update.reason ?? null, updatedAt: update.updatedAt, seenIds, }; } export function newOrder(id) { return { id, status: 'waiting', reason: null, updatedAt: null, seenIds: new Set() }; } 

A few notes on the choices:

  • <= and not <. When a webhook and a poll describe the same change, they carry the same timestamp. The second one is a no-op, which is what you want.
  • The reason is cleared on every accepted update. When an order leaves hold, the old hold reason shouldn't stay on screen.
  • Final states are a set you own. Check your provider's docs for which statuses can never change. If one of them can be reopened (some processors allow a refund after done, for example), take it out of the set and let the timestamp rule handle it.
  • Poll results need an id too. A poll doesn't come with an event id, so make one from the order id and the provider's updatedAt. Two polls that see the same state then count as one event.

In production, seenIds belongs in your database (a table with a unique key on the event id works well), and the read, check and write should happen in one transaction or behind a row lock, so two workers can't apply updates for the same order at once.

Tests

These five cases cover the bugs I've seen most often. They run with Node's built-in test runner (node --test), no extra packages.

import test from 'node:test'; import assert from 'node:assert/strict'; import { applyUpdate, newOrder } from './order-status.mjs'; const ev = (id, status, updatedAt, reason) => ({ id, status, updatedAt, reason }); test('late event does not move the order back', () => { let o = newOrder('A'); o = applyUpdate(o, ev('e2', 'exchanging', '2026-12-04T10:05:00Z')); o = applyUpdate(o, ev('e1', 'confirming', '2026-12-04T10:02:00Z')); assert.equal(o.status, 'exchanging'); }); test('duplicate event is ignored', () => { let o = newOrder('A'); o = applyUpdate(o, ev('e1', 'seen', '2026-12-04T10:00:00Z')); const again = applyUpdate(o, ev('e1', 'seen', '2026-12-04T10:00:00Z')); assert.equal(again, o); }); test('hold and back to exchanging both apply', () => { let o = newOrder('A'); o = applyUpdate(o, ev('e1', 'exchanging', '2026-12-04T10:00:00Z')); o = applyUpdate(o, ev('e2', 'hold', '2026-12-04T10:03:00Z', 'underpaid')); assert.equal(o.status, 'hold'); assert.equal(o.reason, 'underpaid'); o = applyUpdate(o, ev('e3', 'exchanging', '2026-12-04T10:20:00Z')); assert.equal(o.status, 'exchanging'); assert.equal(o.reason, null); }); test('nothing changes a finished order', () => { let o = newOrder('A'); o = applyUpdate(o, ev('e1', 'done', '2026-12-04T10:30:00Z')); o = applyUpdate(o, ev('e2', 'sending', '2026-12-04T10:31:00Z')); assert.equal(o.status, 'done'); }); test('poll result and webhook for the same change agree', () => { let o = newOrder('A'); o = applyUpdate(o, ev('wh-1', 'sending', '2026-12-04T10:10:00Z')); o = applyUpdate(o, ev('poll-10:10:00', 'sending', '2026-12-04T10:10:00Z')); assert.equal(o.status, 'sending'); }); 

When the provider has no timestamp

Some APIs send a status and nothing else. Then you can't order events yourself, and the safest thing is to treat each webhook as a hint: when one arrives, fetch the current state from the API and apply that. You pay one extra request per event, and you never store an old status because a retry happened to land last.

What the user sees

All of this exists so the status line on the screen only moves when something really happened. If you also show the hold reason in plain words ("your deposit was below the minimum, top it up or ask for a refund"), most "where is my money" tickets never get written.

This article was written with help from an AI assistant. The code and the five tests above were run with Node before publishing.