let isNewAccountCreated = false; window.addEventListener("load", () => { const handleCurrencies = async () => { const data = async () => { let buyCurrencyHtml = ""; // GET ALL CURRIENCES const fetchedCurrencies = await getCurrencies(); fetchedCurrencies.forEach((data) => { buyCurrencyHtml += `

${data.ident}

${data.currency_name}

`; }); document.querySelector(".top-sel-modal-cnt-bdy").innerHTML = buyCurrencyHtml; }; data().then(() => { currSetter(); }); }; const handleBankDetails = async () => { const data = async () => { app.getCookie("azer_token", (e, r) => { app.query(".bnk-actt-det").innerHTML = app.loading(); if (e) { app.client .fetch("../../user/bank/get", "POST", { token: r }) .then((x) => { if (x.isSuccess) { if (x.result.length > 0) { let banksDetails = ""; x.result.sort((a, b) => b.bank_id - a.bank_id); x.result.forEach((e) => { if (e.set_default == 1) { app.query(".bnk-actt-det").innerHTML = `

${e.bank_name}

acc: ${e.bank_account}

`; } banksDetails += `

${e.bank_name}

${e.bank_account}

`; }); app.query(".bnk-selt-cover").innerHTML = banksDetails; } else { app.query(".new-bnk-sec").classList.add("active"); app.query(".bnk-actt-det").innerHTML = "

Add New Bank

"; app.query(".bnk-set-def").classList.add("active"); isNewAccountCreated = true; } } else { app.client.sweetAlertE(x.icon, x.message, x.title); } }) .then(() => { const bnkSeltUse = app.queryAll(".bnk-selt-use"); const newBankDet = app.query(".bnk-actt-det"); const acctNum = app.query(".acctNum"); const bankDefault = app.query(".bnk-set-def"); const newBnkSec = app.query(".new-bnk-sec"); bnkSeltUse.forEach((bnkSeltUse) => { bnkSeltUse.addEventListener("click", () => { let bnk = bnkSeltUse.children[0].textContent; let acc = bnkSeltUse.children[1].textContent; newBankDet.innerHTML = `

${bnk}

acc: ${acc}

`; acctNum.textContent = acc; bankDefault.classList.remove("active"); newBnkSec.classList.remove("active"); isNewAccountCreated = false; }); }); }); } else { app.redirectUrl("../../user/login"); } }); }; data(); }; handleCurrencies(); handleBankDetails(); handleBankList().then(() => { bankListManipulation(); }); document.getElementById("currIntVal").value = ""; document.getElementById("amntInput").value = ""; document.getElementById("cardTrans").disabled = true; document.getElementById("bnkTrans").disabled = true; app.query("#setDefault").checked = false; }); //GLOBALS let globalNewCoinId; const withdrawObject = {}; //SETTING SELECT CURRENCY const currSetter = () => { const topSelBtn = document.querySelector(".top-sel-sel"); const topSelModal = document.querySelector(".top-sel-modal"); const topSelModalCnt = document.querySelector(".top-sel-modal-cnt"); const topSelEch = document.querySelectorAll(".top-sel-ech"); const tpModCls = document.querySelector(".tpModCls"); const topSelSelCov = document.querySelector(".top-sel-sel-cover"); const btmSecCover = document.querySelector(".btm-select-sec-cover"); const walletBalace = document.querySelector(".walletBalace"); const witdrawaBalance = document.querySelector(".witdrawaBalance"); const currIntVal = document.getElementById("currIntVal"); //Opening and closing of modals topSelSelCov.addEventListener("click", () => { topSelModal.classList.add("active"); }); tpModCls.addEventListener("click", () => { topSelModal.classList.remove("active"); }); topSelModal.addEventListener("click", (e) => { if (!topSelModalCnt.contains(e.target)) { topSelModal.classList.remove("active"); } }); //Handling picking of coins topSelEch.forEach((newCoin) => { const cardTrans = document.getElementById("cardTrans"); const bnkTrans = document.getElementById("bnkTrans"); const warn = document.querySelector(".warn"); let newImg; let newDet; newCoin.addEventListener("click", () => { // let totalBalance = 0; // let withdrawableBalance = 0; // let activeCurrency = 0; // let activeCurrencyTotalBalance = 0; // let activeCurrencyWithdrawableBalance = 0; newImg = newCoin.children[0].children[0].getAttribute("src"); newDet = newCoin.children[1].children[1].innerHTML; globalNewCoinId = newCoin.dataset.id; //Setting the coin to display topSelSelCov.innerHTML = `

${newDet}

`; const handleCurrWallet = async () => { let totalBalance = 0; let withdrawableBalance = 0; let activeCurrency = 0; let activeCurrencyTotalBalance = 0; let activeCurrencyWithdrawableBalance = 0; app.getCookie("azer_token", (e, r) => { if (e) { btmSecCover.innerHTML = app.loading(); app.client .fetch("/user/wallet/fiat", "POST", { token: r }) .then((dx) => { totalBalance = dx.totalAvailableBalance; withdrawableBalance = dx.AvailableBalance; }) .then(async () => { const fetchedCurrency = await getSingleCurrency( newCoin.dataset.id ); fetchedCurrency.forEach((data) => { activeCurrency = data.buy_rate; }); activeCurrencyTotalBalance = totalBalance * activeCurrency; activeCurrencyWithdrawableBalance = withdrawableBalance * activeCurrency; btmSecCover.innerHTML = `
${newDet} balance: ${activeCurrencyTotalBalance} ${newDet}
`; witdrawaBalance.innerHTML = activeCurrencyTotalBalance; walletBalace.innerHTML = Number( activeCurrencyWithdrawableBalance ); }); } }); }; handleCurrWallet(); //Replace coin name with picked coin name currIntVal.value = newDet; topSelModal.classList.remove("active"); //Setting Available Payment methods if (newCoin.dataset.id !== "ngn") { bnkTrans.disabled = true; cardTrans.disabled = false; warn.classList.add("active"); } else { bnkTrans.disabled = false; cardTrans.disabled = false; warn.classList.remove("active"); } }); }); }; //SETTING THE PAYMENT OPTION const cardTrans = document.getElementById("cardTrans"); const bnkTrans = document.getElementById("bnkTrans"); bnkTrans.addEventListener("click", (e) => { resetCheckBtn(); bnkTrans.checked = true; }); cardTrans.addEventListener("click", (e) => { resetCheckBtn(); cardTrans.checked = true; }); const resetCheckBtn = () => { bnkTrans.checked = false; cardTrans.checked = false; }; // SETTING FIRST PROCCECD BUTTON const ultimBtn = document.querySelector(".ultimBtn"); const paySec = document.querySelector(".pay-sec"); const payClss = document.querySelector(".payClss"); const bankSec = document.querySelector(".bank-sec"); const currIntVal = document.getElementById("currIntVal"); const checkErr = document.querySelector(".check-err"); const currErr = document.querySelector(".curr-err"); ultimBtn.addEventListener("click", (e) => { e.preventDefault(); //SETTING CARD MODAL && BANK MODAL DISPLAY (WITH ERR0R MESSAGES) if (cardTrans.checked == true && currIntVal.value) { paySec.classList.add("active"); } else if (bnkTrans.checked == true && currIntVal.value) { bankSec.classList.add("active"); } else if (currIntVal.value == "") { currErr.classList.add("active"); setTimeout(() => currErr.classList.remove("active"), 3000); } else if (cardTrans.checked === false && bnkTrans.checked === false) { checkErr.classList.add("active"); setTimeout(() => checkErr.classList.remove("active"), 3000); } }); payClss.addEventListener("click", () => { paySec.classList.remove("active"); }); //SETTING BANK MANIPULATIONS const tpBnkCls = document.querySelector(".tpBnkCls"); const amtEch = document.querySelectorAll(".amnt-chss-ech"); const amntInput = document.getElementById("amntInput"); const bankAccBtn = document.querySelector(".bnk-actt-cnt"); const bankDefault = document.querySelector(".bnk-set-def"); const bnkSelct = document.querySelector(".bnk-selt"); const newBankSec = document.querySelector(".new-bnk-sec"); const newBankDet = document.querySelector(".bnk-actt-det"); const bnkDefltBtn = document.querySelector(".bnk-selt-deflt"); const bnkSeltUse = document.querySelectorAll(".bnk-selt-use"); const newBnkSec = document.querySelector(".new-bnk-sec"); const newBnkShw = document.querySelector(".new-shw-cnt"); const newBnkShwCnt = document.querySelector(".new-bnk-selt"); const newBnkShwEch = document.querySelectorAll(".new-bnk-selt-ech"); const newShwDet = document.querySelector(".new-shw-det"); const amntErr = document.querySelector(".amnt-err"); const totWith = document.querySelector(".total-witt"); const withdrawBtn = document.getElementById("withdrawBtn"); const walletBalace = document.querySelector(".walletBalace"); const ballErr = document.querySelector(".ball-err"); const errCls = document.querySelector(".errCls"); const Empterr = document.querySelector(".Empterr"); const acctNum = document.querySelector(".acctNum"); const newAccInput = document.getElementById("newAccInt"); const bnkSelerr = document.querySelector(".bnkSelerr"); const newAccerr = document.querySelector(".newAccerr"); const accCounterr = document.querySelector(".accCounterr"); const setDefault = document.getElementById("setDefault"); const confirmSec = document.querySelector(".confirm-sec"); const conOtpSec = document.querySelector(".conOtp-sec"); const confirmBtn = document.querySelector(".confirmBtn"); const otpBtn = document.querySelector(".otpBtn"); const sellComp = document.querySelector(".sell-comp"); let validAccountNum = ""; amountInputPattern = new RegExp("^[0-9.]+$"); let previousValue = ""; let validInput = ""; //Close Bank Modal tpBnkCls.addEventListener("click", () => { bankSec.classList.remove("active"); app.query("#bnkTrans").checked = false; app.query("#cardTrans").checked = false; amntInput.value = ""; totWith.innerHTML = ""; amtEch.forEach((amtEch) => amtEch.classList.remove("active")); app.query("#setDefault").checked = false; }); //Handlinging fixed amount button amtEch.forEach((amtEch) => { amtEch.addEventListener("click", () => { resetAmtBtn(); // amntInput.value = `₦${amtEch.dataset.fee}`; amntInput.value = amtEch.dataset.fee; amtEch.classList.add("active"); validInput = Number(amtEch.dataset.fee); totWith.children[0].innerHTML = `₦${validInput.toLocaleString()}`; amntErr.classList.remove("active"); }); }); const resetAmtBtn = () => { amtEch.forEach((amtEch) => { amtEch.classList.remove("active"); }); }; bankAccBtn.addEventListener("click", () => { bnkSelct.classList.add("active"); }); //Handling new bank display bnkDefltBtn.addEventListener("click", () => { newBankDet.innerHTML = "

Add New Bank

"; newBnkSec.classList.add("active"); bankDefault.classList.add("active"); isNewAccountCreated = true; }); newBnkShw.addEventListener("click", () => { newBnkShwCnt.classList.add("active"); }); const handleBankList = async () => { let html = ""; const getBankLists = await getAllSupportedBanks(); getBankLists.forEach((data) => { html += `

${data.name}

`; }); app.query(".new-bnk-selt-hold").innerHTML = html; window.addEventListener("click", (e) => { if (!bankAccBtn.contains(e.target)) { bnkSelct.classList.remove("active"); } }); newBnkShwCnt.addEventListener("click", (e) => { if (!app.query(".new-bnk-selt-hold").contains(e.target)) { newBnkShwCnt.classList.remove("active"); } }); }; const bankListManipulation = () => { app.queryAll(".new-bnk-selt-ech").forEach((newBnkShwEch) => { newBnkShwEch.addEventListener("click", () => { newShwDet.innerHTML = newBnkShwEch.innerHTML; newShwDet.dataset.id = newBnkShwEch.children[0].textContent; newBnkShwCnt.classList.remove("active"); }); }); }; //Removing modal after click window.addEventListener("click", (e) => { if (!bankAccBtn.contains(e.target)) { bnkSelct.classList.remove("active"); } }); newBnkShwCnt.addEventListener("click", (e) => { if (!app.query(".new-bnk-selt-hold").contains(e.target)) { newBnkShwCnt.classList.remove("active"); } }); //Handling amount input amntInput.addEventListener("input", (e) => { let currentValue = amntInput.value; //Validating amount input if (e.inputType.includes("delete") || amountInputPattern.test(currentValue)) { previousValue = currentValue; } amntInput.value = Number(previousValue); //Check if it exceeds maximum withdrawal amount if (amntInput.value > 10000000 || amntInput.value < 1000) { amntErr.classList.add("active"); totWith.children[0].innerHTML = ""; } else { validInput = amntInput.value; amntErr.classList.remove("active"); totWith.children[0].innerHTML = `₦${validInput.toLocaleString()}`; } if (amntInput.value == "") { amntErr.classList.remove("active"); } }); //Validating account number input newAccInput.addEventListener("input", (e) => { let currentValue = newAccInput.value; //Checking for numbers only to account input if (e.inputType.includes("delete") || amountInputPattern.test(currentValue)) { previousValue = currentValue; } newAccInput.value = previousValue; if (!(newAccInput.value.length === 10)) { accCounterr.classList.add("active"); } else { accCounterr.classList.remove("active"); // validAccountNum = newAccInput.value; } }); //Closing confirm modal app.query(".closeConfrm").addEventListener("click", () => { amntInput.value = ""; confirmSec.classList.remove("active"); totWith.innerHTML = ""; amtEch.forEach((amtEch) => amtEch.classList.remove("active")); app.query("#bnkTrans").checked = false; app.query("#cardTrans").checked = false; }); app.query(".closeOtp").addEventListener("click", () => { amntInput.value = ""; conOtpSec.classList.remove("active"); totWith.innerHTML = ""; amtEch.forEach((amtEch) => amtEch.classList.remove("active")); app.query("#bnkTrans").checked = false; app.query("#cardTrans").checked = false; }); app.query(".conttt").addEventListener("click", () => { amntInput.value = ""; sellComp.classList.remove("active"); totWith.innerHTML = ""; amtEch.forEach((amtEch) => amtEch.classList.remove("active")); app.query("#bnkTrans").checked = false; app.query("#cardTrans").checked = false; }); //Handling withdraw btn var withdrawData; withdrawBtn.addEventListener("click", (e) => { const newShwDet = document.querySelector(".new-shw-det"); e.preventDefault(); if (parseInt(validInput) > parseInt(walletBalace.innerHTML)) { ballErr.classList.add("active"); errCls.addEventListener("click", () => { ballErr.classList.remove("active"); }); return; } window.setTimeout(() => ballErr.classList.remove("active"), 2000); const getCurrHandler = async () => { let withdrawableBalance = 0; let activeCurrencyTotalBalance = 0; const acctNum = app.query(".acctNum"); app.getCookie("azer_token", (e, r) => { if (e) { app.query(".confirm-bdy").innerHTML = app.loading(); app.client .fetch("/user/wallet/fiat", "POST", { token: r }) .then((dx) => { withdrawableBalance = dx.AvailableBalance; }) .then(async () => { const fetchedCurrency = await getSingleCurrency(globalNewCoinId); fetchedCurrency.forEach((data) => { activeCurrency = data.buy_rate; }); activeCurrencyTotalBalance = ( withdrawableBalance * activeCurrency ).toLocaleString(); }) .then(() => { if (Number(activeCurrencyTotalBalance) < Number(validInput)) return; if (!isNewAccountCreated) { // PUSHED DATA withdrawData = { currency: globalNewCoinId.toUpperCase(), amount: Number(validInput), bank: newBankDet.children[0].innerHTML, accountNumber: acctNum.innerHTML, token: r, profit: null, }; app.query(".confirm-bdy").innerHTML = `

Amount to be withdrawn

${globalNewCoinId.toUpperCase()} ${Number(validInput).toLocaleString()}

Bank

${newBankDet.children[0].innerHTML}

Account Number

${acctNum.innerHTML}

Fee Charge

None

`; } if (isNewAccountCreated) { // PUSHED DATA withdrawData = { currency: globalNewCoinId.toUpperCase(), amount: Number(validInput), bank: app.query(".new-shw-det").children[0].innerHTML, accountNumber: app.query("#newAccInt").value, token: r, profit: null, }; app.query(".confirm-bdy").innerHTML = `

Amount to be withdrawn

${globalNewCoinId.toUpperCase()} ${Number( validInput ).toLocaleString()}

Bank

${newShwDet.dataset.id}

Account Number

${newAccInt.value}

Fee Charge

None

`; } }); } }); }; getCurrHandler(); if (!isNewAccountCreated) { //Checking empty inputs if (amntInput.value == "") { Empterr.classList.add("active"); window.setTimeout(() => Empterr.classList.remove("active"), 2000); return; } else if ( Number(amntInput.value) > 10000000 || Number(amntInput.value) < 1000 ) { amntErr.classList.add("active"); window.setTimeout(() => amntErr.classList.remove("active"), 2000); return; } } if (isNewAccountCreated) { if (Number(amntInput.value) > 10000000 || Number(amntInput.value) < 1000) { amntErr.classList.add("active"); window.setTimeout(() => amntErr.classList.remove("active"), 2000); return; } else if (!newShwDet.dataset.id) { bnkSelerr.classList.add("active"); window.setTimeout(() => bnkSelerr.classList.remove("active"), 2000); return; } else if (newAccInput.value == "") { newAccerr.classList.add("active"); window.setTimeout(() => newAccerr.classList.remove("active"), 2000); return; } } bankSec.classList.remove("active"); confirmSec.classList.add("active"); }); // handling Confirm btn confirmBtn.addEventListener("click", (e) => { e.preventDefault(); confirmSec.classList.remove("active"); conOtpSec.classList.add("active"); app.query(".otpBtn").style.backgroundColor = "#eee"; app.query(".otpBtn").disabled = true; app.getCookie("azer_token", (e, r) => { if (e) { app.query(".azer_input").addEventListener("keyup", (e) => { if (e.target.value.length === 6) { let c = { code: e.target.value, token: r }; swal({ html: "Loading....", padding: "2em", onOpen: function () { swal.showLoading(); }, }).then(function (result) { result.dismiss; }); app.client .fetch("../../../user/bank/withdraw/auth/verify", "POST", c) .then((e) => { if (e.isSuccess) { app.query(".otpBtn").style.backgroundColor = "#4361ee"; app.query(".otpBtn").disabled = false; app.client .fetch("../../../user/bank/withdraw", "POST", withdrawData) .then((e) => { if (e.isSuccess) { console.log(e); swal.close(); conOtpSec.classList.remove("active"); sellComp.classList.add("active"); } }); } else { swal.close(); app.query(".otp_handler").innerHTML = e.message; // app.client.sweetAlertE(e.icon, e.message, e.title); } }); } }); const Auth = () => { app.query(".auth_resend").classList.remove(".auth_key"); app.query(".auth_resend").innerHTML = "Sending...."; app.client .fetch("../../user/bank/withdraw/auth", "POST", { token: r }) .then((e) => { if (e.isSuccess) { var timeleft = 15; var downloadTimer = setInterval(function () { if (timeleft <= 0) { clearInterval(downloadTimer); document.getElementById("auth_resend").innerHTML = "Request again"; app.query(".auth_resend").classList.add(".auth_key"); } else { app.query(".auth_resend").classList.remove(".auth_key"); document.getElementById( "auth_resend" ).innerHTML = ` Sent (${timeleft})`; } timeleft -= 1; }, 1000); } else { app.client.sweetAlertE(e.icon, e.message, e.title); } }); }; Auth(); app.query(".auth_key").addEventListener("click", Auth); } }); // app.client.fetch('../../user/bank/withdraw', 'POST', withdrawData).then((e) => { // console.log(e); // }); }); //handling OTP btn otpBtn.addEventListener("click", (e) => { e.preventDefault(); conOtpSec.classList.remove("active"); sellComp.classList.add("active"); const getCurrHandler = async () => { let withdrawableBalance = 0; let activeCurrencyTotalBalance = 0; app.getCookie("azer_token", (e, r) => { if (e) { app.query(".confirm-bdy").innerHTML = app.loading(); app.client .fetch("/user/wallet/fiat", "POST", { token: r }) .then((dx) => { withdrawableBalance = dx.AvailableBalance; }) .then(async () => { const fetchedCurrency = await getSingleCurrency(globalNewCoinId); fetchedCurrency.forEach((data) => { activeCurrency = data.buy_rate; }); activeCurrencyTotalBalance = ( withdrawableBalance * activeCurrency ).toLocaleString(); }) .then(() => { if (Number(activeCurrencyTotalBalance) < Number(validInput)) return; if (!isNewAccountCreated) { //Checking empty inputs if (amntInput.value == "") { Empterr.classList.add("active"); window.setTimeout( () => Empterr.classList.remove("active"), 2000 ); } else if ( Number(amntInput.value) > 10000000 || Number(amntInput.value) < 1000 ) { amntErr.classList.add("active"); window.setTimeout( () => amntErr.classList.remove("active"), 2000 ); return; } else { //Setting the object with default bank withdrawObject.bank = newBankDet.children[0].innerHTML; withdrawObject.accountNumber = acctNum.innerHTML; withdrawObject.totAmount = validInput; console.log("success", withdrawObject); } } if (isNewAccountCreated) { if ( Number(amntInput.value) > 10000000 || Number(amntInput.value) < 1000 ) { amntErr.classList.add("active"); window.setTimeout( () => amntErr.classList.remove("active"), 2000 ); return; } else if (!newShwDet.dataset.id) { bnkSelerr.classList.add("active"); window.setTimeout( () => bnkSelerr.classList.remove("active"), 2000 ); return; } else if (newAccInput.value == "") { newAccerr.classList.add("active"); window.setTimeout( () => newAccerr.classList.remove("active"), 2000 ); return; } else { //Setting the object with new account bank withdrawObject.bank = newShwDet.dataset.id; withdrawObject.accountNumber = app.query("#newAccInt").value; withdrawObject.totAmount = validInput; app.getCookie("azer_token", (e, r) => { let name = ""; let country = ""; let currency = ""; let keychain = ""; app.client .fetch("../../api/token", "POST", { token: r }) .then((x) => { name = `${x.first_name} ${x.last_name}`; country = x.country; currency = "ngn"; }) .then(() => { const dataObj = { country: country, currency: currency, bankName: newShwDet.dataset.id, accountNumber: Number(app.query("#newAccInt").value), accountName: name, token: r, }; console.log(dataObj); app.client.fetch("../../user/bank/add", "POST", dataObj); // .then((x) => { // if (x.isSuccess) { // app.client.toastS( // x.icon, // x.message, // "../wallet/withdraw_fiat" // ); // } else { // app.client.toastE(x.icon, x.message); // } // }); }) .then(() => { app.client .fetch("../../user/bank/get", "POST", { token: r }) .then((x) => { if (x.isSuccess) { x.result.sort((a, b) => b.bank_id - a.bank_id); x.result.forEach((e) => { if ( Number(app.query("#newAccInt").value) == Number(e.bank_account) ) { keychain = e.keychain; } }); } else { app.client.sweetAlertE(x.icon, x.message, x.title); } }) .then(() => { if (setDefault.checked) { const dataObj = { keychain: keychain, token: r, }; app.client.fetch( "../../user/bank/set_default", "POST", dataObj ); } }); }); }); } } }); } }); }; getCurrHandler(); });