Skip to content

fix(billing): canonical /sync write + recognize founding-era subs by product metadata#3829

Merged
bokelley merged 2 commits into
mainfrom
bokelley/sync-stripe-link-invariants
May 2, 2026
Merged

fix(billing): canonical /sync write + recognize founding-era subs by product metadata#3829
bokelley merged 2 commits into
mainfrom
bokelley/sync-stripe-link-invariants

Conversation

@bokelley

@bokelley bokelley commented May 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #3812. The Adzymic incident (escalation #300) exposed three gaps that the prior PR didn't address. Brian later sent screenshots of Advertible, Bidcliq, and Equativ all showing real Active Stripe subscriptions for "Founding membership (Startup/SMB)" or "(Corporate)" — none of which our DB tracks correctly. The shared root causes are fixed here.

What changed

  1. POST /api/admin/accounts/:orgId/sync was incomplete. The inline UPDATE wrote 6 fields and silently dropped stripe_subscription_id, subscription_price_lookup_key, subscription_product_id/name, and membership_tier. Every successful sync left the row in the partial-truth state the new invariant flags. Now uses buildSubscriptionUpdate() — the same writer the webhook handler runs — so /sync, webhooks, and lazy-reconcile all produce identical row state. (server/src/routes/admin/accounts-billing.ts)

  2. Founding-era Stripe prices have no aao_membership_* lookup_key. They were created in the Stripe Dashboard before the convention existed and rely on metadata.category = "membership" on the product instead. Without recognizing this, pickMembershipSub filtered them out — /sync reported "no membership sub found" for paying customers, and the orphan-customer detection in stripe-sub-reflected-in-org-row never saw them either. isMembershipSub now falls back to product metadata; /sync and the invariant both expand price.product so the metadata is available. (server/src/billing/membership-prices.ts, server/src/audit/integrity/invariants/stripe-sub-reflected-in-org-row.ts)

  3. detectEnvMismatch() refused to run integrity checks in prod. The hostname allowlist had *.fly.dev but not Fly's actual private-service patterns (*.flycast, *.internal). The prod DATABASE_URL host didn't match, the runner classified live as "not prod", and refused with "live key against staging" in production. Allowlist now includes the real Fly patterns plus a positive FLY_APP_NAME signal. (server/src/routes/admin/integrity.ts)

Why an invariant on this matters

Brian asked: "shouldn't we have an invariant like 'every paying stripe subscription maps to an AAO customer'?" The existing stripe-sub-reflected-in-org-row was meant to be that, but it pre-filtered by isMembershipLookupKey() so founding subs without lookup_keys slipped through. After this PR's broadening, it walks every active/trialing sub recognized either by lookup_key prefix or product metadata, and flags the orphan-customer cases (Bidcliq, Equativ) plus the partial-truth org-row cases (Adzymic) — both as critical.

Test plan

  • 97 unit tests pass: new metadata-fallback cases for isMembershipSub/pickMembershipSub, Bidcliq-shape orphan-customer regression in the invariant, and the existing happy paths
  • Typecheck clean
  • Precommit hook (full suite + typecheck + dynamic imports) passed
  • After merge: re-run /sync against Adzymic — row should now show populated stripe_subscription_id + subscription_price_lookup_key + membership_tier, not just amount
  • After merge: re-run /sync against Advertible / Bidcliq / Equativ — should pick up their real founding-membership subs via metadata fallback (assumes the customer linkage is set; Bidcliq/Equativ still need manual relink first via existing POST /api/admin/stripe-customers/:customerId/link)
  • After merge: hit GET /api/admin/integrity/check/stripe-sub-reflected-in-org-row to confirm the founding cohort now surfaces (was invisible pre-PR)

🤖 Generated with Claude Code

bokelley and others added 2 commits May 2, 2026 12:43
…product metadata

Follow-up to #3812. Three gaps the Adzymic incident exposed:

- /sync UPDATE wrote 6 fields and dropped stripe_subscription_id, lookup_key,
  product_id/name, and membership_tier — every successful sync left the row
  in the partial-truth state. Now delegates to buildSubscriptionUpdate so
  /sync, webhook handlers, and lazy-reconcile all write identical state.

- Founding-era Stripe prices have no aao_membership_* lookup_key — they use
  product metadata.category=membership instead. Without recognizing it,
  pickMembershipSub filtered them out and stripe-sub-reflected-in-org-row's
  orphan-customer detection never saw paying customers (Adzymic/Advertible/
  Bidcliq/Equativ). isMembershipSub now falls back to product metadata;
  /sync and the invariant expand price.product so it's available.

- detectEnvMismatch() classified prod as "not prod" because the allowlist
  had *.fly.dev but not Fly's actual private patterns (*.flycast,
  *.internal) or FLY_APP_NAME. The integrity runner refused in production.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…rod app, $10K corporate test

- Revert speculative published_version downgrade in static/schemas/source/index.json:
  the build:schemas regen during pre-push silently rolled adcp_version 3.0.4 -> 3.0.3
  to match package.json. Reviewer correctly flagged this as out-of-scope and incorrect
  (would mis-label the published schema version). Restored to main's state. The pre-push
  hook is currently broken against main on a separate concern (forward-merge release
  process desyncs package.json from the registry intentionally).
- Configurable prod-app allowlist: AAO_PROD_FLY_APPS env var, defaults to "adcp-docs"
  (server/src/routes/admin/integrity.ts).
- Use 'deleted' in product instead of double-cast in isMembershipProductByMetadata
  (server/src/billing/membership-prices.ts).
- Add Equativ-shape regression: founding Corporate sub at $10K with metadata-only
  classification (server/tests/unit/integrity/invariants.test.ts), plus a load-bearing
  comment on the substring assertion.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@bokelley bokelley force-pushed the bokelley/sync-stripe-link-invariants branch from 15b455b to d6a0657 Compare May 2, 2026 16:44
@bokelley bokelley merged commit 7709a85 into main May 2, 2026
13 checks passed
@bokelley bokelley deleted the bokelley/sync-stripe-link-invariants branch May 2, 2026 16:48
bokelley added a commit that referenced this pull request May 2, 2026
…#3850)

* fix(billing): /sync Stripe expansion depth + observable error logging

Hotfix on top of #3829. The customer.retrieve expand
'subscriptions.data.items.data.price.product' is 6 levels deep and exceeds
Stripe's 4-level expansion limit. /sync threw on every org with a Stripe
customer and returned a generic "Failed to sync from Stripe" with no log
line, so the regression was invisible.

- Drop the deep expand on customers.retrieve (just confirms not deleted)
- Fetch subscriptions separately via stripe.subscriptions.list with
  'data.items.data.price.product' expand — only 4 levels deep from the
  subscriptions resource, within Stripe's limit
- Log the actual error on the catch path

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test: mock subscriptions.list in admin-sync-revenue-backfill

The /sync endpoint now fetches subscriptions separately from customer.retrieve
(see prior commit) so it can expand price.product 4 levels deep without
exceeding Stripe's customer-retrieve depth limit. Update the test fixture
to mock subscriptions.list with the same fake sub data.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant