airwallex-payment-elements
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',
});
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',
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:
-
Formatting card information automatically as it’s entered
-
Customize placeholders to fit your UX design
-
Using responsive design to fit the width of your customer’s screen or mobile device
-
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:
-
You can arrange with any order you want
-
Show and hide anyone of them at any moment you want
-
Customize the look and feels as you want
-
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: {
id: 'replace-with-your-intent-id',
client_secret: 'replace-with-your-client-secret'
}
});
dropInElement.mount('dropIn');
window.addEventListener('onSuccess', (event: ElementEvent) => {
});
Card integration
Html template
<div id='card'></div>
Client side script
import { createElement } from 'airwallex-payment-elements';
const cardElement = createElement('card', {
intent: {
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 || {};
} catch (err) {
const { reason } = err;
const { error: { code, message } } = reason || {};
}
Full Featured Card integration
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: {
id: 'replace-with-your-intent-id',
client_secret: 'replace-with-your-client-secret'
}
});
cardElement.mount('full-featured-card');
window.addEventListener('onSuccess', (event: ElementEvent) => {
});
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';
const cardNumber = createElement('cardNumber');
cardNumber.mount('cardNumber');
const expiry = Airwallex.createElement('expiry');
expiry.mount('expiry');
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 || {};
} catch (err) {
const { reason } = err;
const { error: { code, message } } = reason || {};
}
APM (wechat) integration
Html template
<div id='wechat'></div>
Client side script
import { createElement } from 'airwallex-payment-elements';
const wechat = createElement('wechat', {
intent: {
id: 'replace-with-your-intent-id',
client_secret: 'replace-with-your-client-secret'
}
});
wechat.mount('wechat');
window.addEventListener('onSuccess', (event: ElementEvent) => {
});
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';
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 || {};
} catch (err) {
const { reason } = err;
const { error: { code, message } } = reason || {};
}
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>
<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', () => {
Airwallex.redirectToCheckout({
env: 'demo',
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>
<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>
Airwallex.init({
env: 'demo',
origin: window.location.origin,
});
const dropIn = Airwallex.createElement('dropIn', {
intent: {
id: 'replace-with-your-intent-id',
client_secret: 'replace-with-your-client-secret'
}
});
dropIn.mount('dropIn');
window.addEventListener('onSuccess', (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>
<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>
Airwallex.init({
env: 'staging',
origin: window.location.origin,
});
const card = Airwallex.createElement('card');
card.mount('card');
document.getElementById('submit').addEventListener('click', () => {
Airwallex.confirmPaymentIntent({
element: card,
id: 'replace-with-your-intent-id',
client_secret: 'replace-with-your-client-secret'
}).then((response) => {
alert(JSON.stringify(response));
});
});
window.addEventListener('onReady', (event) => {
alert(event.detail)
})
</script>
</body>
</html>
Full Featured Card integration
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>
<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>
Airwallex.init({
env: 'demo',
origin: window.location.origin,
});
const card = Airwallex.createElement('fullFeaturedCard', {
intent: {
id: 'replace-with-your-intent-id',
client_secret: 'replace-with-your-client-secret'
}
});
card.mount('full-featured-card');
window.addEventListener('onSuccess', (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>
<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>
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: 'replace-with-your-intent-id',
client_secret: 'replace-with-your-client-secret'
}).then((response) => {
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>
<script src="https://checkout.airwallex.com/assets/bundle.0.0.xx.min.js"></script>
</head>
<body>
<script>
const id = 'replace-with-your-intent-id';
const client_secret = 'replace-with-your-client-secret';
</script>
<h1>Airwallex checkout integration</h1>
<hr>
<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>
<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>
<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) => {
alert(JSON.stringify(event.detail));
})
</script>
<hr>
<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) => {
alert(JSON.stringify(response));
});
});
</script>
<hr>
</body>
</html>
Below are the live demo site you can play with
Staging Demo
LINK
Demo
LINK