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