# Chargebee Library

# Overview

This page documents the top-level methods available in the Chargebee global object.

# Prerequisite

Before using these methods, ensure you have set up Chargebee.js in your application.

# init()

Initializes the Chargebee library with your site configuration, creating a Chargebee instance that you can use throughout your application.

WARNING

Call tearDown() to clean up before calling init() again within the same session.

# Syntax

const chargebee = Chargebee.init({
   site: "YOUR-CHARGEBEE-BILLING-SUBDOMAIN",
   domain: "https://your-domain.com" // optional
});
1
2
3
4

# Parameters

options
Object Required Hide properties
Initialization options.
site
String Required
The subdomain of your Chargebee Billing site. For example, if your site URL is https://acme-test.chargebee.com, set site to acme-test.
domain
String Required if Custom domain is enabled.
Your custom domain, if configured in Chargebee. Chargebee-hosted pages will be served from this domain. For example: Eg: https://billing.yourdomain.com
publishableKey
Boolean
A publishable API key from Chargebee. This is required for Payment Components, Functions, and Card Components.
isItemsModel
Boolean
This will enable support for product catalog 2.0.
businessEntityId
String
The unique ID of the business entity in Chargebee Billing. Applicable only if business entities are enabled for the site. If omitted, the default business entity configured for the site is used.
enableRedirectMode
Boolean
If this is enabled, end user will be redirected to chargebee checkout and self-serve portal page instead of opening in a modal box. This is needed, if amazon pay is configured as one of your supported payment methods. Please make sure you have configured redirect URL in checkout and self-serve portal, so that the user is able to navigate back to the app.
iframeOnly
Boolean
This will work only on mobile browsers. Set true to open the Checkout or Portal as an iFrame on the same browser tab.

When this is set to true,
  • If Checkout or Portal is opened from within an application, it opens in a WebView tab.
  • If Checkout or Portal is opened from a WebView tab, it opens on the same WebView tab.
  • If Checkout or Portal is opened from a browser tab, it opens on the same browser tab.
enableGATracking
Boolean
This will enable Google Analytics (analytics.js) tracking. Follow the steps to integrate Google Analytics with Chargebee checkout(In-app checkout).
enableGTMTracking
Boolean
This will enable Google Tag Manager. Learn more about on how to use Google Analytics via GTM in Chargebee.
enableFBQTracking
Boolean
This will enable Facebook pixel tracking. Follow the steps to integrate Facebook pixel tracking with Chargebee checkout (In-app checkout).
enableFriendbuyTracking
Boolean
If this is enabled, Chargebee will send customer information and order information to friendbuy and will also open friendbuy's post purchase widget based on your integration settings. Please make sure you allowlist your domain in the checkout settings page.
enableRefersionTracking
Boolean
This will enable Refersion tracking (Referral integration). Learn more about Chargebee-Refersion integration .

Note: This is available only in PC 1.0

# Return value

Returns a Chargebee instance object that provides access to all Chargebee.js features.

# Example

// Initialize Chargebee with your site.
const chargebee = Chargebee.init({
    site: "YOUR-CHARGEBEE-BILLING-SUBDOMAIN", // Your Chargebee site identifier.
    domain: "https://your-domain.com" // Optional: custom domain
});

// Use the instance to access features
const pricingTable = await chargebee.pricingTable();
1
2
3
4
5
6
7
8

# getInstance()

Retrieves the existing Chargebee instance that was created using the init() method. This is useful when you need to access the instance from different parts of your application.

# Syntax

const chargebee = Chargebee.getInstance();
1

# Return value

  • Returns the Chargebee instance object that was created during initialization.

  • Returns an error if no instance has been created yet.

# Example

// Initialize Chargebee
Chargebee.init({
    site: "your-site-name"
});

// Later in your code, get the instance
const chargebee = Chargebee.getInstance();

// Use the instance
const pricingTable = await chargebee.pricingTable();
1
2
3
4
5
6
7
8
9
10

# registerAgain()

Rebinds event listeners for dynamically inserted DOM elements. This is primarily used for drop-in script integrations where you add new Chargebee elements to the page after the initial load.

# Syntax

Chargebee.registerAgain();
1

# Return value

None.

# Example

// Create a new checkout link dynamically.
const checkoutLink = document.createElement('a');
checkoutLink.href = "javascript:void(0)";
checkoutLink.dataset.cbType = "checkout";
checkoutLink.dataset.cbPlanId = "cbdemo_scale";
checkoutLink.textContent = "Subscribe Now";
document.body.appendChild(checkoutLink);

// Rebind listeners to recognize the new element.
Chargebee.registerAgain();
1
2
3
4
5
6
7
8
9
10

# tearDown()

Resets the state of the Chargebee object and removes any Chargebee component from the DOM. Call tearDown() to clean up before invoking init() again within the same session.

# Syntax

Chargebee.tearDown();
1

# Return value

None.

# Example

// Clean up before reinitializing
Chargebee.tearDown();

// Now safe to initialize again
const chargebee = Chargebee.init({
    site: "YOUR-CHARGEBEE-BILLING-SUBDOMAIN"
});
1
2
3
4
5
6
7

# getPortalSections()

Returns an object containing all available sections in the Chargebee customer portal. You can use these section identifiers to navigate users to specific portal sections or open them as individual components.

# Syntax

const portalSections = Chargebee.getPortalSections();
1

# Return value

An object with key-value pairs mapping section names to their identifiers.

# Available sections

{
  "SUBSCRIPTION_DETAILS": "sub_details",
  "SUBSCRIPTION_CANCELLATION": "sub_cancel",
  "EDIT_SUBSCRIPTION": "edit_subscription",
  "VIEW_SCHEDULED_CHANGES": "scheduled_changes",
  "ACCOUNT_DETAILS": "account_details",
  "EDIT_ACCOUNT_DETAILS": "portal_edit_account",
  "ADDRESS": "portal_address",
  "EDIT_BILLING_ADDRESS": "portal_edit_billing_address",
  "EDIT_SHIPPING_ADDRESS": "portal_edit_shipping_address",
  "EDIT_SUBSCRIPTION_CUSTOM_FIELDS": "portal_edit_subscription_cf",
  "PAYMENT_SOURCES": "portal_payment_methods",
  "ADD_PAYMENT_SOURCE": "portal_add_payment_method",
  "EDIT_PAYMENT_SOURCE": "portal_edit_payment_method",
  "VIEW_PAYMENT_SOURCE": "portal_view_payment_method",
  "CHOOSE_PAYMENT_METHOD_FOR_SUBSCRIPTION": "portal_choose_payment_method",
  "BILLING_HISTORY": "portal_billing_history"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Example

// Get all available portal sections.
const sections = Chargebee.getPortalSections();

// Use a section identifier to open a specific portal section.
const chargebee = Chargebee.getInstance();
const chargebeePortal = chargebee.createChargebeePortal();

chargebeePortal.open({
  // Optional callbacks can be added here.
}, {
  sectionType: sections.ACCOUNT_DETAILS
});
1
2
3
4
5
6
7
8
9
10
11
12