Subscription Plans: Technical implementation details
Technical implementation details
Section titled “Technical implementation details”- Entities from Modules with the
Buyablecapability can be bought by users with theBillablecapability. - The plans (products in stripe) and their prices defined in the
BuyableCapability are automatically created and synced with Stripe at bakery apply time.- Uses the
identifieraslookup_keyfor the price in stripe as this is the “identifying information” we use when buying a plan - The identifier of the plan is set as the metadata key
plan_identifieron the stripe product. - Combination of price and plan is validated to prevent a user from buying a “higher value” plan with a lower price from another plan
- Uses the
- The
Billablecapability requires the following fields to exist and be filled on the user Entity:email: email: The email of the user. Should already be there as it is a default field.name: text: The name of the user in stripe.country_code: text: ISO 3166-1 alpha-2 country code the user is located in.postal_code: text: The postal code of the user.
- The fields on the
Billablecapability can be mapped to re-use existing fields in the options of the capability.- This is done via the
field_mappingoption.- The keys of this object are on of the field identifiers above, and the value is the identifier of the (existing!) field to use.
- This is done via the
- The
active_featuresandactive_plansfields on a Buyable Entity are- statically stored list of “slugs” (the identifiers of all features (unique) from all plans the entity has and the identifiers of all plans of an entity respectively).
- e.g.
active_features: "pro_feature_x, pro_feature_y, lite_feature_x"oractive_plans: "pro_monthly, lite"
- e.g.
- updated when the important events (that also trigger the workflow events) are fired.
- When the active plans changed
- i.e. new plan was bought or an existing plan was canceled / deleted
- When the underlying plan changed
- i.e. adding or removing features from a plan
- When the active plans changed
active_featuresis a list of all “feature flags” that are enabled for that entity.- They are derived from the features of all active plans and are a unique list of comma separated feature identifiers as configured.
- You should always use
entity.hasActiveFeature(<feature_identifier>)to check if a feature is active for an entity, but manually looking at theactive_featuresfield is also possible.
- statically stored list of “slugs” (the identifiers of all features (unique) from all plans the entity has and the identifiers of all plans of an entity respectively).
- When existing plans are DELETED i.e. removed from the config, the following happens:
- No changes in Stripe will occur.
- The products and prices will not be deleted or archived.
- Active subscriptions will not be canceled.
- Entities that had the plan active, will have the respective features removed.
- The plan identifier will still be present in the
active_plansfield, but the features will be removed from theactive_featuresfield!
- The plan identifier will still be present in the
- Re-adding the plan will restore features and the Stripe integration will work as if nothing ever happened.
- No changes in Stripe will occur.
Use Cases
Section titled “Use Cases”Technical step, by step detailing of common use cases.
Buying a plan
Section titled “Buying a plan”- Request to
/modules/<module_identifier>/resources/<entity_id>/subscribe/<plan_identifier>/<price_identifier>with payload:OR just use{success_url: "<SPA URL to current entity detail view>", // e.g. /modules/buyable_thingy/1234#subscription-initiatedcancel_url: "<SPA URL to current entity detail view>", // e.g. /modules/buyable_thingy/1234#subscription-failed}Api.getCheckoutSessionURLForNewSubscription(entityID: number, moduleIdentifier: string, planIdentifier: string, priceIdentifier: string)- This will extract the user from the request and construct a URL for a Stripe checkout session.
- Redirect the user to the returned URL.
- This will load a stripe checkout page for the selected plan and price for the given entity.
- Email, name and tax info (country and postal code) will be prefilled from the
Billableuser entity.
- When finished, User will be redirected by stripe back to the URLs provided (e.g.
/modules/buyable_thingy/1234#subscription-initiated).- The added parameter can be captured by widgets on the detail view to show subscription status by looking at the new values of
active_features(oractive_plans).
- The added parameter can be captured by widgets on the detail view to show subscription status by looking at the new values of
Canceling a subscription, checking active subscriptions, changing payment method etc.
Section titled “Canceling a subscription, checking active subscriptions, changing payment method etc.”- Request to
/modules/<module_identifier>/resources/<entity_id>/subscribtion-managerOR just useApi.getBillingPortalURL() - Redirect the user to the returned URL wich will load the stripe customer portal.
If cancelling something, the application will handle this via the webhooks.
Endpoints to get infomation about plans
Section titled “Endpoints to get infomation about plans”/modules/<module-identifier>/subscriptionPlans: Returns all “buyable” subscription plans (i.e. ones with at least one price defined) for the module with identifiers, features and prices./modules/<module-identifier>/allSubscriptionPlans: Returns all configured subscription plans for the module, even ones that don’t have any prices (i.e. private plans that can be used to unlock features manually).