(function () { let executed = false; function startCashback() { if (executed) return; // evita execução duplicada executed = true; const modalPayment = document.querySelector('div[data-loading="modal-payment"]'); const msgCashbackContainer = "O cashback será resgatado como um cupom de desconto que será aplicado automaticamnete, não sendo cumulativo com outros cupons."; let orderValue = 0.00; let documentNumber = ''; // Observa se o modal de pagamento foi desabilitado e se existem valores de documento e de pedido if (modalPayment) { const observer = new MutationObserver(() => { orderValue = document.querySelector('table.order-summary-table .row').innerText.replace(/[^\d]/g, "") / 100; let cpf = document.getElementById('customer-cpf').value; let cnpj = document.getElementById('customer-cnpj').value; documentNumber = (cnpj)? cnpj : cpf; if (modalPayment.classList.contains("hidden")) { if(documentNumber && orderValue > 0) { observer.disconnect(); checkProcessCashback(); console.log('Kiskadi: Verificando se é para iniciar o processo de cashback') } } }); observer.observe(modalPayment, {attributes: true, attributeFilter: ["class"]}) } function checkProcessCashback(){ requestCashback(documentNumber, orderValue, "checagem", function(){ processDocument(documentNumber) console.log('Kiskadi: Iniciando consulta de cashback') }) } function processDocument(documentNumber) { console.log('Kiskadi: Checando se mostra o box de resgate ou de consulta de cashback') documentNumber = documentNumber.replace(/\D/g, ""); if(documentNumber.length >= 11){ requestCashback(documentNumber, orderValue, "consulta", r => { renderCashbackUI(r, documentNumber, orderValue, msgCashbackContainer) }, err => { $('#k-preloader').remove(); $('#k-container').append('' + err.message + "") }) }else{ renderSearchCashbackUI(orderValue, msgCashbackContainer) } } function createSpinner() { return '
' } function requestCashback(documentNumber, amountEl, type, onSuccess, onFail) { onSuccess = typeof onSuccess === "function" ? onSuccess : function () {}; onFail = typeof onFail === "function" ? onFail : function () {}; $.post("https://n8n-integrations.kiskadi.com/webhook/tray/"+ window.location.host, { document: documentNumber, amount: orderValue, type: type }, r => { r.success ? onSuccess(r) : onFail(r) }).fail((f) => { onFail(f.responseJSON.message ?? "Houve um erro ao tentar resgatar o cashback.") }) } function renderCashbackUI(response, doc, amountEl, msg) { console.log('Kiskadi: Mostrando box de resgate de cashback') var markup = '
Aproveite seu cashback

' + msg + '

'; $(markup).insertBefore("#coupon hr"); $('#k-saldo').text(response.discount.toLocaleString("pt-BR", {style: "currency", currency: "BRL"})); $('#redeemCashback').click(function (e) { e.preventDefault(); $(this).prop("disabled", true); $('#k-container').append(createSpinner()); $('#coupon .link')[0].dispatchEvent(new Event("click", {bubbles: true})) requestCashback(doc, amountEl, "resgate", r => { let couponIdentifier = $('input[name="coupon-identifier"]'); couponIdentifier.val(r.coupon).removeClass('empty'); couponIdentifier[0].dispatchEvent(new Event('input', { bubbles: true })); couponIdentifier[0].dispatchEvent(new Event('change', { bubbles: true })); couponIdentifier[0].dispatchEvent(new KeyboardEvent('keyup', { bubbles: true, key: 'M' })); $('#k-container').remove(); setTimeout(() => { $('#validate-coupon-btn').click(); }, 500); }, err => { $('#redeemCashback').prop("disabled", false); $('#k-preloader').remove(); $('#k-container').append('' + err + "") }) }) } function renderSearchCashbackUI(amountEl, msg) { console.log('Kiskadi: Mostrando box de consulta de cashback') var markup = '
Consulte seu saldo de cashback
'; $(markup).insertBefore("#coupon hr"); $('#checkCashback').click(function () { $('#k-msg').html(""); $(this).prop("disabled", true); $('#k-container').append(createSpinner()); var doc = $('#k-document').val().replace(/\D/g, ""); doc.length >= 11 ? requestCashback(doc, amountEl, "consulta", r => { $('#k-container').remove(); renderCashbackUI(r, doc, amountEl, msg) }, err => { $('#k-preloader').remove(); $('#k-container').append('' + err + ""); $('#checkCashback').prop("disabled", false); }) : ($('#k-preloader').remove(), $('#k-msg').html("Por favor insira um CPF válido."), $('#checkCashback').prop("disabled", false)) }) } } if (document.readyState === 'complete' || document.readyState === 'interactive') { // A página já está pronta → executa imediatamente startCashback(); } else { // Ainda não carregou → espera eventos seguros document.addEventListener('DOMContentLoaded', startCashback); window.addEventListener('load', startCashback); } })();