New in Chargebee: Explore Reveal and understand your payment performance end-to-end.Try Now
Docschargebee docs
HomeBillingCPQPaymentsRevRecGrowthReveal
Support

Product Updates


  • Release Notes

Getting Started


  • Overview
  • Chargebee Billing Data Centers
  • Object Relationship Model
  • Understanding Sites
  • Chargebee Tech Glossary
  • Articles and FAQ

Implementing Chargebee


  • Implementation Guide
  • Go-live Checklist
  • Articles and FAQ

Agentic AI


  • Chargebee Copilot
  • Catalog Setup Assistant
  • MCP Servers

Developer Resources


  • Developer Resources Overview
  • API Explorer
  • Articles and FAQ

Chargebee Apps


  • Chargebee Apps CLI Developer Guide

Product Catalog


  • Product Catalog Overview
  • Coupons
  • Articles and FAQ

Subscriptions


  • Working with Subscriptions
  • Billing
  • Orders
  • Articles and FAQ

Customers


  • Managing Customers
  • Account Hierarchy
  • Email Notifications
  • Branding
  • Configure Multiple Languages
  • Articles and FAQ

Entitlements


  • Entitlements Overview
  • Features Overview
  • Feature Management
  • Managing Product Entitlements
  • Subscription Entitlements
  • Customer Entitlements
  • Grandfathering Entitlements
  • Articles and FAQ

Usage Based Billing


  • Understanding Usages
  • Setting up Usage Based Billing
  • Usage Alerts
  • Prepaid credits

Invoices and Credit Notes


  • Invoices
  • Credit Notes
  • Quotes [Legacy]
  • Transactions
  • Articles and FAQ

Taxes


  • Overview
  • Configuring Taxes
  • Country-specific Taxes
  • Articles and FAQ

Hosted Capabilities


  • Overview
  • Hosted Checkout
  • Hosted Self-Serve Portal
    • Integration Options
    • Chargebee Self-Serve Portal
    • Portal Custom Code
      • Fetch Allowed Plan Configuration
        • Use Cases
      • Retrieve Subscription Change Timeline
    • Chargebee Widgets
  • Hosted Pages Features
  • Additional Hosted Pages
  • Payment Components
  • Pricing Table
  • Mobile SDKs and Wrappers
  • Articles and FAQ

Site Configuration


  • Users & Roles
  • Custom Fields & Metadata
  • Approvals
  • Mandatory Fields
  • File Attachments & Comments
  • Advanced Filter Options
  • Multicurrency Pricing
  • Multi-decimal Support
  • Configuring Reason Codes
  • Events and Webhooks
  • API Keys
  • Time Zone
  • Time Machine
  • Transfer Configurations
  • Articles and FAQ

Multi Business Entity


  • Multi Business Entity Overview
  • Customer Transfer Overview
  • Articles and FAQ

Mobile Subscriptions


  • Overview
  • Omnichannel Subscriptions
  • Omnichannel Subscriptions (Legacy)

Reports and Analytics


  • RevenueStory
  • Home Dashboard
  • Frequently Asked Questions
  • FAQs for Classic Reports Sunset
  • Articles and FAQ

Integrations


  • Sales
  • Customer Support and Success
  • Finance
  • Tax
  • eInvoicing
  • Marketing
  • Stitch
  • Collaboration
  • Contract Management
  • Ecommerce Management
  • Articles and FAQ

Data Privacy & Security


  • Two Factor Authentication
  • SAML Single Sign-On
  • System for Cross-Domain Identity Management (SCIM)
  • EU-GDPR
  • Consent Management
  • Personal Data Management
  • Compliance Certificates
  • HIPAA Guidelines
  • PCI Recommendations and Integration Types
  • Articles and FAQ

Data Operations


  • Bulk Operations
  • Migration
  • Articles and FAQ
  1. Billing
  2. Hosted Capabilities
  3. Hosted Self-Serve Portal
  4. Portal Custom Code
  5. Fetch Allowed Plan Configuration
  1. Billing
  2. Hosted Capabilities
  3. Hosted Self-Serve Portal
  4. Portal Custom Code
  5. Fetch Allowed Plan Configuration

Fetch allowed plan configuration

Use the fetchAllowedPlanConfig function to control the plans and addons a customer can select when they change their subscription in the Self-Serve Portal. With this function, you can:

  • Specify the plans and addons that a customer can select.
  • Restrict the addons available for a specific plan.
  • Auto-attach addons to a plan when the customer selects it.
  • Set quantity constraints for a plan or addon.

