# How to Integrate
API-based integration can be used to integrate the below:
- Checkout
- Portal
# Setting up Chargebee JS
Inserting chargebee.js script in your application
Include the following script in your HTML page. You need to do this only once per page.
<script src="https://js.chargebee.com/v2/chargebee.js"></script>
Initializing a Chargebee instance
Inside your JavaScript code, initialize Chargebee and get 'chargebee instance' object. This object is used further to create components.
Example:
var cbInstance = Chargebee.init({
site: "site-name", // your test site
domain: "https://mybilling.acme.com" // this is an optional parameter.
// Use this, if custom domain is enabled for your site
})
2
3
4
5
6
# How to Integrate Checkout
To integrate Checkout via API, follow these steps:
- Open Checkout.
- Create a Chargebee-hosted page to accept payment details from a customer.
# Opening Chargebee Checkout
In your javascript code, use the chargebee instance to open checkout.
Learn more on openCheckout
.
Sample code
- jQuery
- vue
# Create a Chargebee-hosted page using API
Follow the steps below to create a Chargebee hosted page object:
- Create an HTTP endpoint at your server.
- Generate the hosted page object using either of the below APIs based on your use case.
Sample code
- ruby
- java
- python
- php
- .net
Sample response
{
"id": "8ajOxcuyG692GDy9yjnZ2hNM1ATugOFQl",
"type": "checkout_new",
"url": "https://yourapp.chargebee.com/pages/v3/8ajOxcuyG692GDy9yjnZ2hNM1ATugOFQl/",
"state": "created",
"embed": true,
"created_at": 1515494821,
"expires_at": 1515498421
}
2
3
4
5
6
7
8
9
# How to Integrate Portal
To integrate Portal via API, follow these steps:
- Set a portal session.
- Create and open the Self-Serve Portal.
- Create a portal session for your customer.
# Set portal session
Create a function that will return a promise and pass it to setPortalSession
method of Chargebee instance. This promise should resolve a portal session object.
- jQuery
- vue
# Create and open the Self-Serve Portal
Create
and open the self-serve portal using the portal object.
- es6
- es5
Learn more to know more about supported functions.
Logging out
After your end customers access our self-serve portal, we will maintain the session for one hour. If you want to end this session, use logout
method of Chargebee instance.
# Create a portal session using API
Follow the steps below to create a portal session for your customer:
- Create an HTTP endpoint at your server.
- Create the portal session using the portal session API. (opens new window)
Sample code
- ruby
- java
- python
- php
- .net
The portal session response JSON should look like this:
{
"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"
}
2
3
4
5
6
7
8
9
10
Managing customer payment sources
- For PCI compliance, if you want Chargebee to handle your customer payment sources, you can use manage payment sources hosted page api (opens new window)
- You can use chargebee.js to open it in a modal box. Check this code sample (opens new window).