| 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" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 selected_instrument_(nullptr), | 35 selected_instrument_(nullptr), |
| 36 payment_request_delegate_(payment_request_delegate) { | 36 payment_request_delegate_(payment_request_delegate) { |
| 37 PopulateProfileCache(); | 37 PopulateProfileCache(); |
| 38 SetDefaultProfileSelections(); | 38 SetDefaultProfileSelections(); |
| 39 } | 39 } |
| 40 PaymentRequestState::~PaymentRequestState() {} | 40 PaymentRequestState::~PaymentRequestState() {} |
| 41 | 41 |
| 42 bool PaymentRequestState::CanMakePayment() const { | 42 bool PaymentRequestState::CanMakePayment() const { |
| 43 for (const std::unique_ptr<PaymentInstrument>& instrument : | 43 for (const std::unique_ptr<PaymentInstrument>& instrument : |
| 44 available_instruments_) { | 44 available_instruments_) { |
| 45 if (instrument.get()->IsValid() && | 45 if (instrument->IsValidForCanMakePayment() && |
| 46 spec_->supported_card_networks_set().count( | 46 spec_->supported_card_networks_set().count( |
| 47 instrument.get()->method_name())) { | 47 instrument.get()->method_name())) { |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 } | 50 } |
| 51 return false; | 51 return false; |
| 52 } | 52 } |
| 53 | 53 |
| 54 void PaymentRequestState::AddObserver(Observer* observer) { | 54 void PaymentRequestState::AddObserver(Observer* observer) { |
| 55 CHECK(observer); | 55 CHECK(observer); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 personal_data_manager_->GetCreditCardsToSuggest(); | 183 personal_data_manager_->GetCreditCardsToSuggest(); |
| 184 const std::set<std::string>& supported_card_networks = | 184 const std::set<std::string>& supported_card_networks = |
| 185 spec_->supported_card_networks_set(); | 185 spec_->supported_card_networks_set(); |
| 186 for (autofill::CreditCard* card : cards) { | 186 for (autofill::CreditCard* card : cards) { |
| 187 std::string basic_card_network = | 187 std::string basic_card_network = |
| 188 autofill::data_util::GetPaymentRequestData(card->type()) | 188 autofill::data_util::GetPaymentRequestData(card->type()) |
| 189 .basic_card_payment_type; | 189 .basic_card_payment_type; |
| 190 if (!supported_card_networks.count(basic_card_network)) | 190 if (!supported_card_networks.count(basic_card_network)) |
| 191 continue; | 191 continue; |
| 192 | 192 |
| 193 // TODO(crbug.com/701952): Should use the method name preferred by the | |
| 194 // merchant (either "basic-card" or the basic card network e.g. "visa"). | |
| 195 | |
| 196 // Copy the credit cards as part of AutofillPaymentInstrument so they are | 193 // Copy the credit cards as part of AutofillPaymentInstrument so they are |
| 197 // indirectly owned by this object. | 194 // indirectly owned by this object. |
| 198 std::unique_ptr<PaymentInstrument> instrument = | 195 std::unique_ptr<PaymentInstrument> instrument = |
| 199 base::MakeUnique<AutofillPaymentInstrument>( | 196 base::MakeUnique<AutofillPaymentInstrument>( |
| 200 basic_card_network, *card, shipping_profiles_, app_locale_, | 197 basic_card_network, *card, shipping_profiles_, app_locale_, |
| 201 payment_request_delegate_); | 198 payment_request_delegate_); |
| 202 available_instruments_.push_back(std::move(instrument)); | 199 available_instruments_.push_back(std::move(instrument)); |
| 203 } | 200 } |
| 204 } | 201 } |
| 205 | 202 |
| 206 void PaymentRequestState::SetDefaultProfileSelections() { | 203 void PaymentRequestState::SetDefaultProfileSelections() { |
| 207 // Only pre-select an address if the merchant provided at least one selected | 204 // Only pre-select an address if the merchant provided at least one selected |
| 208 // shipping option. | 205 // shipping option. |
| 209 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) | 206 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) |
| 210 selected_shipping_profile_ = shipping_profiles()[0]; | 207 selected_shipping_profile_ = shipping_profiles()[0]; |
| 211 | 208 |
| 212 if (!contact_profiles().empty()) | 209 if (!contact_profiles().empty()) |
| 213 selected_contact_profile_ = contact_profiles()[0]; | 210 selected_contact_profile_ = contact_profiles()[0]; |
| 214 | 211 |
| 215 // TODO(crbug.com/702063): Change this code to prioritize instruments by use | 212 // TODO(crbug.com/702063): Change this code to prioritize instruments by use |
| 216 // count and other means, and implement a way to modify this function's return | 213 // count and other means, and implement a way to modify this function's return |
| 217 // value. | 214 // value. |
| 218 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = | 215 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = |
| 219 available_instruments(); | 216 available_instruments(); |
| 220 auto first_complete_instrument = | 217 auto first_complete_instrument = |
| 221 std::find_if(instruments.begin(), instruments.end(), | 218 std::find_if(instruments.begin(), instruments.end(), |
| 222 [](const std::unique_ptr<PaymentInstrument>& instrument) { | 219 [](const std::unique_ptr<PaymentInstrument>& instrument) { |
| 223 return instrument->IsValid(); | 220 return instrument->IsCompleteForPayment(); |
| 224 }); | 221 }); |
| 225 | 222 |
| 226 selected_instrument_ = first_complete_instrument == instruments.end() | 223 selected_instrument_ = first_complete_instrument == instruments.end() |
| 227 ? nullptr | 224 ? nullptr |
| 228 : first_complete_instrument->get(); | 225 : first_complete_instrument->get(); |
| 229 | 226 |
| 230 UpdateIsReadyToPayAndNotifyObservers(); | 227 UpdateIsReadyToPayAndNotifyObservers(); |
| 231 } | 228 } |
| 232 | 229 |
| 233 void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { | 230 void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { |
| 234 is_ready_to_pay_ = | 231 is_ready_to_pay_ = |
| 235 ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); | 232 ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); |
| 236 NotifyOnSelectedInformationChanged(); | 233 NotifyOnSelectedInformationChanged(); |
| 237 } | 234 } |
| 238 | 235 |
| 239 void PaymentRequestState::NotifyOnSelectedInformationChanged() { | 236 void PaymentRequestState::NotifyOnSelectedInformationChanged() { |
| 240 for (auto& observer : observers_) | 237 for (auto& observer : observers_) |
| 241 observer.OnSelectedInformationChanged(); | 238 observer.OnSelectedInformationChanged(); |
| 242 } | 239 } |
| 243 | 240 |
| 244 bool PaymentRequestState::ArePaymentDetailsSatisfied() { | 241 bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
| 245 // There is no need to check for supported networks, because only supported | 242 // There is no need to check for supported networks, because only supported |
| 246 // instruments are listed/created in the flow. | 243 // instruments are listed/created in the flow. |
| 247 // TODO(crbug.com/702063): A masked card may not satisfy IsValid(). | 244 return selected_instrument_ != nullptr && |
| 248 return selected_instrument_ != nullptr && selected_instrument_->IsValid(); | 245 selected_instrument_->IsCompleteForPayment(); |
| 249 } | 246 } |
| 250 | 247 |
| 251 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 248 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| 252 // TODO(mathp): Have a measure of shipping address completeness. | 249 // TODO(mathp): Have a measure of shipping address completeness. |
| 253 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 250 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
| 254 return false; | 251 return false; |
| 255 | 252 |
| 256 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); | 253 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); |
| 257 return comparator.IsContactInfoComplete(selected_contact_profile_); | 254 return comparator.IsContactInfoComplete(selected_contact_profile_); |
| 258 } | 255 } |
| 259 | 256 |
| 260 } // namespace payments | 257 } // namespace payments |
| OLD | NEW |