93 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
2019-04-11 00:45:01 -06:00
// Set to true to stop pinging the server for tx status
var stopPaymentCheck = false;
2019-04-11 00:45:01 -06:00
$("#payment-popup").on("popup:close", function () {
stopPaymentCheck = true;
});
2019-04-19 15:53:15 -06:00
$(".numpad-button").on("click", function () {
var val = $(this).data("value");
if (val == "C") {
$("#amount-box").val("");
return;
}
$("#amount-box").val($("#amount-box").val() + val);
});
2019-04-11 00:45:01 -06:00
$("#showcodebtn").click(function () {
var amount = $("#amount-box").val() * 1.0;
if (amount <= 0) {
app.dialog.alert("Please specify an amount.", "Error");
return;
} else if (amount > 999.99) {
app.dialog.alert("Please specify an amount less than $999.99.", "Error");
return;
}
app.preloader.show();
makeQrCode(amount, function (ok, msg) {
app.preloader.hide();
if (!ok) {
app.dialog.alert(msg, "Error");
} else {
app.popup.open($("#payment-popup"));
}
});
2019-04-11 00:45:01 -06:00
});
2019-04-11 00:45:01 -06:00
function makeQrCode(amount, callback) {
callAPI("gettxcode", {
key: localStorage.getItem("key"),
2019-04-11 00:45:01 -06:00
amount: amount
}, function (data) {
var typeNumber = 4;
var errorCorrectionLevel = 'L';
var qr = qrcode(typeNumber, errorCorrectionLevel);
2019-04-11 00:45:01 -06:00
qr.addData(SETTINGS['webapp_url'] + '?sendto=' + data.txcode + "&amount=" + data.amount);
qr.make();
var svg = qr.createSvgTag({
margin: 6,
scalable: true
});
var base64 = window.btoa(svg);
$("#qrcode").attr("src", 'data:image/svg+xml;base64,' + base64);
2019-04-19 15:53:15 -06:00
$("#pay-amount").text(data.amount.toFixed(2));
$("#paid-amount").text(data.amount.toFixed(2));
2019-04-11 00:45:01 -06:00
app.preloader.hide();
checkPaymentStatus(data.txcode);
callback(true);
}, function (msg) {
callback(false, msg);
});
}
2019-04-11 00:45:01 -06:00
function checkPaymentStatus(txcode) {
if (stopPaymentCheck) {
stopPaymentCheck = false;
return;
}
callAPI("gettxstatus", {
key: localStorage.getItem("key"),
txcode: txcode
}, function (data) {
if (data.complete == true) {
app.popup.close($("#payment-popup"));
app.popup.open($("#paid-popup"));
2019-04-19 15:53:15 -06:00
$("#amount-box").val("");
2019-04-11 00:45:01 -06:00
} else {
setTimeout(function () {
checkPaymentStatus(txcode);
}, 15 * 100);
}
2019-04-11 00:45:01 -06:00
}, function (msg) {
app.popup.close($("#payment-popup"));
app.popup.close($("#paid-popup"));
app.dialog.alert(msg, "Error");
});
}