This function is one of the two functions supported by Portal Custom Code.

Usage and invocation

Chargebee invokes this function to get the plan and addon combinations for a subscription. The customer sees only the plans and addons that the function returns when they change the subscription. Write your business logic in the function to restrict the plans and addons for your customers.

Example

Suppose your catalog has a basic group and a premium group of plans and addons. In your function, check the customer's current plan and return only the appropriate group: for a customer on the Platinum plan, return the premium group so they can move to a higher tier; for a customer on the Gold plan, return the basic group so they see the relevant options during a downgrade.

Requirements

  • Write the function in JavaScript.
  • Make sure the function runs in a Node.js v6.0 environment.
  • Use the resources that Chargebee passes to the function to write your business logic. These resources use the same format as the output of the Node.js client library.
  • To retrieve additional resources, use Chargebee's Node.js client library, provided you've already configured it with your API key and secret key.
  • Return a JavaScript object that matches the JSON schema on this page.
  • To apply quantity constraints, enable the Customize plan/addon quantity based on meta configuration setting in your Checkout and Self-Serve Portal settings.

Parameters

ParameterTypeDescription
dataObjectThe resources that Chargebee passes to your custom code.
data.subscriptionObjectThe subscription object.
data.customerObjectThe customer object.
data.item_pricesArray<Object>The plan and addon item prices currently part of the subscription.
callbackFunctionFunction to send the output of the custom code to Chargebee.
callback.errorObjectError details to be sent to Chargebee when the custom code has not been successfully executed.
callback.successObjectOutput of the custom code to be sent to Chargebee when the custom code has been successfully executed.
loggerFunctionHelps log debug information for your custom code. To emit a log entry, call logger.debug('key', 'value') in your custom code. The logs appear in Chargebee Billing on the Custom Codes page at https://YOUR_SUBDOMAIN.chargebee.com/custom_codes, under the Execution section, after you execute the custom code.

Return value

Return a JavaScript object with the following properties to pass data to Chargebee. Chargebee then displays this data on the portal for the customer.

PropertyTypeDescription
itemsArray<Object>The plan item prices that are available to the customer for selection, along with the constraints on the addons and quantity for each plan item price.
items[].plan_idString, requiredThe ID of the plan item price that is available to the customer for selection. For example, professional.
items[].allowed_addonsArray<Object>The addon item prices that are available to the customer for selection for this plan item price. This set overrides the top-level allowed_addons property.
items[].addonsArray<Object>List of addon item prices to be auto-attached to the new plan item price.
items[].meta_data.quantity_metaObjectA constraint on the selectable quantity of the plan item price. See Quantity constraints.
allowed_addonsArray<Object>The subset of addons that the customer is allowed to select from. This set is overridden for each plan item price by the items[].allowed_addons property.
use_existing_addonsBooleanIf true, the existing addons on the subscription are auto-attached to the new plan item price. Defaults to false.

Every addon object—in allowed_addons, items[].allowed_addons, and items[].addons—takes the following shape.

PropertyTypeDescription
idString, requiredThe ID of the addon item price. For example, sms_credits.
meta_data.quantity_metaObjectA constraint on the selectable quantity of the addon item price. See Quantity constraints.

Quantity constraints

A quantity_meta object expresses the constraint either as a range (type: range) or as a fixed set of quantities (type: fixed).

PropertyTypeDescription
typeString, requiredThe kind of quantity constraint. One of range or fixed.
minNumberThe minimum quantity that the customer is allowed to select. Applicable and required when type is range.
maxNumberThe maximum quantity that the customer is allowed to select. Applicable and required when type is range.
stepNumberThe step size of the quantity that the customer is allowed to select. Applicable when type is range.
valuesArray<Number>An array of fixed quantities that the customer is allowed to select from. Applicable and required when type is fixed.

Example

The following function offers the customer two plans and auto-attaches an onboarding addon when they select the professional plan. Because the return value is static, this function makes no API calls and is a useful starting point before you add your own business logic.

exports.fetchAllowedPlanConfig = function ({ subscription, customer, item_prices }, callback, logger) {
  const output = {
    items: [
      {
        plan_id: "standard-USD-monthly"
      },
      {
        plan_id: "professional-USD-monthly",
        addons: [
          { id: "onboarding-USD-monthly" }
        ]
      }
    ]
  };

  callback(null, output);
};

