| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/core/payment_request_data_util.h" | 5 #include "components/payments/core/payment_request_data_util.h" |
| 6 | 6 |
| 7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
| 8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_profile.h" | 10 #include "components/autofill/core/browser/autofill_profile.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 return response; | 95 return response; |
| 96 } | 96 } |
| 97 | 97 |
| 98 void ParseBasicCardSupportedNetworks( | 98 void ParseBasicCardSupportedNetworks( |
| 99 const std::vector<PaymentMethodData>& method_data, | 99 const std::vector<PaymentMethodData>& method_data, |
| 100 std::vector<std::string>* out_supported_networks, | 100 std::vector<std::string>* out_supported_networks, |
| 101 std::set<std::string>* out_basic_card_specified_networks) { | 101 std::set<std::string>* out_basic_card_specified_networks) { |
| 102 DCHECK(out_supported_networks->empty()); | 102 DCHECK(out_supported_networks->empty()); |
| 103 DCHECK(out_basic_card_specified_networks->empty()); | 103 DCHECK(out_basic_card_specified_networks->empty()); |
| 104 | 104 |
| 105 std::set<std::string> card_networks{"amex", "diners", "discover", | 105 const std::set<std::string> kBasicCardNetworks{ |
| 106 "jcb", "mastercard", "mir", | 106 "amex", "diners", "discover", "jcb", |
| 107 "unionpay", "visa"}; | 107 "mastercard", "mir", "unionpay", "visa"}; |
| 108 std::set<std::string> remaining_card_networks(kBasicCardNetworks); |
| 108 for (const PaymentMethodData& method_data_entry : method_data) { | 109 for (const PaymentMethodData& method_data_entry : method_data) { |
| 109 if (method_data_entry.supported_methods.empty()) | 110 if (method_data_entry.supported_methods.empty()) |
| 110 return; | 111 return; |
| 111 | 112 |
| 112 for (const std::string& method : method_data_entry.supported_methods) { | 113 for (const std::string& method : method_data_entry.supported_methods) { |
| 113 if (method.empty()) | 114 if (method.empty()) |
| 114 continue; | 115 continue; |
| 115 | 116 |
| 117 const char kBasicCardMethodName[] = "basic-card"; |
| 116 // If a card network is specified right in "supportedMethods", add it. | 118 // If a card network is specified right in "supportedMethods", add it. |
| 117 const char kBasicCardMethodName[] = "basic-card"; | 119 auto card_it = remaining_card_networks.find(method); |
| 118 auto card_it = card_networks.find(method); | 120 if (card_it != remaining_card_networks.end()) { |
| 119 if (card_it != card_networks.end()) { | |
| 120 out_supported_networks->push_back(method); | 121 out_supported_networks->push_back(method); |
| 121 // |method| removed from |card_networks| so that it is not doubly added | 122 // |method| removed from |remaining_card_networks| so that it is not |
| 122 // to |supported_card_networks_| if "basic-card" is specified with no | 123 // doubly added to |out_supported_networks|. |
| 123 // supported networks. | 124 remaining_card_networks.erase(card_it); |
| 124 card_networks.erase(card_it); | |
| 125 } else if (method == kBasicCardMethodName) { | 125 } else if (method == kBasicCardMethodName) { |
| 126 // For the "basic-card" method, check "supportedNetworks". | 126 // For the "basic-card" method, check "supportedNetworks". |
| 127 if (method_data_entry.supported_networks.empty()) { | 127 if (method_data_entry.supported_networks.empty()) { |
| 128 // Empty |supported_networks| means all networks are supported. | 128 // Empty |supported_networks| means all networks are supported. |
| 129 out_supported_networks->insert(out_supported_networks->end(), | 129 out_supported_networks->insert(out_supported_networks->end(), |
| 130 card_networks.begin(), | 130 remaining_card_networks.begin(), |
| 131 card_networks.end()); | 131 remaining_card_networks.end()); |
| 132 out_basic_card_specified_networks->insert(card_networks.begin(), | 132 out_basic_card_specified_networks->insert(kBasicCardNetworks.begin(), |
| 133 card_networks.end()); | 133 kBasicCardNetworks.end()); |
| 134 // Clear the set so that no further networks are added to | 134 // Clear the set so that no further networks are added to |
| 135 // |out_supported_networks|. | 135 // |out_supported_networks|. |
| 136 card_networks.clear(); | 136 remaining_card_networks.clear(); |
| 137 } else { | 137 } else { |
| 138 // The merchant has specified a few basic card supported networks. Use | 138 // The merchant has specified a few basic card supported networks. Use |
| 139 // the mapping to transform to known basic-card types. | 139 // the mapping to transform to known basic-card types. |
| 140 for (const std::string& supported_network : | 140 for (const std::string& supported_network : |
| 141 method_data_entry.supported_networks) { | 141 method_data_entry.supported_networks) { |
| 142 // Make sure that the network was not already added to | 142 // Make sure that the network was not already added to |
| 143 // |out_supported_networks|. If it's still in |card_networks| it's | 143 // |out_supported_networks|. If it's still in |
| 144 // fair game. | 144 // |remaining_card_networks| it's fair game. |
| 145 auto it = card_networks.find(supported_network); | 145 auto it = remaining_card_networks.find(supported_network); |
| 146 if (it != card_networks.end()) { | 146 if (it != remaining_card_networks.end()) { |
| 147 out_supported_networks->push_back(supported_network); | 147 out_supported_networks->push_back(supported_network); |
| 148 remaining_card_networks.erase(it); |
| 149 } |
| 150 if (kBasicCardNetworks.find(supported_network) != |
| 151 kBasicCardNetworks.end()) { |
| 148 out_basic_card_specified_networks->insert(supported_network); | 152 out_basic_card_specified_networks->insert(supported_network); |
| 149 card_networks.erase(it); | |
| 150 } | 153 } |
| 151 } | 154 } |
| 152 } | 155 } |
| 153 } | 156 } |
| 154 } | 157 } |
| 155 } | 158 } |
| 156 } | 159 } |
| 157 | 160 |
| 158 std::string FormatPhoneForDisplay(const std::string& phone_number, | 161 std::string FormatPhoneForDisplay(const std::string& phone_number, |
| 159 const std::string& country_code) { | 162 const std::string& country_code) { |
| 160 return FormatPhoneNumber(phone_number, country_code, | 163 return FormatPhoneNumber(phone_number, country_code, |
| 161 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL); | 164 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL); |
| 162 } | 165 } |
| 163 | 166 |
| 164 std::string FormatPhoneForResponse(const std::string& phone_number, | 167 std::string FormatPhoneForResponse(const std::string& phone_number, |
| 165 const std::string& country_code) { | 168 const std::string& country_code) { |
| 166 return FormatPhoneNumber(phone_number, country_code, | 169 return FormatPhoneNumber(phone_number, country_code, |
| 167 PhoneNumberUtil::PhoneNumberFormat::E164); | 170 PhoneNumberUtil::PhoneNumberFormat::E164); |
| 168 } | 171 } |
| 169 | 172 |
| 170 } // namespace data_util | 173 } // namespace data_util |
| 171 } // namespace payments | 174 } // namespace payments |
| OLD | NEW |