For a benefactor to transfer electronic cash to a beneficiary, the beneficiary's electronic cash address mush be obtained by the benefactor. In this tutorial QR code is used to share the user information or the electronic cash transfers via the device's screen and camera.
Install the QR code creator library to display information on the device screen.
In the repository folder look for the file qr.creator.js and add the lines below in the html page.
<script type="module">
import QrCreator from "./assets/js/qr.creator.js";
// Your codes here...
// ...
// Your codes here...
</script>
Call the function below to request the user electronic cash address.
SCRIPT
function hashAccount(d) {
_uz.api.contentWindow.postMessage({ action: "ecash-api-hash-account" }, _uz.api.src);
};
Add elements below to display the user's electronic cash QR code.
HTML
<div id="show-qr-cont" style="display: none;">
<div class="qr-cont"></div>
</div>
Add post message listener to get the returned user electronic cash address.
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-account)/)) {
createQr({
s: data.hash
});
};
if (data.action && data.action.match(/(hash-fragment)/)) {
for (let i = 0; i < data.hash.length; i++) drawQr({ id: ".qr-cont", piece: data.hash[i] });
_uz.z.cvs = { e: [], i: 0 };
for (let i = 0, j = document.querySelector(".qr-cont").getElementsByTagName("canvas"); i < j.length; i++) {
j[i].style.display = "none";
j[i].style[window.innerWidth < window.innerHeight ? "width" : "height"] = "75%";
_uz.z.cvs.e.push(j[i]);
};
if (_uz.z.cvs.e.length > 1) autoSlide({ t: _uz.qr.delay });
document.getElementById("show-qr-cont").style.display = "block";
};
};
};
} catch (error) {};
};
Add the functions below to create the user electronic cash address QR code.
SCRIPT
<script>
function createQr(d) {
if (d.s) {
d.q = document.querySelector(".qr-cont");
if (d.q) _uz.api.contentWindow.postMessage({
action: "ecash-api-hash-fragment",
hash: d.s,
length: _uz.qr.length
}, _uz.api.src);
}
};
function drawQr(d) {
QrCreator.render({
text: d.piece,
radius: "0.5",
ecLevel: "H",
fill: "#fff",
background: null,
size: "512"
}, document.querySelector(d.id));
};
</script>
The developer may use alternative methods other than a QR code, such as network or internet to share the user electronic cash address.