More Tutorials
Published on

How to use Chargebee self-serve portal widgets?

Self-serve portal

Introduction

In this tutorial, we will explore how to utilize Chargebee's self-serve portal widgets to enable your customers to manage their accounts and subscriptions seamlessly. By implementing the Single Sign-On (SSO) option, you can create new sessions for customers who are already logged into your app, allowing them to access and manage their subscriptions directly through the Chargebee self-serve portal.

Head to the available widgets section in our Portal document to see a demo of how this would look.

Prerequisites

For this tutorial, ensure that the following configurations are set in your Chargebee site:

  1. Enable self-serve portal. Refer to this document for details.
  2. Select Via Single Sign On API for the Customers can access the self-serve portal setting.
Single Sign-On configuration for Chargebee Self-Serve Portal
Single Sign-On configuration for Chargebee Self-Serve Portal
  1. Ensure that you have a valid customer in Chargebee.

Client side implementation

  1. Load Chargebee JS in the <head> section of your HTML.
  2. Initiate Chargebee instance on page load.
  3. When the user clicks on the button, call the setPortalSession method in Chargebee instance.
  4. Call your API (/create-portal-session endpoint in the below example) to get a Chargebee Portal Session object and return that to setPortalSession method. An example of the server side implementation is given in the Server side implementation section.
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://js.chargebee.com/v2/chargebee.js"></script>
  </head>

  <body>
    <h3>Chargebee Portal Widgets</h3>
    <button id="chargebee-portal-btn">Open Chargebee Portal</button>
  </body>
  <script>
    window.addEventListener("DOMContentLoaded", (event) => {
      Chargebee.init({
        site: "your-chargebee-site"
      });

      const cbInstance = Chargebee.getInstance();

      function onOpenPortal(e) {
        cbInstance.setPortalSession(function () {
          // `create-portal-session` would be an endpoint in your server which should call the Chargebee API to create a portal session
          // for the customer and return the Chargebee Portal Session object.
          // Reference API Doc — https://apidocs.chargebee.com/docs/api/portal_sessions?lang=curl#create_a_portal_session
          return fetch("/create-portal-session", {
            method: "POST",
            headers: {
              "Content-Type": "application/json"
            }
          }).then((response) => response.json());
        });

        let cbPortal = cbInstance.createChargebeePortal();

        // List of all the available options for sectionType can be found at https://www.chargebee.com/checkout-portal-docs/api.html#chargebee-object
        cbPortal.openSection({
          sectionType: Chargebee.getPortalSections().PAYMENT_SOURCES
        });
      }

      document
        .getElementById("chargebee-portal-btn")
        .addEventListener("click", onOpenPortal);
    });
  </script>
</html>

Server side implementation

Assuming that the user has signed in to your app and you have the customer ID of the user, the same as the one in Chargebee, you can create a portal session for this user by passing their customer ID. Inside the request handler for /create-portal-session, call the Chargebee API to create portal session and return the portal session object as the response. Sample code using Chargebee SDK in different programming lanaguages is given below:

import { ChargeBee } from "chargebee-typescript";

const chargebee = new ChargeBee();

chargebee.configure({
  site: "<YOUR_SITE_NAME>",
  api_key: "<YOUR_API_KEY>",
});
const response = await chargebee.portal_session
  .create({
    customer_id: "<Customer ID>",
    redirect_url: "<https://yourdomain.com/users/12345>"
  })
  .request();
return response.portal_session;

Conclusion

With the above implementation on the server and client side, you can provide an option for your customers to navigate straight to different sections of the self-serve portal.

Was this tutorial helpful ?
Need more help?

We're always happy to help you with any questions you might have!

support@chargebee.com

In this Page