Subscription Hooks
The Ever Works Template provides a set of React hooks for managing subscriptions across three payment providers: Stripe, LemonSqueezy, and Polar. These hooks handle subscription creation, cancellation, reactivation, auto-renewal toggling, plan access checks, and billing data retrieval.
Architecture Overview
Source Files
| File | Purpose |
|---|---|
hooks/use-subscription.ts | Stripe subscription CRUD + billing portal |
hooks/use-auto-renewal.ts | Auto-renewal toggle with optimistic updates |
hooks/use-lemonsqueezy-subscription.ts | LemonSqueezy plan changes, pause, resume |
hooks/use-polar-subscription.ts | Polar cancel and reactivate |
hooks/use-plan-status.ts | Plan access checks and expiration awareness |
hooks/use-billing-data.ts | Billing history and subscription info queries |
Provider-Specific Hooks
useSubscription (Stripe)
The primary Stripe subscription hook. Provides mutations for the full subscription lifecycle and a billing portal session creator.
function useSubscription(): UseSubscriptionReturn
Mutations:
| Mutation | Method | Endpoint | Description |
|---|---|---|---|
createSubscription | POST | /api/stripe/subscription | Create a new subscription |
updateSubscription | PUT | /api/stripe/subscription | Update plan or billing interval |
cancelSubscription | DELETE | /api/stripe/subscription | Cancel (supports period-end) |
reactivateSubscription | POST | /api/stripe/subscription/:id/reactivate | Reactivate a cancelled sub |
createBillingPortalSession | POST | /api/stripe/subscription/portal or /api/polar/subscription/portal | Open provider billing portal |
The billing portal endpoint is determined automatically based on the active payment provider (Stripe or Polar), using usePaymentProvider and useSelectedCheckoutProvider internally.
Related query hooks:
| Hook | Query Key | Purpose |
|---|---|---|
useUserSubscription() | ['user-subscription'] | Fetch the current user's subscription |
useSubscriptionById(id) | ['subscription', id] | Fetch a specific subscription |
useSubscriptionManager() | -- | Wraps useSubscription with optimistic create |
useSubscriptionActions (LemonSqueezy)
Manages LemonSqueezy-specific subscription operations including pause and resume.
function useSubscriptionActions(): SubscriptionActionsReturn
Mutations:
| Mutation | Endpoint | Description |
|---|---|---|
updatePlan | /api/lemonsqueezy/update-plan | Change subscription variant |
cancelSubscription | /api/lemonsqueezy/cancel | Cancel subscription |
pauseSubscription | /api/lemonsqueezy/pause | Pause (void or free mode) |
resumeSubscription | /api/lemonsqueezy/resume | Resume paused subscription |
reactivateSubscription | /api/lemonsqueezy/reactivate | Reactivate cancelled subscription |
All mutations invalidate ['lemonsqueezy-subscriptions'] and ['lemonsqueezy-stats'] on success.
usePolarSubscription
Handles Polar subscription cancellation and reactivation with retry logic and toast notifications.
function usePolarSubscription(): PolarSubscriptionReturn
| Function | Endpoint | Description |
|---|---|---|
cancel(id, cancelAtPeriodEnd?) | /api/polar/subscription/:id/cancel | Cancel with period-end option |
reactivate(id) | /api/polar/subscription/:id/reactivate | Reactivate cancelled subscription |
The hook implements exponential backoff retry (up to 2 retries) and skips retrying on authentication errors. A simplified useCancelPolarSubscription hook is also exported for direct component use.