For a beneficiary to receive electronic cash from a benefactor, the beneficiary's electronic cash address must be obtained by the benefactor. In this tutorial QR code is used to receive the user information or the electronic cash transfers via the device's screen and camera.
Install the QR code reader library to capture QR code from the device camera.
In the repository folder look for the file qr.reader.js and add the line below in the html page.
>script src="assets/js/qr.reader.js"></script>
Call the function below to scan QR codes.
SCRIPT
function scanQRCodes(d) {
scanQr({});
};
Add element to display the QR code.
HTML
<div id="scan-qr-cont" style="display: none;">
<input hidden class="progress-bar" type="range" step="1" min="0" max="100" value="0"/>
<div id="qr-scanner"></div>
</div>
Add post message listener to get the received QR code information.
SCRIPT
window.addEventListener("message", windowEvent);
windowEvent function (e) {
if (e.type.match(/(message)/)) try {
_uz.session = !_uz.session ? e.data.session : _uz.session;
if (e.data.action && e.data.action.match(/(ecash-api)/)) {
if (e.data.action && e.data.action.match(/(response)/)) {
if (data.action && data.action.match(/(hash-assemble)/)) {
let d = JSON.parse(data.object);
if (d.r) {
if (d.r && d.ah) {
localStorage.beneficiary = JSON.stringify(d.ah);
document.querySelector(".beneficiary-name").innerHTML = d.ah.name;
document.querySelector(".beneficiary-number").innerHTML = d.ah.number;
document.querySelector(".beneficiary-currency").innerHTML = _uz.local.account.currency;
};
if (d.l) {
if (d.r && d.l.received > 0) {
alert(`${
(d.l.currency ? `${d.l.currency}${d.l.received}` : `No`)
} electronic cash is transferred into your account ${
(d.l.ratio && d.l.ratio < 1 ? ` and ${
(100 - parseFloat(d.l.ratio) * 100).toFixed(0)
}% counterfiet is rejected!` : "")
}`);
} else alert(`Electronic cash is spent`);
}
delete _uz.z.qrScan.hashes;
delete _uz.z.qrScan.arr;
} else alert(`Wrong QR codes`);
};
};
};
} catch (error) {};
};
Add functions below to read the received QR code information from the device camera.
SCRIPT
function scanQr(d) {
_uz.qrReader = _uz.qrReader ? _uz.qrReader : {};
if (!_uz.qrReader.JsQR) {
_uz.qrReader.JsQR = new JsQRScanner(onScan);
_uz.qrReader.JsQR.setSnapImageMaxSize(300);
if (_uz.qr.interval !== undefined) _uz.qrReader.JsQR.setScanInterval(_uz.qr.interval);
d.e = document.getElementById("qr-scanner");
if (d.e) _uz.qrReader.JsQR.appendTo(d.e);
}
document.getElementById('scan-qr-cont').style.display = 'block'
setTimeout(() => {
document.querySelector(".ex-loading").style.display = "none";
}, 2400);
};
function onScan(s) {
if (!_uz.z.qrScan.arr.includes(s)) {
s = s.replace(/[!\"#$%&]/g, ",").split(",");
let j = s[0];
s = s.join(",");
for (let i = 81; i <= 90; i++)
s = s.replace(new RegExp(String.raw`${String.fromCharCode(i)}`, "g"), String.fromCharCode(i - 33));
_uz.z.qrScan.arr.push(j + s.slice(j.length));
for (let i = 0, j; i < _uz.z.qrScan.arr.length; i++) {
j = _uz.z.qrScan.arr[i].split(",");
_uz.z.qrScan.hashes[j[2]] = j[0];
_uz.z.qrScan.hlen = j[1];
}
let p = document.getElementsByClassName("progress-bar")[0];
if (_uz.z.qrScan.arr.length > 1 && p) {
if (!_uz.qrReader.progress) {
_uz.qrReader.progress = { e: p };
if (_uz.qrReader.progress.e) {
_uz.qrReader.progress.e.style.display = "block";
_uz.qrReader.progress.e.max = _uz.z.qrScan.hlen;
}
}
_uz.qrReader.progress.e.value = Object.keys(_uz.z.qrScan.hashes).length;
}
_uz.api.contentWindow.postMessage({ action: "ecash-api-hash-assemble", qrScan: _uz.z.qrScan }, _uz.api.src);
};
};
function JsQRScannerReady(d) {
_uz.z.qrScan.ready = !0;
};
The developer may use alternative methods other than a QR code, such as network or internet to receive the user electronic cash address.