Last Updated: 19 September 2024 | Change Log
React integration
Integrate the Access Checkout Web SDK in a React application.
Integration
You must add the SDK
checkout.js
script to your app html code and make sure this is done only once. To do this, you may want to add it in your main application or add it dynamically as part of one of your components.Before initializing the SDK, you must load the
checkout.js
script successfully. The reason is that this script creates an object on thewindow
object which is required by the initialization mechanism.You must call the SDK's
remove()
method only once as part of your component unmounting phase but when the DOM is still available. You can do this by using theuseLayoutEffect
hook and by calling theremove()
method in the cleanup function run by the hook.
Full integration example
import React, {useEffect, useLayoutEffect} from "react"; import "./CheckoutIntegrationSample.css"; function scriptAlreadyLoaded(src) { return document.querySelector(`script[src="${src}"]`); } function loadCheckoutScript(src) { return new Promise((resolve, reject) => { if (scriptAlreadyLoaded(src)) { resolve(); return; } let checkoutScript = document.createElement("script"); checkoutScript.src = src; checkoutScript.onload = resolve; checkoutScript.onerror = reject; document.head.appendChild(checkoutScript); }); } function addWorldpayCheckoutToPage() { return new Promise((resolve, reject) => { (function () { window.Worldpay.checkout.init( { id: "your-checkout-id", form: "#container", fields: { pan: { selector: "#card-pan", }, expiry: { selector: "#card-expiry", }, cvv: { selector: "#card-cvv", }, }, styles: { "input.is-valid": { "color": "green", }, "input.is-invalid": { "color": "red", }, "input.is-onfocus": { "color": "black", }, }, enablePanFormatting: true, }, function (error, checkout) { if (error) { reject(error); } else { resolve(checkout); } }, ); })(); }); } function CheckoutIntegrationSample() { const checkoutScriptUrl = "https://try.access.worldpay.com/access-checkout/v2/checkout.js"; let checkout; function generateSession () { checkout.generateSessionState( function (error, session) { if (error) { console.warn(`Failed to generate session: ${error}`); return; } const infoDiv = document.querySelector(".info"); infoDiv.innerHTML += `<div>Session retrieved is ${session}</div>`; }); } function clearForm () { checkout.clearForm(() => { document.querySelector(".info").innerHTML = ""; }); } useEffect(() => { loadCheckoutScript(checkoutScriptUrl) .then(() => { addWorldpayCheckoutToPage() .then((checkoutInstance) => { checkout = checkoutInstance; }) .catch(console.warn); }) .catch(console.warn); }, []); useLayoutEffect(() => { // Make sure to call the remove method (once) in order to deallocate the SDK from memory return () => checkout.remove(); }, []); return ( <section className="container" id="container"> <section className="card"> <section id="card-pan" className="field" /> <section className="columns"> <section> <section id="card-expiry" className="field" /> </section> <section> <section id="card-cvv" className="field" /> </section> </section> <section className="buttons"> <button className="submit" type="button" onClick={generateSession}> Generate Session </button> <button className="clear" type="button" onClick={clearForm}> Clear </button> </section> </section> <div id="info" className="info" /> </section> ); } export default CheckoutIntegrationSample;