Socket
Socket
Sign inDemoInstall

airwallex-payment-elements

Package Overview
Dependencies
1
Maintainers
3
Versions
161
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    airwallex-payment-elements

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)


Version published
Weekly downloads
6.7K
decreased by-10.6%
Maintainers
3
Created
Weekly downloads
 

Readme

Source

airwallex-payment-elements

semantic-release

This universal library is a lightweight javascript SDK, which allow merchant site to convenient integrate with airwallex checkout flow

From the merchant perspective, The Hosted Payment Page(HPP) / Drop-in / Elements integration happen after shopper decide to checkout an order on merchant's website, Airwallex javascript SDK help merchant accept payments on merchant website, the whole user experience include create an PaymentIntent entity to tacking the payment request, which link to merchant’s order entity, Airwallex using a paymentAttempt entity to collect payment methods details from a shopper, and link back to the PaymentIntent entity, which tracking the state change all the way through user interactions, you can checkout the PaymentIntent section for more details

With the API integration from merchant server, after PaymentIntent is successfully created, the response of the API call will return a unique ID for the PaymentIntent, and a client_secret which act as a single source of truth cross over the shopping experience, each time user's interaction trigger a service call Airwallex client API will validate against the client_secret to secure the whole payment flow

So long story short, the merchant website checkout start with PaymentIntent and client_secret

Below are the step by step guide to use this library

Integrate with universal NPM package

Installation

Install airwallex-payment-elements package

yarn add airwallex-payment-elements

OR with NPM:

npm install airwallex-payment-elements

Getting Started

Initialize

import { loadAirwallex } from 'airwallex-payment-elements';
await loadAirwallex({
  env: 'demo', // 'staging' | 'demo' | 'prod'
});

options pass to loadAirwallex is useful to customize looks and feel of the checkout form when you are adopt Drop-in / Elements integration

Host Payment Page (HPP) integration

The HPP provide the most convenient way to integrate

