70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
(function () {
|
|
var root = document.querySelector('[data-done]');
|
|
if (!root || !window.BX || !BX.ajax || !BX.ajax.runAction) return;
|
|
var button = root.querySelector('[data-done-pay]');
|
|
if (!button) return;
|
|
var error = root.querySelector('[data-done-error]');
|
|
|
|
function busy(on) {
|
|
button.disabled = on;
|
|
if (!window.VinoBusy) return;
|
|
if (on) VinoBusy.show(root.getAttribute('data-text-busy'));
|
|
else VinoBusy.hide();
|
|
}
|
|
|
|
function fail() {
|
|
busy(false);
|
|
error.textContent = root.getAttribute('data-text-fail');
|
|
error.hidden = false;
|
|
}
|
|
|
|
function openWidget(widget) {
|
|
var start = function () {
|
|
if (!window.halyk || !halyk.pay) return fail();
|
|
halyk.pay(widget.payment);
|
|
busy(false);
|
|
};
|
|
if (window.halyk && halyk.pay) return start();
|
|
var script = document.createElement('script');
|
|
script.src = widget.script;
|
|
script.onload = start;
|
|
script.onerror = fail;
|
|
document.head.appendChild(script);
|
|
}
|
|
|
|
function pay() {
|
|
busy(true);
|
|
error.hidden = true;
|
|
BX.ajax.runAction('jinove:vinoteca.order.pay', {
|
|
data: {
|
|
id: root.getAttribute('data-id'),
|
|
hash: root.getAttribute('data-hash'),
|
|
base: root.getAttribute('data-base') || '/'
|
|
}
|
|
}).then(function (res) {
|
|
var data = res && res.data;
|
|
if (data && data.redirect) {
|
|
window.location.href = data.redirect;
|
|
return;
|
|
}
|
|
if (data && data.widget) {
|
|
openWidget(data.widget);
|
|
return;
|
|
}
|
|
throw new Error('pay');
|
|
}).catch(fail);
|
|
}
|
|
|
|
window.addEventListener('pageshow', function (e) {
|
|
if (e.persisted) busy(false);
|
|
});
|
|
|
|
button.addEventListener('click', pay);
|
|
if (root.hasAttribute('data-autopay')) {
|
|
var address = new URL(window.location.href);
|
|
address.searchParams.delete('autopay');
|
|
window.history.replaceState(null, '', address.pathname + address.search + address.hash);
|
|
pay();
|
|
}
|
|
})();
|