# Chargebee Instance Object
Use chargebee instance object for enabling payment integrations, analytics like Google Analytics and Facebook pixel tracking, checkout and portal integrations, and more.
To ensure that you receive all the callbacks which you set, do whitelist your domain in Chargebee. You can whitelist your domain here, Settings > Configure Chargebee >Checkout & Self-Serve Portal Settings > Configuration > Advanced Settings > Add domains
These are the functionalities:
- Checkout and Portal Integration (Drop-in and API)
- Card Components
- Payment Integrations
- Functions
# Checkout and Portal Integration
Use the functions below to use and modify Checkout and Portal integrations on your website.
# setBusinessEntity
Sets the business entity context for checkout.
# Syntax
cbInstance.setBusinessEntity(businessEntityId)
# Parameters
# Return value
This function returns a promise.
# Example
const cbInstance = Chargebee.init({
site: "moments-test",
isItemsModel: true, // Product catalog 2.0
});
cbInstance.setBusinessEntity("acme-inc-us");
const cart = cbInstance.getCart();
const product = cbInstance.initializeProduct("silver-USD-monthly");
cart.replaceProduct(product);
cart.proceedToCheckout();
2
3
4
5
6
7
8
9
# openCheckout
This function is used to open checkout page.
# Syntax
cbInstance.openCheckout(options)
# Parameters
# Return value
NA
# Example
cbInstance.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.
# getCart
This function retrieves the cart object.
# Syntax
const cart = cbInstance.getCart();
# Return value
# Example
const cbInstance = Chargebee.init({
site: "moments-test",
isItemsModel: true, // Product catalog 2.0
});
cbInstance.setBusinessEntity("acme-inc-us");
const cart = cbInstance.getCart();
const product = cbInstance.initializeProduct("silver-USD-monthly");
cart.replaceProduct(product);
cart.proceedToCheckout();
2
3
4
5
6
7
8
9
# getProduct
This function retrieves the Product object associated with a checkout button.
# Syntax
cbInstance.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 = cbInstance.getProduct(subscribeButton);
2
3
# initializeProduct
This function initializes the Product object.
# Syntax
cbInstance.initializeProduct(planId, planQuantity)
# Parameters
# Return value
A function that resolves into a product object.
# Example
const product = cbInstance.initializeProduct("diamond-USD-monthly", 2);
# setCheckoutCallbacks
You can use this function to set callbacks with respect to checkout.
# Syntax
cbInstance.setCheckoutCallbacks(setterFunction);
# Parameters
# Return value
NA
# Example
cbInstance.setCheckoutCallbacks(cart => {
return {
success: (hostedPageId) => {
console.log('Successful checkout', hostedPageId)
},
error: (err) => {
console.error(err);
}
}
})
2
3
4
5
6
7
8
9
10
# createChargebeePortal
This function creates a chargebee portal instance which opens Chargebee’s self-serve portal.
# Syntax
cbInstance.createChargebeePortal()
# Return value
# Example
const cbInstance = Chargebee.getInstance();
// Set portal session from browser
cbInstance.setPortalSession(() => portalSession);
// Create chargebee portal
const cbPortal = cbInstance.createChargebeePortal()
// Open portal
cbPortal.open();
2
3
4
5
6
7
# setPortalSession
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
cbInstance.setPortalSession(setterFunction)
# Parameters
# Return value
This function returns a promise, that resolves a portal session object.
# Example
cbInstance.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
# 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
cbInstance.logout()
# closeAll
This function is used to close all Chargebee modals.
# Syntax
cbInstance.closeAll()
# setPortalCallbacks
You can use this function to set callbacks with respect to Self-serve portal.
# Syntax
cbInstance.setPortalCallbacks(callbacks);
# Parameters
# Example
cbInstance.setPortalCallbacks({
loaded: () => {
console.log('Portal loaded successfully');
},
subscriptionChanged: (data) => {
console.log('Subscription changed', data.subscription.id)
}
})
2
3
4
5
6
7
8
# Payment Integrations
Use the below functions to use payment integrations on your website.
# load
This function is used to load the given module.
# Syntax
cbInstance.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
cbInstance.load('components').then(() => {
// components can be mounted
});
2
3
# 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
cbInstance.load3DSHandler()
# Return value
Promise that resolves 3DS handler object.
# Example
cbInstance.load3DSHandler().then((threeDSHandler) => {
// your code here
});
2
3
# 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
cbInstance.create3DSHandler();
# Return value
# Example
cbInstance.load('3ds-handler').then(() => {
let threeDSHandler = cbInstance.create3DSHandler();
// Your code here
})
2
3
4
# handlePayment
Use this method to initiate the payment process.
# Syntax
cbInstance.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
cbInstance.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.
# createSubscriptionEstimate
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 cbInstance = Chargebee.getInstance();
cbInstance.load('functions').then(() => {
const payload = {...}
cbInstance.estimates.createSubscriptionEstimate(payload).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
# `updateSubscriptionEstimate
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 cbInstance = Chargebee.getInstance();
cbInstance.load('functions').then(() => {
const payload = {...}
cbInstance.estimates.updateSubscriptionEstimate(payload).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
# renewSubscriptionEstimate
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 cbInstance = Chargebee.getInstance();
cbInstance.load('functions').then(() => {
const payload = {...}
cbInstance.estimates.renewSubscriptionEstimate(payload).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.
# validateVat
# Syntax
cbInstance.vat.validateVat(payload)
# 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.