| 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 "ios/chrome/browser/payments/payment_request.h" | 5 #include "ios/chrome/browser/payments/payment_request.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/containers/adapters.h" | 9 #include "base/containers/adapters.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 profile_comparator_(GetApplicationContext()->GetApplicationLocale(), | 66 profile_comparator_(GetApplicationContext()->GetApplicationLocale(), |
| 67 *this) { | 67 *this) { |
| 68 PopulateAvailableShippingOptions(); | 68 PopulateAvailableShippingOptions(); |
| 69 PopulateProfileCache(); | 69 PopulateProfileCache(); |
| 70 PopulateAvailableProfiles(); | 70 PopulateAvailableProfiles(); |
| 71 PopulateCreditCardCache(); | 71 PopulateCreditCardCache(); |
| 72 PopulateAvailableCreditCards(); | 72 PopulateAvailableCreditCards(); |
| 73 | 73 |
| 74 SetSelectedShippingOption(); | 74 SetSelectedShippingOption(); |
| 75 | 75 |
| 76 // If the merchant provided a default shipping option, and the highest-ranking | 76 if (request_shipping()) { |
| 77 // shipping profile is usable, select it. | 77 // If the merchant provided a default shipping option, and the |
| 78 if (selected_shipping_option_ && !shipping_profiles_.empty() && | 78 // highest-ranking shipping profile is usable, select it. |
| 79 profile_comparator_.IsShippingComplete(shipping_profiles_[0])) { | 79 if (selected_shipping_option_ && !shipping_profiles_.empty() && |
| 80 selected_shipping_profile_ = shipping_profiles_[0]; | 80 profile_comparator_.IsShippingComplete(shipping_profiles_[0])) { |
| 81 selected_shipping_profile_ = shipping_profiles_[0]; |
| 82 } |
| 81 } | 83 } |
| 82 | 84 |
| 83 // If the highest-ranking contact profile is usable, select it. Otherwise, | 85 if (request_payer_name() || request_payer_email() || request_payer_phone()) { |
| 84 // select none. | 86 // If the highest-ranking contact profile is usable, select it. Otherwise, |
| 85 if (!contact_profiles_.empty() && | 87 // select none. |
| 86 profile_comparator_.IsContactInfoComplete(contact_profiles_[0])) { | 88 if (!contact_profiles_.empty() && |
| 87 selected_contact_profile_ = contact_profiles_[0]; | 89 profile_comparator_.IsContactInfoComplete(contact_profiles_[0])) { |
| 90 selected_contact_profile_ = contact_profiles_[0]; |
| 91 } |
| 88 } | 92 } |
| 89 | 93 |
| 90 // TODO(crbug.com/702063): Change this code to prioritize credit cards by use | 94 // TODO(crbug.com/702063): Change this code to prioritize credit cards by use |
| 91 // count and other means. | 95 // count and other means. |
| 92 auto first_complete_credit_card = std::find_if( | 96 auto first_complete_credit_card = std::find_if( |
| 93 credit_cards_.begin(), credit_cards_.end(), | 97 credit_cards_.begin(), credit_cards_.end(), |
| 94 [this](const autofill::CreditCard* credit_card) { | 98 [this](const autofill::CreditCard* credit_card) { |
| 95 DCHECK(credit_card); | 99 DCHECK(credit_card); |
| 96 return payment_request_util::IsCreditCardCompleteForPayment( | 100 return payment_request_util::IsCreditCardCompleteForPayment( |
| 97 *credit_card, billing_profiles()); | 101 *credit_card, billing_profiles()); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 billing_profiles()); | 270 billing_profiles()); |
| 267 // A card only has to have a cardholder name and a number for the purposes | 271 // A card only has to have a cardholder name and a number for the purposes |
| 268 // of CanMakePayment. An expired card or one without a billing address is | 272 // of CanMakePayment. An expired card or one without a billing address is |
| 269 // valid for this purpose. | 273 // valid for this purpose. |
| 270 return !(status & autofill::CREDIT_CARD_NO_CARDHOLDER || | 274 return !(status & autofill::CREDIT_CARD_NO_CARDHOLDER || |
| 271 status & autofill::CREDIT_CARD_NO_NUMBER); | 275 status & autofill::CREDIT_CARD_NO_NUMBER); |
| 272 } | 276 } |
| 273 return false; | 277 return false; |
| 274 } | 278 } |
| 275 | 279 |
| 280 void PaymentRequest::RecordUseStats() { |
| 281 if (request_shipping()) { |
| 282 DCHECK(selected_shipping_profile_); |
| 283 personal_data_manager_->RecordUseOf(*selected_shipping_profile_); |
| 284 } |
| 285 |
| 286 if (request_payer_name() || request_payer_email() || request_payer_phone()) { |
| 287 DCHECK(selected_contact_profile_); |
| 288 // If the same address was used for both contact and shipping, the stats |
| 289 // should be updated only once. |
| 290 if (!request_shipping() || (selected_shipping_profile_->guid() != |
| 291 selected_contact_profile_->guid())) { |
| 292 personal_data_manager_->RecordUseOf(*selected_contact_profile_); |
| 293 } |
| 294 } |
| 295 |
| 296 DCHECK(selected_credit_card_); |
| 297 personal_data_manager_->RecordUseOf(*selected_credit_card_); |
| 298 } |
| 299 |
| 276 void PaymentRequest::PopulateCreditCardCache() { | 300 void PaymentRequest::PopulateCreditCardCache() { |
| 277 for (const payments::PaymentMethodData& method_data_entry : | 301 for (const payments::PaymentMethodData& method_data_entry : |
| 278 web_payment_request_.method_data) { | 302 web_payment_request_.method_data) { |
| 279 for (const std::string& method : method_data_entry.supported_methods) { | 303 for (const std::string& method : method_data_entry.supported_methods) { |
| 280 stringified_method_data_[method].insert(method_data_entry.data); | 304 stringified_method_data_[method].insert(method_data_entry.data); |
| 281 } | 305 } |
| 282 } | 306 } |
| 283 | 307 |
| 284 // TODO(crbug.com/709036): Validate method data. | 308 // TODO(crbug.com/709036): Validate method data. |
| 285 payments::data_util::ParseBasicCardSupportedNetworks( | 309 payments::data_util::ParseBasicCardSupportedNetworks( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 void PaymentRequest::SetSelectedShippingOption() { | 362 void PaymentRequest::SetSelectedShippingOption() { |
| 339 // If more than one option has |selected| set, the last one in the sequence | 363 // If more than one option has |selected| set, the last one in the sequence |
| 340 // should be treated as the selected item. | 364 // should be treated as the selected item. |
| 341 for (auto* shipping_option : base::Reversed(shipping_options_)) { | 365 for (auto* shipping_option : base::Reversed(shipping_options_)) { |
| 342 if (shipping_option->selected) { | 366 if (shipping_option->selected) { |
| 343 selected_shipping_option_ = shipping_option; | 367 selected_shipping_option_ = shipping_option; |
| 344 break; | 368 break; |
| 345 } | 369 } |
| 346 } | 370 } |
| 347 } | 371 } |
| OLD | NEW |