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