Chromium Code Reviews| 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 #include "components/payments/core/payment_method_data.h" | |
| 11 #include "components/payments/core/payment_request_data_util.h" | |
| 10 | 12 |
| 11 namespace payments { | 13 namespace payments { |
| 12 | 14 |
| 15 namespace { | |
| 16 | |
| 17 // Returns the card network name associated with a given BasicCardNetwork. Names | |
| 18 // are inspired by https://www.w3.org/Payments/card-network-ids. | |
| 19 std::string GetBasicCardNetworkName(const mojom::BasicCardNetwork& network) { | |
| 20 switch (network) { | |
| 21 case mojom::BasicCardNetwork::AMEX: | |
| 22 return "amex"; | |
|
please use gerrit instead
2017/04/05 19:29:25
Can we define an enum in components/payments/core/
Mathieu
2017/04/05 20:28:33
There is still a moment where we need to convert t
Mathieu
2017/04/05 20:32:44
Actually this string here doesn't make its way to
| |
| 23 case mojom::BasicCardNetwork::DINERS: | |
| 24 return "diners"; | |
| 25 case mojom::BasicCardNetwork::DISCOVER: | |
| 26 return "discover"; | |
| 27 case mojom::BasicCardNetwork::JCB: | |
| 28 return "jcb"; | |
| 29 case mojom::BasicCardNetwork::MASTERCARD: | |
| 30 return "mastercard"; | |
| 31 case mojom::BasicCardNetwork::MIR: | |
| 32 return "mir"; | |
| 33 case mojom::BasicCardNetwork::UNIONPAY: | |
| 34 return "unionpay"; | |
| 35 case mojom::BasicCardNetwork::VISA: | |
| 36 return "visa"; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 13 const char kBasicCardMethodName[] = "basic-card"; | 42 const char kBasicCardMethodName[] = "basic-card"; |
| 14 | 43 |
| 15 PaymentRequestSpec::PaymentRequestSpec( | 44 PaymentRequestSpec::PaymentRequestSpec( |
| 16 mojom::PaymentOptionsPtr options, | 45 mojom::PaymentOptionsPtr options, |
| 17 mojom::PaymentDetailsPtr details, | 46 mojom::PaymentDetailsPtr details, |
| 18 std::vector<mojom::PaymentMethodDataPtr> method_data, | 47 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 19 Observer* observer, | 48 Observer* observer, |
| 20 const std::string& app_locale) | 49 const std::string& app_locale) |
| 21 : options_(std::move(options)), | 50 : options_(std::move(options)), |
| 22 details_(std::move(details)), | 51 details_(std::move(details)), |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 | 119 |
| 91 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { | 120 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { |
| 92 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( | 121 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
| 93 details_->total->amount->currency, | 122 details_->total->amount->currency, |
| 94 details_->total->amount->currency_system, app_locale_); | 123 details_->total->amount->currency_system, app_locale_); |
| 95 | 124 |
| 96 return formatter->formatted_currency_code(); | 125 return formatter->formatted_currency_code(); |
| 97 } | 126 } |
| 98 | 127 |
| 99 void PaymentRequestSpec::PopulateValidatedMethodData( | 128 void PaymentRequestSpec::PopulateValidatedMethodData( |
| 100 const std::vector<mojom::PaymentMethodDataPtr>& method_data) { | 129 const std::vector<mojom::PaymentMethodDataPtr>& method_data_mojom) { |
| 101 if (method_data.empty()) { | 130 if (method_data_mojom.empty()) { |
| 102 LOG(ERROR) << "Invalid payment methods or data"; | 131 LOG(ERROR) << "Invalid payment methods or data"; |
| 103 NotifyOnInvalidSpecProvided(); | 132 NotifyOnInvalidSpecProvided(); |
| 104 return; | 133 return; |
| 105 } | 134 } |
| 106 | 135 |
| 107 std::set<std::string> card_networks{"amex", "diners", "discover", | 136 std::vector<PaymentMethodData> method_data_vector; |
| 108 "jcb", "mastercard", "mir", | 137 method_data_vector.reserve(method_data_mojom.size()); |
| 109 "unionpay", "visa"}; | 138 for (const mojom::PaymentMethodDataPtr& method_data_entry : |
| 110 for (const mojom::PaymentMethodDataPtr& method_data_entry : method_data) { | 139 method_data_mojom) { |
| 111 std::vector<std::string> supported_methods = | 140 PaymentMethodData method_data; |
| 112 method_data_entry->supported_methods; | 141 method_data.supported_methods = method_data_entry->supported_methods; |
| 113 if (supported_methods.empty()) { | 142 // Transfer the supported basic card networks. |
| 114 LOG(ERROR) << "Invalid payment methods or data"; | 143 std::vector<std::string> supported_networks; |
| 115 NotifyOnInvalidSpecProvided(); | 144 for (const mojom::BasicCardNetwork& network : |
| 116 return; | 145 method_data_entry->supported_networks) { |
| 146 supported_networks.push_back(GetBasicCardNetworkName(network)); | |
| 117 } | 147 } |
| 148 method_data.supported_networks = std::move(supported_networks); | |
|
please use gerrit instead
2017/04/05 19:29:25
What happens when moving a vector of strings, exac
Mathieu
2017/04/05 20:28:33
I'm moving this local variable to avoid a copy, es
| |
| 118 | 149 |
| 119 for (const std::string& method : supported_methods) { | 150 // TODO(crbug.com/708603): Add browser-side support for |
| 120 if (method.empty()) | 151 // |method_data.supported_types|. |
| 121 continue; | 152 method_data_vector.push_back(std::move(method_data)); |
| 122 | |
| 123 // If a card network is specified right in "supportedMethods", add it. | |
| 124 auto card_it = card_networks.find(method); | |
| 125 if (card_it != card_networks.end()) { | |
| 126 supported_card_networks_.push_back(method); | |
| 127 // |method| removed from |card_networks| so that it is not doubly added | |
| 128 // to |supported_card_networks_| if "basic-card" is specified with no | |
| 129 // supported networks. | |
| 130 card_networks.erase(card_it); | |
| 131 } else if (method == kBasicCardMethodName) { | |
| 132 // For the "basic-card" method, check "supportedNetworks". | |
| 133 if (method_data_entry->supported_networks.empty()) { | |
| 134 // Empty |supported_networks| means all networks are supported. | |
| 135 supported_card_networks_.insert(supported_card_networks_.end(), | |
| 136 card_networks.begin(), | |
| 137 card_networks.end()); | |
| 138 basic_card_specified_networks_.insert(card_networks.begin(), | |
| 139 card_networks.end()); | |
| 140 // Clear the set so that no further networks are added to | |
| 141 // |supported_card_networks_|. | |
| 142 card_networks.clear(); | |
| 143 } else { | |
| 144 // The merchant has specified a few basic card supported networks. Use | |
| 145 // the mapping to transform to known basic-card types. | |
| 146 using mojom::BasicCardNetwork; | |
| 147 std::unordered_map<BasicCardNetwork, std::string> networks = { | |
| 148 {BasicCardNetwork::AMEX, "amex"}, | |
| 149 {BasicCardNetwork::DINERS, "diners"}, | |
| 150 {BasicCardNetwork::DISCOVER, "discover"}, | |
| 151 {BasicCardNetwork::JCB, "jcb"}, | |
| 152 {BasicCardNetwork::MASTERCARD, "mastercard"}, | |
| 153 {BasicCardNetwork::MIR, "mir"}, | |
| 154 {BasicCardNetwork::UNIONPAY, "unionpay"}, | |
| 155 {BasicCardNetwork::VISA, "visa"}}; | |
| 156 for (const BasicCardNetwork& supported_network : | |
| 157 method_data_entry->supported_networks) { | |
| 158 // Make sure that the network was not already added to | |
| 159 // |supported_card_networks_|. | |
| 160 auto card_it = card_networks.find(networks[supported_network]); | |
| 161 if (card_it != card_networks.end()) { | |
| 162 supported_card_networks_.push_back(networks[supported_network]); | |
| 163 basic_card_specified_networks_.insert( | |
| 164 networks[supported_network]); | |
| 165 card_networks.erase(card_it); | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 } | |
| 170 } | |
| 171 } | 153 } |
| 172 | 154 |
| 155 if (!data_util::ParseBasicCardSupportedNetworks( | |
| 156 method_data_vector, &supported_card_networks_, | |
| 157 &basic_card_specified_networks_)) { | |
| 158 LOG(ERROR) << "Invalid payment methods or data"; | |
| 159 NotifyOnInvalidSpecProvided(); | |
| 160 return; | |
| 161 } | |
| 173 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 162 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
| 174 supported_card_networks_.end()); | 163 supported_card_networks_.end()); |
| 175 } | 164 } |
| 176 | 165 |
| 177 void PaymentRequestSpec::UpdateSelectedShippingOption() { | 166 void PaymentRequestSpec::UpdateSelectedShippingOption() { |
| 178 if (!request_shipping()) | 167 if (!request_shipping()) |
| 179 return; | 168 return; |
| 180 | 169 |
| 181 // As per the spec, the selected shipping option should initially be the last | 170 // As per the spec, the selected shipping option should initially be the last |
| 182 // one in the array that has its selected field set to true. | 171 // one in the array that has its selected field set to true. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 209 const std::string& currency_system, | 198 const std::string& currency_system, |
| 210 const std::string& locale_name) { | 199 const std::string& locale_name) { |
| 211 if (!currency_formatter_) { | 200 if (!currency_formatter_) { |
| 212 currency_formatter_.reset( | 201 currency_formatter_.reset( |
| 213 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 202 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 214 } | 203 } |
| 215 return currency_formatter_.get(); | 204 return currency_formatter_.get(); |
| 216 } | 205 } |
| 217 | 206 |
| 218 } // namespace payments | 207 } // namespace payments |
| OLD | NEW |