| 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 "base/strings/utf_string_conversions.h" |
| 10 #include "components/payments/core/payment_method_data.h" | 11 #include "components/payments/core/payment_method_data.h" |
| 11 #include "components/payments/core/payment_request_data_util.h" | 12 #include "components/payments/core/payment_request_data_util.h" |
| 13 #include "components/strings/grit/components_strings.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 12 | 15 |
| 13 namespace payments { | 16 namespace payments { |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 // Returns the card network name associated with a given BasicCardNetwork. Names | 20 // Returns the card network name associated with a given BasicCardNetwork. Names |
| 18 // are inspired by https://www.w3.org/Payments/card-network-ids. | 21 // are inspired by https://www.w3.org/Payments/card-network-ids. |
| 19 std::string GetBasicCardNetworkName(const mojom::BasicCardNetwork& network) { | 22 std::string GetBasicCardNetworkName(const mojom::BasicCardNetwork& network) { |
| 20 switch (network) { | 23 switch (network) { |
| 21 case mojom::BasicCardNetwork::AMEX: | 24 case mojom::BasicCardNetwork::AMEX: |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 &supported_card_networks_, | 162 &supported_card_networks_, |
| 160 &basic_card_specified_networks_); | 163 &basic_card_specified_networks_); |
| 161 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 164 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
| 162 supported_card_networks_.end()); | 165 supported_card_networks_.end()); |
| 163 } | 166 } |
| 164 | 167 |
| 165 void PaymentRequestSpec::UpdateSelectedShippingOption() { | 168 void PaymentRequestSpec::UpdateSelectedShippingOption() { |
| 166 if (!request_shipping()) | 169 if (!request_shipping()) |
| 167 return; | 170 return; |
| 168 | 171 |
| 172 selected_shipping_option_error_.clear(); |
| 169 // As per the spec, the selected shipping option should initially be the last | 173 // As per the spec, the selected shipping option should initially be the last |
| 170 // one in the array that has its selected field set to true. | 174 // one in the array that has its selected field set to true. |
| 171 auto selected_shipping_option_it = std::find_if( | 175 auto selected_shipping_option_it = std::find_if( |
| 172 details().shipping_options.rbegin(), details().shipping_options.rend(), | 176 details().shipping_options.rbegin(), details().shipping_options.rend(), |
| 173 [](const payments::mojom::PaymentShippingOptionPtr& element) { | 177 [](const payments::mojom::PaymentShippingOptionPtr& element) { |
| 174 return element->selected; | 178 return element->selected; |
| 175 }); | 179 }); |
| 176 if (selected_shipping_option_it != details().shipping_options.rend()) { | 180 if (selected_shipping_option_it != details().shipping_options.rend()) { |
| 177 selected_shipping_option_ = selected_shipping_option_it->get(); | 181 selected_shipping_option_ = selected_shipping_option_it->get(); |
| 178 } else { | 182 } else { |
| 179 // It's possible that there is no selected shipping option. | 183 // It's possible that there is no selected shipping option. |
| 180 // TODO(crbug.com/710004): Show an error in this case. | 184 if (!details().error.empty()) { |
| 185 selected_shipping_option_error_ = base::UTF8ToUTF16(details().error); |
| 186 } else { |
| 187 selected_shipping_option_error_ = |
| 188 l10n_util::GetStringUTF16(IDS_PAYMENTS_UNSUPPORTED_SHIPPING_ADDRESS); |
| 189 } |
| 181 selected_shipping_option_ = nullptr; | 190 selected_shipping_option_ = nullptr; |
| 182 } | 191 } |
| 183 } | 192 } |
| 184 | 193 |
| 185 void PaymentRequestSpec::NotifyOnSpecUpdated() { | 194 void PaymentRequestSpec::NotifyOnSpecUpdated() { |
| 186 for (auto& observer : observers_) | 195 for (auto& observer : observers_) |
| 187 observer.OnSpecUpdated(); | 196 observer.OnSpecUpdated(); |
| 188 if (observer_for_testing_) | 197 if (observer_for_testing_) |
| 189 observer_for_testing_->OnSpecUpdated(); | 198 observer_for_testing_->OnSpecUpdated(); |
| 190 } | 199 } |
| 191 | 200 |
| 192 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( | 201 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
| 193 const std::string& currency_code, | 202 const std::string& currency_code, |
| 194 const std::string& currency_system, | 203 const std::string& currency_system, |
| 195 const std::string& locale_name) { | 204 const std::string& locale_name) { |
| 196 if (!currency_formatter_) { | 205 if (!currency_formatter_) { |
| 197 currency_formatter_.reset( | 206 currency_formatter_.reset( |
| 198 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 207 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 199 } | 208 } |
| 200 return currency_formatter_.get(); | 209 return currency_formatter_.get(); |
| 201 } | 210 } |
| 202 | 211 |
| 203 } // namespace payments | 212 } // namespace payments |
| OLD | NEW |