/* * 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/. */ function addPayment(type, icon, text) { var extrafield = ""; if (type == "giftcard") { extrafield = '' + '
' + ' ' + ' #' + ' ' + '
' + ' '; } var amount = ""; // Autofill the exact due amount for payment methods that are flexible like that if (type == "check" || type == "card" || type == "crypto") { if ($("#owed-amount").text() * 1.0 > 0) { amount = ($("#owed-amount").text() * 1.0).toFixed(2); } } $("#payment-lines").append('' + '
' + '
' + '
' + ' ' + ' ' + text + ' ' + '
' + '
' + ' ' + ' $' + ' ' + '
' + ' ' + extrafield + '
' + ' ' + ' ' + ' ' + '
' + '
' + '
'); $("#payment-lines .payment-entry").last().focus(); } function checkGiftCardBalance() { $("#payment-lines .list-group-item:has(.giftcard-number)").each(function () { var paymentbox = $(".payment-entry", this); var cardnumberbox = $(".giftcard-number", this); var amount = paymentbox.val() * 1.0; var cardnumber = cardnumberbox.val(); if (cardnumber == "") { return; } $.get("action.php", { action: "giftcard_lookup", code: cardnumber }, function (json) { if (json.status == "OK") { if (json.cards.length == 0) { bsalert("Invalid Gift Card", "Gift card #" + cardnumber + " does not exist. Remove it to finish the transaction."); cardnumberbox.addClass('is-invalid'); } else if (json.cards.length == 1) { if (json.cards[0].balance < amount) { bsalert("Insufficient Gift Card Balance", "Gift card #" + cardnumber + " does not contain enough funds. The amount has been set to the full amount remaining on the card ($" + json.cards[0].balance + ")."); paymentbox.val(json.cards[0].balance); } cardnumberbox.removeClass('is-invalid'); } else { bsalert("Invalid Gift Card", "Unable to determine which gift card #" + cardnumber + " is referring to. Remove it to finish the transaction."); cardnumberbox.addClass('is-invalid'); } } else { bsalert("Invalid Gift Card", "Unable to determine which gift card #" + cardnumber + " is referring to. Remove it to finish the transaction."); cardnumberbox.addClass('is-invalid'); } }); }); } $("#payment-lines").on("change keyup blur", ".payment-entry", function () { recalculate(); }); $("#payment-lines").on("blur", ".giftcard-number,.payment-entry[data-type=giftcard]", function () { checkGiftCardBalance(); }); $("#payment-lines").on("keypress", ".giftcard-number,.payment-entry[data-type=giftcard]", function (e) { if (e.which === 13) { checkGiftCardBalance(); } }); $("#payment-lines").on("click", ".remove-payment-btn", function () { $(this).closest(".list-group-item").remove(); recalculate(); }); $("#paymentbtn").click(function () { $("#paymentui").removeClass("d-none"); }); $(".payment-method-button").click(function () { addPayment($(this).data("payment-method"), $(this).data("icon"), $(this).data("text")); });