import { redirectToCheckout } from 'airwallex-payment-elements';
redirectToCheckout({
  env: 'demo', // 'staging' | 'demo' | 'prod'
  id: 'replace-with-your-intent-id',
  client_secret: 'replace-with-your-client-secret'
}

DropIn / Card / FullFeaturedCard / Card elements / APM Elements integration

The primary integration path through this library is with Elements, which enables merchant to collect sensitive payment information(PCI) using customizable UI elements. this library also provides a single interface for Payment Request API

With this library, you can also tokenize sensitive information, and handle 3D Secure authentication

Airwallex element is a set of prebuilt UI components, like inputs and buttons, for building your checkout flow. It’s available as a feature of this library. this library handle checkout confirm within an Element without ever having it touch your server

Elements includes features like:

  1. Formatting card information automatically as it’s entered

  2. Customize placeholders to fit your UX design

  3. Using responsive design to fit the width of your customer’s screen or mobile device

  4. Customizing the styling to match the look and feel of your checkout flow

Create dropIn | card | fullFeaturedCard | cardNumber | expiry | cvc | wechat | paymentRequestButton | elements

When you select dropIn element type you are actually adopt Drop-in integration

With Drop-in integration, those prebuilt UI components has been composed into a payment form with all available payment methods, you can put it directly into you html page, and integrate with the rest of your own components to render your checkout page.

With cardNumber | expiry | cvc | paymentRequestButton | wechat elements, they are just building blocks:

  1. You can arrange with any order you want

  2. Show and hide anyone of them at any moment you want

  3. Customize the look and feels as you want

  4. Listen on the events and interact with shoppers when they click or typing in those elements

DropIn integration

Html template
<div id='dropIn'></div>
Client side script
import { createElement } from 'airwallex-payment-elements';
const dropInElement = createElement('dropIn', {
  intent: { // Required, dropIn use intent Id and client_secret to prepare checkout
    id: 'replace-with-your-intent-id',
    client_secret: 'replace-with-your-client-secret'
  }
});
dropInElement.mount('dropIn');
window.addEventListener('onSuccess', (event: ElementEvent) => {
  // Handle checkout success event
});

Card integration

Html template
<div id='card'></div>
Client side script

import { createElement } from 'airwallex-payment-elements';
const cardElement = createElement('card', {
  intent: { // Required, Card use intent Id and client_secret to prepare checkout
    id: 'replace-with-your-intent-id',
    client_secret: 'replace-with-your-client-secret'
  }
});
cardElement.mount('card');
try {
  const res = await confirmPaymentIntent({
    element: cardElement,
    id: 'replace-with-your-intent-id',
    client_secret: 'replace-with-your-client-secret',
  });
  const { confirmResult } = res || {};
  const { id, status } = confirmResult || {};
  // Handle success
} catch (err) {
  const { reason } = err;
  const { error: { code, message } } = reason || {};
  // Handle fail
}

What's different from card is that full featured card contains payment button so that you don't need to confirm payment intent yourself.

Html template
<div id='fullFeaturedCard'></div>
Client side script
import { createElement } from 'airwallex-payment-elements';
const cardElement = createElement('fullFeaturedCard', {
  intent: { // Required, Card use intent Id and client_secret to prepare checkout
    id: 'replace-with-your-intent-id',
    client_secret: 'replace-with-your-client-secret'
  }
});
cardElement.mount('full-featured-card');
window.addEventListener('onSuccess', (event: ElementEvent) => {
  // Handle checkout success event
});

Card elements integration

Html template
<label>
  Card number
  <div id='cardNumber'></div>
</label>
<label>
  Expiry
  <div id='expiry'></div>
</label>
<label>
  CVC
  <div id='cvc'></div>
</label>
Client side script
import { createElement, confirmPaymentIntent } from 'airwallex-payment-elements';

// Create and mount 'cardNumber' element
const cardNumber = createElement('cardNumber');
cardNumber.mount('cardNumber');

// Create and mount 'expiry' element
const expiry = Airwallex.createElement('expiry');
expiry.mount('expiry');

// Create and mount 'cvc' element
const cvc = Airwallex.createElement('cvc');
cvc.mount('cvc');

try {
  const res = await confirmPaymentIntent({
    element: cardNumber,
    id: 'replace-with-your-intent-id',
    client_secret: 'replace-with-your-client-secret',
  });
  const { confirmResult } = res || {};
  const { id, status } = confirmResult || {};
  // Handle success
} catch (err) {
  const { reason } = err;
  const { error: { code, message } } = reason || {};
  // Handle fail
}

APM (wechat) integration

Html template
<div id='wechat'></div>
Client side script
import { createElement } from 'airwallex-payment-elements';

// Create and mount 'wechat' element
const wechat = createElement('wechat', {
  intent: { // Required, dropIn use intent Id and client_secret to prepare checkout
    id: 'replace-with-your-intent-id',
    client_secret: 'replace-with-your-client-secret'
  }
});
wechat.mount('wechat');

window.addEventListener('onSuccess', (event: ElementEvent) => {
  // Handle checkout success event
});

HPP / DropIn / FullFeaturedCard return customer checkout flow

Add customer_id when create payment intent and we will get the saved payment methods for the customer and redner saved cards list in the element. Nothing need to change during the integration.

Card / Card elements return customer checkout flow

You could to get saved cards list when create intent or query intent. Then render them by your self to let shopper choose one of them.

Then create a cvc element to enter cvc code and call confirmPaymentIntentWithSavedCard function to finish checkout.

Html template
<label>
  CVC
  <div id='cvc'></div>
</label>
Client side script
import { createElement, confirmPaymentIntentWithSavedCard } from 'airwallex-payment-elements';

// Create and mount 'cvc' element
const cvc = Airwallex.createElement('cvc');
cvc.mount('cvc');

try {
  const res = await confirmPaymentIntentWithSavedCard({
    id: 'replace-with-your-intent-id',
    methodId: 'replace-with-chosen-payment-method-id',
    customerId: 'replace-with-your-customer-id',
    client_secret: 'replace-with-your-client-secret',
    element: cvcElement,
  });
  const { confirmResult } = res || {};
  const { id, status } = confirmResult || {};
  // Handle success
} catch (err) {
  const { reason } = err;
  const { error: { code, message } } = reason || {};
  // Handle fail
}

For more detail please check on the interface in the package

Integrate with Universal Module Definition (UMD) javascript

Below are universal javascript in browser to get start with Airwallex checkout integration

Download and run those example from github airwallex-payment-demo

!!!Those examples only show integration with required parameters and steps, more details can be found in product doc Payment-Web integration section or in the types folder of the package inside node_modules after npm install airwallex-payment-elements command

Hosted payment page (HPP) integration

<!doctype html>
<html>
  <head lang='en'>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>Airwallex Checkout Playground</title>
    <!-- Step #1: Load Checkout Universal Module Definition (UMD) bundle js-->
    <script src="https://checkout.airwallex.com/assets/bundle.0.0.xx.min.js"></script>
  </head>
  <body>
    <h1>Hosted payment page (HPP) integration</h1>
    <button id='hpp'>Redirect to HPP</button>
    <script>
      document.getElementById('hpp').addEventListener('click', () => {
        // Step #2: Call the HPP function to checkout 
        Airwallex.redirectToCheckout({
          env: 'demo', // Which env('staging' | 'demo' | 'prod') you would like to integrate with
          id: 'replace-with-your-intent-id',
          client_secret: 'replace-with-your-client-secret'
        });
      })
    </script>
  </body>
</html>

DropIn integration

<!doctype html>
<html>
  <head lang='en'>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>Airwallex Checkout Playground</title>
    <!-- Step #1: Load Checkout Universal Module Definition (UMD) bundle js-->
    <script src="https://checkout.airwallex.com/assets/bundle.0.0.xx.min.js"></script>
  </head>
  <body>
    <h1>DropIn integration</h1>
    <div id='dropIn'></div>
    <script>
      // Step #2: Initialize the Airwallex global context for event communication
      Airwallex.init({
        env: 'demo', // Setup which env('staging' | 'demo' | 'prod') you would like to integrate with
        origin: window.location.origin, // Setup your event target to receive the browser events message
      });
      // Step #3: Create 'dropIn' element
      const dropIn = Airwallex.createElement('dropIn', {
        intent: { // Required, dropIn use intent Id and client_secret to prepare checkout
          id: 'replace-with-your-intent-id',
          client_secret: 'replace-with-your-client-secret'
        }
      });
      // Step #4: Mount 'dropIn' element
      dropIn.mount('dropIn');
      window.addEventListener('onSuccess', (event) => {
        // Handle event
        alert(JSON.stringify(event.detail));
      })
    </script>
  </body>
</html>

Card integration

<!doctype html>
<html>

<head lang='en'>
  <meta charset='utf-8'>
  <meta name='viewport' content='width=device-width, initial-scale=1'>
  <title>Airwallex Checkout Playground</title>
  <!-- Step #1: Load Checkout Universal Module Definition (UMD) bundle js-->
  <script src="https://checkout.airwallex.com/assets/bundle.xx.xx.xx.min.js"></script>
</head>

<body>
  <h1>Card integration</h1>
  <label>
    Card Information
    <div id='card'></div>
  </label>
  <br>
  <button id='submit'>Submit</button>
  <script>
    // Step #2: Initialize the Airwallex global context for event communication
    Airwallex.init({
      env: 'staging', // Setup which env('staging' | 'demo' | 'prod') you would like to integrate with
      origin: window.location.origin, // Setup your event target to receive the browser events message
    });
    // Step #3: Create 'card' element
    const card = Airwallex.createElement('card');
    // Step #4: Mount card element
    card.mount('card');
    document.getElementById('submit').addEventListener('click', () => {
      // Step #5: Confirm payment intent with id and client_secret
      Airwallex.confirmPaymentIntent({
        element: card,
        id: 'replace-with-your-intent-id',
        client_secret: 'replace-with-your-client-secret'
      }).then((response) => {
        /* handle confirm response in your business flow */
        alert(JSON.stringify(response));
      });
    });
    window.addEventListener('onReady', (event) => {
      /*
        ... Handle event
      */
      alert(event.detail)
    })
  </script>
</body>

</html>

What's different from card is that full featured card contains payment button so that you don't need to confirm payment intent yourself.

<!doctype html>
<html>
  <head lang='en'>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>Airwallex Checkout Playground</title>
    <!-- Step #1: Load Checkout Universal Module Definition (UMD) bundle js-->
    <script src="https://checkout.airwallex.com/assets/bundle.0.0.xx.min.js"></script>
  </head>
  <body>
    <h1>Card integration</h1>
    <div id='full-featured-card'></div>
    <script>
      // Step #2: Initialize the Airwallex global context for event communication
      Airwallex.init({
        env: 'demo', // Setup which env('staging' | 'demo' | 'prod') you would like to integrate with
        origin: window.location.origin, // Setup your event target to receive the browser events message
      });
      // Step #3: Create 'card' element
      const card = Airwallex.createElement('fullFeaturedCard', {
        intent: { // Required, use intent Id and client_secret to prepare checkout
          id: 'replace-with-your-intent-id',
          client_secret: 'replace-with-your-client-secret'
        }
      });
      // Step #4: Mount 'card' element
      card.mount('full-featured-card');
      window.addEventListener('onSuccess', (event) => {
        // Handle event
        alert(JSON.stringify(event.detail));
      })
    </script>
  </body>
</html>

Card elements integration

<!doctype html>
<html>
  <head lang='en'>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>Airwallex Checkout Playground</title>
    <!-- Step #1: Load Checkout Universal Module Definition (UMD) bundle js-->
    <script src="https://checkout.airwallex.com/assets/bundle.0.0.xx.min.js"></script>
  </head>
  <body>
    <h1>Card elements integration</h1>
    <label>
      Card number
      <div id='cardNumber'></div>
    </label>
    <label>
      Expiry
      <div id='expiry'></div>
    </label>
    <label>
      CVC
      <div id='cvc'></div>
    </label>
    <br>
    <button id='submit'>Submit</button>
    <script>
      // Step #2: Initialize the Airwallex global context for event communication
      Airwallex.init({
        env: 'demo', // Setup which env('staging' | 'demo' | 'prod') you would like to integrate with
        origin: window.location.origin, // Setup your event target to receive the browser events message
      });
      // Step #3.1: Create 'cardNumber' element
      const cardNumber = Airwallex.createElement('cardNumber');
      // Step #3.2: Create 'expiry' element
      const expiry = Airwallex.createElement('expiry');
      // Step #3.3: Create 'cvc' element
      const cvc = Airwallex.createElement('cvc');
      // Step #4: Mount card elements
      cardNumber.mount('cardNumber');
      expiry.mount('expiry');
      cvc.mount('cvc');
      document.getElementById('submit').addEventListener('click', () => {
        // Step #5: Confirm payment intent with id and client_secret
        Airwallex.confirmPaymentIntent({
          element: cardNumber,
          id: 'replace-with-your-intent-id',
          client_secret: 'replace-with-your-client-secret'
        }).then((response) => {
          // handle confirm response in your business flow
          alert(JSON.stringify(response));
        });
      });
    </script>
  </body>
</html>

Put all integration methods in one place for your choose

<!doctype html>
<html>
  <head lang='en'>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>Airwallex Checkout Playground</title>
    <!-- Step #1: Load Checkout Universal Module Definition (UMD) bundle js-->
    <script src="https://checkout.airwallex.com/assets/bundle.0.0.xx.min.js"></script>
  </head>
  <body>
    <script>
      // Define the intent id and client_secret to checkout across different integration methods
      const id = 'replace-with-your-intent-id';
      const client_secret = 'replace-with-your-client-secret';
    </script>
    <h1>Airwallex checkout integration</h1>
    <hr>
    <!-- HPP -->
    <h2>Option #1: Hosted payment page (HPP) integration</h2>
    <button id='hpp'>Redirect to HPP</button>
    <script>
      document.getElementById('hpp').addEventListener('click', () => {
        Airwallex.redirectToCheckout({
          env: 'demo',
          id,
          client_secret
        });
      })
    </script>
    <hr>
    <!-- DropIn integration-->
    <h2>Option #2: DropIn integration</h2>
    <div id='dropIn'></div>
    <script>
      Airwallex.init({
        env: 'demo',
        origin: window.location.origin,
      });
      const dropIn = Airwallex.createElement('dropIn', {
        intent: {
          id,
          client_secret
        }
      });
      dropIn.mount('dropIn');
    </script>
    <hr>
    <!-- Card integration-->
    <h2>Option #3: Card integration</h2>
    <div id='card'></div>
    <script>
      Airwallex.init({
        origin: window.location.origin,
        env: 'demo'
      });
      const card = Airwallex.createElement('card', {
        intent: {
          id,
          client_secret
        }
      });
      card.mount('card');
    </script>
    <script>
      window.addEventListener('onSuccess', (event) => {
        /*
          ... Handle event
        */
        alert(JSON.stringify(event.detail));
      })
    </script>
    <hr>
    <!-- Card elements integration-->
    <h2>Option #4: Card elements integration</h2>
    <label>
      Card number
      <div id='cardNumber'></div>
    </label>
    <label>
      Expiry
      <div id='expiry'></div>
    </label>
    <label>
      CVC
      <div id='cvc'></div>
    </label>
    <br>
    <button id='submit'>Submit</button>
    <script>
      Airwallex.init({
        env: 'demo',
        origin: window.location.origin,
      });
      const cardNumber = Airwallex.createElement('cardNumber');
      const expiry = Airwallex.createElement('expiry');
      const cvc = Airwallex.createElement('cvc');
      cardNumber.mount('cardNumber');
      expiry.mount('expiry');
      cvc.mount('cvc');
      document.getElementById('submit').addEventListener('click', () => {
        Airwallex.confirmPaymentIntent({
          element: cardNumber,
          id,
          client_secret
        }).then((response) => {
          // handle confirm response
          alert(JSON.stringify(response));
        });
      });
    </script>
    <hr>
  </body>
</html>

Below are the live demo site you can play with

Staging Demo

LINK

Demo

LINK

Keywords

FAQs

Last updated on 05 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc