Subscriptions
Sell access to your product as a recurring paid subscription.
🔐 All methods except getAllSubscriptions() require authorization.
More information about the module's user interface https://doc.oneentry.cloud/docs/category/subscriptions (opens in a new tab)
The Subscriptions module manages paid subscriptions for the authenticated user. You can create a subscription and get a payment session for it, cancel it, open a Stripe Billing Portal session after a failed payment, list the available subscription plans, and get the markers of the user's active ones.
Instead of placing one-off orders, the user subscribes to a plan (a marker such as premium) and the payment provider bills them automatically. Plans are configured in the OneEntry admin panel and connected to a payment account (for example Stripe).
Quickstart
Initialize the module from defineOneEntry:
const { } = defineOneEntry( , { });
Subscribe the current user to a plan and redirect them to the payment session (requires authorization):
// Create a subscription for the "premium" plan.
const session = await Subscriptions.subscribe({ marker: "premium" });
if ('statusCode' in session) throw new Error(session.message);
// Send the user to the payment page to complete the first payment.
if (session && 'paymentUrl' in session && session.paymentUrl) {
window.location.href = session.paymentUrl;
}
For a custom payment account the API creates a pending subscription without a payment session, and the method resolves to a full ICreatedSubscription with paymentUrl: null (an admin activates the subscription outside the platform).
Plan markers
A marker is the unique text identifier of a subscription plan, for example premium. Every method of this module takes it to know which plan you mean. Markers never change, so reference plans by marker in your code, never by display name.
subscribe(), cancelSubscription() and recoverSubscriptions() take a body of { marker }.
Payment session
subscribe() returns a payment session (ICreatedSubscription) with a paymentUrl. Redirect the user to that URL to complete the first payment and activate the subscription, and handle the return URL in your app.
Things to know
- All methods except
getAllSubscriptions()require an authenticated user. Call them after the user has logged in with the AuthProvider module. getAllSubscriptions()returns all available subscriptions as fullISubscriptionEntityobjects (the marker is inidentifier).getActiveSubscriptions()returns only the markers of the plans the current user is subscribed to, asstring[]. Use it to gate premium features in your UI.cancelSubscription()stops the recurring billing for a plan.recoverSubscriptions()works only for Stripe plans whose subscription ispast_due,incompleteorunpaid(a failed payment); it can't restore a cancelled subscription. The platform creates a Stripe Billing Portal session where the user can update the payment method, but the SDK (through 1.0.186) returns onlytrueand drops the portal URL, so the method gives you nothing to redirect the user to.- Both resolve to
trueon success and to anIErrorobject on failure (forrecoverSubscriptions(), also when the plan isn't on Stripe or the subscription isn't in a recoverable state), so checkresult === true, not just truthiness.
Methods
| Method | Description |
|---|---|
| subscribe() 🔐 | Create a subscription and get a payment session |
| getAllSubscriptions() | Get all available subscriptions |
| getActiveSubscriptions() 🔐 | Get the markers of the user's active subscriptions |
| cancelSubscription() 🔐 | Cancel a subscription |
| recoverSubscriptions() 🔐 | Open a Stripe Billing Portal session for a failed payment |
🔐 = Requires authorization
Related
- AuthProvider Module: authorizes the user
- Payments Module: payment accounts and sessions
- Users Module: the subscribing user