More Tutorials
Published on

Build a pricing table for returning users using Pricing Tables

Pricing Tables
Checkout

Introduction

This tutorial covers how you can create a pricing table for returning users using Chargebee Pricing Tables to support plan upgrades, downgrades, and so on for your customers by providing relevant inputs.

Setup

  • Connect your Chargebee account to Pricing Tables by referring to this video.

  • Customize your pricing table further by referring to these Tutorials.

Setup Client Library

Import Chargebee SDK

Download and import the client library of your choice and configure the client library with your Chargebee TEST site name and API key.

Environment.configure("your_site_domain","your_api_key");

Create a pricing table session

On the server side, use Chargebee’s pricing table session API to create a pricing table session object.

@WebServlet(name = "pricing_page_session", value = {"generate_pricing_page_session"})
public class GeneratePricingPageSession extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Result result = null;
        try {
            result = PricingPageSession.createForNewSubscription()
                    .pricingPageId("your_atomicpricing_pricing_page_id")
                    .customerId("customer_id") // new or existing customer id
                    .subscriptionId("new_subscription_id")
                    .request(new Environment("your_site_domain", "your_api_key"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        PricingPageSession pricingPageSession = result.pricingPageSession();
        // Don't add the below header in production. This is only for demo
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.setContentType("application/json");
        response.getWriter().print(pricingPageSession.jsonObj);
    }
}

Setup your frontend

On the client side, you can use the Pricing Tables object in the Chargebee.js SDK and call pricingTable.open() function. The pricingTable parameter passed to this method expects a function that returns a Promise. This Promise can be a result of an API call that creates a pricing table session (as shown in the previous step), and this promise must resolve to a pricing table session object.

<script src="https://js.chargebee.com/v2/chargebee.js" onload="onLoad()"></script>

<script>
async function onLoad() {
const chargebee = Chargebee.init({
    site: "YOUR-CHARGEBEE-SUBDOMAIN",
})
const pricingTable = await chargebee.pricingTable();
pricingTable.open({
  pricingTable: function() {
    return fetch('https://YOUR_DOMAIN/create_pricing_table', {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
    }).then(response => response.json());
  },
});
</script>

<div
  id="chargebee-pricing-table"
  data-pricing-table-integration-type="api"
></div>

Was this tutorial helpful ?
Need more help?

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

support@chargebee.com