Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / toolkit / components / formautofill / shared / CreditCardRecord.sys.mjs
blob97235e8cddad7828fa8598f7d7d534d531d7dab2
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";
9 /**
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.
13  */
14 export class CreditCardRecord {
15   static normalizeFields(creditCard) {
16     this.#normalizeCCNameFields(creditCard);
17     this.#normalizeCCNumberFields(creditCard);
18     this.#normalizeCCExpirationDateFields(creditCard);
19     this.#normalizeCCTypeFields(creditCard);
20   }
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"] ?? "",
28       });
29     }
31     delete creditCard["cc-given-name"];
32     delete creditCard["cc-additional-name"];
33     delete creditCard["cc-family-name"];
34   }
36   static #normalizeCCNumberFields(creditCard) {
37     if (!("cc-number" in creditCard)) {
38       return;
39     }
41     if (!CreditCard.isValidNumber(creditCard["cc-number"])) {
42       delete creditCard["cc-number"];
43       return;
44     }
46     const card = new CreditCard({ number: creditCard["cc-number"] });
47     creditCard["cc-number"] = card.number;
48   }
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"],
55     });
57     creditCard["cc-exp-month"] = normalizedExpiration.month ?? "";
58     creditCard["cc-exp-year"] = normalizedExpiration.year ?? "";
59     delete creditCard["cc-exp"];
60   }
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"]) ?? "";
65   }