lmc/account/make_a_payment/make_a_payment.js

145 lines
4.2 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.method = "method";
// Data received from API calls
var data = {};
data.methods = [];
// Static references to UI elements
var ui = {};
ui.balance = {};
ui.ccAmount = {};
ui.ccCharge = {};
ui.method = {};
// 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;
if (response.balance < 5) {
ui.ccAmount.min = response.balance.toFixed(2);
ui.ccAmount.value = response.balance.toFixed(2);
} else if (response.balance > 2000) {
ui.ccAmount.max = Math.min(50000, response.balance.toFixed(2));
}
} else {
ui.balance.innerHTML += response.balance.toFixed(2);
}
ui.ccCharge.disabled = false;
};
// Callback for payment methods API call
var displayMethods = function(response)
{
data.methods = data.methods.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/account/payment-methods?page=" + (response.page + 1), displayMethods, null);
return;
}
var typeNames = {
"credit_card": "Credit Card",
"google_pay": "Google Pay",
"paypal": "PayPal"
};
for (var i = 0; i < data.methods.length; i++) {
var option = document.createElement("option");
option.value = data.methods[i].id;
if (typeNames[data.methods[i].type])
option.innerHTML = typeNames[data.methods[i].type];
else
option.innerHTML = data.methods[i].type;
if (data.methods[i].type == "credit_card" || data.methods[i].type == "google_pay")
option.innerHTML += ": " + data.methods[i].data.card_type + " ****" + data.methods[i].data.last_four;
else if (data.methods[i].type == "paypal")
option.innerHTML += ": " + data.methods[i].data.email;
ui.method.appendChild(option);
if (data.methods[i].is_default) {
option.innerHTML += " (default)";
ui.method.value = data.methods[i].id;
}
}
ui.ccCharge.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 = {
"payment_method_id": parseInt(ui.method.value),
"usd": ui.ccAmount.value
};
apiPost("/account/payments", req, function(response)
{
location.href = "/account";
});
};
// 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.method = document.getElementById(elements.method);
// Register event handlers
ui.ccCharge.addEventListener("click", handleCharge);
// Get data from API
apiGet("/account", displayAccount, null);
apiGet("/account/payment-methods", displayMethods, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();