Skip to content

Subscription Plans: Technical implementation details

Back to Subscription Plans.

  • Entities from Modules with the Buyable capability can be bought by users with the Billable capability.
  • The plans (products in stripe) and their prices defined in the Buyable Capability are automatically created and synced with Stripe at bakery apply time.
    • Uses the identifier as lookup_key for 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_identifier on 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
  • The Billable capability 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 Billable capability can be mapped to re-use existing fields in the options of the capability.
    • This is done via the field_mapping option.
      • The keys of this object are on of the field identifiers above, and the value is the identifier of the (existing!) field to use.
  • The active_features and active_plans fields 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" or active_plans: "pro_monthly, lite"
    • 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
    • active_features is 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 the active_features field is also possible.
  • 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_plans field, but the features will be removed from the active_features field!
    • Re-adding the plan will restore features and the Stripe integration will work as if nothing ever happened.

Technical step, by step detailing of common use cases.

  1. Request to /modules/<module_identifier>/resources/<entity_id>/subscribe/<plan_identifier>/<price_identifier> with payload:
    {
    success_url: "<SPA URL to current entity detail view>", // e.g. /modules/buyable_thingy/1234#subscription-initiated
    cancel_url: "<SPA URL to current entity detail view>", // e.g. /modules/buyable_thingy/1234#subscription-failed
    }
    OR just use 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.
  2. 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 Billable user entity.
  3. 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 (or active_plans).

Canceling a subscription, checking active subscriptions, changing payment method etc.

Section titled “Canceling a subscription, checking active subscriptions, changing payment method etc.”
  1. Request to /modules/<module_identifier>/resources/<entity_id>/subscribtion-manager OR just use Api.getBillingPortalURL()
  2. 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.

  • /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).

Historical checklist: part 1, part 2.