# Embedded Checkout
While the other integration options, i.e., Drop-in Script (opens new window), Payment Link (opens new window) and Hosted Pages via API (opens new window) allow you to use checkout in your web application either as a stand-alone page or as an overlay, the Embedded Checkout allows you to embed the Chargebee Hosted Checkout within your web pages as an iFrame.
Embedding a hosted checkout involves two steps:
- Integrating Checkout: Chargebee offers multiple methods to integrate hosted checkout, i.e. via Drop-in Script, Payment Link and Hosted Pages API.
- Embedding Checkout: This step is comprised of two steps: a. Creating an HTML container for the checkout b. Mounting the checkout in the HTML container
Let us dive deeper into these steps:
Creating an HTML container: Create an html widget space to contain the checkout form. This container accommodates your checkout session on your webpage. Here is an example of an HTML container.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>HoneyComics | Pricing</title> <script src="https://js.chargebee.com/v2/chargebee.js" data-cb-site="honeycomics-v3-test"></script> </head> <body> <!-- Checkout Placeholder Element --> <div id="checkout"> <!-- Chargebee will embed checkout form here --> </div> </body> </html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14Mounting the checkout in the HTML container: Once the container is ready, mount the checkout between the
<div id="checkout">
</div>
HTML tags. Checkout can be mounted in the placeholder element by using different approaches based on the mode of integration (drop-in script using cart, URL via the payment link or hosted page via API). Below are the code samples for embedding checkout for each integration method showcasing the use of the following functions:- createCheckout(): This function creates the checkout page. This method returns a checkout object. The checkout object allows you to control the checkout instance embedded in your page.
- checkout.mount(string containerID): This function instructs the checkout to be mounted in the div element of the HTML container that you have created. The containerID to be added to this function can be retrieved from the
<div id="checkout">
tag in the HTML container that you have created in the first step.
Resizing the checkout iFrame: Ensure the iframe containing your embedded checkout page dynamically resizes to fit its content. By leveraging the resize callback function, the iframe will automatically adjust its height based on interactions within the checkout page. Learn more (opens new window) about callback functions.
cart.js
(Drop in Integration with Cart)
async function createCheckoutWithCart() {
try {
const chargebee = Chargebee.init({
site: "your-chargebee-subdomain",
publishableKey: "your-chargebee-publishable-api-key",
})
// Prepare your cart
const cbInstance = chargebee.getInstance();
const cart = cbInstance.getCart();
// Set customer information
const customer = {
first_name: "John",
last_name: "Smith",
locale: "it",
billing_address: {
first_name: "John",
last_name: "Smith",
line1: "132, My Street",
line2: "Kingston, New York",
state_code: "NY",
country: "US",
zip: "12401",
},
};
cart.setCustomer(customer);
// Initialize and configure the product
const product = cbInstance.initializeProduct("cbdemo_advanced-USD-monthly", 5);
product.isItemsModel = true;
product.setLayout('full_page');
cart.replaceProduct(product);
// Create the checkout
const checkout = await chargebee.getInstance().createCheckout({
cart,
callbacks: {
loaded: () => {
console.log("Checkout opened");
},
error: (error: any) => {
console.error("Error during checkout:", error);
},
close: () => {
console.log("Checkout closed");
},
success: (data: { id: string }) => {
console.log("Checkout success. ID:", data.id);
},
step: (value: string) => {
console.log("Checkout step:", value);
},
resize: (value: number) => {
console.log("Checkout page height:", value);
},
},
});
// Mount checkout on the container
checkout.mount('#checkout');
} catch (error) {
console.error("An error occurred:", error);
}
}
// Call the function to create and mount the checkout
createCheckoutWithCart();
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
url.js
(API)
async function createCheckoutWithApi() {
try {
const response = await fetch("/create-checkout", {
method: "POST",
});
const { url } = response.json();
const chargebee = Chargebee.init({
site: "your-chargebee-subdomain",
publishableKey: "your-chargebee-publishable-api-key",
})
const checkout = await chargebee.getInstance().createCheckout({
url,
callbacks: {
loaded: () => {
console.log("Checkout opened");
},
error: (error: any) => {
console.error("Error during checkout:", error);
},
close: () => {
console.log("Checkout closed");
},
success: (data: { id: string }) => {
console.log("Checkout success. ID:", data.id);
},
step: (value: string) => {
console.log("Checkout step:", value);
},
resize: (value: number) => {
console.log("Checkout page height:", value);
},
},
});
// Mount checkout on the container
checkout.mount('#checkout');
} catch (error) {
console.error("An error occurred:", error);
}
}
// Call the function to create and mount the checkout
createCheckoutWithApi();
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
url.js
(Payment Links)
async function createCheckoutWithPaymentLink() {
try {
const paymentLink = "";
const chargebee = Chargebee.init({
site: "your-chargebee-subdomain",
publishableKey: "your-chargebee-publishable-api-key",
})
const checkout = await chargebee.getInstance().createCheckout({
url: paymentLink,
callbacks: {
loaded: () => {
console.log("Checkout opened");
},
error: (error: any) => {
console.error("Error during checkout:", error);
},
close: () => {
console.log("Checkout closed");
},
success: (data: { id: string }) => {
console.log("Checkout success. ID:", data.id);
},
step: (value: string) => {
console.log("Checkout step:", value);
},
resize: (value: number) => {
console.log("Checkout page height:", value);
},
},
});
// Mount checkout on the container
checkout.mount('#checkout');
} catch (error) {
console.error("An error occurred:", error);
}
}
// Call the function to create and mount the checkout
createCheckoutWithPaymentLink();
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
# Managing the Embedded Checkout via Callbacks
Callbacks serve as dynamic functions triggering additional actions following specific events within the checkout flow. By using Callbacks, you can seamlessly incorporate supplementary functions, customize event triggers, and gain precise control over navigation and transitions. This flexibility extends to dynamic error handling and cross-platform compatibility, ensuring a smooth and consistent checkout experience.
Learn more (opens new window) about callback functions.