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
  • Developer Resources
  • 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

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

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
      • Retrieve Subscription Change Timeline
    • Chargebee Widgets
  • Hosted Pages Features
  • Additional Hosted Pages
  • Payment Components
  • Mobile SDKs and Wrappers
  • Articles and FAQ

Site Configuration


  • Users & Roles
  • Custom Fields & Metadata
  • 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

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
  • Marketing
  • Stitch
  • Collaboration
  • Contract Management
  • Order Management
  • Mobile Subscriptions (Legacy)
  • Other Integrations
  • 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.

Note

This page covers sites on Product Catalog 1.0, where the function receives plans and addons. If your site is on Product Catalog 2.0, the function receives item prices instead, and the code on this page does not apply.

Note

fetchAllowedPlanConfig does not override mandatory addons. Include mandatory addons as part of the output JSON.

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.plansObjectCurrent plan details.
data.addonsObjectList of current addons linked to 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 plans that are available to the customer for selection, along with the constraints on the addons and quantity for each plan.
items[].plan_idString, requiredThe ID of the plan that is available to the customer for selection. For example, professional.
items[].allowed_addonsArray<Object>The addons that are available to the customer for selection for this plan. This set overrides the top-level allowed_addons property.
items[].addonsArray<Object>List of addons to be auto-attached to the new plan.
items[].meta_data.quantity_metaObjectA constraint on the selectable quantity of the plan. 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 by the items[].allowed_addons property.
use_existing_addonsBooleanIf true, the existing addons on the subscription are auto-attached to the new plan. 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. For example, sms_credits.
meta_data.quantity_metaObjectA constraint on the selectable quantity of the addon. 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

This function lists the per-unit plans on the site and applies a quantity range to each one, using the cf_seats custom field on the subscription as the minimum.

var chargebee = require("chargebee")
exports.fetchAllowedPlanConfig = function({subscription, customer, plan}, callback, logger){
 chargebee.plan.list().request(function(error,result){
  if(error){
   callback(error, null);
  } 
  else {
   let selectedPlans = result.list.map((p)=> p.plan).filter(p => p.charge_model =="per_unit");
   let seats = subscription.cf_seats; 
   let output ={}; 
   output.items =[]; 
   selectedPlans.forEach(p =>{
    let entry ={
     plan_id: p.id
    } 
    if(p.charge_model =="per_unit"&& seats){
     entry["meta_data"]={
      quantity_meta:{
       type:"range", 
       min: seats,
       max:100, 
       step:1
       }
      }
     }
    output.items.push(entry);
    }); 
    callback(null, output);
    }
  });
}
Sample output
{
  "items":[{
    "plan_id":"plan3",
    "meta_data":{
      "quantity_meta":{
        "type":"range",
        "min":5,
        "max":100,
        "step":1
      }
    }
  },{
    "plan_id":"basic",
    "meta_data":{
      "quantity_meta":{
        "type":"range",
        "min":5,
        "max":100,
        "step":1
      }
    }
  },{
    "plan_id":"no-trial",
    "meta_data":{
      "quantity_meta":{
        "type":"range",
        "min":5,
        "max":100,
        "step":1
      }
    }
  },{
    "plan_id":"plan1",
    "meta_data":{
      "quantity_meta":{
        "type":"range",
        "min":5,
        "max":100,
        "step":1
      }
    }
  },{
    "plan_id":"professional",
    "meta_data":{
      "quantity_meta":{
        "type":"range",
        "min":5,
        "max":100,
        "step":1
      }
    }
  },{
    "plan_id":"plan2",
    "meta_data":{
      "quantity_meta":{
        "type":"range",
        "min":5,
        "max":100,
        "step":1
      }
    }
  }]
}

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",
            "additionalProperties": false,
            "properties":{
                "type":{
                    "type":"string",
                    "enum":["range"]
                    },
                    "min":{
                        "type":"number"
                        },
                    "max":{
                        "type":"number"
                        },
                    "step":{
                        "type":"number"
                    }
                },
                "required":["type","min","max"]
            },
            "addon":{
                "additionalProperties": false,
                "type":"object",
                "required":["id"],
                "properties":{
                    "id":{
                        "type":"string",
                        "examples":[
                            "sms_credits"
                        ]
                    },
                    "meta_data":{
                        "additionalProperties": false,
                        "type":"object",
                        "properties":{
                            "quantity_meta":{
                                "oneOf":[{
                                    "$ref":"#/definitions/range_quantity_meta"
                                },{
                                    "$ref":"#/definitions/fixed_quantity_meta"
                                }]
                            }
                        }
                    }
                }
            },
            "fixed_quantity_meta":{
                "type":"object",
                "additionalProperties": false,
                "properties":{
                    "type":{
                        "type":"string",
                        "enum":["fixed"]
                    },
                    "values":{
                        "type":"array",
                        "items":{
                            "type":"number"
                        }
                    }
                },
                "required":["type","values"]
            }
        },
        "properties":{
            "items":{
                "type":"array",
                "items":{
                    "$id":"/properties/product-item",
                    "type":"object","additionalProperties": false,
                    "required":["plan_id"],
                    "properties":{
                        "plan_id":{
                            "type":"string",
                            "examples":["professional"]
                        },
                        "allowed_addons":{
                            "type":"array",
                            "items":{
                                "$ref":"#/definitions/addon"
                            }
                        },
                        "meta_data":{
                            "type":"object",
                            "additionalProperties": false,
                            "properties":{
                                "quantity_meta":{
                                    "oneOf":[{
                                        "$ref":"#/definitions/range_quantity_meta"
                                    },{
                                        "$ref":"#/definitions/fixed_quantity_meta"
                                    }]
                                }
                            }
                        },
                        "addons":{
                            "type":"array",
                            "items":{
                                "$ref":"#/definitions/addon"
                            }
                        }
                    }
                }
            },
            "allowed_addons":{
                "type":"array",
                "items":{
                    "$ref":"#/definitions/addon"
                }
            },
            "use_existing_addons":{
                "type":"boolean",
                "default": false,
                "examples":[ true]
            }
        },
        "additionalProperties": false
}

Was this article helpful?