| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/payments/content/payment_request_state.h" |
| 6 | |
| mad | 4527193 | 2017-04-13 16:07:38 | [diff] [blame] | 7 | #include <algorithm> |
| anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 8 | #include <set> |
| mad | 4527193 | 2017-04-13 16:07:38 | [diff] [blame] | 9 | #include <utility> |
| anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 10 | |
| sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 12 | #include "components/autofill/core/browser/autofill_country.h" |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 13 | #include "components/autofill/core/browser/autofill_data_util.h" |
| 14 | #include "components/autofill/core/browser/autofill_profile.h" |
| 15 | #include "components/autofill/core/browser/credit_card.h" |
| mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 16 | #include "components/autofill/core/browser/personal_data_manager.h" |
| sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame] | 17 | #include "components/payments/content/payment_response_helper.h" |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 18 | #include "components/payments/core/autofill_payment_instrument.h" |
| sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 19 | #include "components/payments/core/payment_instrument.h" |
| tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 20 | #include "components/payments/core/payment_request_data_util.h" |
| anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 21 | #include "components/payments/core/payment_request_delegate.h" |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 22 | |
| 23 | namespace payments { |
| 24 | |
| mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 25 | PaymentRequestState::PaymentRequestState( |
| 26 | PaymentRequestSpec* spec, |
| 27 | Delegate* delegate, |
| 28 | const std::string& app_locale, |
| anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 29 | autofill::PersonalDataManager* personal_data_manager, |
| 30 | PaymentRequestDelegate* payment_request_delegate) |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 31 | : is_ready_to_pay_(false), |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 32 | is_waiting_for_merchant_validation_(false), |
| mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 33 | app_locale_(app_locale), |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 34 | spec_(spec), |
| 35 | delegate_(delegate), |
| mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 36 | personal_data_manager_(personal_data_manager), |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 37 | selected_shipping_profile_(nullptr), |
| 38 | selected_contact_profile_(nullptr), |
| anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 39 | selected_instrument_(nullptr), |
| tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 40 | payment_request_delegate_(payment_request_delegate), |
| 41 | profile_comparator_(app_locale, *spec) { |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 42 | PopulateProfileCache(); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 43 | SetDefaultProfileSelections(); |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 44 | spec_->AddObserver(this); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 45 | } |
| mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 46 | PaymentRequestState::~PaymentRequestState() {} |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 47 | |
| sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 48 | void PaymentRequestState::OnPaymentResponseReady( |
| 49 | mojom::PaymentResponsePtr payment_response) { |
| 50 | delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 51 | } |
| 52 | |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 53 | void PaymentRequestState::OnAddressNormalized( |
| 54 | const autofill::AutofillProfile& normalized_profile) { |
| 55 | delegate_->OnShippingAddressSelected( |
| 56 | PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( |
| 57 | normalized_profile, app_locale_)); |
| 58 | } |
| 59 | |
| 60 | void PaymentRequestState::OnCouldNotNormalize( |
| 61 | const autofill::AutofillProfile& profile) { |
| 62 | // Since the phone number is formatted in either case, this profile should be |
| 63 | // used. |
| 64 | OnAddressNormalized(profile); |
| 65 | } |
| 66 | |
| 67 | void PaymentRequestState::OnSpecUpdated() { |
| 68 | is_waiting_for_merchant_validation_ = false; |
| 69 | UpdateIsReadyToPayAndNotifyObservers(); |
| 70 | } |
| 71 | |
| mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 72 | bool PaymentRequestState::CanMakePayment() const { |
| mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 73 | for (const std::unique_ptr<PaymentInstrument>& instrument : |
| 74 | available_instruments_) { |
| rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 75 | if (instrument->IsValidForCanMakePayment()) { |
| 76 | // AddAutofillPaymentInstrument() filters out available instruments based |
| 77 | // on supported card networks. |
| 78 | DCHECK(spec_->supported_card_networks_set().find( |
| 79 | instrument->method_name()) != |
| 80 | spec_->supported_card_networks_set().end()); |
| mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 87 | bool PaymentRequestState::AreRequestedMethodsSupported() const { |
| 88 | return !spec_->supported_card_networks().empty(); |
| 89 | } |
| 90 | |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 91 | void PaymentRequestState::AddObserver(Observer* observer) { |
| 92 | CHECK(observer); |
| 93 | observers_.AddObserver(observer); |
| 94 | } |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 95 | |
| 96 | void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 97 | observers_.RemoveObserver(observer); |
| 98 | } |
| 99 | |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 100 | void PaymentRequestState::GeneratePaymentResponse() { |
| mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 101 | DCHECK(is_ready_to_pay()); |
| sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 102 | |
| 103 | // Once the response is ready, will call back into OnPaymentResponseReady. |
| 104 | response_helper_ = base::MakeUnique<PaymentResponseHelper>( |
| sebsg | 8a9c234 | 2017-04-21 17:05:15 | [diff] [blame] | 105 | app_locale_, spec_, selected_instrument_, payment_request_delegate_, |
| 106 | selected_shipping_profile_, selected_contact_profile_, this); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 107 | } |
| 108 | |
| mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 109 | void PaymentRequestState::AddAutofillPaymentInstrument( |
| 110 | bool selected, |
| 111 | const autofill::CreditCard& card) { |
| 112 | std::string basic_card_network = |
| rouslan | 4e981bd3 | 2017-05-01 17:49:16 | [diff] [blame^] | 113 | autofill::data_util::GetPaymentRequestData(card.network()) |
| 114 | .basic_card_issuer_network; |
| mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 115 | if (!spec_->supported_card_networks_set().count(basic_card_network)) |
| 116 | return; |
| 117 | |
| 118 | // AutofillPaymentInstrument makes a copy of |card| so it is effectively |
| 119 | // owned by this object. |
| 120 | std::unique_ptr<PaymentInstrument> instrument = |
| 121 | base::MakeUnique<AutofillPaymentInstrument>( |
| 122 | basic_card_network, card, shipping_profiles_, app_locale_, |
| 123 | payment_request_delegate_); |
| 124 | available_instruments_.push_back(std::move(instrument)); |
| 125 | |
| 126 | if (selected) |
| 127 | SetSelectedInstrument(available_instruments_.back().get()); |
| 128 | } |
| 129 | |
| mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 130 | void PaymentRequestState::AddAutofillShippingProfile( |
| 131 | bool selected, |
| 132 | const autofill::AutofillProfile& profile) { |
| 133 | profile_cache_.push_back( |
| 134 | base::MakeUnique<autofill::AutofillProfile>(profile)); |
| 135 | // TODO(tmartino): Implement deduplication rules specific to shipping |
| 136 | // profiles. |
| 137 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 138 | shipping_profiles_.push_back(new_cached_profile); |
| 139 | |
| 140 | if (selected) |
| 141 | SetSelectedShippingProfile(new_cached_profile); |
| 142 | } |
| 143 | |
| anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 144 | void PaymentRequestState::SetSelectedShippingOption( |
| mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 145 | const std::string& shipping_option_id) { |
| anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 146 | spec_->StartWaitingForUpdateWith( |
| 147 | PaymentRequestSpec::UpdateReason::SHIPPING_OPTION); |
| mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 148 | // This will inform the merchant and will lead to them calling updateWith with |
| 149 | // new PaymentDetails. |
| 150 | delegate_->OnShippingOptionIdSelected(shipping_option_id); |
| anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 151 | } |
| 152 | |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 153 | void PaymentRequestState::SetSelectedShippingProfile( |
| 154 | autofill::AutofillProfile* profile) { |
| anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 155 | spec_->StartWaitingForUpdateWith( |
| 156 | PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 157 | selected_shipping_profile_ = profile; |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 158 | |
| 159 | // The user should not be able to click on pay until the callback from the |
| 160 | // merchant. |
| 161 | is_waiting_for_merchant_validation_ = true; |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 162 | UpdateIsReadyToPayAndNotifyObservers(); |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 163 | |
| 164 | // Start the normalization of the shipping address. |
| 165 | // Use the country code from the profile if it is set, otherwise infer it |
| 166 | // from the |app_locale_|. |
| tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 167 | std::string country_code = data_util::GetCountryCodeWithFallback( |
| 168 | selected_shipping_profile_, app_locale_); |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 169 | payment_request_delegate_->GetAddressNormalizer()->StartAddressNormalization( |
| 170 | *selected_shipping_profile_, country_code, /*timeout_seconds=*/2, this); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void PaymentRequestState::SetSelectedContactProfile( |
| 174 | autofill::AutofillProfile* profile) { |
| 175 | selected_contact_profile_ = profile; |
| 176 | UpdateIsReadyToPayAndNotifyObservers(); |
| 177 | } |
| 178 | |
| mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 179 | void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { |
| 180 | selected_instrument_ = instrument; |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 181 | UpdateIsReadyToPayAndNotifyObservers(); |
| 182 | } |
| 183 | |
| mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 184 | const std::string& PaymentRequestState::GetApplicationLocale() { |
| 185 | return app_locale_; |
| 186 | } |
| 187 | |
| 188 | autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { |
| 189 | return personal_data_manager_; |
| 190 | } |
| 191 | |
| mad | 763ed2b | 2017-04-24 20:28:47 | [diff] [blame] | 192 | autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { |
| 193 | return payment_request_delegate_->GetRegionDataLoader(); |
| mad | 4527193 | 2017-04-13 16:07:38 | [diff] [blame] | 194 | } |
| 195 | |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 196 | void PaymentRequestState::PopulateProfileCache() { |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 197 | std::vector<autofill::AutofillProfile*> profiles = |
| mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 198 | personal_data_manager_->GetProfilesToSuggest(); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 199 | |
| 200 | // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 201 | // Thus, we store copies, and return a vector of pointers to these copies |
| 202 | // whenever Profiles are requested. The same is true for credit cards. |
| 203 | for (size_t i = 0; i < profiles.size(); i++) { |
| 204 | profile_cache_.push_back( |
| 205 | base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
| 206 | |
| tmartino | a6eb22f | 2017-04-06 20:16:24 | [diff] [blame] | 207 | // TODO(tmartino): Implement deduplication rules specific to shipping |
| 208 | // profiles. |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 209 | shipping_profiles_.push_back(profile_cache_[i].get()); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 210 | } |
| 211 | |
| tmartino | a6eb22f | 2017-04-06 20:16:24 | [diff] [blame] | 212 | std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering( |
| 213 | profile_cache_.size()); |
| 214 | std::transform(profile_cache_.begin(), profile_cache_.end(), |
| 215 | raw_profiles_for_filtering.begin(), |
| 216 | [](const std::unique_ptr<autofill::AutofillProfile>& p) { |
| 217 | return p.get(); |
| 218 | }); |
| 219 | |
| tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 220 | contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
| 221 | raw_profiles_for_filtering); |
| tmartino | a6eb22f | 2017-04-06 20:16:24 | [diff] [blame] | 222 | |
| mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 223 | // Create the list of available instruments. |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 224 | const std::vector<autofill::CreditCard*>& cards = |
| mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 225 | personal_data_manager_->GetCreditCardsToSuggest(); |
| mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 226 | for (autofill::CreditCard* card : cards) |
| 227 | AddAutofillPaymentInstrument(/*selected=*/false, *card); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void PaymentRequestState::SetDefaultProfileSelections() { |
| mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 231 | // Only pre-select an address if the merchant provided at least one selected |
| 232 | // shipping option. |
| 233 | if (!shipping_profiles().empty() && spec_->selected_shipping_option()) |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 234 | selected_shipping_profile_ = shipping_profiles()[0]; |
| 235 | |
| 236 | if (!contact_profiles().empty()) |
| 237 | selected_contact_profile_ = contact_profiles()[0]; |
| 238 | |
| mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 239 | // TODO(crbug.com/702063): Change this code to prioritize instruments by use |
| 240 | // count and other means, and implement a way to modify this function's return |
| 241 | // value. |
| 242 | const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = |
| 243 | available_instruments(); |
| 244 | auto first_complete_instrument = |
| 245 | std::find_if(instruments.begin(), instruments.end(), |
| 246 | [](const std::unique_ptr<PaymentInstrument>& instrument) { |
| mathp | 4baea33 | 2017-04-10 21:42:29 | [diff] [blame] | 247 | return instrument->IsCompleteForPayment(); |
| mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 248 | }); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 249 | |
| mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 250 | selected_instrument_ = first_complete_instrument == instruments.end() |
| 251 | ? nullptr |
| 252 | : first_complete_instrument->get(); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 253 | |
| 254 | UpdateIsReadyToPayAndNotifyObservers(); |
| 255 | } |
| 256 | |
| 257 | void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { |
| 258 | is_ready_to_pay_ = |
| 259 | ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); |
| 260 | NotifyOnSelectedInformationChanged(); |
| 261 | } |
| 262 | |
| 263 | void PaymentRequestState::NotifyOnSelectedInformationChanged() { |
| 264 | for (auto& observer : observers_) |
| 265 | observer.OnSelectedInformationChanged(); |
| 266 | } |
| 267 | |
| 268 | bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
| mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 269 | // There is no need to check for supported networks, because only supported |
| 270 | // instruments are listed/created in the flow. |
| mathp | 4baea33 | 2017-04-10 21:42:29 | [diff] [blame] | 271 | return selected_instrument_ != nullptr && |
| 272 | selected_instrument_->IsCompleteForPayment(); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 276 | if (is_waiting_for_merchant_validation_) |
| 277 | return false; |
| 278 | |
| tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 279 | if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) |
| 280 | return false; |
| 281 | |
| 282 | return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 283 | } |
| 284 | |
| mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 285 | } // namespace payments |