On This Page
Step 3: Set Up the Client-Side Component
To add the payment interface to your e-commerce site, you need to set up the
client-side component using the
Unified Checkout
JavaScript library. This setup
involves two primary components:- The button widget, which lists available payment methods for the customer.
- The payment acceptance page, which captures payment information from the cardholder. This can be integrated with your webpage or added as a sidebar.
This example shows a complete client-side
integration:
Follow these steps to create the client-side integration:async function launchCheckout() { try { const client = await VAS.UnifiedCheckout(sessionJWT); const checkout = await client.createCheckout(); const result = await checkout.mount('#payment-buttons'); // result contains the completed payment result JWT // Send result to your server for verification sendToServer(result); } catch (error) { if (error.name === 'UnifiedCheckoutError') { handleError(error.reason, error.message); } } finally { checkout.destroy(); client.destroy(); } } launchCheckout();
- Load theUnified CheckoutJavaScript library:
ADDITIONAL INFORMATION
<script src="https://apitest.cybersource.com/uc/v1/assets/1.0.0/UnifiedCheckout.js"></script>Replace the domain with the production URL for live environments:- Test:https://apitest.cybersource.com
- Production:https://api.cybersource.com
You must include the library in your webpage's HTML. - Initialize the SDK by callingVAS.UnifiedCheckout(). This returns a client instance:
ADDITIONAL INFORMATION
const client = await VAS.UnifiedCheckout(sessionJWT);Use the JWT obtained from the server-side setup in Step 2: Set Up the Server-Side Component.Initialization validates the JWT signature, checks that the current page origin matchestargetOrigins, and prepares the SDK for use. If the JWT is invalid or expired, aUnifiedCheckoutErroris returned. - Create a checkout to render a list of available payment methods and handles the payment flow:
ADDITIONAL INFORMATION
const checkout = await client.createCheckout();When you includeautoProcessingand set it totrue,mount()returns the completed payment result:
When you include it and set it to// Explicit auto-processing const checkout = await client.createCheckout({ autoProcessing: true });false,mount()returns a transient token that you pass tocheckout.complete():
The default value of// Explicit manual processing const checkout = await client.createCheckout({ autoProcessing: false });autoProcessingustruewhen acompleteMandateis included in the session. - Mount the checkout by callingmount(). This attaches the payment UI to your page. The argument determines the display mode:
- Sidebar Mode
- The payment screen appears as an overlay sidebar. Pass a CSS selector for the payment button list, or omit it entirely:// Full sidebar — buttons and payment screen both in sidebar const result = await checkout.mount(); // Buttons embedded, payment screen in sidebar const result = await checkout.mount('#payment-buttons');
- Embedded Mode
- Both the button list and payment screen render inline within your page layout:const result = await checkout.mount({ paymentSelection: '#payment-buttons', paymentScreen: '#payment-form' });
- Mount Result
- WhenautoProcessingis enabled,mount()resolves with the completed payment result JWT once the customer finishes the payment flow.WhenautoProcessingis disabled,mount()resolves with a transient token JWT. You then callcheckout.complete()to finish the payment:const checkout = await client.createCheckout({ autoProcessing: false }); const transientToken = await checkout.mount('#payment-buttons'); // Later, complete the payment const result = await checkout.complete(transientToken);
- Unmount
- You can remove the payment UI from the page without destroying the checkout:checkout.unmount(); // Later, mount again const result = await checkout.mount('#payment-buttons');
AFTER COMPLETING THE TASK
For more information about setting up the client side, see Client-Side Set Up. For information about handling errors on
the client side, see Handle Errors. Proceed to Step 4: Configure Unified Checkout.