You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB
JavaScript

/*
* 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/.
*/
var stripe = null;
var elements = null;
var card = null;
function initPaymentPage() {
$("#tx-off-percent").text(+(SETTINGS["addfunds_percent"] * 100).toFixed(2));
initStripe();
}
function initStripe() {
// Wait for Stripe to be loaded
if (typeof Stripe == 'undefined') {
setTimeout(initStripe, 500);
return;
}
stripe = Stripe(SETTINGS["stripe_pubkey"]);
elements = stripe.elements();
card = elements.create('card', {});
card.mount('#card-element');
card.addEventListener('change', function (event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
}
function addFeesToAmount() {
if ($("#funds-amount").val() == "") {
return;
}
var amount = $("#funds-amount").val() * 1.0;
var stripefees = 0.30 + (amount * 0.029);
var totalfees = amount * SETTINGS["addfunds_percent"];
amount = (amount + (stripefees > totalfees ? 0.30 : 0)) /
(1 - (stripefees > totalfees ? 0.029 : SETTINGS["addfunds_percent"]));
$("#funds-amount").val(amount.toFixed(2));
}
function chargeCard() {
if ($("#funds-amount").val() == "") {
return;
}
if (stripe == null) {
return;
}
app.preloader.show();
stripe.createToken(card).then(function (result) {
if (result.error) {
// Inform the customer that there was an error.
app.preloader.hide();
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
callAPI("addfunds", {
key: localStorage.getItem("key"),
amount: $("#funds-amount").val(),
token: result.token.id
}, function (response) {
app.preloader.hide();
router.navigate("/fundsadded/" + response.charged_amount.toFixed(2) + "/" + response.final_amount.toFixed(2));
}, function (error) {
app.preloader.hide();
app.dialog.alert(error, "Error");
});
}
});
}
$(".preset-amount-button").click(function () {
$($(this).data("target")).val($(this).data("amount"));
});