Subscription Plans: Architecture overview
Architecture overview
Section titled “Architecture overview”Buyable
Section titled “Buyable”Type definitions on module level:
type Plan = { id: string; // Unique identifier for that plan. Used as `lookup_key` for stripe product and as identifier to use when checking for features. name: string; // Human readable name of the plan and name of the product in stripe. features: Feature[]; // Features of the plan. The same feature can be used in multiple plans. price: Price[]; // Prices of the plan.};
type Feature = { identifier: string; // Unique identifier for the feature. Can be used to enable or disable the feature in the system. name: string; // Human readable name of the feature.};
type Price = { identifier: string; // Unique identifier for the price. Used as `lookup_key` for stripe price. price: number; // Price with 2 decimal places. E.g. 9.99 currency: string; // Currency to use in stripe. 3 letter ISO Currency code E.g. "EUR", "USD", "INR" interval: "day"| "week" | "month" | "year"; // Payment interval to be used for the subscription.};Fields present on an Entity that could have active plans (i.e. from a Module with the Buyable capability):
type Entity = { active_plans: string; // Comma seperated list of active plans for that entity. e.g. "pro_monthly, lite" active_features: string; // Comma seperated list of active features of that entity. e.g. "pro_feature_x, pro_feature_y, lite_feature_x". This is derived from all features of all active plans, and unique.
// ... All other fields of the entity, including id, module_id etc.};Billable
Section titled “Billable”Field requirements on entity that should be able to buy something:
[ { identifier: "name", // Name of the user to use in stripe type: "text", options: { rules: "required", // This is required for stripe taxes to work }, }, { identifier: "email", // Email to register the user in stripe with type: "email", options: { rules: "required", // This is required for stripe taxes to work }, }, { identifier: "country_code", // ISO 3166-1 alpha-2 country code of the user type: "text", options: { rules: "required", // This is required for stripe taxes to work }, }, { identifier: "postal_code", // Postal code of the user type: "text", options: { rules: "required", // This is required for stripe taxes to work }, },]- Set up the required Stripe configuration
- Set up a Stripe account and get your API keys. Fill
STRIPE_KEY,STRIPE_SECRETandSTRIPE_WEBHOOK_SECRETin your.envfile.- You can find the value for
STRIPE_WEBHOOK_SECRETin the Stripe dashboard under Developers -> Webhooks -> the webhook the initial setup created for you.-
It is expected to fill this AFTER the first
bakery applyas before that, you cannot know the value. Technically, everything work without this, but it is highly recommended to set it up due to security reasons.
-
- You can find the value for
- Take a look and do the required configurations do be done in the stripe dashboard
- Set up a Stripe account and get your API keys. Fill
- Add the
Billablecapability to a User module. - Define a module with the
Buyablecapability. - Configure the available
subscription_plansin the options of that capability. - Apply those changes via the bakery planner.
- This will create the products and prices in Stripe and get everything ready for the user to buy a plan.
- Make the user buy a plan for an entity.
- Either using the default integration (a button in the detail view of any Buyable entity), or build a custom widget that uses the provided Api functions to initiate a subscription.
- Use the
active_featuresfields on the entity to enable or disable features in your workflows, policies, recipes etc.- Specifically, use the
entity.hasActiveFeature(<feature_identifier>)function to check if a feature is active for that entity.
- Specifically, use the
Example configuration of a user
Section titled “Example configuration of a user”This defines a user with the Billable capability.
It uses all the default fields (will create them in this case) except for the name field, which is mapped to the existing full_name field.
JSON Configuration for a user
{ "resource_user": { "identifier": "user", "type": "user", "options": { "capabilities": [ { "identifier": "Billable", "options": { "field_mapping": { "name": "full_name" } } } ] }, "fields": [ { "identifier": "full_name", "type": "text" } ] }}