summaryrefslogtreecommitdiffstats
path: root/browser/components/payments/res/unprivileged-fallbacks.js
blob: ee7e47a7dfd98a78ed903bd095b7694d39b743df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * This file defines fallback objects to be used during development outside
 * of the paymentDialogWrapper. When loaded in the wrapper, a frame script
 * overwrites these methods. Since these methods need to get overwritten in the
 * global scope, it can't be converted into an ES module.
 */

/* eslint-disable no-console */
/* exported log, PaymentDialogUtils */

"use strict";

var log = {
  error: console.error.bind(console, "paymentRequest.xhtml:"),
  warn: console.warn.bind(console, "paymentRequest.xhtml:"),
  info: console.info.bind(console, "paymentRequest.xhtml:"),
  debug: console.debug.bind(console, "paymentRequest.xhtml:"),
};

var PaymentDialogUtils = {
  getAddressLabel(address, addressFields = null) {
    if (addressFields) {
      let requestedFields = addressFields.trim().split(/\s+/);
      return (
        requestedFields
          .filter(f => f && address[f])
          .map(f => address[f])
          .join(", ") + ` (${address.guid})`
      );
    }
    return `${address.name} (${address.guid})`;
  },

  getCreditCardNetworks() {
    // Shim for list of known and supported credit card network ids as exposed by
    // toolkit/modules/CreditCard.jsm
    return [
      "amex",
      "cartebancaire",
      "diners",
      "discover",
      "jcb",
      "mastercard",
      "mir",
      "unionpay",
      "visa",
    ];
  },
  isCCNumber(str) {
    return !!str.replace(/[-\s]/g, "").match(/^\d{9,}$/);
  },
  DEFAULT_REGION: "US",
  countries: new Map([
    ["US", "United States"],
    ["CA", "Canada"],
    ["DE", "Germany"],
  ]),
  getFormFormat(country) {
    if (country == "DE") {
      return {
        addressLevel3Label: "suburb",
        addressLevel2Label: "city",
        addressLevel1Label: "province",
        addressLevel1Options: null,
        postalCodeLabel: "postalCode",
        fieldsOrder: [
          {
            fieldId: "name",
            newLine: true,
          },
          {
            fieldId: "organization",
            newLine: true,
          },
          {
            fieldId: "street-address",
            newLine: true,
          },
          { fieldId: "postal-code" },
          { fieldId: "address-level2" },
        ],
        postalCodePattern: "\\d{5}",
        countryRequiredFields: [
          "street-address",
          "address-level2",
          "postal-code",
        ],
      };
    }

    let addressLevel1Options = null;
    if (country == "US") {
      addressLevel1Options = new Map([
        ["CA", "California"],
        ["MA", "Massachusetts"],
        ["MI", "Michigan"],
      ]);
    } else if (country == "CA") {
      addressLevel1Options = new Map([
        ["NS", "Nova Scotia"],
        ["ON", "Ontario"],
        ["YT", "Yukon"],
      ]);
    }

    let fieldsOrder = [
      { fieldId: "name", newLine: true },
      { fieldId: "street-address", newLine: true },
      { fieldId: "address-level2" },
      { fieldId: "address-level1" },
      { fieldId: "postal-code" },
      { fieldId: "organization" },
    ];
    if (country == "BR") {
      fieldsOrder.splice(2, 0, { fieldId: "address-level3" });
    }

    return {
      addressLevel3Label: "suburb",
      addressLevel2Label: "city",
      addressLevel1Label: country == "US" ? "state" : "province",
      addressLevel1Options,
      postalCodeLabel: country == "US" ? "zip" : "postalCode",
      fieldsOrder,
      // The following values come from addressReferences.js and should not be changed.
      /* eslint-disable-next-line max-len */
      postalCodePattern:
        country == "US"
          ? "(\\d{5})(?:[ \\-](\\d{4}))?"
          : "[ABCEGHJKLMNPRSTVXY]\\d[ABCEGHJ-NPRSTV-Z] ?\\d[ABCEGHJ-NPRSTV-Z]\\d",
      countryRequiredFields:
        country == "US" || country == "CA"
          ? [
              "street-address",
              "address-level2",
              "address-level1",
              "postal-code",
            ]
          : ["street-address", "address-level2", "postal-code"],
    };
  },
  findAddressSelectOption(selectEl, address, fieldName) {
    return null;
  },
  getDefaultPreferences() {
    let prefValues = {
      saveCreditCardDefaultChecked: false,
      saveAddressDefaultChecked: true,
    };
    return prefValues;
  },
  isOfficialBranding() {
    return false;
  },
};