More Tutorials

Create a New Subscription with Chargebee Checkout

Chargebee.js
Checkout
Payments

When a new customer signs up, you can run the signup through Chargebee Checkout. Checkout is PCI DSS compliant, so sensitive card information never reaches your servers.

Honey Comics is a fictitious online comic book store that sends comic books to its subscribers every week. Customers sign up for a subscription by providing their payment details. Try the demo, then follow the steps below to build something similar.

Prerequisites

  • Sign up for a Chargebee account.
  • Create a plan with a trial period on your Chargebee test site.
  • Get the API key for your Chargebee test site.

Build the signup page

This tutorial uses a two-step checkout. Your page collects the customer's account information first, and Chargebee Checkout opens when they click subscribe.

A sample input element looks like this.

<div class="row">
  <div class="col-sm-6">
    <div class="form-group">
      <label for="company">Company</label>
      <input type="text" class="form-control" name="company">
    </div>
  </div>
</div>

Set up the client library

Download and import the client library for your language, then configure it with your Chargebee test site name and API key.

ChargeBee.configure(:site => "honeycomics-v3-test",
                     :api_key => "<full-access-key>")

Add an endpoint that returns a hosted page object

On your server, call the checkout new subscription API with the information you collected on the client. It returns a hosted page object for the new subscription.

# routes.rb
post "/api/generate_checkout_new_url" => "chargebee#checkout_new"

# controller
def checkout_new
  result = ChargeBee::HostedPage.checkout_new_for_items({
    :subscription_items => [{:item_price_id => params[:item_price_id] }],
    :customer => {:first_name => params[:first_name],
      :last_name => params[:last_name],
      :company => params[:company],
      :phone => params[:phone],
      :email => params[:email]
    },
    :embed => false
  })
  render :json => result.hosted_page.to_s
end

Open Checkout on click

On the client, openCheckout takes a hostedPage callback that returns a promise. Make it call the endpoint you created above and resolve to the hosted page object.

cbInstance.openCheckout({
  hostedPage: function() {
    // Hit your end point that returns hosted page JSON object as response
    // This sample end point will call checkout new api
    // https://apidocs.chargebee.com/docs/api/hosted_pages/create-checkout-for-a-new-subscription
    // If you want to use paypal, go cardless and plaid, pass embed parameter as false
    return $.ajax({
      method: "post",
      url: "http://localhost:8000/api/generate_checkout_new_url",
      data: $("#subscribe-form").serialize()
    });
  },
  loaded: function() {
    console.log("checkout opened");
  },
  error: function() {
    $("#loader").hide();
    $("#errorContainer").show();
  },
  close: function() {
    $("#loader").hide();
    $("#errorContainer").hide();
    console.log("checkout closed");
  },
  success: function(hostedPageId) {
    console.log(hostedPageId);
    // Hosted page id will be unique token for the checkout that happened
    // You can pass this hosted page id to your backend
    // and then call our retrieve hosted page api to get subscription details
    // https://apidocs.chargebee.com/docs/api/hosted_pages/retrieve-a-hosted-page
  },
  step: function(value) {
    // value -> which step in checkout
    console.log(value);
  }
});

The success callback receives a hosted page ID, a unique token for that checkout. Pass it to your backend and call the retrieve a hosted page API to get the subscription details.

Reference

For every openCheckout option and callback, see the Chargebee instance reference in the Chargebee.js Reference.

To upgrade an existing subscription instead of creating a new one, see Upgrade an existing subscription with Chargebee Checkout.

Was this tutorial helpful ?
Need more help?

We're always happy to help you with any questions you might have! Click here to reach out to us.