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