Repository navigation
feat(billing): lazy reconcile on certification paywall hit (#3623 step 3) - #3663
Merged
Merged
Conversation
…p 3)
When a Stripe customer is re-linked between orgs (admin audit, support
fix-up) without subscription state being transferred to the new org
row, the user appears as a non-member at the next paywall hit despite
holding an active membership in Stripe. Three real cases observed in
production (Lina/HYPD/Yoshihiko, manually fixed via /sync). Diagnosis
showed these are not missed webhooks — the original webhook fired
correctly against the original org; some later admin process re-linked
the customer's stripe_customer_id pointer without transferring state.
The four certification gates (get_certification_module,
start_certification_module, test_out_modules, start_certification_exam)
now call ensureMembership() before denying. ensureMembership delegates
to attemptStripeReconciliation which:
- Reads org row; skips if already entitled or no stripe_customer_id
- Pulls customer + active subs from Stripe
- pickMembershipSub filters to membership lookup_keys
- UPDATEs subscription_* columns with WHERE subscription_status IS NULL
guard so concurrent webhook writes always win
- Returns {healed, reason} for caller logging
The user clicking the paywall is the trigger; they never see the drift.
Within this handler turn, in-memory memberContext is updated so the rest
of the handler proceeds with member-status semantics.
Deliberate scope: does NOT write user_agreement_acceptances, audit log,
or org_activities rows. The webhook handler (handle-subscription-created)
is the canonical place for those side effects, keyed off
pending_agreement_user_id from checkbox-click time. Paywall click is
action-signal but not fresh consent.
Verified end-to-end against the sandbox lina_class fixture: drift
detected, heal succeeds, row populated with subscription_id + amount +
lookup_key. Re-run on healed row returns already_entitled (idempotent).
Adds .stripe-key to .gitignore (live Stripe key file).
Tests: 11-case unit suite covering happy path, all skip reasons
(no_stripe_customer, customer_deleted, no_membership_sub, sub_not_entitled),
multi-sub correct picking, concurrent-webhook race, transient Stripe error,
trialing-as-entitled.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This was referenced Apr 30, 2026
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
When a Stripe customer is re-linked between orgs (admin audit, support fix-up) without subscription state being transferred to the new org row, the user appears as a non-member at the next paywall hit despite holding an active membership in Stripe. The four certification gates now lazy-heal from Stripe before denying. The user clicking the paywall is the trigger; they never see the drift.
Diagnosis
Three real cases observed in production: Lina (
cus_UF4bV…), HYPD (cus_TajjN…), Yoshihiko (cus_Tpzj…). All three already manually fixed during the investigation that motivated this work (see #3627 and #3643 PR descriptions).The Stripe-side investigation revealed these are not missed-webhook bugs. Stripe customer metadata explicitly records
relinked_at/previous_workos_organization_idfor at least one (Lina). The originalcustomer.subscription.createdwebhook fired correctly against the original org. Some later admin process re-linked the customer to a different org but didn't transfer subscription state to the new org's row.This makes lazy reconciliation the right shape (instead of the cron auto-remediation we were originally scoping):
Lazy reconcile dominates on every axis at AAO's scale.
What changed
New module
server/src/billing/lazy-reconcile.tsexportingattemptStripeReconciliation(orgId, deps).stripe_customer_idpickMembershipSub(from fix(billing): /sync picks membership sub, not subscriptions.data[0] (#3623 step 2) #3646)UPDATE … WHERE subscription_status IS NULL OR = 'none'— concurrent webhook writes always win{ healed, reason }for caller loggingserver/src/addie/mcp/certification-tools.ts:memberContextis now aletso successful heals are visible to subsequent calls in the same handler turnensureMembership()helper wraps the heal callget_certification_module,start_certification_module,test_out_modules,start_certification_exam) callensureMembership()before denying.gitignore: adds.stripe-key(live Stripe key file).What it deliberately doesn't do
handle-subscription-created.ts) is the canonical place for those side effects, keyed offpending_agreement_user_idfrom checkbox-click time. A paywall click is action-signal but not fresh consent. Lazy reconcile heals the entitlement gate; legal records remain a webhook concern.(NULL|none) → entitledonly; never overwrites an existingactive/canceled/etc. status.Verification
End-to-end against the sandbox
lina_classfixture (#3643):Test plan
attemptStripeReconciliation: happy path, all skip reasons, multi-sub correct picking (the data[0] regression class), concurrent-webhook race, transient Stripe error, trialing-as-entitledtests/unit/integrity/andtests/unit/billing/still passnpx tsc --noEmitcleanlina_classfixturestripe_customer_id IS NOT NULL, the heal would fire on the next gate hitRefs
data[0]fix (fix(billing): /sync picks membership sub, not subscriptions.data[0] (#3623 step 2) #3646) — all merged🤖 Generated with Claude Code