lmc/account/make_a_payment/make_a_payment.js

126 lines
3.5 KiB
JavaScript

/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, apiPost, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.balance = "balance";
elements.balanceNegative = "balance-negative";
elements.ccAmount = "cc-amount";
elements.ccCharge = "cc-charge";
elements.ccExp = "cc-exp";
elements.ccNumber = "cc-number";
elements.cvv = "cvv";
elements.paypalAmount = "paypal-amount";
elements.paypalCharge = "paypal-charge";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.balance = {};
ui.ccAmount = {};
ui.ccCharge = {};
ui.ccExp = {};
ui.ccNumber = {};
ui.cvv = {};
ui.paypalAmount = {};
ui.paypalCharge = {};
// Callback for account details API call
var displayAccount = function(response)
{
ui.balance.innerHTML = "$";
if (response.balance < 0) {
ui.balance.innerHTML += (-response.balance).toFixed(2) + " credit";
} else if (response.balance > 0) {
ui.balance.innerHTML += response.balance.toFixed(2) + " outstanding";
ui.balance.className = elements.balanceNegative;
} else {
ui.balance.innerHTML += response.balance.toFixed(2);
}
if (response.credit_card) {
ui.ccNumber.innerHTML = response.credit_card.last_four;
ui.ccExp.innerHTML = response.credit_card.expiry;
}
ui.ccCharge.disabled = false;
//ui.paypalCharge.disabled = false;
};
// Click handler for CC charge button
var handleCharge = function(event)
{
if (event.currentTarget.disabled)
return;
if (!confirm("Charge $" + parseInt(ui.ccAmount.value).toFixed(2) + " against your credit card?"))
return;
var req = {
"usd": ui.ccAmount.value
};
if (ui.cvv.value.length)
req.cvv = ui.cvv.value;
apiPost("/account/payments", req, function(response)
{
location.href = "/account";
});
};
// Click handler for PayPal button
var handlePayPal = function(event)
{
if (event.currentTarget.disabled)
return;
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.balance = document.getElementById(elements.balance);
ui.ccAmount = document.getElementById(elements.ccAmount);
ui.ccCharge = document.getElementById(elements.ccCharge);
ui.ccExp = document.getElementById(elements.ccExp);
ui.ccNumber = document.getElementById(elements.ccNumber);
ui.cvv = document.getElementById(elements.cvv);
ui.paypalAmount = document.getElementById(elements.paypalAmount);
ui.paypalCharge = document.getElementById(elements.paypalCharge);
// Register event handlers
ui.ccCharge.addEventListener("click", handleCharge);
ui.paypalCharge.addEventListener("click", handlePayPal);
// Get data from API
apiGet("/account", displayAccount, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();