(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 = '' + msg + '