| 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/payment_request.h" | 5 #include "components/payments/payment_request.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "components/payments/payment_details_validation.h" | 8 #include "components/payments/payment_details_validation.h" |
| 9 #include "components/payments/payment_request_web_contents_manager.h" | 9 #include "components/payments/payment_request_web_contents_manager.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 | 12 |
| 13 using payments::mojom::BasicCardNetwork; |
| 14 |
| 13 namespace payments { | 15 namespace payments { |
| 14 | 16 |
| 17 namespace { |
| 18 |
| 19 // Identifier for the basic card payment method in the PaymentMethodData. |
| 20 const char* const kBasicCardMethodName = "basic-card"; |
| 21 |
| 22 } // namespace |
| 23 |
| 15 PaymentRequest::PaymentRequest( | 24 PaymentRequest::PaymentRequest( |
| 16 content::WebContents* web_contents, | 25 content::WebContents* web_contents, |
| 17 std::unique_ptr<PaymentRequestDelegate> delegate, | 26 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 18 PaymentRequestWebContentsManager* manager, | 27 PaymentRequestWebContentsManager* manager, |
| 19 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | 28 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
| 20 : web_contents_(web_contents), | 29 : web_contents_(web_contents), |
| 21 delegate_(std::move(delegate)), | 30 delegate_(std::move(delegate)), |
| 22 manager_(manager), | 31 manager_(manager), |
| 23 binding_(this, std::move(request)), | 32 binding_(this, std::move(request)), |
| 24 selected_shipping_profile_(nullptr), | 33 selected_shipping_profile_(nullptr), |
| 25 selected_contact_profile_(nullptr) { | 34 selected_contact_profile_(nullptr) { |
| 26 // OnConnectionTerminated will be called when the Mojo pipe is closed. This | 35 // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
| 27 // will happen as a result of many renderer-side events (both successful and | 36 // will happen as a result of many renderer-side events (both successful and |
| 28 // erroneous in nature). | 37 // erroneous in nature). |
| 29 // TODO(crbug.com/683636): Investigate using | 38 // TODO(crbug.com/683636): Investigate using |
| 30 // set_connection_error_with_reason_handler with Binding::CloseWithReason. | 39 // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
| 31 binding_.set_connection_error_handler(base::Bind( | 40 binding_.set_connection_error_handler(base::Bind( |
| 32 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); | 41 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); |
| 33 | 42 |
| 34 } | 43 } |
| 35 | 44 |
| 36 PaymentRequest::~PaymentRequest() {} | 45 PaymentRequest::~PaymentRequest() {} |
| 37 | 46 |
| 38 void PaymentRequest::Init( | 47 void PaymentRequest::Init( |
| 39 payments::mojom::PaymentRequestClientPtr client, | 48 payments::mojom::PaymentRequestClientPtr client, |
| 40 std::vector<payments::mojom::PaymentMethodDataPtr> methodData, | 49 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, |
| 41 payments::mojom::PaymentDetailsPtr details, | 50 payments::mojom::PaymentDetailsPtr details, |
| 42 payments::mojom::PaymentOptionsPtr options) { | 51 payments::mojom::PaymentOptionsPtr options) { |
| 43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 44 std::string error; | 53 std::string error; |
| 45 if (!payments::validatePaymentDetails(details, &error)) { | 54 if (!payments::validatePaymentDetails(details, &error)) { |
| 46 LOG(ERROR) << error; | 55 LOG(ERROR) << error; |
| 47 OnConnectionTerminated(); | 56 OnConnectionTerminated(); |
| 48 return; | 57 return; |
| 49 } | 58 } |
| 50 client_ = std::move(client); | 59 client_ = std::move(client); |
| 51 details_ = std::move(details); | 60 details_ = std::move(details); |
| 61 PopulateValidatedMethodData(method_data); |
| 52 PopulateProfileCache(); | 62 PopulateProfileCache(); |
| 53 SetDefaultProfileSelections(); | 63 SetDefaultProfileSelections(); |
| 54 } | 64 } |
| 55 | 65 |
| 56 void PaymentRequest::Show() { | 66 void PaymentRequest::Show() { |
| 57 if (!client_.is_bound() || !binding_.is_bound()) { | 67 if (!client_.is_bound() || !binding_.is_bound()) { |
| 58 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; | 68 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 59 OnConnectionTerminated(); | 69 OnConnectionTerminated(); |
| 60 return; | 70 return; |
| 61 } | 71 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 163 } |
| 154 | 164 |
| 155 void PaymentRequest::SetDefaultProfileSelections() { | 165 void PaymentRequest::SetDefaultProfileSelections() { |
| 156 if (!shipping_profiles().empty()) | 166 if (!shipping_profiles().empty()) |
| 157 set_selected_shipping_profile(shipping_profiles()[0]); | 167 set_selected_shipping_profile(shipping_profiles()[0]); |
| 158 | 168 |
| 159 if (!contact_profiles().empty()) | 169 if (!contact_profiles().empty()) |
| 160 set_selected_contact_profile(contact_profiles()[0]); | 170 set_selected_contact_profile(contact_profiles()[0]); |
| 161 } | 171 } |
| 162 | 172 |
| 173 void PaymentRequest::PopulateValidatedMethodData( |
| 174 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) { |
| 175 if (method_data.empty()) { |
| 176 LOG(ERROR) << "Invalid payment methods or data"; |
| 177 OnConnectionTerminated(); |
| 178 return; |
| 179 } |
| 180 |
| 181 std::set<std::string> card_networks{"amex", "diners", "discover", |
| 182 "jcb", "mastercard", "mir", |
| 183 "unionpay", "visa"}; |
| 184 for (const payments::mojom::PaymentMethodDataPtr& method_data_entry : |
| 185 method_data) { |
| 186 std::vector<std::string> supported_methods = |
| 187 method_data_entry->supported_methods; |
| 188 if (supported_methods.empty()) { |
| 189 LOG(ERROR) << "Invalid payment methods or data"; |
| 190 OnConnectionTerminated(); |
| 191 return; |
| 192 } |
| 193 |
| 194 for (const std::string& method : supported_methods) { |
| 195 if (method.empty()) |
| 196 continue; |
| 197 |
| 198 // If a card network is specified right in "supportedMethods", add it. |
| 199 auto card_it = card_networks.find(method); |
| 200 if (card_it != card_networks.end()) { |
| 201 supported_card_networks_.push_back(method); |
| 202 // |method| removed from |card_networks| so that it is not doubly added |
| 203 // to |supported_card_networks_| if "basic-card" is specified with no |
| 204 // supported networks. |
| 205 card_networks.erase(card_it); |
| 206 } else if (method == kBasicCardMethodName) { |
| 207 // For the "basic-card" method, check "supportedNetworks". |
| 208 if (method_data_entry->supported_networks.empty()) { |
| 209 // Empty |supported_networks| means all networks are supported. |
| 210 supported_card_networks_.insert(supported_card_networks_.end(), |
| 211 card_networks.begin(), |
| 212 card_networks.end()); |
| 213 // Clear the set so that no further networks are added to |
| 214 // |supported_card_networks_|. |
| 215 card_networks.clear(); |
| 216 } else { |
| 217 // The merchant has specified a few basic card supported networks. Use |
| 218 // the mapping to transform to known basic-card types. |
| 219 std::unordered_map<BasicCardNetwork, std::string> networks = { |
| 220 {BasicCardNetwork::AMEX, "amex"}, |
| 221 {BasicCardNetwork::DINERS, "diners"}, |
| 222 {BasicCardNetwork::DISCOVER, "discover"}, |
| 223 {BasicCardNetwork::JCB, "jcb"}, |
| 224 {BasicCardNetwork::MASTERCARD, "mastercard"}, |
| 225 {BasicCardNetwork::MIR, "mir"}, |
| 226 {BasicCardNetwork::UNIONPAY, "unionpay"}, |
| 227 {BasicCardNetwork::VISA, "visa"}}; |
| 228 for (const BasicCardNetwork& supported_network : |
| 229 method_data_entry->supported_networks) { |
| 230 // Make sure that the network was not already added to |
| 231 // |supported_card_networks_|. |
| 232 auto card_it = card_networks.find(networks[supported_network]); |
| 233 if (card_it != card_networks.end()) { |
| 234 supported_card_networks_.push_back(networks[supported_network]); |
| 235 card_networks.erase(card_it); |
| 236 } |
| 237 } |
| 238 } |
| 239 } |
| 240 } |
| 241 } |
| 242 } |
| 243 |
| 163 } // namespace payments | 244 } // namespace payments |
| OLD | NEW |