1 /* eslint-disable no-useless-concat */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 import { CreditCard } from "resource://gre/modules/CreditCard.sys.mjs";
7 import { FormAutofillNameUtils } from "resource://gre/modules/shared/FormAutofillNameUtils.sys.mjs";
10 * The CreditCardRecord class serves to handle and normalize internal credit card records.
11 * Unlike the CreditCard class, which represents actual card data, CreditCardRecord is used
12 * for processing and consistent data representation.
14 export class CreditCardRecord {
15 static normalizeFields(creditCard) {
16 this.#normalizeCCNameFields(creditCard);
17 this.#normalizeCCNumberFields(creditCard);
18 this.#normalizeCCExpirationDateFields(creditCard);
19 this.#normalizeCCTypeFields(creditCard);
22 static #normalizeCCNameFields(creditCard) {
23 if (!creditCard["cc-name"]) {
24 creditCard["cc-name"] = FormAutofillNameUtils.joinNameParts({
25 given: creditCard["cc-given-name"] ?? "",
26 middle: creditCard["cc-additional-name"] ?? "",
27 family: creditCard["cc-family-name"] ?? "",
31 delete creditCard["cc-given-name"];
32 delete creditCard["cc-additional-name"];
33 delete creditCard["cc-family-name"];
36 static #normalizeCCNumberFields(creditCard) {
37 if (!("cc-number" in creditCard)) {
41 if (!CreditCard.isValidNumber(creditCard["cc-number"])) {
42 delete creditCard["cc-number"];
46 const card = new CreditCard({ number: creditCard["cc-number"] });
47 creditCard["cc-number"] = card.number;
50 static #normalizeCCExpirationDateFields(creditCard) {
51 let normalizedExpiration = CreditCard.normalizeExpiration({
52 expirationMonth: creditCard["cc-exp-month"],
53 expirationYear: creditCard["cc-exp-year"],
54 expirationString: creditCard["cc-exp"],
57 creditCard["cc-exp-month"] = normalizedExpiration.month ?? "";
58 creditCard["cc-exp-year"] = normalizedExpiration.year ?? "";
59 delete creditCard["cc-exp"];
62 static #normalizeCCTypeFields(creditCard) {
63 // Let's overwrite the credit card type with auto-detect algorithm
64 creditCard["cc-type"] = CreditCard.getType(creditCard["cc-number"]) ?? "";