[PR #7142] [MERGED] feat(stripe): enhance stripe plugin with organization customer support #32698

Closed
opened 2026-04-17 23:27:06 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7142
Author: @bytaesu
Created: 1/6/2026
Status: Merged
Merged: 1/7/2026
Merged by: @bytaesu

Base: canaryHead: 2026-01-16/feat/stripe-organization


📝 Commits (10+)

  • 6f08504 feat: basic groundwork to support organization stripe customer
  • 7b8b474 feat: redesign stripe plugin to support organization customer
  • 0ca2836 test: update the test code and move it to the test folder
  • 30a98e5 test: add organization customer test cases
  • 4bee90d fix: drop afterDeleteOrganization hook
  • bceea27 test: update cases
  • 2b4fdc0 chore: improve params comments
  • 61942d1 docs: update stripe plugin content including organization customer
  • 05755fd Merge branch 'canary' into 2026-01-16/feat/stripe-organization
  • 3adcfb7 fix: improve stripe search query

📊 Changes

11 files changed (+3485 additions, -292 deletions)

View changed files

📝 docs/content/docs/plugins/stripe.mdx (+108 -23)
📝 packages/stripe/src/error-codes.ts (+7 -0)
📝 packages/stripe/src/hooks.ts (+69 -48)
📝 packages/stripe/src/index.ts (+140 -32)
📝 packages/stripe/src/middleware.ts (+81 -33)
📝 packages/stripe/src/routes.ts (+370 -123)
📝 packages/stripe/src/schema.ts (+18 -0)
📝 packages/stripe/src/types.ts (+75 -19)
📝 packages/stripe/src/utils.ts (+11 -0)
packages/stripe/test/stripe-organization.test.ts (+1993 -0)
📝 packages/stripe/test/stripe.test.ts (+613 -14)

📄 Description

This PR is inspired by #2794 from @Cmion

Description

For B2B services, subscriptions are often managed with an Organization as the billing entity. This PR adds support for assigning Stripe Customer ID to organizations. To ensure stability, it includes refactoring and logic improvements to the existing plugin.

  • Add customerType to subscription endpoints to distinguish the billing owner
  • Add stripeCustomerId field to the Organization schema when enabled
  • Extend referenceMiddleware to validate organization subscription access

Organization hooks integration

  • afterUpdateOrganization: Sync Stripe customer when the organization name changes
  • beforeDeleteOrganization: Prevent deletion if there is an active subscription

To-do

  • Implement feature
  • Improve and add test cases
  • Test with a real app
  • Update documentation

Summary by cubic

Add organization customer support to the Stripe plugin so B2B subscriptions can be owned by an organization, not just a user. This introduces customerType across endpoints, new org-specific middleware and hooks, and updates the schema and tests.

  • New Features

    • Organization Stripe customers with subscriptions tied to an org.
    • customerType parameter on subscription endpoints to select user or organization as the billing owner.
    • Organization schema field: stripeCustomerId (when enabled).
    • Middleware: stripeSessionMiddleware and extended referenceMiddleware to authorize org access.
    • Organization hooks: sync customer on name change, block deletion with active subscription.
    • New error codes for organization flows and expanded types (CustomerType, AuthorizeReferenceAction, StripeCtxSession, WithStripeCustomerId, WithActiveOrganizationId).
    • Comprehensive tests for organization customer scenarios.
  • Migration

    • Enable organization support in Stripe options (organization.enabled: true) and include the organization plugin.
    • Apply DB schema updates to add organization.stripeCustomerId.
    • Pass customerType=organization in client calls for org-owned billing actions; user remains the default.
    • For org-owned actions, provide referenceId or ensure activeOrganizationId is set in the session.

Written for commit 2672ae76eb. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7142 **Author:** [@bytaesu](https://github.com/bytaesu) **Created:** 1/6/2026 **Status:** ✅ Merged **Merged:** 1/7/2026 **Merged by:** [@bytaesu](https://github.com/bytaesu) **Base:** `canary` ← **Head:** `2026-01-16/feat/stripe-organization` --- ### 📝 Commits (10+) - [`6f08504`](https://github.com/better-auth/better-auth/commit/6f085048f629cafa9248ef36da6ad7aefad9d3b2) feat: basic groundwork to support organization stripe customer - [`7b8b474`](https://github.com/better-auth/better-auth/commit/7b8b474c3c3173dda5d515bbddc7c1d69ce01e52) feat: redesign stripe plugin to support organization customer - [`0ca2836`](https://github.com/better-auth/better-auth/commit/0ca28364a8d8a8bdff1cd3c6aa6c8258663a0313) test: update the test code and move it to the test folder - [`30a98e5`](https://github.com/better-auth/better-auth/commit/30a98e5035cc12d00e3a997e05dca62352f82edf) test: add organization customer test cases - [`4bee90d`](https://github.com/better-auth/better-auth/commit/4bee90d1e4c6dad64fd8810a44d8a7c968c955ef) fix: drop afterDeleteOrganization hook - [`bceea27`](https://github.com/better-auth/better-auth/commit/bceea27bada894e23f16cd1d5873d8a5b1297d41) test: update cases - [`2b4fdc0`](https://github.com/better-auth/better-auth/commit/2b4fdc04265c3f74de5325fc467695096795c6b4) chore: improve params comments - [`61942d1`](https://github.com/better-auth/better-auth/commit/61942d17d292055ae40fe230c09552136c0fc4a8) docs: update stripe plugin content including organization customer - [`05755fd`](https://github.com/better-auth/better-auth/commit/05755fd3255a80383b4be5fb3b82a704c965fba7) Merge branch 'canary' into 2026-01-16/feat/stripe-organization - [`3adcfb7`](https://github.com/better-auth/better-auth/commit/3adcfb7655c88f4961f675d69aaba541f55ba538) fix: improve stripe search query ### 📊 Changes **11 files changed** (+3485 additions, -292 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/stripe.mdx` (+108 -23) 📝 `packages/stripe/src/error-codes.ts` (+7 -0) 📝 `packages/stripe/src/hooks.ts` (+69 -48) 📝 `packages/stripe/src/index.ts` (+140 -32) 📝 `packages/stripe/src/middleware.ts` (+81 -33) 📝 `packages/stripe/src/routes.ts` (+370 -123) 📝 `packages/stripe/src/schema.ts` (+18 -0) 📝 `packages/stripe/src/types.ts` (+75 -19) 📝 `packages/stripe/src/utils.ts` (+11 -0) ➕ `packages/stripe/test/stripe-organization.test.ts` (+1993 -0) 📝 `packages/stripe/test/stripe.test.ts` (+613 -14) </details> ### 📄 Description > This PR is inspired by #2794 from @Cmion - Closes https://github.com/better-auth/better-auth/issues/2079 - Closes https://github.com/better-auth/better-auth/issues/3772 ### Description For B2B services, subscriptions are often managed with an Organization as the billing entity. This PR adds support for assigning Stripe Customer ID to organizations. To ensure stability, it includes refactoring and logic improvements to the existing plugin. - Add `customerType` to subscription endpoints to distinguish the billing owner - Add `stripeCustomerId` field to the Organization schema when enabled - Extend `referenceMiddleware` to validate organization subscription access ### Organization hooks integration - `afterUpdateOrganization`: Sync Stripe customer when the organization name changes - `beforeDeleteOrganization`: Prevent deletion if there is an active subscription ### To-do - [x] Implement feature - [x] Improve and add test cases - [x] Test with a real app - [x] Update documentation <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Add organization customer support to the Stripe plugin so B2B subscriptions can be owned by an organization, not just a user. This introduces customerType across endpoints, new org-specific middleware and hooks, and updates the schema and tests. - New Features - Organization Stripe customers with subscriptions tied to an org. - customerType parameter on subscription endpoints to select user or organization as the billing owner. - Organization schema field: stripeCustomerId (when enabled). - Middleware: stripeSessionMiddleware and extended referenceMiddleware to authorize org access. - Organization hooks: sync customer on name change, block deletion with active subscription. - New error codes for organization flows and expanded types (CustomerType, AuthorizeReferenceAction, StripeCtxSession, WithStripeCustomerId, WithActiveOrganizationId). - Comprehensive tests for organization customer scenarios. - Migration - Enable organization support in Stripe options (organization.enabled: true) and include the organization plugin. - Apply DB schema updates to add organization.stripeCustomerId. - Pass customerType=organization in client calls for org-owned billing actions; user remains the default. - For org-owned actions, provide referenceId or ensure activeOrganizationId is set in the session. <sup>Written for commit 2672ae76eb75571ea1ccec8a6a8377fcb848b4d6. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 23:27:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#32698