More Tutorials

Chargebee.js Card Components integration

Chargebee.js
Payments

Set up Chargebee.js

Include the Chargebee.js script on your page and initialize a Chargebee instance before you start. For the script tag, the Chargebee.init options, and the tearDown() caveat, see Set up Chargebee.js.

Integrating Components

Following are the modes of integration for card components supported by Chargebee.js:

  • Default mode
  • Fields mode

Learn more about components and fields.

Using card component in the Default mode

Adding HTML

Create a container element in your HTML to mount the Component. For Angular, Vue and React, use the respective Components provided by us.

  <div class="ex2-fieldset">
    <label class="ex2-field">
      <span id="card-combined" class="ex2-input"></span>
    </label>
  </div>

Adding JavaScript

You need to write JavaScript code to mount Chargebee Components and for tokenization(getting Chargebee's temp token)

  • Mounting
    For Angular, React and Vue, Chargebee JS wrappers would automatically mount the Components. For other types of integrations, you need to manually mount them.

  • Tokenizing

  var options = {
    icon: true,
    classes: {
      focus: 'focus',
      invalid: 'invalid',
      empty: 'empty',
      complete: 'complete',
    }
  }

  cbInstance.load("components").then(() => {
    // Mounting Card component
    var cardComponent = cbInstance.createComponent("card", options).at("#card-combined");
    cardComponent.mount();

    $("#payment").on("submit", function(event) {
      event.preventDefault();

      // Tokenization
      // Use the card component instance to tokenize the card details
      // Additional details can be passed to the tokenize method
      cardComponent.tokenize().then(data => {
        var token = data.token;
        // Send ajax call to create a subscription or to create a card payment source
      }).catch(error => {
        console.error(error);
      });
    });
  });

Using card component in the Fields mode

Adding HTML

Create container elements in your HTML to mount Chargebee components. For Angular, Vue and React, the corresponding wrappers will create the container elements for you.

  <div class="ex1-fieldset">
    <div class="ex1-field">
        <input class="ex1-input" type="text" placeholder="John Doe" >
        <label class="ex1-label">Name on Card</label><i class="ex1-bar"></i>
    </div>                          
    <div class="ex1-field">
        <!-- Container element for number field -->
        <div id="card-number" class="ex1-input"></div>
        <label class="ex1-label">Card Number</label><i class="ex1-bar"></i>
    </div>
    <div class="ex1-fields">
      <div class="ex1-field">        
        <!-- Container element for expiry field -->
        <div id="card-expiry" class="ex1-input"></div>
        <label class="ex1-label">Expiry</label><i class="ex1-bar"></i>
      </div>
      <div class="ex1-field">
        <!-- Container element for CVV field -->
        <div id="card-cvc" class="ex1-input"></div>
        <label class="ex1-label">CVC</label><i class="ex1-bar"></i>
      </div>
    </div>
  </div>

Adding JavaScript

You need to write JavaScript code to mount all fields and for tokenization(getting Chargebee's temp token)

  • Mounting
    For Angular, React and Vue, Chargebee js wrappers would automatically mount the component. For other types of integrations, you need to manually mount the component.

  • Tokenizing

  var options = {
    icon: true,
    classes: {
      focus: 'focus',
      invalid: 'invalid',
      empty: 'empty',
      complete: 'complete',
    }
  }

  cbInstance.load("components").then(() => {
    // Mounting part
    var cardComponent = cbInstance.createComponent("card", options);
    cardComponent.createField("number").at("#card-number");
    cardComponent.createField("expiry").at("#card-expiry");
    cardComponent.createField("cvv").at("#card-cvc");
    cardComponent.mount();

    $("#payment").on("submit", function(event) {
      event.preventDefault();
      // Tokenization part
      cardComponent.tokenize().then(data => {
        var token = data.token;
        // Send ajax call to create a subscription or to create a card payment source
      }).catch(error => {
        console.log(error);
      });
    });
  });

How it works

Card Components support two payment flows. Use tokenization when the payment does not need Strong Customer Authentication, and 3D Secure when it does.

Tokenization

Tokenization workflow without 3D Secure

3D Secure

Card Components workflow with 3D Secure

Processing Payments

Payments can be processed using card components and fields in two ways:

  • Tokenization
  • 3D Secure

Learn more about tokenization and 3D Secure workflows.

Tokenization

To perform tokenization, enter card details and initiate tokenization using tokenize. Create a subscription using the Chargebee token returned by tokenize.

Using 3D Secure

To Perform 3D-Secure authorization, create a paymentIntent on the server-side and then pass the created paymentIntent to authorizeWith3ds function. Components & Fields internally uses the tokenize function to perform tokenization and then 3DS Helper Module to perform 3DS authorization.

The authorized paymentIntent can then be used to create a subscription, create a payment source, etc.

...
    $("#payment").on("submit", function(event) {
      event.preventDefault();
      ...
      // Perform 3D Secure authorization
      cardComponent.authorizeWith3ds(paymentIntent, additionalData, callbacks).then(paymentIntent => {
        // Send ajax call to create a subscription or to create a card payment source using the paymentIntent ID
      }).catch(error => {
        console.log(error);
      });
    });
...

Examples

See a live example of Card Components, or browse the sample code on GitHub for jQuery, Angular, React, and Vue.

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.