Managing the frontend 3DS loops can be done in several ways however, to keep things simple, we've added all of the necessary functions to the Javascript file you created in the previous steps
Step 1: Add a 'result' object directly to the Javascript file which contains instructions for what to show on the checkout page, if required, add the CRSF token to the checkout page (used to submit the ACS iframe)
Step 3: Add a function to create the ACS iframe with the relevant data if result object states ACS
function submitAcsForm() {
var acsContainer = document.createElement('div');
document.body.appendChild(acsContainer);
var acsIframe = document.createElement("iframe");
acsIframe.setAttribute("name", "acs_iframe");
acsIframe.setAttribute("height", "1");
acsIframe.setAttribute("width", "1");
acsIframe.setAttribute("sandbox", "allow-scripts allow-forms allow-top-navigation");
acsContainer.appendChild(acsIframe);
var acsForm = document.createElement("form");
acsForm.setAttribute("method", "post");
acsForm.setAttribute("target", "_parent");
acsForm.setAttribute("action", result.url);
acsForm.classList.add('hidden');
acsContainer.appendChild(acsForm);
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("name", "threeDSResponse");
input.value = JSON.stringify(result.post);
acsForm.appendChild(input);
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("name", "_token");
input.value = csrf;
acsForm.appendChild(input);
var button = document.createElement("button");
button.setAttribute("type", "submit");
acsForm.appendChild(button);
button.click();
}
Step 4: Add the following code to the top of your checkout.js file, CHANGE the on document load method to the one below:
//Add these to the top of the checkout.js file
var script = document.currentScript;
var result;
var csrf;
//Change the on DOMContentLoaded function to this
document.addEventListener('DOMContentLoaded', function() {
result = script.getAttribute('result');
csrf = script.getAttribute('csrf');
if(result) {
result = JSON.parse(result);
if(result) {
switch(result.status) {
case '3ds':
submit3DSForm();
break;
case 'acs':
submitAcsForm();
break;
}
}
} else {
setupCardForm();
}
});