summaryrefslogtreecommitdiffstats
path: root/browser/components/payments/res/containers/payment-method-picker.js
blob: 3693d352a53a19927183e00373ce8707068026e3 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* 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/. */

import BasicCardOption from "../components/basic-card-option.js";
import CscInput from "../components/csc-input.js";
import HandleEventMixin from "../mixins/HandleEventMixin.js";
import RichPicker from "./rich-picker.js";
import paymentRequest from "../paymentRequest.js";

/* import-globals-from ../unprivileged-fallbacks.js */

/**
 * <payment-method-picker></payment-method-picker>
 * Container around add/edit links and <rich-select> with
 * <basic-card-option> listening to savedBasicCards.
 */

export default class PaymentMethodPicker extends HandleEventMixin(RichPicker) {
  constructor() {
    super();
    this.dropdown.setAttribute("option-type", "basic-card-option");
    this.securityCodeInput = new CscInput();
    this.securityCodeInput.className = "security-code-container";
    this.securityCodeInput.placeholder = this.dataset.cscPlaceholder;
    this.securityCodeInput.backTooltip = this.dataset.cscBackTooltip;
    this.securityCodeInput.frontTooltip = this.dataset.cscFrontTooltip;
    this.securityCodeInput.addEventListener("change", this);
    this.securityCodeInput.addEventListener("input", this);
  }

  connectedCallback() {
    super.connectedCallback();
    this.dropdown.after(this.securityCodeInput);
  }

  get fieldNames() {
    let fieldNames = [...BasicCardOption.recordAttributes];
    return fieldNames;
  }

  render(state) {
    let basicCards = paymentRequest.getBasicCards(state);
    let desiredOptions = [];
    for (let [guid, basicCard] of Object.entries(basicCards)) {
      let optionEl = this.dropdown.getOptionByValue(guid);
      if (!optionEl) {
        optionEl = document.createElement("option");
        optionEl.value = guid;
      }

      for (let key of BasicCardOption.recordAttributes) {
        let val = basicCard[key];
        if (val) {
          optionEl.setAttribute(key, val);
        } else {
          optionEl.removeAttribute(key);
        }
      }

      optionEl.textContent = BasicCardOption.formatSingleLineLabel(basicCard);
      desiredOptions.push(optionEl);
    }

    this.dropdown.popupBox.textContent = "";
    for (let option of desiredOptions) {
      this.dropdown.popupBox.appendChild(option);
    }

    // Update selectedness after the options are updated
    let selectedPaymentCardGUID = state[this.selectedStateKey];
    if (selectedPaymentCardGUID) {
      this.dropdown.value = selectedPaymentCardGUID;

      if (selectedPaymentCardGUID !== this.dropdown.value) {
        throw new Error(
          `The option ${selectedPaymentCardGUID} ` +
            `does not exist in the payment method picker`
        );
      }
    } else {
      this.dropdown.value = "";
    }

    let securityCodeState = state[this.selectedStateKey + "SecurityCode"];
    if (
      securityCodeState &&
      securityCodeState != this.securityCodeInput.value
    ) {
      this.securityCodeInput.defaultValue = securityCodeState;
    }

    let selectedCardType =
      (basicCards[selectedPaymentCardGUID] &&
        basicCards[selectedPaymentCardGUID]["cc-type"]) ||
      "";
    this.securityCodeInput.cardType = selectedCardType;

    super.render(state);
  }

  errorForSelectedOption(state) {
    let superError = super.errorForSelectedOption(state);
    if (superError) {
      return superError;
    }
    let selectedOption = this.selectedOption;
    if (!selectedOption) {
      return "";
    }

    let basicCardMethod = state.request.paymentMethods.find(
      method => method.supportedMethods == "basic-card"
    );
    let merchantNetworks =
      basicCardMethod &&
      basicCardMethod.data &&
      basicCardMethod.data.supportedNetworks;
    let acceptedNetworks =
      merchantNetworks || PaymentDialogUtils.getCreditCardNetworks();
    let selectedCard = paymentRequest.getBasicCards(state)[
      selectedOption.value
    ];
    let isSupported =
      selectedCard["cc-type"] &&
      acceptedNetworks.includes(selectedCard["cc-type"]);
    return isSupported ? "" : this.dataset.invalidLabel;
  }

  get selectedStateKey() {
    return this.getAttribute("selected-state-key");
  }

  onInput(event) {
    this.onInputOrChange(event);
  }

  onChange(event) {
    this.onInputOrChange(event);
  }

  onInputOrChange({ currentTarget }) {
    let selectedKey = this.selectedStateKey;
    let stateChange = {};

    if (!selectedKey) {
      return;
    }

    switch (currentTarget) {
      case this.dropdown: {
        stateChange[selectedKey] = this.dropdown.value;
        break;
      }
      case this.securityCodeInput: {
        stateChange[
          selectedKey + "SecurityCode"
        ] = this.securityCodeInput.value;
        break;
      }
      default: {
        return;
      }
    }

    this.requestStore.setState(stateChange);
  }

  onClick({ target }) {
    let nextState = {
      page: {
        id: "basic-card-page",
      },
      "basic-card-page": {
        selectedStateKey: this.selectedStateKey,
      },
    };

    switch (target) {
      case this.addLink: {
        nextState["basic-card-page"].guid = null;
        break;
      }
      case this.editLink: {
        let state = this.requestStore.getState();
        let selectedPaymentCardGUID = state[this.selectedStateKey];
        nextState["basic-card-page"].guid = selectedPaymentCardGUID;
        break;
      }
      default: {
        throw new Error("Unexpected onClick");
      }
    }

    this.requestStore.setState(nextState);
  }
}

customElements.define("payment-method-picker", PaymentMethodPicker);