2020-03-13 23:01:39 -04:00
|
|
|
/*
|
|
|
|
* 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";
|
2024-06-17 18:21:01 -04:00
|
|
|
elements.method = "method";
|
2020-03-13 23:01:39 -04:00
|
|
|
|
|
|
|
// Data received from API calls
|
|
|
|
var data = {};
|
2024-06-17 18:21:01 -04:00
|
|
|
data.methods = [];
|
2020-03-13 23:01:39 -04:00
|
|
|
|
|
|
|
// Static references to UI elements
|
|
|
|
var ui = {};
|
|
|
|
ui.balance = {};
|
|
|
|
ui.ccAmount = {};
|
|
|
|
ui.ccCharge = {};
|
2024-06-17 18:21:01 -04:00
|
|
|
ui.method = {};
|
2020-03-13 23:01:39 -04:00
|
|
|
|
|
|
|
// 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;
|
2024-06-17 18:21:01 -04:00
|
|
|
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));
|
|
|
|
}
|
2020-03-13 23:01:39 -04:00
|
|
|
} else {
|
|
|
|
ui.balance.innerHTML += response.balance.toFixed(2);
|
|
|
|
}
|
|
|
|
|
2024-06-17 18:21:01 -04:00
|
|
|
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;
|
|
|
|
}
|
2020-03-13 23:01:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
2024-06-17 18:21:01 -04:00
|
|
|
"payment_method_id": parseInt(ui.method.value),
|
2020-03-13 23:01:39 -04:00
|
|
|
"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);
|
2024-06-17 18:21:01 -04:00
|
|
|
ui.method = document.getElementById(elements.method);
|
2020-03-13 23:01:39 -04:00
|
|
|
|
|
|
|
// Register event handlers
|
|
|
|
ui.ccCharge.addEventListener("click", handleCharge);
|
|
|
|
|
|
|
|
// Get data from API
|
|
|
|
apiGet("/account", displayAccount, null);
|
2024-06-17 18:21:01 -04:00
|
|
|
apiGet("/account/payment-methods", displayMethods, null);
|
2020-03-13 23:01:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Attach onload handler
|
|
|
|
window.addEventListener("load", setup);
|
|
|
|
})();
|