Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

9.2 KB
Raw Blame

B2B Company Self-Registration & Activation Mechanisms

Status: Draft — design finalized 2026-08-30, nothing built Target: Spree 6.0 (retargeted 2026-08-30, sequencing review — originally 6.1 on the additive test, pulled forward because the Enterprise onboarding product must ship on 6.0 GA and these mechanisms are its only missing input; every piece remains additive and small) Depends on: B2B Companies & Catalogs (6.0-b2b-companies-and-catalogs.md), B2B Quantity Rules (6.0-b2b-quantity-rules.md — catalog terms), gated storefront access (shipped 5.6) Author: Damian + Claude Last updated: 2026-08-30

Summary

The B2B flow has no front door: there is no storefront way for a business to register itself (/store/companies is show + update only), and no way for a merchant to gate pricing on having a commercial relationship. This plan adds the open-source mechanisms: storefront company self-registration, an approval_required storefront access posture ("prices and checkout require an active company standing"), a machine-readable pricing_access reason code beside hidden prices, and a registered company activation policy consulted everywhere a company's commercial standing matters. In OSS the default policy activates every company, so registration is instant and the posture means "company members only" — a useful trade-store feature on its own. The merchant-approval flow (pending applications, verification requirements, review queue, request-more-information) is the Enterprise onboarding product built on these mechanisms (tier split decided 2026-08-30 — see ee/docs/plans/b2b-company-onboarding.md; no competitor ships approval in open source, and everyone who has it charges for it).

Key Decisions (do not deviate without discussion)

  • Registration is OSS and companies are born active (2026-08-30, tier split): POST /api/v3/store/companies lets an authenticated customer — including an existing retail customer — create a root company (kind company) plus their own membership in one call; a guest storefront chains POST /store/customers first so the UX is one flow. Free-form registration answers land in metadata['registration']. Rate-limited like customer registration. Core publishes company.registered and sends no mail.
  • Spree::Companies::ActivationPolicy is the single seam (2026-08-30): a dependency-registered class (Spree::Dependencies.company_activation_policy_class, mirroring storefront_access_policy_class) answering active?(company) and pricing_access_code(user:, store:). The OSS default answers active for every company. Every place that resolves a company for visibility, pricing, or checkout consults it: Catalog.for_company, both sole_standing_company implementations (Purchase::Company and Products::ForContext — mirrored, by existing constraint), the cart company_id write validation, and the approval_required posture. Subtree semantics (a suspended parent covering its divisions) are the policy implementation's job, which is why the seam takes the node.
  • No status column on spree_companies in OSS. The lifecycle (pending / review / approved / rejected / suspended, the requirements checklist, the queue) lives on an Enterprise-owned application record referencing the company — OSS tables never reference Enterprise models, and OSS behavior flows entirely through the policy. This supersedes the same-day draft that put five statuses on Company.
  • A fourth storefront access posture: approval_required (2026-08-30): beside public | prices_hidden | login_required. Guests and authenticated buyers without a standing over a policy-active company browse the catalog with prices null and checkout refused; prices_hidden keeps its shipped guest-only semantics untouched. Under the default policy this reads as "prices for company members only"; under the Enterprise policy it becomes "prices for approved companies only".
  • A reason code travels with hidden prices. Serializers today null prices with no explanation. The gating concern computes pricing_access — null (visible) | login_required | company_required | codes the registered policy supplies (Enterprise adds approval_pending) — surfaced wherever prices are nulled, following the checkout-requirements code: convention; the channel serializer also exposes storefront_access. Storefronts render "Awaiting approval" / "Register your business" from the code, never by interpreting bare nulls.
  • Checkout is gated by a requirement, standing is not gated at all. A new checkout requirement (company_activation_required code) refuses customer-initiated completion on approval_required channels without a policy-active cart company — same shape as guest_checkout_not_allowed. Self-service standing (standing_for?, company_standing, Storefront::AccessPolicy) never consults the policy: members of an inactive company keep their company pages, address book, members and invitations — that is the pre-approval workspace the Enterprise flow needs.
  • Customer-group membership is never an activation mechanism — groups price (catalog assignments), the policy activates. The seeded Wholesale group's "approval primitive" comment is retired with this plan.

Design Details

Registration

Companies::Register workflow (hooks :validate, :after_register): builds the root node + membership in one transaction, stamps metadata['registration'], publishes company.registered. The validate hook is the screening point (domain rules, fraud checks — host or Enterprise handlers); it does NOT fire on CompanyInvitations::Accept (joining an existing company is not founding one), while Customers::Create hooks keep firing on both paths as today. Controller: POST /store/companies (authenticated, rate-limited); the account companies index and company serializer are unchanged except where the policy adds codes.

One open guard: at most one self-registered root per customer per store (validation in Register) — keeps duplicate applications out without a uniqueness constraint on names.

Activation policy

class Spree::Companies::ActivationPolicy
  # Whether the company may act commercially (catalogs, pricing, checkout).
  def active?(_company) = true

  # The pricing_access code for a viewer lacking prices, or nil.
  def pricing_access_code(user:, store:)
    return 'login_required' if user.blank?
    'company_required' unless user.company_standing(store: store).any? { |c| active?(c) }
  end
end

Consulted (all existing call sites, no new resolution paths): Catalog.for_company (assignments of active companies only), both sole_standing_companys (memberships of active companies only), customer_has_standing_over_company (cart write gains a distinct :not_active error), the posture + checkout requirement. Completed orders answer from their stamped company_id, untouched.

Gating

Channel::Gating::STOREFRONT_ACCESS += ['approval_required']; store-level fallback validation updated. hide_prices? becomes posture-aware; the pricing_access code rides serializer_params beside hide_prices and is exposed on the payloads that null prices.

Migration Path

  1. ActivationPolicy + dependency registration + the resolution call-site consultations (behavior-neutral under the default policy — specs assert that explicitly; shared-concern battery runs against Cart AND Order).
  2. approval_required posture + pricing_access code + checkout requirement + serializer exposure.
  3. Companies::Register + POST /store/companies + SDK types + docs.
  4. Storefront reference: registration page + code-driven price labels.
  5. Retire the Wholesale-group seed comment.

Nothing to migrate; every change is additive and inert until a merchant selects the posture or registers a policy.

Constraints on Current Work

  • Any new code resolving a company for visibility, pricing, or checkout consults the activation policy — and the two sole_standing_company implementations stay mirrored.
  • Never approval-gate self-service standing.
  • New prices-hidden surfaces carry the pricing_access code — no bare nulls.
  • No status/approval columns on spree_companies — lifecycle state is the Enterprise application record's; OSS asks the policy.
  • Don't build interim approval primitives beside this mechanism (customer-group membership, boolean flags); anything approval-shaped waits for this mechanism.

Open Questions

None for the OSS scope. The flow-side questions (statuses, requirements, queue) belong to the Enterprise plan.

References

  • Merchant feedback, 2026-08 (register → browse "awaiting approval" → verify → assign the agreement → ordering enabled).
  • Tier split + rationale: docs/plans/decisions.md 2026-08-30; ee/docs/plans/b2b-company-onboarding.md (the flow).
  • docs/plans/6.0-b2b-companies-and-catalogs.md — tree, memberships, self-service, Catalog.for_context.
  • Gating: spree/core/app/models/spree/channel/gating.rb, spree/api/app/controllers/concerns/spree/api/v3/storefront_gating.rb.
  • Registration hook precedent: spree/core/app/workflows/spree/customers/create.rb.