More Tutorials
Published on

Build a pricing page for returning users using Atomic Pricing.

Atomic Pricing
Checkout

Introduction

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

Setup

  • Connect your Chargebee account to atomic pricing by referring to this video.

  • Customize your pricing page 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 page session

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

@WebServlet(name = "pricing_page_session", value = {"generate_pricing_page_session"})
public class GeneratePricingPageSession extends HttpServlet {
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();
// Dont 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 Atomic pricing JS and call openPricingPage function. The pricingPage attribute 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 page session (as shown in the previous step), and this promise must resolve to a pricing page session object.

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

<script>
  function onLoad() {
    Pricify.openPricingPage({
      pricingPage: function() {
        return fetch('http://localhost:8000/api/generate_pricing_page_session', {
          method: "POST",
          data: data,
        }).then(res => res.json()),
      },
    });
  }
</script>

<div
  id="pricify-hosted-pricing-page"
  data-pricify-integrationtype="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