| 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"; |
| 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 NOTREACHED(); |
| 39 return std::string(); |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 13 const char kBasicCardMethodName[] = "basic-card"; | 44 const char kBasicCardMethodName[] = "basic-card"; |
| 14 | 45 |
| 15 PaymentRequestSpec::PaymentRequestSpec( | 46 PaymentRequestSpec::PaymentRequestSpec( |
| 16 mojom::PaymentOptionsPtr options, | 47 mojom::PaymentOptionsPtr options, |
| 17 mojom::PaymentDetailsPtr details, | 48 mojom::PaymentDetailsPtr details, |
| 18 std::vector<mojom::PaymentMethodDataPtr> method_data, | 49 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 19 Observer* observer, | 50 Observer* observer, |
| 20 const std::string& app_locale) | 51 const std::string& app_locale) |
| 21 : options_(std::move(options)), | 52 : options_(std::move(options)), |
| 22 details_(std::move(details)), | 53 details_(std::move(details)), |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 121 |
| 91 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { | 122 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { |
| 92 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( | 123 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
| 93 details_->total->amount->currency, | 124 details_->total->amount->currency, |
| 94 details_->total->amount->currency_system, app_locale_); | 125 details_->total->amount->currency_system, app_locale_); |
| 95 | 126 |
| 96 return formatter->formatted_currency_code(); | 127 return formatter->formatted_currency_code(); |
| 97 } | 128 } |
| 98 | 129 |
| 99 void PaymentRequestSpec::PopulateValidatedMethodData( | 130 void PaymentRequestSpec::PopulateValidatedMethodData( |
| 100 const std::vector<mojom::PaymentMethodDataPtr>& method_data) { | 131 const std::vector<mojom::PaymentMethodDataPtr>& method_data_mojom) { |
| 101 if (method_data.empty()) { | 132 if (method_data_mojom.empty()) { |
| 102 LOG(ERROR) << "Invalid payment methods or data"; | 133 LOG(ERROR) << "Invalid payment methods or data"; |
| 103 NotifyOnInvalidSpecProvided(); | 134 NotifyOnInvalidSpecProvided(); |
| 104 return; | 135 return; |
| 105 } | 136 } |
| 106 | 137 |
| 107 std::set<std::string> card_networks{"amex", "diners", "discover", | 138 std::vector<PaymentMethodData> method_data_vector; |
| 108 "jcb", "mastercard", "mir", | 139 method_data_vector.reserve(method_data_mojom.size()); |
| 109 "unionpay", "visa"}; | 140 for (const mojom::PaymentMethodDataPtr& method_data_entry : |
| 110 for (const mojom::PaymentMethodDataPtr& method_data_entry : method_data) { | 141 method_data_mojom) { |
| 111 std::vector<std::string> supported_methods = | 142 PaymentMethodData method_data; |
| 112 method_data_entry->supported_methods; | 143 method_data.supported_methods = method_data_entry->supported_methods; |
| 113 if (supported_methods.empty()) { | 144 // Transfer the supported basic card networks. |
| 114 LOG(ERROR) << "Invalid payment methods or data"; | 145 std::vector<std::string> supported_networks; |
| 115 NotifyOnInvalidSpecProvided(); | 146 for (const mojom::BasicCardNetwork& network : |
| 116 return; | 147 method_data_entry->supported_networks) { |
| 148 supported_networks.push_back(GetBasicCardNetworkName(network)); |
| 117 } | 149 } |
| 150 method_data.supported_networks = std::move(supported_networks); |
| 118 | 151 |
| 119 for (const std::string& method : supported_methods) { | 152 // TODO(crbug.com/708603): Add browser-side support for |
| 120 if (method.empty()) | 153 // |method_data.supported_types|. |
| 121 continue; | 154 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 } | 155 } |
| 172 | 156 |
| 157 if (!data_util::ParseBasicCardSupportedNetworks( |
| 158 method_data_vector, &supported_card_networks_, |
| 159 &basic_card_specified_networks_)) { |
| 160 LOG(ERROR) << "Invalid payment methods or data"; |
| 161 NotifyOnInvalidSpecProvided(); |
| 162 return; |
| 163 } |
| 173 supported_card_networks_set_.insert(supported_card_networks_.begin(), | 164 supported_card_networks_set_.insert(supported_card_networks_.begin(), |
| 174 supported_card_networks_.end()); | 165 supported_card_networks_.end()); |
| 175 } | 166 } |
| 176 | 167 |
| 177 void PaymentRequestSpec::UpdateSelectedShippingOption() { | 168 void PaymentRequestSpec::UpdateSelectedShippingOption() { |
| 178 if (!request_shipping()) | 169 if (!request_shipping()) |
| 179 return; | 170 return; |
| 180 | 171 |
| 181 // As per the spec, the selected shipping option should initially be the last | 172 // 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. | 173 // 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, | 200 const std::string& currency_system, |
| 210 const std::string& locale_name) { | 201 const std::string& locale_name) { |
| 211 if (!currency_formatter_) { | 202 if (!currency_formatter_) { |
| 212 currency_formatter_.reset( | 203 currency_formatter_.reset( |
| 213 new CurrencyFormatter(currency_code, currency_system, locale_name)); | 204 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 214 } | 205 } |
| 215 return currency_formatter_.get(); | 206 return currency_formatter_.get(); |
| 216 } | 207 } |
| 217 | 208 |
| 218 } // namespace payments | 209 } // namespace payments |
| OLD | NEW |