Skip to main content
Hooks for reading and mutating the subscriptions of the customer in <UnitPayProvider> context — list them, resolve the live one, create a new subscription, change plans, attach addons, and cancel or resume. Every hook returns loading/error state — reads via isLoading: boolean, mutations via isPending: boolean — plus error: Error | null. See Introduction. The sections below list each hook’s specific returns. The four mutation hooks that move money (useCreateSubscription, useChangePlan, useAttachAddon) resolve to a SettleOutcome — the server decides whether to charge inline, mint an inline <PaymentForm>, send an invoice, or take no action — which you handle via the on* settlement callbacks in options. useCancelSubscription and useUncancelSubscription move no money, so they return a plain SubscriptionMutationResult instead.

useSubscriptions

Lists every subscription belonging to the customer — across every product, plan, and status — as one cached list. Takes no arguments; the customer comes from <UnitPayProvider> context. Returnssubscriptions: Subscription[] — every subscription on the customer (up to 50), or [] while loading or when none exist.

useActiveSubscription

Answers “what is the customer billing on right now?” It filters useSubscriptions (no extra request) to live statuses — active, trialing, pending, past_due — returning both the first match for single-sub portals and the full filtered list for multi-sub customers. Parameters
readonly string[]
Restrict to subscriptions whose planId is in this list. Use when the portal is product-scoped — pass the product’s plan ids (e.g. from usePricing(productId)). Omit to consider every plan the customer has.
Returns
  • subscriptionSubscription | null — the first live subscription matching the filter, or null while loading or when none are live.
  • subscriptionsSubscription[] — every live subscription matching the filter. Use this when a customer legitimately has more than one (different products, currencies, addons).
Scope to one product by passing its plan ids:

useSubscription

A read-only hook that fetches one subscription by id and exposes previewChange, a parameterized read that previews what switching to another plan would cost. Mutations live in the dedicated hooks below. Parameters
string
The subscription to fetch. The query stays disabled until an id is provided, so it is safe to pass undefined while the id is still loading.
Returns
  • subscriptionSubscription | null — the subscription, or null while loading or when no id is set.
  • previewChange(newPlanId)(newPlanId: string) => Promise<SubscriptionPreviewChange> — previews switching to newPlanId. Returns the full change breakdown: direction (upgrade / downgrade / lateral), timing (immediate / deferred), dueNow, effectiveAt, entitlement diffs, and credit deltas. Memoized, so it is safe in a useEffect dependency array.

useCreateSubscription

Mutation that subscribes the customer in context to a plan. salesMotion defaults to self_serve server-side. useCreateSubscription(options?) takes no id; options is a HandleSettlementOptions object of settlement callbacks (onChargedInline, onRequiresForm, onInvoiceSent, onCreated, onDeferred, onNoAction, …). The returned createSubscription(input) alias accepts:
string
required
The plan to subscribe to.
string
Override the billing currency. Defaults to the plan’s currency.
Record<string, unknown>
Arbitrary metadata to attach to the subscription.
boolean
Force the inline <PaymentForm> even when a payment method is already on file.
string
Override the auto-generated Idempotency-Key.
Returns — a React Query mutation plus createSubscription(input) => Promise<SettleOutcome> (an ergonomic alias for mutateAsync; the on* callbacks have already been dispatched against the outcome).

useChangePlan

Mutation that moves an existing subscription to a new plan (upgrade, downgrade, or lateral). The server dispatches inline charge (PLG with a payment method), recovery checkout (no PM or decline), an SLG invoice, or a deferred change. Preview the change first with useSubscription’s previewChange. useChangePlan(subscriptionId, options?) — the subscription id is the first argument; options is a HandleSettlementOptions object. The returned changePlan(input) alias accepts:
string
required
The plan to switch to.
boolean
Force the inline <PaymentForm> even when a payment method is already on file.
string
Override the auto-generated Idempotency-Key.
Returns — a React Query mutation plus changePlan(input) => Promise<SettleOutcome>.

useAttachAddon

Mutation that attaches an addon plan to an existing subscription. Portal callers typically set forceForm so addon purchases always confirm through the inline <PaymentForm> rather than silently debiting the saved card. useAttachAddon(subscriptionId, options?) — the parent subscription id is the first argument; options is a HandleSettlementOptions object (also supports onInvoiceAdded). The returned attachAddon(input) alias accepts:
string
required
The addon plan to attach.
boolean
Force the dispatcher to mint requires_form even when a payment method is on file, so the purchase always shows the inline <PaymentForm> and is explicitly confirmed — no silent off-session debit of the default card.
string
Override the auto-generated Idempotency-Key.
Returns — a React Query mutation plus attachAddon(input) => Promise<SettleOutcome>.

useCancelSubscription

Mutation that cancels a subscription. Cancellation moves no money, so — unlike the mutations above — it does not return a SettleOutcome; it resolves to a plain SubscriptionMutationResult. useCancelSubscription(subscriptionId, options?) — the subscription id is the first argument. options carries a single onSuccess(result: SubscriptionMutationResult) => void callback. The returned cancel(input?) alias accepts (all optional — call cancel() for a default period-end cancel):
boolean
Cancel right now instead of at the end of the current billing period.
string
Machine-readable reason code stored on the subscription.
string
Free-text note explaining the cancellation.
Returns — a React Query mutation plus cancel(input?) => Promise<SubscriptionMutationResult>, resolving to { subscriptionId, status, cancelAtPeriodEnd, effectiveAt }.

useUncancelSubscription

Mutation that reverses a pending cancellation, keeping the subscription live. Like cancel, it moves no money and resolves to a plain SubscriptionMutationResult. useUncancelSubscription(subscriptionId, options?) — the subscription id is the first argument. The uncancel alias takes no input (call uncancel()). options carries a single onSuccess(result: SubscriptionMutationResult) => void callback. Returns — a React Query mutation plus uncancel() => Promise<SubscriptionMutationResult>, resolving to { subscriptionId, status, cancelAtPeriodEnd, effectiveAt }.

See also

Settlement model

The SettleOutcome union and on* handlers the mutation hooks dispatch.

Invoices

Preview renewals with useUpcomingInvoice and collect payment.

Entitlements & gates

What each subscription grants the customer.

Customer

The customer these subscriptions belong to.