OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/payments/content/payment_request_spec.h" | 5 #include "components/payments/content/payment_request_spec.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace payments { | 11 namespace payments { |
12 | 12 |
13 namespace { | 13 const char kBasicCardMethodName[] = "basic-card"; |
14 // Identifier for the basic card payment method in the PaymentMethodData. | |
15 static const char* const kBasicCardMethodName = "basic-card"; | |
16 } // namespace | |
17 | 14 |
18 PaymentRequestSpec::PaymentRequestSpec( | 15 PaymentRequestSpec::PaymentRequestSpec( |
19 mojom::PaymentOptionsPtr options, | 16 mojom::PaymentOptionsPtr options, |
20 mojom::PaymentDetailsPtr details, | 17 mojom::PaymentDetailsPtr details, |
21 std::vector<mojom::PaymentMethodDataPtr> method_data, | 18 std::vector<mojom::PaymentMethodDataPtr> method_data, |
22 Observer* observer, | 19 Observer* observer, |
23 const std::string& app_locale) | 20 const std::string& app_locale) |
24 : options_(std::move(options)), | 21 : options_(std::move(options)), |
25 details_(std::move(details)), | 22 details_(std::move(details)), |
26 app_locale_(app_locale) { | 23 app_locale_(app_locale) { |
27 if (observer) | 24 if (observer) |
28 AddObserver(observer); | 25 AddObserver(observer); |
29 PopulateValidatedMethodData(method_data); | 26 PopulateValidatedMethodData(method_data); |
30 } | 27 } |
31 PaymentRequestSpec::~PaymentRequestSpec() {} | 28 PaymentRequestSpec::~PaymentRequestSpec() {} |
32 | 29 |
33 void PaymentRequestSpec::AddObserver(Observer* observer) { | 30 void PaymentRequestSpec::AddObserver(Observer* observer) { |
34 CHECK(observer); | 31 CHECK(observer); |
35 observers_.AddObserver(observer); | 32 observers_.AddObserver(observer); |
36 } | 33 } |
37 | 34 |
38 void PaymentRequestSpec::RemoveObserver(Observer* observer) { | 35 void PaymentRequestSpec::RemoveObserver(Observer* observer) { |
39 observers_.RemoveObserver(observer); | 36 observers_.RemoveObserver(observer); |
40 } | 37 } |
41 | 38 |
| 39 bool PaymentRequestSpec::IsMethodSupportedThroughBasicCard( |
| 40 const std::string& method_name) { |
| 41 return basic_card_specified_networks_.count(method_name) > 0; |
| 42 } |
| 43 |
42 base::string16 PaymentRequestSpec::GetFormattedCurrencyAmount( | 44 base::string16 PaymentRequestSpec::GetFormattedCurrencyAmount( |
43 const std::string& amount) { | 45 const std::string& amount) { |
44 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( | 46 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
45 details_->total->amount->currency, | 47 details_->total->amount->currency, |
46 details_->total->amount->currency_system, app_locale_); | 48 details_->total->amount->currency_system, app_locale_); |
47 return formatter->Format(amount); | 49 return formatter->Format(amount); |
48 } | 50 } |
49 | 51 |
50 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { | 52 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { |
51 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( | 53 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // to |supported_card_networks_| if "basic-card" is specified with no | 89 // to |supported_card_networks_| if "basic-card" is specified with no |
88 // supported networks. | 90 // supported networks. |
89 card_networks.erase(card_it); | 91 card_networks.erase(card_it); |
90 } else if (method == kBasicCardMethodName) { | 92 } else if (method == kBasicCardMethodName) { |
91 // For the "basic-card" method, check "supportedNetworks". | 93 // For the "basic-card" method, check "supportedNetworks". |
92 if (method_data_entry->supported_networks.empty()) { | 94 if (method_data_entry->supported_networks.empty()) { |
93 // Empty |supported_networks| means all networks are supported. | 95 // Empty |supported_networks| means all networks are supported. |
94 supported_card_networks_.insert(supported_card_networks_.end(), | 96 supported_card_networks_.insert(supported_card_networks_.end(), |
95 card_networks.begin(), | 97 card_networks.begin(), |
96 card_networks.end()); | 98 card_networks.end()); |
| 99 basic_card_specified_networks_.insert(card_networks.begin(), |
| 100 card_networks.end()); |
97 // Clear the set so that no further networks are added to | 101 // Clear the set so that no further networks are added to |
98 // |supported_card_networks_|. | 102 // |supported_card_networks_|. |
99 card_networks.clear(); | 103 card_networks.clear(); |
100 } else { | 104 } else { |
101 // The merchant has specified a few basic card supported networks. Use | 105 // The merchant has specified a few basic card supported networks. Use |
102 // the mapping to transform to known basic-card types. | 106 // the mapping to transform to known basic-card types. |
103 using mojom::BasicCardNetwork; | 107 using mojom::BasicCardNetwork; |
104 std::unordered_map<BasicCardNetwork, std::string> networks = { | 108 std::unordered_map<BasicCardNetwork, std::string> networks = { |
105 {BasicCardNetwork::AMEX, "amex"}, | 109 {BasicCardNetwork::AMEX, "amex"}, |
106 {BasicCardNetwork::DINERS, "diners"}, | 110 {BasicCardNetwork::DINERS, "diners"}, |
107 {BasicCardNetwork::DISCOVER, "discover"}, | 111 {BasicCardNetwork::DISCOVER, "discover"}, |
108 {BasicCardNetwork::JCB, "jcb"}, | 112 {BasicCardNetwork::JCB, "jcb"}, |
109 {BasicCardNetwork::MASTERCARD, "mastercard"}, | 113 {BasicCardNetwork::MASTERCARD, "mastercard"}, |
110 {BasicCardNetwork::MIR, "mir"}, | 114 {BasicCardNetwork::MIR, "mir"}, |
111 {BasicCardNetwork::UNIONPAY, "unionpay"}, | 115 {BasicCardNetwork::UNIONPAY, "unionpay"}, |
112 {BasicCardNetwork::VISA, "visa"}}; | 116 {BasicCardNetwork::VISA, "visa"}}; |
113 for (const BasicCardNetwork& supported_network : | 117 for (const BasicCardNetwork& supported_network : |
114 method_data_entry->supported_networks) { | 118 method_data_entry->supported_networks) { |
115 // Make sure that the network was not already added to | 119 // Make sure that the network was not already added to |
116 // |supported_card_networks_|. | 120 // |supported_card_networks_|. |
117 auto card_it = card_networks.find(networks[supported_network]); | 121 auto card_it = card_networks.find(networks[supported_network]); |
118 if (card_it != card_networks.end()) { | 122 if (card_it != card_networks.end()) { |
119 supported_card_networks_.push_back(networks[supported_network]); | 123 supported_card_networks_.push_back(networks[supported_network]); |
| 124 basic_card_specified_networks_.insert( |
| 125 networks[supported_network]); |
120 card_networks.erase(card_it); | 126 card_networks.erase(card_it); |
121 } | 127 } |
122 } | 128 } |
123 } | 129 } |
124 } | 130 } |
125 } | 131 } |
126 } | 132 } |
127 | 133 |
128 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 134 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
129 supported_card_networks_.end()); | 135 supported_card_networks_.end()); |
130 } | 136 } |
131 | 137 |
132 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { | 138 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { |
133 for (auto& observer : observers_) | 139 for (auto& observer : observers_) |
134 observer.OnInvalidSpecProvided(); | 140 observer.OnInvalidSpecProvided(); |
135 } | 141 } |
136 | 142 |
137 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( | 143 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
138 const std::string& currency_code, | 144 const std::string& currency_code, |
139 const std::string& currency_system, | 145 const std::string& currency_system, |
140 const std::string& locale_name) { | 146 const std::string& locale_name) { |
141 if (!currency_formatter_) { | 147 if (!currency_formatter_) { |
142 currency_formatter_.reset( | 148 currency_formatter_.reset( |
143 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 149 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
144 } | 150 } |
145 return currency_formatter_.get(); | 151 return currency_formatter_.get(); |
146 } | 152 } |
147 | 153 |
148 } // namespace payments | 154 } // namespace payments |
OLD | NEW |