lmc/account/creditcard/creditcard.js

98 lines
2.7 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, apiPost, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.ccNew = "cc-new";
elements.cvv = "cvv";
elements.default = "default";
elements.expiryMonth = "expiry-month";
elements.expiryYear = "expiry-year";
elements.updateButton = "update-button";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.ccNew = {};
ui.cvv = {};
ui.default = {};
ui.expiryMonth = {};
ui.expiryYear = {};
ui.updateButton = {};
// Click handler for update button
var handleUpdate = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"data": {
"card_number": ui.ccNew.value.replaceAll("-", "").replaceAll(" ", ""),
"cvv": ui.cvv.value,
"expiry_month": parseInt(ui.expiryMonth.value),
"expiry_year": parseInt(ui.expiryYear.value)
},
"is_default": ui.default.checked,
"type": "credit_card"
};
apiPost("/account/payment-methods", req, function(response)
{
location.href = "/account";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.ccNew = document.getElementById(elements.ccNew);
ui.cvv = document.getElementById(elements.cvv);
ui.default = document.getElementById(elements.default);
ui.expiryMonth = document.getElementById(elements.expiryMonth);
ui.expiryYear = document.getElementById(elements.expiryYear);
ui.updateButton = document.getElementById(elements.updateButton);
// Populate expiry selectors
var now = new Date();
ui.expiryMonth.value = now.getMonth() + 1;
for (var i = 0; i < 25; i++) {
var year = document.createElement("option");
year.value = now.getFullYear() + i;
year.innerHTML = now.getFullYear() + i;
ui.expiryYear.appendChild(year);
}
// Register event handlers
ui.updateButton.addEventListener("click", handleUpdate);
};
// Attach onload handler
window.addEventListener("load", setup);
})();