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), |
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 | 151bd31e | 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(); |
| 149 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 150 | // Fullfill the pending CanMakePayment call. |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 151 | if (can_make_payment_callback_) |
| 152 | CheckCanMakePayment(std::move(can_make_payment_callback_)); |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 153 | |
| 154 | // Fullfill the pending AreRequestedMethodsSupported call. |
| 155 | if (are_requested_methods_supported_callback_) |
| 156 | CheckRequestedMethodsSupported( |
| 157 | std::move(are_requested_methods_supported_callback_)); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 158 | } |
| 159 | |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 160 | void PaymentRequestState::OnPaymentResponseReady( |
| 161 | mojom::PaymentResponsePtr payment_response) { |
| 162 | delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 163 | } |
| 164 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 165 | void PaymentRequestState::OnSpecUpdated() { |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 166 | autofill::AutofillProfile* selected_shipping_profile = |
| 167 | selected_shipping_profile_; |
| 168 | autofill::AutofillProfile* selected_contact_profile = |
| 169 | selected_contact_profile_; |
| 170 | |
| 171 | if (spec_->current_update_reason() == |
| 172 | PaymentRequestSpec::UpdateReason::RETRY) { |
| 173 | if (spec_->has_shipping_address_error() && selected_shipping_profile) { |
| 174 | invalid_shipping_profile_ = selected_shipping_profile; |
| 175 | selected_shipping_profile_ = nullptr; |
| 176 | } |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 177 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 178 | if (spec_->has_payer_error() && selected_contact_profile) { |
| 179 | invalid_contact_profile_ = selected_contact_profile; |
| 180 | selected_contact_profile_ = nullptr; |
| 181 | } |
| 182 | } |
| 183 | |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 184 | if (spec_->selected_shipping_option_error().empty()) { |
| 185 | selected_shipping_option_error_profile_ = nullptr; |
| 186 | } else { |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 187 | selected_shipping_option_error_profile_ = selected_shipping_profile; |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 188 | selected_shipping_profile_ = nullptr; |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 189 | if (spec_->has_shipping_address_error() && selected_shipping_profile) { |
| 190 | invalid_shipping_profile_ = selected_shipping_profile; |
| 191 | } |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 192 | } |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 193 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 194 | is_waiting_for_merchant_validation_ = false; |
| 195 | UpdateIsReadyToPayAndNotifyObservers(); |
| 196 | } |
| 197 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 198 | void PaymentRequestState::CanMakePayment(StatusCallback callback) { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 199 | if (!get_all_instruments_finished_) { |
| 200 | can_make_payment_callback_ = std::move(callback); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 205 | FROM_HERE, |
| 206 | base::BindOnce(&PaymentRequestState::CheckCanMakePayment, |
| 207 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 208 | } |
| 209 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 210 | void PaymentRequestState::CheckCanMakePayment(StatusCallback callback) { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 211 | bool can_make_payment_value = false; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 212 | for (const auto& instrument : available_instruments_) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 213 | if (instrument->IsValidForCanMakePayment()) { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 214 | can_make_payment_value = true; |
| 215 | break; |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 216 | } |
| 217 | } |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 218 | std::move(callback).Run(can_make_payment_value); |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 219 | } |
| 220 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 221 | void PaymentRequestState::AreRequestedMethodsSupported( |
| 222 | StatusCallback callback) { |
| 223 | if (!get_all_instruments_finished_) { |
| 224 | are_requested_methods_supported_callback_ = std::move(callback); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 229 | FROM_HERE, |
| 230 | base::BindOnce(&PaymentRequestState::CheckRequestedMethodsSupported, |
| 231 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 232 | } |
| 233 | |
| 234 | void PaymentRequestState::CheckRequestedMethodsSupported( |
| 235 | StatusCallback callback) { |
| 236 | DCHECK(get_all_instruments_finished_); |
| 237 | |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 238 | std::move(callback).Run(are_requested_methods_supported_); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 239 | } |
| 240 | |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 241 | std::string PaymentRequestState::GetAuthenticatedEmail() const { |
| 242 | return payment_request_delegate_->GetAuthenticatedEmail(); |
| 243 | } |
| 244 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 245 | void PaymentRequestState::AddObserver(Observer* observer) { |
| 246 | CHECK(observer); |
| 247 | observers_.AddObserver(observer); |
| 248 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 249 | |
| 250 | void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 251 | observers_.RemoveObserver(observer); |
| 252 | } |
| 253 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 254 | void PaymentRequestState::GeneratePaymentResponse() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 255 | DCHECK(is_ready_to_pay()); |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 256 | |
| 257 | // Once the response is ready, will call back into OnPaymentResponseReady. |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 258 | response_helper_ = std::make_unique<PaymentResponseHelper>( |
sebsg | 8a9c234 | 2017-04-21 17:05:15 | [diff] [blame] | 259 | app_locale_, spec_, selected_instrument_, payment_request_delegate_, |
| 260 | selected_shipping_profile_, selected_contact_profile_, this); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 261 | } |
| 262 | |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 263 | void PaymentRequestState::RecordUseStats() { |
| 264 | if (spec_->request_shipping()) { |
| 265 | DCHECK(selected_shipping_profile_); |
| 266 | personal_data_manager_->RecordUseOf(*selected_shipping_profile_); |
| 267 | } |
| 268 | |
| 269 | if (spec_->request_payer_name() || spec_->request_payer_email() || |
| 270 | spec_->request_payer_phone()) { |
| 271 | DCHECK(selected_contact_profile_); |
| 272 | |
| 273 | // If the same address was used for both contact and shipping, the stats |
| 274 | // should only be updated once. |
| 275 | if (!spec_->request_shipping() || (selected_shipping_profile_->guid() != |
| 276 | selected_contact_profile_->guid())) { |
| 277 | personal_data_manager_->RecordUseOf(*selected_contact_profile_); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | selected_instrument_->RecordUse(); |
| 282 | } |
| 283 | |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 284 | void PaymentRequestState::AddAutofillPaymentInstrument( |
| 285 | bool selected, |
| 286 | const autofill::CreditCard& card) { |
| 287 | std::string basic_card_network = |
rouslan | 4e981bd3 | 2017-05-01 17:49:16 | [diff] [blame] | 288 | autofill::data_util::GetPaymentRequestData(card.network()) |
| 289 | .basic_card_issuer_network; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 290 | if (!spec_->supported_card_networks_set().count(basic_card_network) || |
| 291 | !spec_->supported_card_types_set().count(card.card_type())) { |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 292 | return; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // The total number of card types: credit, debit, prepaid, unknown. |
| 296 | constexpr size_t kTotalNumberOfCardTypes = 4U; |
| 297 | |
| 298 | // Whether the card type (credit, debit, prepaid) matches thetype that the |
| 299 | // merchant has requested exactly. This should be false for unknown card |
| 300 | // types, if the merchant cannot accept some card types. |
| 301 | bool matches_merchant_card_type_exactly = |
| 302 | card.card_type() != autofill::CreditCard::CARD_TYPE_UNKNOWN || |
| 303 | spec_->supported_card_types_set().size() == kTotalNumberOfCardTypes; |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 304 | |
| 305 | // AutofillPaymentInstrument makes a copy of |card| so it is effectively |
| 306 | // owned by this object. |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 307 | auto instrument = std::make_unique<AutofillPaymentInstrument>( |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 308 | basic_card_network, card, matches_merchant_card_type_exactly, |
| 309 | shipping_profiles_, app_locale_, payment_request_delegate_); |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 310 | available_instruments_.push_back(std::move(instrument)); |
| 311 | |
| 312 | if (selected) |
| 313 | SetSelectedInstrument(available_instruments_.back().get()); |
| 314 | } |
| 315 | |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 316 | void PaymentRequestState::AddAutofillShippingProfile( |
| 317 | bool selected, |
| 318 | const autofill::AutofillProfile& profile) { |
| 319 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 320 | std::make_unique<autofill::AutofillProfile>(profile)); |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 321 | // TODO(tmartino): Implement deduplication rules specific to shipping |
| 322 | // profiles. |
| 323 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 324 | shipping_profiles_.push_back(new_cached_profile); |
| 325 | |
| 326 | if (selected) |
| 327 | SetSelectedShippingProfile(new_cached_profile); |
| 328 | } |
| 329 | |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 330 | void PaymentRequestState::AddAutofillContactProfile( |
| 331 | bool selected, |
| 332 | const autofill::AutofillProfile& profile) { |
| 333 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 334 | std::make_unique<autofill::AutofillProfile>(profile)); |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 335 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 336 | contact_profiles_.push_back(new_cached_profile); |
| 337 | |
| 338 | if (selected) |
| 339 | SetSelectedContactProfile(new_cached_profile); |
| 340 | } |
| 341 | |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 342 | void PaymentRequestState::SetSelectedShippingOption( |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 343 | const std::string& shipping_option_id) { |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 344 | spec_->StartWaitingForUpdateWith( |
| 345 | PaymentRequestSpec::UpdateReason::SHIPPING_OPTION); |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 346 | // This will inform the merchant and will lead to them calling updateWith with |
| 347 | // new PaymentDetails. |
| 348 | delegate_->OnShippingOptionIdSelected(shipping_option_id); |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 349 | } |
| 350 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 351 | void PaymentRequestState::SetSelectedShippingProfile( |
| 352 | autofill::AutofillProfile* profile) { |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 353 | spec_->StartWaitingForUpdateWith( |
| 354 | PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 355 | selected_shipping_profile_ = profile; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 356 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 357 | // Changing the shipping address clears shipping address validation errors |
| 358 | // from retry(). |
| 359 | invalid_shipping_profile_ = nullptr; |
| 360 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 361 | // The user should not be able to click on pay until the callback from the |
| 362 | // merchant. |
| 363 | is_waiting_for_merchant_validation_ = true; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 364 | |
| 365 | // Start the normalization of the shipping address. |
Mathieu Perreault | c94700f11 | 2017-10-23 20:28:39 | [diff] [blame] | 366 | payment_request_delegate_->GetAddressNormalizer()->NormalizeAddressAsync( |
Mathieu Perreault | 9b40d510 | 2017-11-21 20:51:29 | [diff] [blame] | 367 | *selected_shipping_profile_, /*timeout_seconds=*/2, |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 368 | base::BindOnce(&PaymentRequestState::OnAddressNormalized, |
| 369 | weak_ptr_factory_.GetWeakPtr())); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void PaymentRequestState::SetSelectedContactProfile( |
| 373 | autofill::AutofillProfile* profile) { |
| 374 | selected_contact_profile_ = profile; |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 375 | |
| 376 | // Changing the contact information clears contact information validation |
| 377 | // errors from retry(). |
| 378 | invalid_contact_profile_ = nullptr; |
| 379 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 380 | UpdateIsReadyToPayAndNotifyObservers(); |
Jinho Bang | bb17815 | 2018-09-13 09:44:43 | [diff] [blame^] | 381 | |
| 382 | if (IsPaymentAppInvoked()) { |
| 383 | delegate_->OnPayerInfoSelected(response_helper_->GeneratePayerDetail()); |
| 384 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 385 | } |
| 386 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 387 | void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { |
| 388 | selected_instrument_ = instrument; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 389 | UpdateIsReadyToPayAndNotifyObservers(); |
| 390 | } |
| 391 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 392 | const std::string& PaymentRequestState::GetApplicationLocale() { |
| 393 | return app_locale_; |
| 394 | } |
| 395 | |
| 396 | autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { |
| 397 | return personal_data_manager_; |
| 398 | } |
| 399 | |
mad | 763ed2b | 2017-04-24 20:28:47 | [diff] [blame] | 400 | autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { |
| 401 | return payment_request_delegate_->GetRegionDataLoader(); |
mad | 4527193 | 2017-04-13 16:07:38 | [diff] [blame] | 402 | } |
| 403 | |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 404 | bool PaymentRequestState::IsPaymentAppInvoked() const { |
| 405 | return !!response_helper_; |
| 406 | } |
| 407 | |
Mathieu Perreault | eea046d1 | 2017-09-28 15:51:21 | [diff] [blame] | 408 | autofill::AddressNormalizer* PaymentRequestState::GetAddressNormalizer() { |
Anthony Vallee-Dubois | 71bf34946 | 2017-07-20 23:56:45 | [diff] [blame] | 409 | return payment_request_delegate_->GetAddressNormalizer(); |
| 410 | } |
| 411 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 412 | void PaymentRequestState::PopulateProfileCache() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 413 | std::vector<autofill::AutofillProfile*> profiles = |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 414 | personal_data_manager_->GetProfilesToSuggest(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 415 | |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 416 | std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering; |
| 417 | raw_profiles_for_filtering.reserve(profiles.size()); |
| 418 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 419 | // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 420 | // Thus, we store copies, and return a vector of pointers to these copies |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 421 | // whenever Profiles are requested. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 422 | for (size_t i = 0; i < profiles.size(); i++) { |
| 423 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 424 | std::make_unique<autofill::AutofillProfile>(*profiles[i])); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 425 | raw_profiles_for_filtering.push_back(profile_cache_.back().get()); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 426 | } |
| 427 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 428 | contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
| 429 | raw_profiles_for_filtering); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 430 | shipping_profiles_ = profile_comparator()->FilterProfilesForShipping( |
| 431 | raw_profiles_for_filtering); |
tmartino | a6eb22f | 2017-04-06 20:16:24 | [diff] [blame] | 432 | |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 433 | // Set the number of suggestions shown for the sections requested by the |
| 434 | // merchant. |
| 435 | if (spec_->request_payer_name() || spec_->request_payer_phone() || |
| 436 | spec_->request_payer_email()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 437 | bool has_complete_contact = |
| 438 | contact_profiles_.empty() |
| 439 | ? false |
| 440 | : profile_comparator()->IsContactInfoComplete(contact_profiles_[0]); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 441 | journey_logger_->SetNumberOfSuggestionsShown( |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 442 | JourneyLogger::Section::SECTION_CONTACT_INFO, contact_profiles_.size(), |
| 443 | has_complete_contact); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 444 | } |
| 445 | if (spec_->request_shipping()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 446 | bool has_complete_shipping = |
| 447 | shipping_profiles_.empty() |
| 448 | ? false |
| 449 | : profile_comparator()->IsShippingComplete(shipping_profiles_[0]); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 450 | journey_logger_->SetNumberOfSuggestionsShown( |
| 451 | JourneyLogger::Section::SECTION_SHIPPING_ADDRESS, |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 452 | shipping_profiles_.size(), has_complete_shipping); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 453 | } |
| 454 | |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 455 | // Create the list of available instruments. A copy of each card will be made |
| 456 | // by their respective AutofillPaymentInstrument. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 457 | const std::vector<autofill::CreditCard*>& cards = |
Amirhossein Simjour | d3b7ba4a | 2018-05-07 15:27:21 | [diff] [blame] | 458 | personal_data_manager_->GetCreditCardsToSuggest( |
Mathieu Perreault | 157f399 | 2018-06-29 18:48:06 | [diff] [blame] | 459 | /*include_server_cards=*/base::FeatureList::IsEnabled( |
| 460 | payments::features::kReturnGooglePayInBasicCard)); |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 461 | for (autofill::CreditCard* card : cards) |
| 462 | AddAutofillPaymentInstrument(/*selected=*/false, *card); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void PaymentRequestState::SetDefaultProfileSelections() { |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 466 | // Only pre-select an address if the merchant provided at least one selected |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 467 | // shipping option, and the top profile is complete. Assumes that profiles |
| 468 | // have already been sorted for completeness and frecency. |
| 469 | if (!shipping_profiles().empty() && spec_->selected_shipping_option() && |
| 470 | profile_comparator()->IsShippingComplete(shipping_profiles_[0])) { |
| 471 | selected_shipping_profile_ = shipping_profiles()[0]; |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 472 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 473 | |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 474 | // Contact profiles were ordered by completeness in addition to frecency; |
| 475 | // the first one is the best default selection. |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 476 | if (!contact_profiles().empty() && |
| 477 | profile_comparator()->IsContactInfoComplete(contact_profiles_[0])) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 478 | selected_contact_profile_ = contact_profiles()[0]; |
| 479 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 480 | // TODO(crbug.com/702063): Change this code to prioritize instruments by use |
| 481 | // count and other means, and implement a way to modify this function's return |
| 482 | // value. |
| 483 | const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = |
| 484 | available_instruments(); |
| 485 | auto first_complete_instrument = |
| 486 | std::find_if(instruments.begin(), instruments.end(), |
| 487 | [](const std::unique_ptr<PaymentInstrument>& instrument) { |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 488 | return instrument->IsCompleteForPayment() && |
| 489 | instrument->IsExactlyMatchingMerchantRequest(); |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 490 | }); |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 491 | selected_instrument_ = first_complete_instrument == instruments.end() |
| 492 | ? nullptr |
| 493 | : first_complete_instrument->get(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 494 | UpdateIsReadyToPayAndNotifyObservers(); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 495 | |
| 496 | bool has_complete_instrument = |
| 497 | available_instruments().empty() |
| 498 | ? false |
| 499 | : available_instruments()[0]->IsCompleteForPayment(); |
| 500 | |
| 501 | journey_logger_->SetNumberOfSuggestionsShown( |
| 502 | JourneyLogger::Section::SECTION_PAYMENT_METHOD, |
| 503 | available_instruments().size(), has_complete_instrument); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { |
| 507 | is_ready_to_pay_ = |
| 508 | ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); |
| 509 | NotifyOnSelectedInformationChanged(); |
| 510 | } |
| 511 | |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 512 | void PaymentRequestState::NotifyOnGetAllPaymentInstrumentsFinished() { |
| 513 | for (auto& observer : observers_) |
| 514 | observer.OnGetAllPaymentInstrumentsFinished(); |
| 515 | } |
| 516 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 517 | void PaymentRequestState::NotifyOnSelectedInformationChanged() { |
| 518 | for (auto& observer : observers_) |
| 519 | observer.OnSelectedInformationChanged(); |
| 520 | } |
| 521 | |
| 522 | bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 523 | // There is no need to check for supported networks, because only supported |
| 524 | // instruments are listed/created in the flow. |
mathp | 4baea33 | 2017-04-10 21:42:29 | [diff] [blame] | 525 | return selected_instrument_ != nullptr && |
| 526 | selected_instrument_->IsCompleteForPayment(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 530 | if (is_waiting_for_merchant_validation_) |
| 531 | return false; |
| 532 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 533 | if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) |
| 534 | return false; |
| 535 | |
Anthony Vallee-Dubois | a881467 | 2017-06-01 18:43:20 | [diff] [blame] | 536 | if (spec_->request_shipping() && !spec_->selected_shipping_option()) |
| 537 | return false; |
| 538 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 539 | return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 540 | } |
| 541 | |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 542 | void PaymentRequestState::OnAddressNormalized( |
| 543 | bool success, |
| 544 | const autofill::AutofillProfile& normalized_profile) { |
| 545 | delegate_->OnShippingAddressSelected( |
Tommy Martino | 21d415f | 2018-01-12 20:08:22 | [diff] [blame] | 546 | data_util::GetPaymentAddressFromAutofillProfile(normalized_profile, |
| 547 | app_locale_)); |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 548 | } |
| 549 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 550 | } // namespace payments |