The customer sees only the standard and professional plans. When they select professional, Chargebee auto-attaches the onboarding addon.

Sample output
{
  "items": [
    {
      "plan_id": "standard-USD-monthly"
    },
    {
      "plan_id": "professional-USD-monthly",
      "addons": [
        { "id": "onboarding-USD-monthly" }
      ]
    }
  ]
}

Use cases

For end-to-end examples that show how to build the return value for common scenarios, such as restricting plans by billing frequency or blocking downgrades, see Fetch allowed plan configuration use cases.

Expected JSON schema for the output

The return value must match the following JSON schema.

JSON schema
{
  "type": "object",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "range_quantity_meta": {
      "type": "object",
      "description": "A constraint on the selectable quantity of the item price expressed as a range.",
      "additionalProperties": false,
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "range"
          ]
        },
        "min": {
          "type": "number",
          "description": "The minimum quantity of the item price that the customer is allowed to select."
        },
        "max": {
          "type": "number",
          "description": "The maximum quantity of the item price that the customer is allowed to select."
        },
        "step": {
          "type": "number",
          "description": "The step size of the quantity of the item price that the customer is allowed to select."
        }
      },
      "required": [
        "type",
        "min",
        "max"
      ]
    },
    "addon": {
      "additionalProperties": false,
      "type": "object",
      "description": "An addon item price.",
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "string",
          "description": "The ID of the addon item price.",
          "examples": [
            "sms_credits"
          ]
        },
        "meta_data": {
          "additionalProperties": false,
          "type": "object",
          "description": "Other details about the addon item price.",
          "properties": {
            "quantity_meta": {
              "description": "A constraint on the selectable quantity of the addon item price.",
              "oneOf": [
                {
                  "$ref": "#/definitions/range_quantity_meta"
                },
                {
                  "$ref": "#/definitions/fixed_quantity_meta"
                }
              ]
            }
          }
        }
      }
    },
    "fixed_quantity_meta": {
      "type": "object",
      "description": "A constraint on the selectable quantity of the item price expressed as a fixed set of quantities.",
      "additionalProperties": false,
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "fixed"
          ]
        },
        "values": {
          "type": "array",
          "description": "An array of fixed quantities of the item price that the customer is allowed to select from.",
          "items": {
            "type": "number",
            "description": "One of the fixed quantities of the item price that the customer is allowed to select from."
          }
        }
      },
      "required": [
        "type",
        "values"
      ]
    }
  },
  "properties": {
    "items": {
      "type": "array",
      "description": "",
      "items": {
        "$id": "/properties/product-item",
        "type": "object",
        "description": "A plan item price that is available to the customer for selection, and the constraints on the addons and quantity for that plan item price.",
        "additionalProperties": false,
        "required": [
          "plan_id"
        ],
        "properties": {
          "plan_id": {
            "type": "string",
            "description": "The ID of the plan item price that is available to the customer for selection.",
            "examples": [
              "professional"
            ]
          },
          "allowed_addons": {
            "type": "array",
            "description": "The addon item prices that are available to the customer for selection for this plan item price. This set overrides the `allowed_addons` property.",
            "items": {
              "$ref": "#/definitions/addon"
            }
          },
          "meta_data": {
            "type": "object",
            "description": "Other details about the plan item price.",
            "additionalProperties": false,
            "properties": {
              "quantity_meta": {
                "description": "A constraint on the selectable quantity of the plan item price.",
                "oneOf": [
                  {
                    "$ref": "#/definitions/range_quantity_meta"
                  },
                  {
                    "$ref": "#/definitions/fixed_quantity_meta"
                  }
                ]
              }
            }
          },
          "addons": {
            "type": "array",
            "description": "List of addon item prices to be auto-attached to the new plan item price.",
            "items": {
              "$ref": "#/definitions/addon"
            }
          }
        }
      }
    },
    "allowed_addons": {
      "type": "array",
      "description": "The subset of addons that the customer is allowed to select from. This set is overridden for each plan item price by the `items[].allowed_addons` property.",
      "items": {
        "$ref": "#/definitions/addon"
      }
    },
    "use_existing_addons": {
      "type": "boolean",
      "default": false,
      "description": "If true, the existing addons on the subscription are auto-attached to the new plan item price.",
      "examples": [
        true
      ]
    }
  },
  "additionalProperties": false
}

Was this article helpful?