| 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 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 void PaymentRequestSpec::AddObserver(Observer* observer) { | 33 void PaymentRequestSpec::AddObserver(Observer* observer) { |
| 34 CHECK(observer); | 34 CHECK(observer); |
| 35 observers_.AddObserver(observer); | 35 observers_.AddObserver(observer); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void PaymentRequestSpec::RemoveObserver(Observer* observer) { | 38 void PaymentRequestSpec::RemoveObserver(Observer* observer) { |
| 39 observers_.RemoveObserver(observer); | 39 observers_.RemoveObserver(observer); |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool PaymentRequestSpec::IsMethodSupportedThroughBasicCard( |
| 43 const std::string& method_name) { |
| 44 return basic_card_specified_networks_.count(method_name); |
| 45 } |
| 46 |
| 42 base::string16 PaymentRequestSpec::GetFormattedCurrencyAmount( | 47 base::string16 PaymentRequestSpec::GetFormattedCurrencyAmount( |
| 43 const std::string& amount) { | 48 const std::string& amount) { |
| 44 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( | 49 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
| 45 details_->total->amount->currency, | 50 details_->total->amount->currency, |
| 46 details_->total->amount->currency_system, app_locale_); | 51 details_->total->amount->currency_system, app_locale_); |
| 47 return formatter->Format(amount); | 52 return formatter->Format(amount); |
| 48 } | 53 } |
| 49 | 54 |
| 50 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { | 55 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { |
| 51 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( | 56 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 | 92 // to |supported_card_networks_| if "basic-card" is specified with no |
| 88 // supported networks. | 93 // supported networks. |
| 89 card_networks.erase(card_it); | 94 card_networks.erase(card_it); |
| 90 } else if (method == kBasicCardMethodName) { | 95 } else if (method == kBasicCardMethodName) { |
| 91 // For the "basic-card" method, check "supportedNetworks". | 96 // For the "basic-card" method, check "supportedNetworks". |
| 92 if (method_data_entry->supported_networks.empty()) { | 97 if (method_data_entry->supported_networks.empty()) { |
| 93 // Empty |supported_networks| means all networks are supported. | 98 // Empty |supported_networks| means all networks are supported. |
| 94 supported_card_networks_.insert(supported_card_networks_.end(), | 99 supported_card_networks_.insert(supported_card_networks_.end(), |
| 95 card_networks.begin(), | 100 card_networks.begin(), |
| 96 card_networks.end()); | 101 card_networks.end()); |
| 102 basic_card_specified_networks_.insert(card_networks.begin(), |
| 103 card_networks.end()); |
| 97 // Clear the set so that no further networks are added to | 104 // Clear the set so that no further networks are added to |
| 98 // |supported_card_networks_|. | 105 // |supported_card_networks_|. |
| 99 card_networks.clear(); | 106 card_networks.clear(); |
| 100 } else { | 107 } else { |
| 101 // The merchant has specified a few basic card supported networks. Use | 108 // The merchant has specified a few basic card supported networks. Use |
| 102 // the mapping to transform to known basic-card types. | 109 // the mapping to transform to known basic-card types. |
| 103 using mojom::BasicCardNetwork; | 110 using mojom::BasicCardNetwork; |
| 104 std::unordered_map<BasicCardNetwork, std::string> networks = { | 111 std::unordered_map<BasicCardNetwork, std::string> networks = { |
| 105 {BasicCardNetwork::AMEX, "amex"}, | 112 {BasicCardNetwork::AMEX, "amex"}, |
| 106 {BasicCardNetwork::DINERS, "diners"}, | 113 {BasicCardNetwork::DINERS, "diners"}, |
| 107 {BasicCardNetwork::DISCOVER, "discover"}, | 114 {BasicCardNetwork::DISCOVER, "discover"}, |
| 108 {BasicCardNetwork::JCB, "jcb"}, | 115 {BasicCardNetwork::JCB, "jcb"}, |
| 109 {BasicCardNetwork::MASTERCARD, "mastercard"}, | 116 {BasicCardNetwork::MASTERCARD, "mastercard"}, |
| 110 {BasicCardNetwork::MIR, "mir"}, | 117 {BasicCardNetwork::MIR, "mir"}, |
| 111 {BasicCardNetwork::UNIONPAY, "unionpay"}, | 118 {BasicCardNetwork::UNIONPAY, "unionpay"}, |
| 112 {BasicCardNetwork::VISA, "visa"}}; | 119 {BasicCardNetwork::VISA, "visa"}}; |
| 113 for (const BasicCardNetwork& supported_network : | 120 for (const BasicCardNetwork& supported_network : |
| 114 method_data_entry->supported_networks) { | 121 method_data_entry->supported_networks) { |
| 115 // Make sure that the network was not already added to | 122 // Make sure that the network was not already added to |
| 116 // |supported_card_networks_|. | 123 // |supported_card_networks_|. |
| 117 auto card_it = card_networks.find(networks[supported_network]); | 124 auto card_it = card_networks.find(networks[supported_network]); |
| 118 if (card_it != card_networks.end()) { | 125 if (card_it != card_networks.end()) { |
| 119 supported_card_networks_.push_back(networks[supported_network]); | 126 supported_card_networks_.push_back(networks[supported_network]); |
| 127 basic_card_specified_networks_.insert( |
| 128 networks[supported_network]); |
| 120 card_networks.erase(card_it); | 129 card_networks.erase(card_it); |
| 121 } | 130 } |
| 122 } | 131 } |
| 123 } | 132 } |
| 124 } | 133 } |
| 125 } | 134 } |
| 126 } | 135 } |
| 127 | 136 |
| 128 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 137 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
| 129 supported_card_networks_.end()); | 138 supported_card_networks_.end()); |
| 130 } | 139 } |
| 131 | 140 |
| 132 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { | 141 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { |
| 133 for (auto& observer : observers_) | 142 for (auto& observer : observers_) |
| 134 observer.OnInvalidSpecProvided(); | 143 observer.OnInvalidSpecProvided(); |
| 135 } | 144 } |
| 136 | 145 |
| 137 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( | 146 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
| 138 const std::string& currency_code, | 147 const std::string& currency_code, |
| 139 const std::string& currency_system, | 148 const std::string& currency_system, |
| 140 const std::string& locale_name) { | 149 const std::string& locale_name) { |
| 141 if (!currency_formatter_) { | 150 if (!currency_formatter_) { |
| 142 currency_formatter_.reset( | 151 currency_formatter_.reset( |
| 143 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 152 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 144 } | 153 } |
| 145 return currency_formatter_.get(); | 154 return currency_formatter_.get(); |
| 146 } | 155 } |
| 147 | 156 |
| 148 } // namespace payments | 157 } // namespace payments |
| OLD | NEW |