Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(warehouse-sources): keep the further invoice status on a stripe webhook tie (#113207)

Daniel RC31ae8b42a70f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
index acc96ee44a5c..51afed0f549f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
@@ -1157,6 +1157,16 @@ def get_rows(
)
+# How far an invoice is through its lifecycle. An uncollectible invoice can still be paid or voided.
+_INVOICE_STATUS_STAGE = {"draft": 1, "open": 2, "uncollectible": 3, "paid": 4, "void": 4}
+
+
+def _webhook_tie_break(obj: dict) -> int:
+ if obj.get("object") != "invoice":
+ return 0
+ return _INVOICE_STATUS_STAGE.get(obj.get("status") or "", 0)
+
+
def _webhook_table_transformer(table: pa.Table) -> pa.Table:
data_col = table.column("data").to_pylist()
created_col = table.column("created").to_pylist()
@@ -1164,7 +1174,7 @@ def _webhook_table_transformer(table: pa.Table) -> pa.Table:
# Deduplicate by object id, keeping the event with the latest created timestamp.
# Multiple webhook events (e.g. customer.created then customer.updated) can reference
# the same object, and delta merge doesn't deduplicate within the source batch.
- best_by_id: dict[str, tuple[int, dict]] = {}
+ best_by_id: dict[str, tuple[tuple[int, int], dict]] = {}
for data_str, event_created in zip(data_col, created_col):
if data_str is None:
continue
@@ -1174,13 +1184,16 @@ def _webhook_table_transformer(table: pa.Table) -> pa.Table:
continue
ts = event_created if isinstance(event_created, int) else 0
+ # `created` is a whole second and one Stripe operation emits several events for an object
+ # inside it. Stripe does not deliver those events in order, so on a tie the row order can
+ # put a pre-payment snapshot after the payment event. The further invoice status wins the
+ # tie, because an invoice does not move back to an earlier status.
+ rank = (ts, _webhook_tie_break(obj))
existing = best_by_id.get(obj_id)
- # Later rows win ties. `created` is a whole second and one Stripe operation emits several
- # events for an object inside it, so the only finer order the batch holds is the row order,
- # a best-effort proxy for arrival order. Keeping the first row let a pre-payment snapshot
- # outrank the payment event that followed it in the same second.
- if existing is None or ts >= existing[0]:
- best_by_id[obj_id] = (ts, obj)
+ # Later rows win full ties. Row order is a best-effort proxy for arrival order, and it is
+ # the only finer order the batch holds for objects without a ranked status.
+ if existing is None or rank >= existing[0]:
+ best_by_id[obj_id] = (rank, obj)
rows = [_scrub_client_secrets(obj) for _, obj in best_by_id.values()]
return table_from_py_list(rows)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
index 4f8ecb2e4dc2..2e1cbb2266db 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -1451,6 +1451,12 @@ class TestWebhookUpsertCollapse:
# A redelivery can arrive after a newer event, so a plain last-row-wins rule would
# reinstate the older state.
("older event delivered last", [(1700000100, "paid"), (1700000050, "open")], "paid"),
+ # Stripe does not deliver events in order and the webhook handler does not finish them
+ # in order, so on a `created` tie the stale snapshot can be the last row.
+ ("tie with the stale snapshot last", [(1700000100, "paid"), (1700000100, "open")], "paid"),
+ ("tie between draft and open", [(1700000100, "open"), (1700000100, "draft")], "open"),
+ # An uncollectible invoice can still be paid or voided.
+ ("tie after uncollectible", [(1700000100, "void"), (1700000100, "uncollectible")], "void"),
]
)
def test_latest_state_per_object_wins(