| 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_state.h" | 5 #include "components/payments/content/payment_request_state.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" | 10 #include "components/autofill/core/browser/autofill_data_util.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" | 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" | 13 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 #include "components/payments/content/payment_request_spec.h" | 14 #include "components/payments/content/payment_request_spec.h" |
| 15 #include "components/payments/content/payment_response_helper.h" | 15 #include "components/payments/content/payment_response_helper.h" |
| 16 #include "components/payments/core/autofill_payment_instrument.h" | 16 #include "components/payments/core/autofill_payment_instrument.h" |
| 17 #include "components/payments/core/payment_request_delegate.h" | 17 #include "components/payments/core/payment_request_delegate.h" |
| 18 #include "components/payments/core/profile_util.h" |
| 18 | 19 |
| 19 namespace payments { | 20 namespace payments { |
| 20 | 21 |
| 21 PaymentRequestState::PaymentRequestState( | 22 PaymentRequestState::PaymentRequestState( |
| 22 PaymentRequestSpec* spec, | 23 PaymentRequestSpec* spec, |
| 23 Delegate* delegate, | 24 Delegate* delegate, |
| 24 const std::string& app_locale, | 25 const std::string& app_locale, |
| 25 autofill::PersonalDataManager* personal_data_manager, | 26 autofill::PersonalDataManager* personal_data_manager, |
| 26 PaymentRequestDelegate* payment_request_delegate) | 27 PaymentRequestDelegate* payment_request_delegate) |
| 27 : is_ready_to_pay_(false), | 28 : is_ready_to_pay_(false), |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 std::vector<autofill::AutofillProfile*> profiles = | 155 std::vector<autofill::AutofillProfile*> profiles = |
| 155 personal_data_manager_->GetProfilesToSuggest(); | 156 personal_data_manager_->GetProfilesToSuggest(); |
| 156 | 157 |
| 157 // PaymentRequest may outlive the Profiles returned by the Data Manager. | 158 // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 158 // Thus, we store copies, and return a vector of pointers to these copies | 159 // Thus, we store copies, and return a vector of pointers to these copies |
| 159 // whenever Profiles are requested. The same is true for credit cards. | 160 // whenever Profiles are requested. The same is true for credit cards. |
| 160 for (size_t i = 0; i < profiles.size(); i++) { | 161 for (size_t i = 0; i < profiles.size(); i++) { |
| 161 profile_cache_.push_back( | 162 profile_cache_.push_back( |
| 162 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); | 163 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
| 163 | 164 |
| 164 // TODO(tmartino): Implement deduplication rules specific to shipping and | 165 // TODO(tmartino): Implement deduplication rules specific to shipping |
| 165 // contact profiles. | 166 // profiles. |
| 166 shipping_profiles_.push_back(profile_cache_[i].get()); | 167 shipping_profiles_.push_back(profile_cache_[i].get()); |
| 167 contact_profiles_.push_back(profile_cache_[i].get()); | |
| 168 } | 168 } |
| 169 | 169 |
| 170 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering( |
| 171 profile_cache_.size()); |
| 172 std::transform(profile_cache_.begin(), profile_cache_.end(), |
| 173 raw_profiles_for_filtering.begin(), |
| 174 [](const std::unique_ptr<autofill::AutofillProfile>& p) { |
| 175 return p.get(); |
| 176 }); |
| 177 |
| 178 contact_profiles_ = profile_util::FilterProfilesForContact( |
| 179 raw_profiles_for_filtering, GetApplicationLocale(), *spec_); |
| 180 |
| 170 // Create the list of available instruments. | 181 // Create the list of available instruments. |
| 171 const std::vector<autofill::CreditCard*>& cards = | 182 const std::vector<autofill::CreditCard*>& cards = |
| 172 personal_data_manager_->GetCreditCardsToSuggest(); | 183 personal_data_manager_->GetCreditCardsToSuggest(); |
| 173 const std::set<std::string>& supported_card_networks = | 184 const std::set<std::string>& supported_card_networks = |
| 174 spec_->supported_card_networks_set(); | 185 spec_->supported_card_networks_set(); |
| 175 for (autofill::CreditCard* card : cards) { | 186 for (autofill::CreditCard* card : cards) { |
| 176 std::string basic_card_network = | 187 std::string basic_card_network = |
| 177 autofill::data_util::GetPaymentRequestData(card->type()) | 188 autofill::data_util::GetPaymentRequestData(card->type()) |
| 178 .basic_card_payment_type; | 189 .basic_card_payment_type; |
| 179 if (!supported_card_networks.count(basic_card_network)) | 190 if (!supported_card_networks.count(basic_card_network)) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 // instruments are listed/created in the flow. | 246 // instruments are listed/created in the flow. |
| 236 // TODO(crbug.com/702063): A masked card may not satisfy IsValid(). | 247 // TODO(crbug.com/702063): A masked card may not satisfy IsValid(). |
| 237 return selected_instrument_ != nullptr && selected_instrument_->IsValid(); | 248 return selected_instrument_ != nullptr && selected_instrument_->IsValid(); |
| 238 } | 249 } |
| 239 | 250 |
| 240 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 251 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| 241 // TODO(mathp): Have a measure of shipping address completeness. | 252 // TODO(mathp): Have a measure of shipping address completeness. |
| 242 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 253 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
| 243 return false; | 254 return false; |
| 244 | 255 |
| 245 // TODO(mathp): Make an encompassing class to validate contact info. | 256 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); |
| 246 if (spec_->request_payer_name() && | 257 return comparator.IsContactInfoComplete(selected_contact_profile_); |
| 247 (selected_contact_profile_ == nullptr || | |
| 248 selected_contact_profile_ | |
| 249 ->GetInfo(autofill::AutofillType(autofill::NAME_FULL), app_locale_) | |
| 250 .empty())) { | |
| 251 return false; | |
| 252 } | |
| 253 if (spec_->request_payer_email() && | |
| 254 (selected_contact_profile_ == nullptr || | |
| 255 selected_contact_profile_ | |
| 256 ->GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS), | |
| 257 app_locale_) | |
| 258 .empty())) { | |
| 259 return false; | |
| 260 } | |
| 261 if (spec_->request_payer_phone() && | |
| 262 (selected_contact_profile_ == nullptr || | |
| 263 selected_contact_profile_ | |
| 264 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), | |
| 265 app_locale_) | |
| 266 .empty())) { | |
| 267 return false; | |
| 268 } | |
| 269 | |
| 270 return true; | |
| 271 } | 258 } |
| 272 | 259 |
| 273 } // namespace payments | 260 } // namespace payments |
| OLD | NEW |