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 | |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 11 | #include "base/feature_list.h" |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 13 | #include "components/autofill/core/browser/autofill_country.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 14 | #include "components/autofill/core/browser/autofill_data_util.h" |
| 15 | #include "components/autofill/core/browser/autofill_profile.h" |
| 16 | #include "components/autofill/core/browser/credit_card.h" |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 17 | #include "components/autofill/core/browser/personal_data_manager.h" |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 18 | #include "components/payments/content/content_payment_request_delegate.h" |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 19 | #include "components/payments/content/payment_manifest_web_data_service.h" |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame] | 20 | #include "components/payments/content/payment_response_helper.h" |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 21 | #include "components/payments/content/service_worker_payment_instrument.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 22 | #include "components/payments/core/autofill_payment_instrument.h" |
Mathieu Perreault | 157f399 | 2018-06-29 18:48:06 | [diff] [blame^] | 23 | #include "components/payments/core/features.h" |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 24 | #include "components/payments/core/journey_logger.h" |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 25 | #include "components/payments/core/payment_instrument.h" |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 26 | #include "components/payments/core/payment_request_data_util.h" |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 27 | #include "content/public/common/content_features.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 28 | |
| 29 | namespace payments { |
| 30 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 31 | PaymentRequestState::PaymentRequestState( |
Rouslan Solomakhin | dbf593d9 | 2017-11-21 19:20:57 | [diff] [blame] | 32 | content::WebContents* web_contents, |
gogerald | 7a0cc3e | 2017-09-19 03:35:48 | [diff] [blame] | 33 | const GURL& top_level_origin, |
| 34 | const GURL& frame_origin, |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 35 | PaymentRequestSpec* spec, |
| 36 | Delegate* delegate, |
| 37 | const std::string& app_locale, |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 38 | autofill::PersonalDataManager* personal_data_manager, |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 39 | ContentPaymentRequestDelegate* payment_request_delegate, |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 40 | JourneyLogger* journey_logger) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 41 | : is_ready_to_pay_(false), |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 42 | get_all_instruments_finished_(true), |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 43 | is_waiting_for_merchant_validation_(false), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 44 | app_locale_(app_locale), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 45 | spec_(spec), |
| 46 | delegate_(delegate), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 47 | personal_data_manager_(personal_data_manager), |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 48 | journey_logger_(journey_logger), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 49 | selected_shipping_profile_(nullptr), |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 50 | selected_shipping_option_error_profile_(nullptr), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 51 | selected_contact_profile_(nullptr), |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 52 | selected_instrument_(nullptr), |
gogerald | ac15d236 | 2017-11-15 01:04:19 | [diff] [blame] | 53 | number_of_pending_sw_payment_instruments_(0), |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 54 | payment_request_delegate_(payment_request_delegate), |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 55 | profile_comparator_(app_locale, *spec), |
| 56 | weak_ptr_factory_(this) { |
Mathieu Perreault | 157f399 | 2018-06-29 18:48:06 | [diff] [blame^] | 57 | if (base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps)) { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 58 | get_all_instruments_finished_ = false; |
gogerald | 97e1b331 | 2017-11-21 23:31:10 | [diff] [blame] | 59 | ServiceWorkerPaymentAppFactory::GetInstance()->GetAllPaymentApps( |
Rouslan Solomakhin | dbf593d9 | 2017-11-21 19:20:57 | [diff] [blame] | 60 | web_contents, |
Rouslan Solomakhin | 438d8c9 | 2017-10-26 21:33:18 | [diff] [blame] | 61 | payment_request_delegate_->GetPaymentManifestWebDataService(), |
Rouslan Solomakhin | 06df42d | 2017-11-14 20:35:11 | [diff] [blame] | 62 | spec_->method_data(), |
Rouslan Solomakhin | e36f41f | 2018-03-19 16:27:09 | [diff] [blame] | 63 | /*may_crawl_for_installable_payment_apps=*/ |
| 64 | !spec_->supports_basic_card(), |
Rouslan Solomakhin | 438d8c9 | 2017-10-26 21:33:18 | [diff] [blame] | 65 | base::BindOnce(&PaymentRequestState::GetAllPaymentAppsCallback, |
gogerald | 3f1cabf | 2018-02-08 14:19:18 | [diff] [blame] | 66 | weak_ptr_factory_.GetWeakPtr(), web_contents, |
| 67 | top_level_origin, frame_origin), |
gogerald | 97e1b331 | 2017-11-21 23:31:10 | [diff] [blame] | 68 | base::BindOnce([]() { |
| 69 | /* Nothing needs to be done after writing cache. This callback is used |
| 70 | * only in tests. */ |
| 71 | })); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 72 | } else { |
| 73 | PopulateProfileCache(); |
| 74 | SetDefaultProfileSelections(); |
| 75 | } |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 76 | spec_->AddObserver(this); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 77 | } |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 78 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 79 | PaymentRequestState::~PaymentRequestState() {} |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 80 | |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 81 | void PaymentRequestState::GetAllPaymentAppsCallback( |
gogerald | 3f1cabf | 2018-02-08 14:19:18 | [diff] [blame] | 82 | content::WebContents* web_contents, |
gogerald | 7a0cc3e | 2017-09-19 03:35:48 | [diff] [blame] | 83 | const GURL& top_level_origin, |
| 84 | const GURL& frame_origin, |
gogerald | 84ae626 | 2018-02-06 06:21:46 | [diff] [blame] | 85 | content::PaymentAppProvider::PaymentApps apps, |
| 86 | ServiceWorkerPaymentAppFactory::InstallablePaymentApps installable_apps) { |
gogerald | 3f1cabf | 2018-02-08 14:19:18 | [diff] [blame] | 87 | number_of_pending_sw_payment_instruments_ = |
| 88 | apps.size() + installable_apps.size(); |
gogerald | ac15d236 | 2017-11-15 01:04:19 | [diff] [blame] | 89 | if (number_of_pending_sw_payment_instruments_ == 0U) { |
| 90 | FinishedGetAllSWPaymentInstruments(); |
| 91 | return; |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 92 | } |
| 93 | |
gogerald | ac15d236 | 2017-11-15 01:04:19 | [diff] [blame] | 94 | for (auto& app : apps) { |
| 95 | std::unique_ptr<ServiceWorkerPaymentInstrument> instrument = |
| 96 | std::make_unique<ServiceWorkerPaymentInstrument>( |
gogerald | 3f1cabf | 2018-02-08 14:19:18 | [diff] [blame] | 97 | web_contents->GetBrowserContext(), top_level_origin, frame_origin, |
| 98 | spec_, std::move(app.second), payment_request_delegate_); |
| 99 | instrument->ValidateCanMakePayment( |
| 100 | base::BindOnce(&PaymentRequestState::OnSWPaymentInstrumentValidated, |
| 101 | weak_ptr_factory_.GetWeakPtr())); |
| 102 | available_instruments_.push_back(std::move(instrument)); |
| 103 | } |
| 104 | |
| 105 | for (auto& installable_app : installable_apps) { |
| 106 | std::unique_ptr<ServiceWorkerPaymentInstrument> instrument = |
| 107 | std::make_unique<ServiceWorkerPaymentInstrument>( |
| 108 | web_contents, top_level_origin, frame_origin, spec_, |
| 109 | std::move(installable_app.second), installable_app.first.spec(), |
| 110 | payment_request_delegate_); |
gogerald | ac15d236 | 2017-11-15 01:04:19 | [diff] [blame] | 111 | instrument->ValidateCanMakePayment( |
| 112 | base::BindOnce(&PaymentRequestState::OnSWPaymentInstrumentValidated, |
| 113 | weak_ptr_factory_.GetWeakPtr())); |
| 114 | available_instruments_.push_back(std::move(instrument)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void PaymentRequestState::OnSWPaymentInstrumentValidated( |
| 119 | ServiceWorkerPaymentInstrument* instrument, |
| 120 | bool result) { |
| 121 | // Remove service worker payment instruments failed on validation. |
| 122 | if (!result) { |
| 123 | for (size_t i = 0; i < available_instruments_.size(); i++) { |
| 124 | if (available_instruments_[i].get() == instrument) { |
| 125 | available_instruments_.erase(available_instruments_.begin() + i); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (--number_of_pending_sw_payment_instruments_ > 0) |
| 132 | return; |
| 133 | |
| 134 | FinishedGetAllSWPaymentInstruments(); |
| 135 | } |
| 136 | |
| 137 | void PaymentRequestState::FinishedGetAllSWPaymentInstruments() { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 138 | PopulateProfileCache(); |
| 139 | SetDefaultProfileSelections(); |
| 140 | |
| 141 | get_all_instruments_finished_ = true; |
| 142 | NotifyOnGetAllPaymentInstrumentsFinished(); |
| 143 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 144 | // Fullfill the pending CanMakePayment call. |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 145 | if (can_make_payment_callback_) |
| 146 | CheckCanMakePayment(std::move(can_make_payment_callback_)); |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 147 | |
| 148 | // Fullfill the pending AreRequestedMethodsSupported call. |
| 149 | if (are_requested_methods_supported_callback_) |
| 150 | CheckRequestedMethodsSupported( |
| 151 | std::move(are_requested_methods_supported_callback_)); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 152 | } |
| 153 | |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 154 | void PaymentRequestState::OnPaymentResponseReady( |
| 155 | mojom::PaymentResponsePtr payment_response) { |
| 156 | delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 157 | } |
| 158 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 159 | void PaymentRequestState::OnSpecUpdated() { |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 160 | if (spec_->selected_shipping_option_error().empty()) { |
| 161 | selected_shipping_option_error_profile_ = nullptr; |
| 162 | } else { |
| 163 | selected_shipping_option_error_profile_ = selected_shipping_profile_; |
| 164 | selected_shipping_profile_ = nullptr; |
| 165 | } |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 166 | is_waiting_for_merchant_validation_ = false; |
| 167 | UpdateIsReadyToPayAndNotifyObservers(); |
| 168 | } |
| 169 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 170 | void PaymentRequestState::CanMakePayment(StatusCallback callback) { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 171 | if (!get_all_instruments_finished_) { |
| 172 | can_make_payment_callback_ = std::move(callback); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 177 | FROM_HERE, |
| 178 | base::BindOnce(&PaymentRequestState::CheckCanMakePayment, |
| 179 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 180 | } |
| 181 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 182 | void PaymentRequestState::CheckCanMakePayment(StatusCallback callback) { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 183 | bool can_make_payment_value = false; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 184 | for (const auto& instrument : available_instruments_) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 185 | if (instrument->IsValidForCanMakePayment()) { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 186 | can_make_payment_value = true; |
| 187 | break; |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 188 | } |
| 189 | } |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 190 | std::move(callback).Run(can_make_payment_value); |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 191 | } |
| 192 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 193 | void PaymentRequestState::AreRequestedMethodsSupported( |
| 194 | StatusCallback callback) { |
| 195 | if (!get_all_instruments_finished_) { |
| 196 | are_requested_methods_supported_callback_ = std::move(callback); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 201 | FROM_HERE, |
| 202 | base::BindOnce(&PaymentRequestState::CheckRequestedMethodsSupported, |
| 203 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 204 | } |
| 205 | |
| 206 | void PaymentRequestState::CheckRequestedMethodsSupported( |
| 207 | StatusCallback callback) { |
| 208 | DCHECK(get_all_instruments_finished_); |
| 209 | |
| 210 | std::move(callback).Run(!spec_->supported_card_networks().empty() || |
| 211 | !available_instruments_.empty()); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 212 | } |
| 213 | |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 214 | std::string PaymentRequestState::GetAuthenticatedEmail() const { |
| 215 | return payment_request_delegate_->GetAuthenticatedEmail(); |
| 216 | } |
| 217 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 218 | void PaymentRequestState::AddObserver(Observer* observer) { |
| 219 | CHECK(observer); |
| 220 | observers_.AddObserver(observer); |
| 221 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 222 | |
| 223 | void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 224 | observers_.RemoveObserver(observer); |
| 225 | } |
| 226 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 227 | void PaymentRequestState::GeneratePaymentResponse() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 228 | DCHECK(is_ready_to_pay()); |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 229 | |
| 230 | // Once the response is ready, will call back into OnPaymentResponseReady. |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 231 | response_helper_ = std::make_unique<PaymentResponseHelper>( |
sebsg | 8a9c234 | 2017-04-21 17:05:15 | [diff] [blame] | 232 | app_locale_, spec_, selected_instrument_, payment_request_delegate_, |
| 233 | selected_shipping_profile_, selected_contact_profile_, this); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 234 | } |
| 235 | |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 236 | void PaymentRequestState::RecordUseStats() { |
| 237 | if (spec_->request_shipping()) { |
| 238 | DCHECK(selected_shipping_profile_); |
| 239 | personal_data_manager_->RecordUseOf(*selected_shipping_profile_); |
| 240 | } |
| 241 | |
| 242 | if (spec_->request_payer_name() || spec_->request_payer_email() || |
| 243 | spec_->request_payer_phone()) { |
| 244 | DCHECK(selected_contact_profile_); |
| 245 | |
| 246 | // If the same address was used for both contact and shipping, the stats |
| 247 | // should only be updated once. |
| 248 | if (!spec_->request_shipping() || (selected_shipping_profile_->guid() != |
| 249 | selected_contact_profile_->guid())) { |
| 250 | personal_data_manager_->RecordUseOf(*selected_contact_profile_); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | selected_instrument_->RecordUse(); |
| 255 | } |
| 256 | |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 257 | void PaymentRequestState::AddAutofillPaymentInstrument( |
| 258 | bool selected, |
| 259 | const autofill::CreditCard& card) { |
| 260 | std::string basic_card_network = |
rouslan | 4e981bd3 | 2017-05-01 17:49:16 | [diff] [blame] | 261 | autofill::data_util::GetPaymentRequestData(card.network()) |
| 262 | .basic_card_issuer_network; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 263 | if (!spec_->supported_card_networks_set().count(basic_card_network) || |
| 264 | !spec_->supported_card_types_set().count(card.card_type())) { |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 265 | return; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // The total number of card types: credit, debit, prepaid, unknown. |
| 269 | constexpr size_t kTotalNumberOfCardTypes = 4U; |
| 270 | |
| 271 | // Whether the card type (credit, debit, prepaid) matches thetype that the |
| 272 | // merchant has requested exactly. This should be false for unknown card |
| 273 | // types, if the merchant cannot accept some card types. |
| 274 | bool matches_merchant_card_type_exactly = |
| 275 | card.card_type() != autofill::CreditCard::CARD_TYPE_UNKNOWN || |
| 276 | spec_->supported_card_types_set().size() == kTotalNumberOfCardTypes; |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 277 | |
| 278 | // AutofillPaymentInstrument makes a copy of |card| so it is effectively |
| 279 | // owned by this object. |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 280 | auto instrument = std::make_unique<AutofillPaymentInstrument>( |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 281 | basic_card_network, card, matches_merchant_card_type_exactly, |
| 282 | shipping_profiles_, app_locale_, payment_request_delegate_); |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 283 | available_instruments_.push_back(std::move(instrument)); |
| 284 | |
| 285 | if (selected) |
| 286 | SetSelectedInstrument(available_instruments_.back().get()); |
| 287 | } |
| 288 | |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 289 | void PaymentRequestState::AddAutofillShippingProfile( |
| 290 | bool selected, |
| 291 | const autofill::AutofillProfile& profile) { |
| 292 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 293 | std::make_unique<autofill::AutofillProfile>(profile)); |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 294 | // TODO(tmartino): Implement deduplication rules specific to shipping |
| 295 | // profiles. |
| 296 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 297 | shipping_profiles_.push_back(new_cached_profile); |
| 298 | |
| 299 | if (selected) |
| 300 | SetSelectedShippingProfile(new_cached_profile); |
| 301 | } |
| 302 | |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 303 | void PaymentRequestState::AddAutofillContactProfile( |
| 304 | bool selected, |
| 305 | const autofill::AutofillProfile& profile) { |
| 306 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 307 | std::make_unique<autofill::AutofillProfile>(profile)); |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 308 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 309 | contact_profiles_.push_back(new_cached_profile); |
| 310 | |
| 311 | if (selected) |
| 312 | SetSelectedContactProfile(new_cached_profile); |
| 313 | } |
| 314 | |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 315 | void PaymentRequestState::SetSelectedShippingOption( |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 316 | const std::string& shipping_option_id) { |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 317 | spec_->StartWaitingForUpdateWith( |
| 318 | PaymentRequestSpec::UpdateReason::SHIPPING_OPTION); |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 319 | // This will inform the merchant and will lead to them calling updateWith with |
| 320 | // new PaymentDetails. |
| 321 | delegate_->OnShippingOptionIdSelected(shipping_option_id); |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 322 | } |
| 323 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 324 | void PaymentRequestState::SetSelectedShippingProfile( |
| 325 | autofill::AutofillProfile* profile) { |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 326 | spec_->StartWaitingForUpdateWith( |
| 327 | PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 328 | selected_shipping_profile_ = profile; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 329 | |
| 330 | // The user should not be able to click on pay until the callback from the |
| 331 | // merchant. |
| 332 | is_waiting_for_merchant_validation_ = true; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 333 | |
| 334 | // Start the normalization of the shipping address. |
Mathieu Perreault | c94700f | 2017-10-23 20:28:39 | [diff] [blame] | 335 | payment_request_delegate_->GetAddressNormalizer()->NormalizeAddressAsync( |
Mathieu Perreault | 9b40d510 | 2017-11-21 20:51:29 | [diff] [blame] | 336 | *selected_shipping_profile_, /*timeout_seconds=*/2, |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 337 | base::BindOnce(&PaymentRequestState::OnAddressNormalized, |
| 338 | weak_ptr_factory_.GetWeakPtr())); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void PaymentRequestState::SetSelectedContactProfile( |
| 342 | autofill::AutofillProfile* profile) { |
| 343 | selected_contact_profile_ = profile; |
| 344 | UpdateIsReadyToPayAndNotifyObservers(); |
| 345 | } |
| 346 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 347 | void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { |
| 348 | selected_instrument_ = instrument; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 349 | UpdateIsReadyToPayAndNotifyObservers(); |
| 350 | } |
| 351 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 352 | const std::string& PaymentRequestState::GetApplicationLocale() { |
| 353 | return app_locale_; |
| 354 | } |
| 355 | |
| 356 | autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { |
| 357 | return personal_data_manager_; |
| 358 | } |
| 359 | |
mad | 763ed2b | 2017-04-24 20:28:47 | [diff] [blame] | 360 | autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { |
| 361 | return payment_request_delegate_->GetRegionDataLoader(); |
mad | 4527193 | 2017-04-13 16:07:38 | [diff] [blame] | 362 | } |
| 363 | |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 364 | bool PaymentRequestState::IsPaymentAppInvoked() const { |
| 365 | return !!response_helper_; |
| 366 | } |
| 367 | |
Mathieu Perreault | eea046d1 | 2017-09-28 15:51:21 | [diff] [blame] | 368 | autofill::AddressNormalizer* PaymentRequestState::GetAddressNormalizer() { |
Anthony Vallee-Dubois | 71bf34946 | 2017-07-20 23:56:45 | [diff] [blame] | 369 | return payment_request_delegate_->GetAddressNormalizer(); |
| 370 | } |
| 371 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 372 | void PaymentRequestState::PopulateProfileCache() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 373 | std::vector<autofill::AutofillProfile*> profiles = |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 374 | personal_data_manager_->GetProfilesToSuggest(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 375 | |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 376 | std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering; |
| 377 | raw_profiles_for_filtering.reserve(profiles.size()); |
| 378 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 379 | // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 380 | // Thus, we store copies, and return a vector of pointers to these copies |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 381 | // whenever Profiles are requested. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 382 | for (size_t i = 0; i < profiles.size(); i++) { |
| 383 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 384 | std::make_unique<autofill::AutofillProfile>(*profiles[i])); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 385 | raw_profiles_for_filtering.push_back(profile_cache_.back().get()); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 386 | } |
| 387 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 388 | contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
| 389 | raw_profiles_for_filtering); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 390 | shipping_profiles_ = profile_comparator()->FilterProfilesForShipping( |
| 391 | raw_profiles_for_filtering); |
tmartino | a6eb22f | 2017-04-06 20:16:24 | [diff] [blame] | 392 | |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 393 | // Set the number of suggestions shown for the sections requested by the |
| 394 | // merchant. |
| 395 | if (spec_->request_payer_name() || spec_->request_payer_phone() || |
| 396 | spec_->request_payer_email()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 397 | bool has_complete_contact = |
| 398 | contact_profiles_.empty() |
| 399 | ? false |
| 400 | : profile_comparator()->IsContactInfoComplete(contact_profiles_[0]); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 401 | journey_logger_->SetNumberOfSuggestionsShown( |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 402 | JourneyLogger::Section::SECTION_CONTACT_INFO, contact_profiles_.size(), |
| 403 | has_complete_contact); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 404 | } |
| 405 | if (spec_->request_shipping()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 406 | bool has_complete_shipping = |
| 407 | shipping_profiles_.empty() |
| 408 | ? false |
| 409 | : profile_comparator()->IsShippingComplete(shipping_profiles_[0]); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 410 | journey_logger_->SetNumberOfSuggestionsShown( |
| 411 | JourneyLogger::Section::SECTION_SHIPPING_ADDRESS, |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 412 | shipping_profiles_.size(), has_complete_shipping); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 413 | } |
| 414 | |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 415 | // Create the list of available instruments. A copy of each card will be made |
| 416 | // by their respective AutofillPaymentInstrument. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 417 | const std::vector<autofill::CreditCard*>& cards = |
Amirhossein Simjour | d3b7ba4a | 2018-05-07 15:27:21 | [diff] [blame] | 418 | personal_data_manager_->GetCreditCardsToSuggest( |
Mathieu Perreault | 157f399 | 2018-06-29 18:48:06 | [diff] [blame^] | 419 | /*include_server_cards=*/base::FeatureList::IsEnabled( |
| 420 | payments::features::kReturnGooglePayInBasicCard)); |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 421 | for (autofill::CreditCard* card : cards) |
| 422 | AddAutofillPaymentInstrument(/*selected=*/false, *card); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void PaymentRequestState::SetDefaultProfileSelections() { |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 426 | // Only pre-select an address if the merchant provided at least one selected |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 427 | // shipping option, and the top profile is complete. Assumes that profiles |
| 428 | // have already been sorted for completeness and frecency. |
| 429 | if (!shipping_profiles().empty() && spec_->selected_shipping_option() && |
| 430 | profile_comparator()->IsShippingComplete(shipping_profiles_[0])) { |
| 431 | selected_shipping_profile_ = shipping_profiles()[0]; |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 432 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 433 | |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 434 | // Contact profiles were ordered by completeness in addition to frecency; |
| 435 | // the first one is the best default selection. |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 436 | if (!contact_profiles().empty() && |
| 437 | profile_comparator()->IsContactInfoComplete(contact_profiles_[0])) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 438 | selected_contact_profile_ = contact_profiles()[0]; |
| 439 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 440 | // TODO(crbug.com/702063): Change this code to prioritize instruments by use |
| 441 | // count and other means, and implement a way to modify this function's return |
| 442 | // value. |
| 443 | const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = |
| 444 | available_instruments(); |
| 445 | auto first_complete_instrument = |
| 446 | std::find_if(instruments.begin(), instruments.end(), |
| 447 | [](const std::unique_ptr<PaymentInstrument>& instrument) { |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 448 | return instrument->IsCompleteForPayment() && |
| 449 | instrument->IsExactlyMatchingMerchantRequest(); |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 450 | }); |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 451 | selected_instrument_ = first_complete_instrument == instruments.end() |
| 452 | ? nullptr |
| 453 | : first_complete_instrument->get(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 454 | UpdateIsReadyToPayAndNotifyObservers(); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 455 | |
| 456 | bool has_complete_instrument = |
| 457 | available_instruments().empty() |
| 458 | ? false |
| 459 | : available_instruments()[0]->IsCompleteForPayment(); |
| 460 | |
| 461 | journey_logger_->SetNumberOfSuggestionsShown( |
| 462 | JourneyLogger::Section::SECTION_PAYMENT_METHOD, |
| 463 | available_instruments().size(), has_complete_instrument); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { |
| 467 | is_ready_to_pay_ = |
| 468 | ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); |
| 469 | NotifyOnSelectedInformationChanged(); |
| 470 | } |
| 471 | |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 472 | void PaymentRequestState::NotifyOnGetAllPaymentInstrumentsFinished() { |
| 473 | for (auto& observer : observers_) |
| 474 | observer.OnGetAllPaymentInstrumentsFinished(); |
| 475 | } |
| 476 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 477 | void PaymentRequestState::NotifyOnSelectedInformationChanged() { |
| 478 | for (auto& observer : observers_) |
| 479 | observer.OnSelectedInformationChanged(); |
| 480 | } |
| 481 | |
| 482 | bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 483 | // There is no need to check for supported networks, because only supported |
| 484 | // instruments are listed/created in the flow. |
mathp | 4baea33 | 2017-04-10 21:42:29 | [diff] [blame] | 485 | return selected_instrument_ != nullptr && |
| 486 | selected_instrument_->IsCompleteForPayment(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 490 | if (is_waiting_for_merchant_validation_) |
| 491 | return false; |
| 492 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 493 | if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) |
| 494 | return false; |
| 495 | |
Anthony Vallee-Dubois | a881467 | 2017-06-01 18:43:20 | [diff] [blame] | 496 | if (spec_->request_shipping() && !spec_->selected_shipping_option()) |
| 497 | return false; |
| 498 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 499 | return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 500 | } |
| 501 | |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 502 | void PaymentRequestState::OnAddressNormalized( |
| 503 | bool success, |
| 504 | const autofill::AutofillProfile& normalized_profile) { |
| 505 | delegate_->OnShippingAddressSelected( |
Tommy Martino | 21d415f | 2018-01-12 20:08:22 | [diff] [blame] | 506 | data_util::GetPaymentAddressFromAutofillProfile(normalized_profile, |
| 507 | app_locale_)); |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 508 | } |
| 509 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 510 | } // namespace payments |