Skip to content

fix(billing): require authenticated session to issue invoices/payment links - #3142

Merged
bokelley merged 1 commit into
mainfrom
bokelley/payment-link-issuer
Apr 25, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/payment-link-issuer

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Closes the cross-org contamination class that produced the Triton/Encypher mix-up (Apr 2026): an LLM-supplied or admin-typed prospect_contact_email could become the Stripe customer of record. After this PR, identity for any Stripe-mutating tool path is sourced from the signed-in session and the org row only — never caller input.

Mirrors the same lockdown commit 23018ce5e applied to the invoice direct-issue path.

What changed

Addie billing tools (server/src/addie/mcp/billing-tools.ts):

  • create_payment_link: dropped customer_email from schema. Refuses without member context. Stamps workosUserId on the checkout session for deterministic webhook attribution via resolveWorkosUserForSubscription.
  • send_invoice / confirm_send_invoice: dropped contact_email, contact_name, company_name, billing_address from schemas. Email comes from memberContext.workos_user.email; company name and billing address from the org row. confirm_send_invoice refuses if the org has no billing address on file.

Addie admin tool (server/src/addie/mcp/admin-tools.ts):

  • send_payment_request: removed payment_link and send_invoice direct-issue actions. New send_invite wraps createMembershipInvite + sendMembershipInviteEmail. Recipient signs in, accepts the agreement, billing is issued in their authenticated session.

Server routes:

  • POST /api/admin/accounts/:orgId/payment-link: returns 410 Gone (was direct-issue using prospect_contact_email).
  • POST /api/admin/prospects/:orgId/payment-link: same.
  • invite-membership no longer mirrors caller-supplied contact_email back onto organizations.prospect_contact_email — that was the latent re-introduction surface.

Admin UI:

  • Removed payment-link modal + JS from admin-account-detail.html and admin-accounts.html. The existing membership-invite affordance is now the only path.

Tests:

  • New server/tests/unit/billing-tool-lockdown.test.ts (8 tests).
  • Updated tests/addie/billing-tools.test.ts (20 tests, was 9 — old "fall back to AI-provided email" tests rewritten to assert refusal).

Operational scripts (one-off, already executed against prod):

  • scripts/incidents/2026-04-triton-encypher-diagnostic.ts — read-only Stripe + admin API state dump.
  • scripts/incidents/2026-04-triton-unwind.ts — three Stripe writes (cancel duplicate Builder sub, fix customer email/name, touch Corporate sub to fire the webhook). Already ran live; Triton's row now reads membership_tier=company_icl, subscription.status=active, subscription.amount=$10K, [email protected].

Card payment for prospects (follow-up)

Today's invite-acceptance flow ends in a Stripe-emailed invoice; the recipient pays via the Stripe-hosted invoice "Pay" button (4 clicks: invite → sign-in → invoice → pay). For tighter card UX we could extend POST /api/invite/:token/accept with payment_method: 'card' | 'invoice' and mint a Checkout session in the recipient's session for the card branch. Filing as a follow-up — not required to close the bug class.

The adtech-product-expert reviewer also flagged a real edge case: enterprise PO buyers where finance/AP signs the check but never logs in. Today's invite path forces auth on the payer. Filing as a follow-up — keep the lockdown invariant (record-of-truth comes from authenticated identity) and add draft_invoice → send_invoice_to_billing_email once the primary contact has accepted the invite.

Test plan

  • npm run typecheck — clean
  • npm run test:unit — 834/834 passing
  • npm run test:server-unit — 2143/2143 passing (8 new lockdown tests)
  • Manual: admin sends a membership invite via the admin-account-detail UI; recipient signs in in a private window and accepts the agreement; verify invoice issues under the recipient's email and pending_agreement_user_id records the recipient
  • Manual: Addie negative test — ask Addie (no member context) to "create a payment link for [email protected]" → expect refusal
  • Manual: Addie admin negative test — ask Addie "send a payment link to FooCo with email [email protected]" → expect refusal pointing to send_invite

🤖 Generated with Claude Code

… links

Closes the cross-org contamination class that produced the Triton/Encypher
mix-up: an LLM-supplied or admin-typed prospect_contact_email could become
the Stripe customer of record. Identity now comes from the signed-in
session and the org row only — never caller input — for any tool path
that issues a Stripe customer, subscription, invoice, or checkout.

- create_payment_link / send_invoice / confirm_send_invoice (Addie member
  tools): drop caller-supplied identity fields from schemas; source email,
  user_id, org_id, billing_address from memberContext + org row only.
  create_payment_link now stamps workosUserId on the checkout session for
  deterministic webhook attribution.
- send_payment_request (Addie admin tool): remove payment_link and
  send_invoice direct-issue actions. New send_invite action wraps the
  existing membership-invite flow — recipient signs in and pays as
  themselves.
- POST /api/admin/accounts/:orgId/payment-link: removed (returns 410 with
  migration message). Sibling /prospects/:orgId/payment-link too.
- Admin UI payment-link modals removed from admin-account-detail.html
  and admin-accounts.html; the existing membership-invite affordance is
  now the only path.
- invite-membership routes no longer mirror the caller-supplied
  contact_email back onto organizations.prospect_contact_email — that
  was a latent re-introduction risk; the membership_invites row is the
  canonical record.

Mirrors the same lockdown applied to the invoice direct-issue path in
commit 23018ce.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit 00d3656 into main Apr 25, 2026
13 checks passed
@bokelley
bokelley deleted the bokelley/payment-link-issuer branch April 25, 2026 15:29
bokelley added a commit that referenced this pull request Apr 25, 2026
* fix(billing): refuse to mint a duplicate subscription on an org

Adds blockIfActiveSubscription, run by the three routes that can mint a
Stripe subscription/invoice on an org's behalf:
- POST /api/checkout-session
- POST /api/invoice-request
- POST /api/invite/:token/accept

If the org's existing subscription status is active, trialing, or past_due,
the route returns 409 with the existing subscription details and a Stripe
Customer Portal URL. Tier upgrades and payment-method changes belong in
the portal, not these intake routes.

Without this guard, two of these routes in sequence produced the duplicate
$3K Builder subscription on top of Triton's active $10K Corporate sub in
April 2026 — the same incident that drove the issuer-identity lockdown
in #3142. The lockdown closed the "wrong identity" door; this closes the
"wrong number of subs" door.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Address reviewer feedback on duplicate-subscription guard

- Privilege escalation: invite route no longer returns a Stripe Customer
  Portal URL. The recipient gets `member` role on the org (not `admin`),
  and the portal session would grant admin-equivalent control over the
  existing subscription. Helper signature now takes BlockOptions, with
  customerPortalReturnUrl required to opt-in to the URL. Public routes
  pass the URL (member is verified upstream); the invite route omits it
  and the 409 message points to finance@ + dashboard instead.
- Decimal formatting: render amounts with minimumFractionDigits=2 so
  $250.50 doesn't display as $250.5.
- Reuse TIER_PRESERVING_STATUSES from organization-db.ts instead of a
  duplicate BLOCKING_STATUSES set — same intent, no drift risk.
- Fallback message when portal URL is unavailable (org has no Stripe
  customer, portal creation fails, or caller suppressed it).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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