const vanta_loadedChElem = '.gcpay-widget-payment-method-btn.credit'; let vanta_isContainerLoad = false; let vanta_dropdownIsShowing = false; let vanta_checkInterval = null; let vanta_attempts = 0; const vanta_maxAttempts = 20; // Максимальное число проверок (10 секунд при интервале 500 мс) const vanta_checkDelay = 500; // Интервал проверки (500 мс) function getVantaSettings(key) { const map = new Map([ ['partnerId', '1ef4358e-8b25-68dc-97ea-3723d76a0599'], ['calculatorParams', { terms: [6, 10, 12], creditType: 2 }] ]); return map.get(key) ?? null; } function vanta_checkForElement() { // Предотвращаем дублирование интервалов if (vanta_checkInterval) return; vanta_checkInterval = setInterval(() => { const element = document.querySelector(vanta_loadedChElem); if (element) { console.log('Vanta Элемент найден!'); vanta_addListenerToCredotDropdown(); clearInterval(vanta_checkInterval); vanta_checkInterval = null; } else { vanta_attempts++; console.log(`Vanta Попытка ${vanta_attempts}/${vanta_maxAttempts} — элемент не найден`); if (vanta_attempts >= vanta_maxAttempts) { console.warn('Vanta Лимит попыток исчерпан, останавливаем проверку'); clearInterval(vanta_checkInterval); vanta_checkInterval = null; } } }, vanta_checkDelay); } function vanta_blockTempl() { return `
`; } function vanta_addListenerToCredotDropdown() { document.querySelector(vanta_loadedChElem).addEventListener('click', () => { console.log('dropdown clicked!'); const installmentContainer = document.querySelector('.gcpay-widget-installment-plan-list'); if (installmentContainer) { setTimeout(() => { installmentContainer.insertAdjacentHTML('afterBegin', vanta_blockTempl()); }, 500); } }); } function vanta_getProductData() { let resultObj = {}; const priceElem = document.querySelector('.gcpay-widget .gcpay-widget-order-price'); if (priceElem) { resultObj['price'] = parseFloat(priceElem.innerText.replace(/[^+\d]/g, '')); } const orderElem = document.querySelector('.gcpay-widget .gcpay-widget-head h1 span'); if (orderElem) { resultObj['model'] = String(orderElem.innerText.replace(/[^+\d]/g, '')); resultObj['brand'] = String(`Оплата заказа ${orderElem.innerText}`); } resultObj['count'] = 1; return [resultObj]; } function vanta_callWidget(event) { event.stopPropagation(); event.preventDefault(); const currentURL = window.location.href; const calculatorParams = getVantaSettings('calculatorParams'); const goods = vanta_getProductData(); const orderId = goods[0]['model']; fetch('https://widget-api.vanta.ru/v1/widget/online', { method: 'POST', headers: { 'x-partner-id': getVantaSettings('partnerId'), 'Content-Type': 'application/json' }, body: JSON.stringify({ calculatorParams, orderId, goods, "redirectUrl": { "defaultUrl": currentURL, "successUrl": `https://${window.location.hostname}/sales/shop/dealPaid/`, "errorUrl": currentURL, }, }) }).then((response) => { return response.json(); }).then((data) => { if (data?.link) { window.location.href = data.link; } else { alert("Сервис временно недоступен. Обратитесь в тех поддержку"); } }); } vanta_checkForElement();