fix(billing): canonical /sync write + recognize founding-era subs by product metadata#3829
Merged
Merged
Conversation
3 tasks
…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>
15b455b to
d6a0657
Compare
4 tasks
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
POST /api/admin/accounts/:orgId/syncwas incomplete. The inline UPDATE wrote 6 fields and silently droppedstripe_subscription_id,subscription_price_lookup_key,subscription_product_id/name, andmembership_tier. Every successful sync left the row in the partial-truth state the new invariant flags. Now usesbuildSubscriptionUpdate()— 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)Founding-era Stripe prices have no
aao_membership_*lookup_key. They were created in the Stripe Dashboard before the convention existed and rely onmetadata.category = "membership"on the product instead. Without recognizing this,pickMembershipSubfiltered them out —/syncreported "no membership sub found" for paying customers, and the orphan-customer detection instripe-sub-reflected-in-org-rownever saw them either.isMembershipSubnow falls back to product metadata;/syncand the invariant both expandprice.productso the metadata is available. (server/src/billing/membership-prices.ts,server/src/audit/integrity/invariants/stripe-sub-reflected-in-org-row.ts)detectEnvMismatch()refused to run integrity checks in prod. The hostname allowlist had*.fly.devbut not Fly's actual private-service patterns (*.flycast,*.internal). The prodDATABASE_URLhost 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 positiveFLY_APP_NAMEsignal. (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-rowwas meant to be that, but it pre-filtered byisMembershipLookupKey()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
isMembershipSub/pickMembershipSub, Bidcliq-shape orphan-customer regression in the invariant, and the existing happy pathsstripe_subscription_id+subscription_price_lookup_key+membership_tier, not just amountPOST /api/admin/stripe-customers/:customerId/link)GET /api/admin/integrity/check/stripe-sub-reflected-in-org-rowto confirm the founding cohort now surfaces (was invisible pre-PR)🤖 Generated with Claude Code