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