# Chargebee Object
Create a Chargebee object to use the Chargebee.js SDK features.
WARNING
Allowlist your domain (opens new window) to ensure that all callbacks in Chargbee.js are fired.
# Checkout and Portal
Use the functions below to use and modify Checkout and Portal integrations on your website.
# chargebee.setBusinessEntity(businessEntityId)
Sets the business entity context for checkout.
# Syntax
chargebee.setBusinessEntity(businessEntityId)
# Parameters
# Return value
This function returns a promise.
# Example
const chargebee = Chargebee.init({
site: "moments-test",
isItemsModel: true, // Product catalog 2.0
});
chargebee.setBusinessEntity("acme-inc-us");
const cart = chargebee.getCart();
const product = chargebee.initializeProduct("silver-USD-monthly");
cart.replaceProduct(product);
cart.proceedToCheckout();
2
3
4
5
6
7
8
9
# chargebee.openCheckout(options)
This function is used to open checkout page.
# Syntax
chargebee.openCheckout(options)
# Parameters
# Return value
NA
# Example
chargebee.openCheckout({
hostedPage: function() {
// required
// This function should return a promise, that will resolve a hosted page object
// If the library that you use for making ajax calls, can return a promise,
// you can directly return that.
return new Promise(function(resolve, reject){
// This sampe response should be fetched as a result of an ajax call
var sampleResponse = {
"id": "8ajOxcuyG692GDy9yjnZ2hNM1ATugOFQl",
"type": "checkout_new",
"url": "https://yourapp.chargebee.com/pages/v3/8ajOxcuyG692GDy9yjnZ2hNM1ATugOFQl/",
"state": "created",
"embed": true,
"created_at": 1515494821,
"expires_at": 1515498421
}
resolve(sampleResponse);
});
},
loaded: function() {
// Optional
// will be called once checkout page is loaded
},
error: function(error) {
// Optional
// will be called if the promise passed causes an error
},
step: function(step) {
// Optional
// will be called for each step involved in the checkout process
},
success: function(hostedPageId) {
// Optional
// will be called when a successful checkout happens.
},
close: function() {
// Optional
// will be called when the user closes the checkout modal box
}
})
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
TIP
Find the entire suppported callbacks for Checkout.
# chargebee.getCart()
This function retrieves the cart object.
# Syntax
const cart = chargebee.getCart();
# Return value
# Example
const chargebee = Chargebee.init({
site: "moments-test",
isItemsModel: true, // Product catalog 2.0
});
chargebee.setBusinessEntity("acme-inc-us");
const cart = chargebee.getCart();
const product = chargebee.initializeProduct("silver-USD-monthly");
cart.replaceProduct(product);
cart.proceedToCheckout();
2
3
4
5
6
7
8
9
# chargebee.getProduct(checkoutButtonElement)
This function retrieves the Product object associated with a checkout button.
# Syntax
chargebee.getProduct(checkoutButtonElement)
# Parameters
# Return value
# Example
<a href="javascript:void(0)" id="diamond-subscribe-button" data-cb-type="checkout" data-cb-item-0="diamond-USD-monthly" data-cb-item-1="silver-pass-USD-monthly" data-cb-item-1-quantity="2" >
Subscribe
</a>
2
3
const subscribeButton = document.getElementById("diamond-subscribe-button");
// Get product object from HTML element
const product = chargebee.getProduct(subscribeButton);
2
3
# chargebee.initializeProduct(planId, planQuantity?)
This function initializes the Product object.
# Syntax
chargebee.initializeProduct(planId,planQuantity)
# Parameters
# Return value
A function that resolves into a product object.
# Example
const product = chargebee.initializeProduct("diamond-USD-monthly", 2);
# chargebee.setCheckoutCallbacks(setterFunction)
You can use this function to set callbacks with respect to checkout.
# Syntax
chargebee.setCheckoutCallbacks(setterFunction);
# Parameters
# Return value
NA
# Example
chargebee.setCheckoutCallbacks(cart => {
return {
success: (hostedPageId) => {
console.log('Successful checkout', hostedPageId)
},
error: (err) => {
console.error(err);
}
}
})
2
3
4
5
6
7
8
9
10
# chargebee.createChargebeePortal()
This function creates a chargebee portal instance which opens Chargebee’s self-serve portal.
# Syntax
chargebee.createChargebeePortal()
# Return value
# Example
const chargebee = Chargebee.getInstance();
// Set portal session from browser
chargebee.setPortalSession(() => portalSession);
// Create chargebee portal
const cbPortal = chargebee.createChargebeePortal()
// Open portal
cbPortal.open();
2
3
4
5
6
7
# chargebee.setPortalSession(setterFunction)
This function is used to open customer portal with the portal session created using Create Portal sessions API (opens new window).
TIP
Enable SSO(single-sign-on) in Chargebee portal settings page.
# Syntax
chargebee.setPortalSession(setterFunction)
# Parameters
# Return value
This function returns a promise, that resolves a portal session object.
# Example
chargebee.setPortalSession(function() {
// This function should return a promise, that will resolve a portal session object
// If the library that you use for making ajax calls, can return a promise, you can directly return that.
return new Promise(function(resolve, reject){
// This sampe response should be fetched as a result of an ajax call
var sampleResponse = {
"id": "portal_XpbGElGQgEHspHB",
"token": "cuqdrWacuITd2cabvf97KJD73SpNcd7BICB",
"access_url": "https://yourapp.chargebeeportal.com/portal/access/cuqdrWacuITd2cabvf97KJD73SpNcd7BICB",
"status": "created",
"created_at": 1515494835,
"expires_at": 1515498435,
"object": "portal_session",
"customer_id": "XpbGEt7QgEHsnL7O"
}
resolve(sampleResponse);
});
})
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# chargebee.logout()
This function is used to logout Chargebee’s Self serve portal session. When you do not use this function, the current session will be valid for up to an hour only.
# Syntax
chargebee.logout()
# chargebee.closeAll()
This function is used to close all Chargebee modals.
# Syntax
chargebee.closeAll()
# chargebee.setPortalCallbacks(callbacks);
You can use this function to set callbacks with respect to Self-serve portal.
# Syntax
chargebee.setPortalCallbacks(callbacks);
# Parameters
# Example
chargebee.setPortalCallbacks({
loaded: () => {
console.log('Portal loaded successfully');
},
subscriptionChanged: (data) => {
console.log('Subscription changed', data.subscription.id)
}
})
2
3
4
5
6
7
8
# Pricing Table ✨
Previous Version
The earlier version of Pricing Table was available as the @chargebee/atomicpricing
(opens new window) NPM package. That package has since been deprecated.
# chargebee.pricingTable()
Returns a Pricing Table instance.
# Syntax
chargebee.pricingTable();
# Parameters
None.
# Returns
Object representing a Pricing Table instance.
# Example
const chargebee = window.Chargebee.init({
site: "YOUR-CHARGEBEE-SUBDOMAIN",
});
const pricingTable = await chargebee.pricingTable();
pricingTable.init();
2
3
4
5
6
7
# Personalized Offers ✨
Previous Version
The earlier version of Personalized Offers was delivered via Retention.js, which has since been deprecated. You can still view the documentation (opens new window) for reference.
# chargebee.personalizedOffers()
Returns a Personalized Offers instance used to dynamically display eligible offers to the user.
# Syntax
chargebee.personalizedOffers();
# Parameters
None.
# Returns
Object representing a Personalized Offers instance.
# Example
const chargebee = window.Chargebee.init({
site: "YOUR-CHARGEBEE-SUBDOMAIN",
})
const personalizedOffers = await chargebee.personalizedOffers();
personalizedOffers.init({
account: {
customerId: "16CRibUdE6pV6HoU"
},
externalUserId: "jane_doe",
firstName: "Jane",
lastName: "Doe",
roles: ['sales-manager'],
custom: {
viewCount: 6
}
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Retention ✨
Previous Version
The earlier version of Retention was delivered via Brightback.js, which has since been deprecated. You can still view the documentation (opens new window) for reference.
# chargebee.cancelPage()
Returns a Cancel Page instance used used to control and integrate cancel pages during subscription cancellation flows.
# Example
const cancelPage = await chargebee.cancelPage();
# Parameters
None.
# Returns
Object representing a Cancel Page instance.
# Example
<a id="cb-cancel" href="https://app.yourcompany.com/cancel" class="btn btn-danger">
Cancel Subscription
</a>
2
3
const chargebee = window.Chargebee.init({
site: "YOUR-CHARGEBEE-SUBDOMAIN",
})
const cancelPage = await chargebee.cancelPage();
cancelPage.attachCancelHandler({
account: {
customerId: "16CRibUdE6pV6HoU",
firstPurchaseDate: "2024-06-26"
},
externalUserId: "jane_doe",
firstName: "Jane",
lastName: "Doe",
saveReturnUrl: "https://app.yourcompany.com/save?id=jane_doe",
cancelConfirmationUrl: "https://app.yourcompany.com/cancel_confirm?id=jane_doe",
custom: {
emailCount: 4208
}
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Payments
Use the below functions to use payment integrations on your website.
# chargebee.load(moduleName)
This function is used to load the given module.
# Syntax
chargebee.load(moduleName)
# Parameters
components
3ds-handler
functions
ideal
sofort
google-pay
bancontact
giropay
dotpay
paypal
netbanking_emandates
apple-pay
upi
# Return Value
Returns a promise that initializes and resolves to the corresponding module handler.
# Example
chargebee.load('components').then(() => {
// components can be mounted
});
2
3
# chargebee.load3DSHandler()
Use the load3DSHandler
method to load and initialize the 3DS Helper module. Then use the threeDSHandler
to initiate 3DS flow.
This function loads the 3ds-helper module and initializes 3DS handler object.
# Syntax
chargebee.load3DSHandler()
# Return value
Promise that resolves 3DS handler object.
# Example
chargebee.load3DSHandler().then((threeDSHandler) => {
// your code here
});
2
3
# chargebee.create3DSHandler()
You can create a 3DS handler object using this method as well. 3ds-handler
module should be loaded prior to using this method.
# Syntax
chargebee.create3DSHandler();
# Return value
# Example
chargebee.load('3ds-handler').then(() => {
let threeDSHandler = chargebee.create3DSHandler();
// Your code here
})
2
3
4
# chargebee.handlePayment(paymentMethodType, paymentOptions?)
Use this method to initiate the payment process.
# Syntax
chargebee.handlePayment(paymentMethodType, paymentOptions);
# Parameters
ideal
sofort
bancontact
giropay
dotpay
netbanking_emandates
direct_debit
# Return value
Returns a promise that resolves to an authorized payment intent object
# Example
chargebee.handlePayment('ideal', {
paymentIntent: () => {
// Make ajax call to your server to create a payment intent
return createPaymentIntent()
}
}).then(paymentIntent => {
// Use the authorized payment intent to create a subscription.
}).catch(err => {
// Payment authorization failed
})
2
3
4
5
6
7
8
9
10
# Functions
Chargebee JS supports the below functions:
- Estimates
createSubscriptionEstimate
updateSubscriptionEstimate
renewSubscriptionEstimate
- EU vat validation function
validateVat
# Estimate functions
"Estimates", as the name implies, can be used to find the estimate for performing an operation as against performing the operation itself. For example, if you want to create a new subscription or update an existing one, you can deduce the details such as the amount the customer needs to be charged for this operation, the state the subscription would be in, etc. using the estimate functions.
# chargebee.estimates.createSubscriptionEstimate(options)
Generates an estimate for the 'create subscription' operation. This is similar to the Create Subscription (opens new window) API but no subscription will be created, only an estimate for this operation is created.
# Syntax
let chargebee = Chargebee.getInstance();
chargebee.load('functions').then(() => {
const options = {...}
chargebee.estimates.createSubscriptionEstimate(options).then(data => {
// Your code here
}).catch(err => {
// Your code here
})
});
2
3
4
5
6
7
8
9
# Parameters
immediate
delayed
# Return value
Resource object (opens new window) representing estimate.
# Example
{"estimate": {
"created_at": 1517507462,
"invoice_estimate": {
"amount_due": 895,
"amount_paid": 0,
"credits_applied": 0,
"currency_code": "USD",
"date": 1517507462,
"line_item_discounts": [],
"line_item_taxes": [],
"line_items": [
{
"amount": 895,
"date_from": 1517507462,
"date_to": 1519926662,
"description": "No Trial",
"discount_amount": 0,
"entity_id": "no_trial",
"entity_type": "plan",
"id": "li___test__5SK0bLNFRFuFFZLf3",
"is_taxed": false,
"item_level_discount_amount": 0,
"object": "line_item",
"pricing_model": "per_unit",
"quantity": 1,
"tax_amount": 0,
"unit_amount": 895
},
{..}
],
"object": "invoice_estimate",
"price_type": "tax_exclusive",
"recurring": true,
"round_off_amount": 0,
"sub_total": 895,
"taxes": [],
"total": 895
},
"object": "estimate",
"subscription_estimate": {
"currency_code": "USD",
"next_billing_at": 1519926662,
"object": "subscription_estimate",
"status": "active"
}
}}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# chargebee.estimates.updateSubscriptionEstimate(options)
Generates an estimate for the 'update subscription' operation. While the input is similar to the Update Subscription (opens new window) API, the subscription is not updated and only an estimate is created.
# Syntax***
let chargebee = Chargebee.getInstance();
chargebee.load('functions').then(() => {
const options = {...}
chargebee.estimates.updateSubscriptionEstimate(options).then(data => {
// Your code here
}).catch(err => {
// Your code here
})
});
2
3
4
5
6
7
8
9
# Parameters
immediate
delayed
# Return value
Resource object (opens new window) representing estimate.
# Example
{"estimate": {
"created_at": 1517507463,
"credit_note_estimates": [
{
"amount_allocated": 895,
"amount_available": 0,
"currency_code": "USD",
"line_item_discounts": [],
"line_item_taxes": [],
"line_items": [
{
"amount": 895,
"date_from": 1517507463,
"date_to": 1519926663,
"description": "No Trial - Prorated Credits for 01-Feb-2018 - 01-Mar-2018",
"discount_amount": 0,
"entity_id": "no_trial",
"entity_type": "plan",
"id": "li___test__5SK0bLNFRFuFFhhfS",
"is_taxed": false,
"item_level_discount_amount": 0,
"object": "line_item",
"pricing_model": "per_unit",
"quantity": 1,
"subscription_id": "__test__5SK0bLNFRFuFFekfI",
"tax_amount": 0,
"unit_amount": 895
},
{..}
],
"object": "credit_note_estimate",
"price_type": "tax_exclusive",
"reference_invoice_id": "__demo_inv__6",
"round_off_amount": 0,
"sub_total": 895,
"taxes": [],
"total": 895,
"type": "adjustment"
},
{..}
],
"invoice_estimate": {
"amount_due": 1500,
"amount_paid": 0,
"credits_applied": 0,
"currency_code": "USD",
"date": 1517507463,
"line_item_discounts": [],
"line_item_taxes": [],
"line_items": [
{
"amount": 1500,
"date_from": 1517507463,
"date_to": 1519926663,
"description": "Plan1 - Prorated Charges",
"discount_amount": 0,
"entity_id": "plan1",
"entity_type": "plan",
"id": "li___test__5SK0bLNFRFuFFhbfQ",
"is_taxed": false,
"item_level_discount_amount": 0,
"object": "line_item",
"pricing_model": "per_unit",
"quantity": 1,
"subscription_id": "__test__5SK0bLNFRFuFFekfI",
"tax_amount": 0,
"unit_amount": 1500
},
{..}
],
"object": "invoice_estimate",
"price_type": "tax_exclusive",
"recurring": true,
"round_off_amount": 0,
"sub_total": 1500,
"taxes": [],
"total": 1500
},
"object": "estimate",
"subscription_estimate": {
"currency_code": "USD",
"id": "__test__5SK0bLNFRFuFFekfI",
"next_billing_at": 1519926663,
"object": "subscription_estimate",
"status": "active"
}
}}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# chargebee.estimates.renewSubscriptionEstimate(options)
This returns an estimate of the amount that will be charged when the subscription is billed next. The estimate is calculated based on the current recurring items of the subscription - plan, addons, and coupons.
# Syntax
let chargebee = Chargebee.getInstance();
chargebee.load('functions').then(() => {
const options = {...}
chargebee.estimates.renewSubscriptionEstimate(options).then(data => {
// Your code here
}).catch(err => {
// Your code here
})
});
2
3
4
5
6
7
8
9
# Parameters
# Return Value
Resource object (opens new window) representing estimate.
# Example
{"estimate": {
"created_at": 1517507462,
"invoice_estimate": {
"amount_due": 895,
"amount_paid": 0,
"credits_applied": 0,
"currency_code": "USD",
"line_item_discounts": [],
"line_item_taxes": [],
"line_items": [
{
"amount": 895,
"date_from": 1519926661,
"date_to": 1522605061,
"description": "No Trial",
"discount_amount": 0,
"entity_id": "no_trial",
"entity_type": "plan",
"id": "li___test__5SK0bLNFRFuFFNnek",
"is_taxed": false,
"item_level_discount_amount": 0,
"object": "line_item",
"pricing_model": "per_unit",
"quantity": 1,
"subscription_id": "__test__5SK0bLNFRFuFFLAed",
"tax_amount": 0,
"unit_amount": 895
},
{..}
],
"object": "invoice_estimate",
"price_type": "tax_exclusive",
"recurring": true,
"round_off_amount": 0,
"sub_total": 895,
"taxes": [],
"total": 895
},
"object": "estimate",
"subscription_estimate": {
"currency_code": "USD",
"id": "__test__5SK0bLNFRFuFFLAed",
"next_billing_at": 1519926661,
"object": "subscription_estimate",
"status": "active"
}
}}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# EU vat validation function
Chargebee validates a VAT number by sending a validation request to VAT Information Exchange System (VIES (opens new window)), a search engine owned by the European Commision.
# chargebee.vat.validateVat(options)
# Syntax
chargebee.vat.validateVat(options)
# Parameters
# Return value
Returns an object with VAT validation status and an optional message.
# Example
{
"status": "INVALID",
"message": "Customer VAT number is invalid"
}
2
3
4
# Response Properties
status
:
Validation Status | Description |
---|---|
VALID | VAT Number validation was successful and the number is valid |
INVALID | VAT Number validation failed. |
UNDETERMINED | No response from VIES, or other errors. |
message
: Describes the response received from VIES.