2020-07-02 13:02:23 +03:00
|
|
|
function onboardingCallback(authCode, sharedId) {
|
2020-09-24 10:29:09 +03:00
|
|
|
const sandboxSwitchElement = document.querySelector('#ppcp-sandbox_on')
|
2020-08-31 09:37:15 +03:00
|
|
|
fetch(
|
|
|
|
PayPalCommerceGatewayOnboarding.endpoint,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(
|
|
|
|
{
|
|
|
|
authCode: authCode,
|
|
|
|
sharedId: sharedId,
|
2020-09-24 10:29:09 +03:00
|
|
|
nonce: PayPalCommerceGatewayOnboarding.nonce,
|
|
|
|
env: sandboxSwitchElement && sandboxSwitchElement.checked ? 'sandbox' : 'production'
|
2020-08-31 09:37:15 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.then( response => response.json() )
|
|
|
|
.then(
|
|
|
|
(data) => {
|
|
|
|
if (data.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
alert( PayPalCommerceGatewayOnboarding.error )
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 09:58:28 +03:00
|
|
|
|
2020-09-24 12:50:35 +03:00
|
|
|
/**
|
|
|
|
* Since the PayPal modal will redirect the user a dirty form
|
|
|
|
* provokes an alert if the user wants to leave the page. Since the user
|
|
|
|
* needs to toggle the sandbox switch, we disable this dirty state with the
|
|
|
|
* following workaround for checkboxes.
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
const checkBoxOnClick = (event) => {
|
|
|
|
const value = event.target.checked;
|
2020-09-24 13:40:43 +03:00
|
|
|
if (event.target.getAttribute('id') === 'ppcp-sandbox_on') {
|
|
|
|
toggleConnectButtons(! value);
|
|
|
|
}
|
2020-09-24 12:50:35 +03:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
setTimeout( () => {
|
|
|
|
event.target.checked = value;
|
|
|
|
},1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-24 13:40:43 +03:00
|
|
|
const toggleConnectButtons = (showProduction) => {
|
|
|
|
if (showProduction) {
|
|
|
|
document.querySelector('#connect-to-production').style.display = '';
|
|
|
|
document.querySelector('#connect-to-sandbox').style.display = 'none';
|
|
|
|
return;
|
2020-09-24 09:58:28 +03:00
|
|
|
}
|
2020-09-24 13:40:43 +03:00
|
|
|
document.querySelector('#connect-to-production').style.display = 'none';
|
|
|
|
document.querySelector('#connect-to-sandbox').style.display = '';
|
|
|
|
}
|
2020-09-24 09:58:28 +03:00
|
|
|
|
|
|
|
(() => {
|
|
|
|
const sandboxSwitchElement = document.querySelector('#ppcp-sandbox_on');
|
|
|
|
if (sandboxSwitchElement) {
|
2020-09-24 13:40:43 +03:00
|
|
|
toggleConnectButtons(! sandboxSwitchElement.checked);
|
2020-09-24 09:58:28 +03:00
|
|
|
}
|
2020-09-24 12:50:35 +03:00
|
|
|
|
|
|
|
document.querySelectorAll('#mainform input[type="checkbox"]').forEach(
|
|
|
|
(checkbox) => {
|
|
|
|
checkbox.addEventListener('click', checkBoxOnClick);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-09-24 09:58:28 +03:00
|
|
|
})();
|