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