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" |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 13 | #include "base/metrics/histogram_functions.h" |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 15 | #include "components/autofill/core/browser/autofill_data_util.h" |
Dominic Battre | aed20f9f | 2019-05-10 19:09:52 | [diff] [blame] | 16 | #include "components/autofill/core/browser/data_model/autofill_profile.h" |
| 17 | #include "components/autofill/core/browser/data_model/credit_card.h" |
Dominic Battre | dd73d26 | 2019-05-15 20:10:38 | [diff] [blame] | 18 | #include "components/autofill/core/browser/geo/autofill_country.h" |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 19 | #include "components/autofill/core/browser/personal_data_manager.h" |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 20 | #include "components/autofill/core/browser/validation.h" |
Rouslan Solomakhin | 2039a34 | 2020-05-21 19:21:04 | [diff] [blame] | 21 | #include "components/payments/content/autofill_payment_app.h" |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 22 | #include "components/payments/content/autofill_payment_app_factory.h" |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 23 | #include "components/payments/content/content_payment_request_delegate.h" |
Rouslan Solomakhin | 2039a34 | 2020-05-21 19:21:04 | [diff] [blame] | 24 | #include "components/payments/content/payment_app.h" |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 25 | #include "components/payments/content/payment_app_service.h" |
| 26 | #include "components/payments/content/payment_app_service_factory.h" |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 27 | #include "components/payments/content/payment_manifest_web_data_service.h" |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame] | 28 | #include "components/payments/content/payment_response_helper.h" |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 29 | #include "components/payments/content/service_worker_payment_app.h" |
Sahel Sharify | 13331e8 | 2019-07-12 01:00:30 | [diff] [blame] | 30 | #include "components/payments/core/autofill_card_validation.h" |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 31 | #include "components/payments/core/error_strings.h" |
Mathieu Perreault | 157f399 | 2018-06-29 18:48:06 | [diff] [blame] | 32 | #include "components/payments/core/features.h" |
Rouslan Solomakhin | 85b10da | 2019-11-05 20:03:17 | [diff] [blame] | 33 | #include "components/payments/core/method_strings.h" |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 34 | #include "components/payments/core/payment_request_data_util.h" |
Danyao Wang | 761739b | 2019-06-27 23:59:32 | [diff] [blame] | 35 | #include "components/payments/core/payments_experimental_features.h" |
Danyao Wang | 4f2ba63 | 2020-03-11 13:56:54 | [diff] [blame] | 36 | #include "content/public/browser/render_frame_host.h" |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 37 | #include "content/public/common/content_features.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 38 | |
| 39 | namespace payments { |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 40 | namespace { |
| 41 | |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 42 | // Invokes the |callback| with |status|. |
| 43 | void CallStatusCallback(PaymentRequestState::StatusCallback callback, |
| 44 | bool status) { |
| 45 | std::move(callback).Run(status); |
| 46 | } |
| 47 | |
| 48 | // Posts the |callback| to be invoked with |status| asynchronously. |
| 49 | void PostStatusCallback(PaymentRequestState::StatusCallback callback, |
| 50 | bool status) { |
| 51 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 52 | FROM_HERE, |
| 53 | base::BindOnce(&CallStatusCallback, std::move(callback), status)); |
| 54 | } |
| 55 | |
| 56 | } // namespace |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 57 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 58 | PaymentRequestState::PaymentRequestState( |
Rouslan Solomakhin | dbf593d9 | 2017-11-21 19:20:57 | [diff] [blame] | 59 | content::WebContents* web_contents, |
Danyao Wang | 4f2ba63 | 2020-03-11 13:56:54 | [diff] [blame] | 60 | content::RenderFrameHost* initiator_render_frame_host, |
gogerald | 7a0cc3e | 2017-09-19 03:35:48 | [diff] [blame] | 61 | const GURL& top_level_origin, |
| 62 | const GURL& frame_origin, |
Rouslan Solomakhin | c71cca62 | 2020-01-31 22:15:35 | [diff] [blame] | 63 | const url::Origin& frame_security_origin, |
DongJun Kim | 54690d5 | 2020-09-26 12:07:54 | [diff] [blame] | 64 | base::WeakPtr<PaymentRequestSpec> spec, |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame] | 65 | base::WeakPtr<Delegate> delegate, |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 66 | const std::string& app_locale, |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 67 | autofill::PersonalDataManager* personal_data_manager, |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 68 | ContentPaymentRequestDelegate* payment_request_delegate, |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 69 | JourneyLogger* journey_logger) |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 70 | : web_contents_(web_contents), |
Danyao Wang | 4f2ba63 | 2020-03-11 13:56:54 | [diff] [blame] | 71 | initiator_render_frame_host_(initiator_render_frame_host), |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 72 | top_origin_(top_level_origin), |
| 73 | frame_origin_(frame_origin), |
Rouslan Solomakhin | c71cca62 | 2020-01-31 22:15:35 | [diff] [blame] | 74 | frame_security_origin_(frame_security_origin), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 75 | app_locale_(app_locale), |
DongJun Kim | 54690d5 | 2020-09-26 12:07:54 | [diff] [blame] | 76 | spec_(spec), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 77 | delegate_(delegate), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 78 | personal_data_manager_(personal_data_manager), |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 79 | journey_logger_(journey_logger), |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 80 | are_requested_methods_supported_( |
| 81 | !spec_->supported_card_networks().empty()), |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 82 | payment_request_delegate_(payment_request_delegate), |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 83 | profile_comparator_(app_locale, *spec) { |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 84 | PopulateProfileCache(); |
Danyao Wang | 761739b | 2019-06-27 23:59:32 | [diff] [blame] | 85 | |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 86 | // |web_contents_| is null in unit tests. |
Danyao Wang | e865a30 | 2020-04-01 18:35:14 | [diff] [blame] | 87 | PaymentAppService* service = PaymentAppServiceFactory::GetForContext( |
| 88 | web_contents_ ? web_contents_->GetBrowserContext() : nullptr); |
| 89 | number_of_payment_app_factories_ = service->GetNumberOfFactories(); |
| 90 | service->Create(weak_ptr_factory_.GetWeakPtr()); |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 91 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 92 | spec_->AddObserver(this); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 93 | } |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 94 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 95 | PaymentRequestState::~PaymentRequestState() {} |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 96 | |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 97 | content::WebContents* PaymentRequestState::GetWebContents() { |
| 98 | return web_contents_; |
gogerald | ac15d236 | 2017-11-15 01:04:19 | [diff] [blame] | 99 | } |
| 100 | |
Danyao Wang | aed5b0a9 | 2020-04-01 22:43:14 | [diff] [blame] | 101 | ContentPaymentRequestDelegate* PaymentRequestState::GetPaymentRequestDelegate() |
| 102 | const { |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 103 | return payment_request_delegate_; |
| 104 | } |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 105 | |
Rouslan Solomakhin | 535fe30 | 2020-06-02 20:17:39 | [diff] [blame] | 106 | void PaymentRequestState::ShowProcessingSpinner() { |
| 107 | GetPaymentRequestDelegate()->ShowProcessingSpinner(); |
| 108 | } |
| 109 | |
DongJun Kim | 54690d5 | 2020-09-26 12:07:54 | [diff] [blame] | 110 | base::WeakPtr<PaymentRequestSpec> PaymentRequestState::GetSpec() const { |
| 111 | return spec_; |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 112 | } |
gogerald | ac15d236 | 2017-11-15 01:04:19 | [diff] [blame] | 113 | |
Rouslan Solomakhin | cbdeef9 | 2020-08-15 12:39:30 | [diff] [blame] | 114 | std::string PaymentRequestState::GetTwaPackageName() const { |
| 115 | return GetPaymentRequestDelegate()->GetTwaPackageName(); |
| 116 | } |
| 117 | |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 118 | const GURL& PaymentRequestState::GetTopOrigin() { |
| 119 | return top_origin_; |
| 120 | } |
| 121 | |
| 122 | const GURL& PaymentRequestState::GetFrameOrigin() { |
| 123 | return frame_origin_; |
| 124 | } |
| 125 | |
Rouslan Solomakhin | c71cca62 | 2020-01-31 22:15:35 | [diff] [blame] | 126 | const url::Origin& PaymentRequestState::GetFrameSecurityOrigin() { |
| 127 | return frame_security_origin_; |
| 128 | } |
| 129 | |
Danyao Wang | 4f2ba63 | 2020-03-11 13:56:54 | [diff] [blame] | 130 | content::RenderFrameHost* PaymentRequestState::GetInitiatorRenderFrameHost() |
| 131 | const { |
| 132 | return initiator_render_frame_host_; |
| 133 | } |
| 134 | |
Danyao Wang | aed5b0a9 | 2020-04-01 22:43:14 | [diff] [blame] | 135 | const std::vector<mojom::PaymentMethodDataPtr>& |
| 136 | PaymentRequestState::GetMethodData() const { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 137 | DCHECK(GetSpec()); |
Danyao Wang | aed5b0a9 | 2020-04-01 22:43:14 | [diff] [blame] | 138 | return GetSpec()->method_data(); |
| 139 | } |
| 140 | |
Rouslan Solomakhin | 914686c5 | 2020-08-21 15:15:41 | [diff] [blame] | 141 | std::unique_ptr<autofill::InternalAuthenticator> |
| 142 | PaymentRequestState::CreateInternalAuthenticator() const { |
Rouslan Solomakhin | 54e0d1e | 2020-08-30 20:23:31 | [diff] [blame] | 143 | return GetPaymentRequestDelegate()->CreateInternalAuthenticator(); |
Rouslan Solomakhin | 914686c5 | 2020-08-21 15:15:41 | [diff] [blame] | 144 | } |
| 145 | |
Danyao Wang | aed5b0a9 | 2020-04-01 22:43:14 | [diff] [blame] | 146 | scoped_refptr<PaymentManifestWebDataService> |
| 147 | PaymentRequestState::GetPaymentManifestWebDataService() const { |
| 148 | return GetPaymentRequestDelegate()->GetPaymentManifestWebDataService(); |
| 149 | } |
| 150 | |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 151 | const std::vector<autofill::AutofillProfile*>& |
| 152 | PaymentRequestState::GetBillingProfiles() { |
| 153 | return shipping_profiles_; |
| 154 | } |
| 155 | |
| 156 | bool PaymentRequestState::IsRequestedAutofillDataAvailable() { |
| 157 | return is_requested_autofill_data_available_; |
| 158 | } |
| 159 | |
| 160 | bool PaymentRequestState::MayCrawlForInstallablePaymentApps() { |
| 161 | return PaymentsExperimentalFeatures::IsEnabled( |
| 162 | features::kAlwaysAllowJustInTimePaymentApp) || |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 163 | !spec_ || !spec_->supports_basic_card(); |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 164 | } |
| 165 | |
Rouslan Solomakhin | 535fe30 | 2020-06-02 20:17:39 | [diff] [blame] | 166 | bool PaymentRequestState::IsOffTheRecord() const { |
| 167 | return GetPaymentRequestDelegate()->IsOffTheRecord(); |
| 168 | } |
| 169 | |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 170 | void PaymentRequestState::OnPaymentAppCreated(std::unique_ptr<PaymentApp> app) { |
| 171 | if (app->type() == PaymentApp::Type::AUTOFILL) { |
| 172 | journey_logger_->SetEventOccurred( |
| 173 | JourneyLogger::EVENT_AVAILABLE_METHOD_BASIC_CARD); |
| 174 | } else if (base::Contains(app->GetAppMethodNames(), methods::kGooglePay) || |
| 175 | base::Contains(app->GetAppMethodNames(), methods::kAndroidPay)) { |
Danyao Wang | 25f72dc | 2019-10-18 05:11:32 | [diff] [blame] | 176 | journey_logger_->SetEventOccurred( |
| 177 | JourneyLogger::EVENT_AVAILABLE_METHOD_GOOGLE); |
| 178 | } else { |
| 179 | journey_logger_->SetEventOccurred( |
| 180 | JourneyLogger::EVENT_AVAILABLE_METHOD_OTHER); |
| 181 | } |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 182 | available_apps_.emplace_back(std::move(app)); |
gogerald | ac15d236 | 2017-11-15 01:04:19 | [diff] [blame] | 183 | } |
| 184 | |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 185 | void PaymentRequestState::OnPaymentAppCreationError( |
| 186 | const std::string& error_message) { |
| 187 | get_all_payment_apps_error_ = error_message; |
| 188 | } |
| 189 | |
Danyao Wang | aed5b0a9 | 2020-04-01 22:43:14 | [diff] [blame] | 190 | bool PaymentRequestState::SkipCreatingNativePaymentApps() const { |
| 191 | return false; |
| 192 | } |
| 193 | |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 194 | void PaymentRequestState::OnDoneCreatingPaymentApps() { |
| 195 | DCHECK_NE(0U, number_of_payment_app_factories_); |
| 196 | if (--number_of_payment_app_factories_ > 0U) |
| 197 | return; |
| 198 | |
Nick Burris | fca09083 | 2020-07-28 00:17:42 | [diff] [blame] | 199 | if (IsInTwa()) { |
| 200 | // If a preferred payment app is present (e.g. Play Billing within a TWA), |
| 201 | // all other payment apps are ignored. |
| 202 | bool has_preferred_app = |
| 203 | std::any_of(available_apps_.begin(), available_apps_.end(), |
| 204 | [](const auto& app) { return app->IsPreferred(); }); |
| 205 | if (has_preferred_app) { |
| 206 | available_apps_.erase( |
| 207 | std::remove_if(available_apps_.begin(), available_apps_.end(), |
| 208 | [](const auto& app) { return !app->IsPreferred(); }), |
| 209 | available_apps_.end()); |
| 210 | |
| 211 | // By design, only one payment app can be preferred. |
| 212 | DCHECK_EQ(available_apps_.size(), 1u); |
| 213 | if (available_apps_.size() > 1) |
| 214 | available_apps_.resize(1); |
| 215 | } |
| 216 | } |
| 217 | |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 218 | SetDefaultProfileSelections(); |
| 219 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 220 | get_all_apps_finished_ = true; |
Danyao Wang | 5a14e98 | 2020-02-12 21:34:09 | [diff] [blame] | 221 | has_enrolled_instrument_ = |
| 222 | std::any_of(available_apps_.begin(), available_apps_.end(), |
| 223 | [](const auto& app) { return app->HasEnrolledInstrument(); }); |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 224 | are_requested_methods_supported_ |= !available_apps_.empty(); |
| 225 | NotifyOnGetAllPaymentAppsFinished(); |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 226 | NotifyInitialized(); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 227 | |
Danyao Wang | 4bc0606a | 2018-12-27 16:54:53 | [diff] [blame] | 228 | // Fulfill the pending CanMakePayment call. |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 229 | if (can_make_payment_callback_) |
Rouslan Solomakhin | ebf9f16 | 2020-08-27 15:28:13 | [diff] [blame] | 230 | std::move(can_make_payment_callback_).Run(GetCanMakePaymentValue()); |
Danyao Wang | 4bc0606a | 2018-12-27 16:54:53 | [diff] [blame] | 231 | |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 232 | // Fulfill the pending HasEnrolledInstrument call. |
| 233 | if (has_enrolled_instrument_callback_) |
Rouslan Solomakhin | ebf9f16 | 2020-08-27 15:28:13 | [diff] [blame] | 234 | std::move(has_enrolled_instrument_callback_) |
| 235 | .Run(GetHasEnrolledInstrumentValue()); |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 236 | |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 237 | // Fulfill the pending AreRequestedMethodsSupported call. |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 238 | if (are_requested_methods_supported_callback_) |
| 239 | CheckRequestedMethodsSupported( |
| 240 | std::move(are_requested_methods_supported_callback_)); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 241 | } |
| 242 | |
Rouslan Solomakhin | ebf9f16 | 2020-08-27 15:28:13 | [diff] [blame] | 243 | void PaymentRequestState::SetCanMakePaymentEvenWithoutApps() { |
| 244 | can_make_payment_even_without_apps_ = true; |
| 245 | } |
| 246 | |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 247 | void PaymentRequestState::OnPaymentResponseReady( |
| 248 | mojom::PaymentResponsePtr payment_response) { |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame] | 249 | if (!delegate_) |
| 250 | return; |
| 251 | |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 252 | delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 253 | } |
| 254 | |
Rouslan Solomakhin | 68429b7 | 2019-06-27 15:12:39 | [diff] [blame] | 255 | void PaymentRequestState::OnPaymentResponseError( |
| 256 | const std::string& error_message) { |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame] | 257 | if (!delegate_) |
| 258 | return; |
| 259 | |
Rouslan Solomakhin | 68429b7 | 2019-06-27 15:12:39 | [diff] [blame] | 260 | delegate_->OnPaymentResponseError(error_message); |
| 261 | } |
| 262 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 263 | void PaymentRequestState::OnSpecUpdated() { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 264 | if (!spec_) |
| 265 | return; |
| 266 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 267 | autofill::AutofillProfile* selected_shipping_profile = |
| 268 | selected_shipping_profile_; |
| 269 | autofill::AutofillProfile* selected_contact_profile = |
| 270 | selected_contact_profile_; |
| 271 | |
| 272 | if (spec_->current_update_reason() == |
| 273 | PaymentRequestSpec::UpdateReason::RETRY) { |
| 274 | if (spec_->has_shipping_address_error() && selected_shipping_profile) { |
| 275 | invalid_shipping_profile_ = selected_shipping_profile; |
| 276 | selected_shipping_profile_ = nullptr; |
| 277 | } |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 278 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 279 | if (spec_->has_payer_error() && selected_contact_profile) { |
| 280 | invalid_contact_profile_ = selected_contact_profile; |
| 281 | selected_contact_profile_ = nullptr; |
| 282 | } |
| 283 | } |
| 284 | |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 285 | if (spec_->selected_shipping_option_error().empty()) { |
| 286 | selected_shipping_option_error_profile_ = nullptr; |
| 287 | } else { |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 288 | selected_shipping_option_error_profile_ = selected_shipping_profile; |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 289 | selected_shipping_profile_ = nullptr; |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 290 | if (spec_->has_shipping_address_error() && selected_shipping_profile) { |
| 291 | invalid_shipping_profile_ = selected_shipping_profile; |
| 292 | } |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 293 | } |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 294 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 295 | is_waiting_for_merchant_validation_ = false; |
| 296 | UpdateIsReadyToPayAndNotifyObservers(); |
| 297 | } |
| 298 | |
Danyao Wang | 03a4cbd | 2019-08-15 23:47:11 | [diff] [blame] | 299 | void PaymentRequestState::CanMakePayment(StatusCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 300 | if (!get_all_apps_finished_) { |
Danyao Wang | 4bc0606a | 2018-12-27 16:54:53 | [diff] [blame] | 301 | DCHECK(!can_make_payment_callback_); |
| 302 | can_make_payment_callback_ = std::move(callback); |
| 303 | return; |
| 304 | } |
| 305 | |
Rouslan Solomakhin | 159e513 | 2020-08-29 21:05:02 | [diff] [blame] | 306 | PostStatusCallback(std::move(callback), GetCanMakePaymentValue()); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void PaymentRequestState::HasEnrolledInstrument(StatusCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 310 | if (!get_all_apps_finished_) { |
Danyao Wang | 4bc0606a | 2018-12-27 16:54:53 | [diff] [blame] | 311 | DCHECK(!has_enrolled_instrument_callback_); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 312 | has_enrolled_instrument_callback_ = std::move(callback); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
Rouslan Solomakhin | ebf9f16 | 2020-08-27 15:28:13 | [diff] [blame] | 316 | PostStatusCallback(std::move(callback), GetHasEnrolledInstrumentValue()); |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 317 | } |
| 318 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 319 | void PaymentRequestState::AreRequestedMethodsSupported( |
Rouslan Solomakhin | d5dcc32 | 2019-07-11 21:47:20 | [diff] [blame] | 320 | MethodsSupportedCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 321 | if (!get_all_apps_finished_) { |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 322 | are_requested_methods_supported_callback_ = std::move(callback); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 327 | FROM_HERE, |
| 328 | base::BindOnce(&PaymentRequestState::CheckRequestedMethodsSupported, |
| 329 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 330 | } |
| 331 | |
Sahel Sharify | d341822 | 2020-02-19 21:50:02 | [diff] [blame] | 332 | void PaymentRequestState::OnAbort() { |
| 333 | // Reset supported method callback when the merchant calls abort before |
| 334 | // OnDoneCreatingPaymentApps(). |
| 335 | if (are_requested_methods_supported_callback_) |
| 336 | are_requested_methods_supported_callback_.Reset(); |
| 337 | } |
| 338 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 339 | void PaymentRequestState::CheckRequestedMethodsSupported( |
Rouslan Solomakhin | d5dcc32 | 2019-07-11 21:47:20 | [diff] [blame] | 340 | MethodsSupportedCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 341 | DCHECK(get_all_apps_finished_); |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 342 | |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 343 | if (!spec_) |
| 344 | return; |
| 345 | |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 346 | // Don't modify the value of |are_requested_methods_supported_|, because it's |
| 347 | // used for canMakePayment(). |
| 348 | bool supported = are_requested_methods_supported_; |
| 349 | if (supported && is_show_user_gesture_ && |
Rouslan Solomakhin | 85b10da | 2019-11-05 20:03:17 | [diff] [blame] | 350 | base::Contains(spec_->payment_method_identifiers_set(), |
| 351 | methods::kBasicCard) && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 352 | !has_non_autofill_app_ && !has_enrolled_instrument_ && |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 353 | PaymentsExperimentalFeatures::IsEnabled( |
| 354 | features::kStrictHasEnrolledAutofillInstrument)) { |
| 355 | supported = false; |
| 356 | get_all_payment_apps_error_ = errors::kStrictBasicCardShowReject; |
| 357 | } |
| 358 | |
Liquan (Max) Gu | a3db8c07 | 2020-06-22 16:24:21 | [diff] [blame] | 359 | if (!supported && get_all_payment_apps_error_.empty() && |
| 360 | base::Contains(spec_->payment_method_identifiers_set(), |
Rouslan Solomakhin | a1ff8150 | 2020-06-26 00:56:00 | [diff] [blame] | 361 | methods::kGooglePlayBilling) && |
Nick Burris | fca09083 | 2020-07-28 00:17:42 | [diff] [blame] | 362 | !IsInTwa()) { |
Liquan (Max) Gu | a3db8c07 | 2020-06-22 16:24:21 | [diff] [blame] | 363 | get_all_payment_apps_error_ = errors::kAppStoreMethodOnlySupportedInTwa; |
| 364 | } |
| 365 | |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 366 | std::move(callback).Run(supported, get_all_payment_apps_error_); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 367 | } |
| 368 | |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 369 | std::string PaymentRequestState::GetAuthenticatedEmail() const { |
| 370 | return payment_request_delegate_->GetAuthenticatedEmail(); |
| 371 | } |
| 372 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 373 | void PaymentRequestState::AddObserver(Observer* observer) { |
| 374 | CHECK(observer); |
| 375 | observers_.AddObserver(observer); |
| 376 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 377 | |
| 378 | void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 379 | observers_.RemoveObserver(observer); |
| 380 | } |
| 381 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 382 | void PaymentRequestState::GeneratePaymentResponse() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 383 | DCHECK(is_ready_to_pay()); |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 384 | |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 385 | if (!spec_) |
| 386 | return; |
| 387 | |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 388 | // Once the response is ready, will call back into OnPaymentResponseReady. |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 389 | response_helper_ = std::make_unique<PaymentResponseHelper>( |
DongJun Kim | 54690d5 | 2020-09-26 12:07:54 | [diff] [blame] | 390 | app_locale_, spec_, selected_app_, payment_request_delegate_, |
| 391 | selected_shipping_profile_, selected_contact_profile_, |
| 392 | weak_ptr_factory_.GetWeakPtr()); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 393 | } |
| 394 | |
Rouslan Solomakhin | 761d65eb | 2019-04-29 20:08:01 | [diff] [blame] | 395 | void PaymentRequestState::OnPaymentAppWindowClosed() { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 396 | DCHECK(selected_app_); |
Rouslan Solomakhin | 761d65eb | 2019-04-29 20:08:01 | [diff] [blame] | 397 | response_helper_.reset(); |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 398 | selected_app_->OnPaymentAppWindowClosed(); |
Rouslan Solomakhin | 761d65eb | 2019-04-29 20:08:01 | [diff] [blame] | 399 | } |
| 400 | |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 401 | void PaymentRequestState::RecordUseStats() { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 402 | if (ShouldShowShippingSection()) { |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 403 | DCHECK(selected_shipping_profile_); |
| 404 | personal_data_manager_->RecordUseOf(*selected_shipping_profile_); |
| 405 | } |
| 406 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 407 | if (ShouldShowContactSection()) { |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 408 | DCHECK(selected_contact_profile_); |
| 409 | |
| 410 | // If the same address was used for both contact and shipping, the stats |
| 411 | // should only be updated once. |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 412 | if (!ShouldShowShippingSection() || (selected_shipping_profile_->guid() != |
| 413 | selected_contact_profile_->guid())) { |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 414 | personal_data_manager_->RecordUseOf(*selected_contact_profile_); |
| 415 | } |
| 416 | } |
| 417 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 418 | selected_app_->RecordUse(); |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 419 | } |
| 420 | |
Sahel Sharify | cf4e213 | 2020-01-24 23:59:08 | [diff] [blame] | 421 | void PaymentRequestState::SetAvailablePaymentAppForRetry() { |
| 422 | DCHECK(selected_app_); |
| 423 | base::EraseIf(available_apps_, [this](const auto& payment_app) { |
| 424 | // Remove the app if it is not selected. |
| 425 | return payment_app.get() != selected_app_; |
| 426 | }); |
| 427 | is_retry_called_ = true; |
| 428 | } |
| 429 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 430 | void PaymentRequestState::AddAutofillPaymentApp( |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 431 | bool selected, |
| 432 | const autofill::CreditCard& card) { |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 433 | auto app = |
| 434 | AutofillPaymentAppFactory::ConvertCardToPaymentAppIfSupportedNetwork( |
| 435 | card, weak_ptr_factory_.GetWeakPtr()); |
| 436 | if (!app) |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 437 | return; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 438 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 439 | available_apps_.push_back(std::move(app)); |
Danyao Wang | 25f72dc | 2019-10-18 05:11:32 | [diff] [blame] | 440 | journey_logger_->SetEventOccurred( |
| 441 | JourneyLogger::EVENT_AVAILABLE_METHOD_BASIC_CARD); |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 442 | |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 443 | if (selected) { |
Danyao Wang | 46e7d94 | 2020-09-25 15:45:18 | [diff] [blame] | 444 | SetSelectedApp(available_apps_.back().get()); |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 445 | } |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 446 | } |
| 447 | |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 448 | void PaymentRequestState::AddAutofillShippingProfile( |
| 449 | bool selected, |
| 450 | const autofill::AutofillProfile& profile) { |
| 451 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 452 | std::make_unique<autofill::AutofillProfile>(profile)); |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 453 | // TODO(tmartino): Implement deduplication rules specific to shipping |
| 454 | // profiles. |
| 455 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 456 | shipping_profiles_.push_back(new_cached_profile); |
| 457 | |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 458 | if (selected) { |
Danyao Wang | 46e7d94 | 2020-09-25 15:45:18 | [diff] [blame] | 459 | SetSelectedShippingProfile(new_cached_profile); |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 460 | } |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 461 | } |
| 462 | |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 463 | void PaymentRequestState::AddAutofillContactProfile( |
| 464 | bool selected, |
| 465 | const autofill::AutofillProfile& profile) { |
| 466 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 467 | std::make_unique<autofill::AutofillProfile>(profile)); |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 468 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 469 | contact_profiles_.push_back(new_cached_profile); |
| 470 | |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 471 | if (selected) { |
Danyao Wang | 46e7d94 | 2020-09-25 15:45:18 | [diff] [blame] | 472 | SetSelectedContactProfile(new_cached_profile); |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 473 | } |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 474 | } |
| 475 | |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 476 | void PaymentRequestState::SetSelectedShippingOption( |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 477 | const std::string& shipping_option_id) { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 478 | if (!spec_) |
| 479 | return; |
| 480 | |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 481 | spec_->StartWaitingForUpdateWith( |
| 482 | PaymentRequestSpec::UpdateReason::SHIPPING_OPTION); |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame] | 483 | if (delegate_) { |
| 484 | // This will inform the merchant and will lead to them calling updateWith |
| 485 | // with new PaymentDetails. |
| 486 | delegate_->OnShippingOptionIdSelected(shipping_option_id); |
| 487 | } |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 488 | } |
| 489 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 490 | void PaymentRequestState::SetSelectedShippingProfile( |
Danyao Wang | 46e7d94 | 2020-09-25 15:45:18 | [diff] [blame] | 491 | autofill::AutofillProfile* profile) { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 492 | if (!spec_) |
| 493 | return; |
| 494 | |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 495 | spec_->StartWaitingForUpdateWith( |
| 496 | PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 497 | selected_shipping_profile_ = profile; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 498 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 499 | // Changing the shipping address clears shipping address validation errors |
| 500 | // from retry(). |
| 501 | invalid_shipping_profile_ = nullptr; |
| 502 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 503 | // The user should not be able to click on pay until the callback from the |
| 504 | // merchant. |
| 505 | is_waiting_for_merchant_validation_ = true; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 506 | |
| 507 | // Start the normalization of the shipping address. |
Mathieu Perreault | c94700f | 2017-10-23 20:28:39 | [diff] [blame] | 508 | payment_request_delegate_->GetAddressNormalizer()->NormalizeAddressAsync( |
Mathieu Perreault | 9b40d510 | 2017-11-21 20:51:29 | [diff] [blame] | 509 | *selected_shipping_profile_, /*timeout_seconds=*/2, |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 510 | base::BindOnce(&PaymentRequestState::OnAddressNormalized, |
| 511 | weak_ptr_factory_.GetWeakPtr())); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void PaymentRequestState::SetSelectedContactProfile( |
Danyao Wang | 46e7d94 | 2020-09-25 15:45:18 | [diff] [blame] | 515 | autofill::AutofillProfile* profile) { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 516 | selected_contact_profile_ = profile; |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 517 | |
| 518 | // Changing the contact information clears contact information validation |
| 519 | // errors from retry(). |
| 520 | invalid_contact_profile_ = nullptr; |
| 521 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 522 | UpdateIsReadyToPayAndNotifyObservers(); |
Jinho Bang | bb17815 | 2018-09-13 09:44:43 | [diff] [blame] | 523 | |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame] | 524 | if (IsPaymentAppInvoked() && delegate_) { |
Jinho Bang | 21d6be9 | 2018-12-18 15:30:31 | [diff] [blame] | 525 | delegate_->OnPayerInfoSelected( |
| 526 | response_helper_->GeneratePayerDetail(profile)); |
Jinho Bang | bb17815 | 2018-09-13 09:44:43 | [diff] [blame] | 527 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 528 | } |
| 529 | |
Danyao Wang | 46e7d94 | 2020-09-25 15:45:18 | [diff] [blame] | 530 | void PaymentRequestState::SetSelectedApp(PaymentApp* app) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 531 | selected_app_ = app; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 532 | UpdateIsReadyToPayAndNotifyObservers(); |
| 533 | } |
| 534 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 535 | const std::string& PaymentRequestState::GetApplicationLocale() { |
| 536 | return app_locale_; |
| 537 | } |
| 538 | |
| 539 | autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { |
| 540 | return personal_data_manager_; |
| 541 | } |
| 542 | |
mad | 763ed2b | 2017-04-24 20:28:47 | [diff] [blame] | 543 | autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { |
| 544 | return payment_request_delegate_->GetRegionDataLoader(); |
mad | 4527193 | 2017-04-13 16:07:38 | [diff] [blame] | 545 | } |
| 546 | |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 547 | bool PaymentRequestState::IsPaymentAppInvoked() const { |
| 548 | return !!response_helper_; |
| 549 | } |
| 550 | |
Mathieu Perreault | eea046d1 | 2017-09-28 15:51:21 | [diff] [blame] | 551 | autofill::AddressNormalizer* PaymentRequestState::GetAddressNormalizer() { |
Anthony Vallee-Dubois | 71bf34946 | 2017-07-20 23:56:45 | [diff] [blame] | 552 | return payment_request_delegate_->GetAddressNormalizer(); |
| 553 | } |
| 554 | |
Rouslan Solomakhin | 48fd6e87 | 2019-02-22 17:23:10 | [diff] [blame] | 555 | bool PaymentRequestState::IsInitialized() const { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 556 | return get_all_apps_finished_; |
Rouslan Solomakhin | 48fd6e87 | 2019-02-22 17:23:10 | [diff] [blame] | 557 | } |
| 558 | |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 559 | void PaymentRequestState::SelectDefaultShippingAddressAndNotifyObservers() { |
| 560 | // Only pre-select an address if the merchant provided at least one selected |
| 561 | // shipping option, and the top profile is complete. Assumes that profiles |
| 562 | // have already been sorted for completeness and frecency. |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 563 | if (!shipping_profiles().empty() && spec_ && |
| 564 | spec_->selected_shipping_option() && |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 565 | profile_comparator()->IsShippingComplete(shipping_profiles_[0])) { |
| 566 | selected_shipping_profile_ = shipping_profiles()[0]; |
| 567 | } |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 568 | // Record the missing required fields (if any) of the most complete shipping |
| 569 | // profile. |
| 570 | profile_comparator()->RecordMissingFieldsOfShippingProfile( |
| 571 | shipping_profiles().empty() ? nullptr : shipping_profiles()[0]); |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 572 | UpdateIsReadyToPayAndNotifyObservers(); |
| 573 | } |
| 574 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 575 | bool PaymentRequestState::ShouldShowShippingSection() const { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 576 | if (!spec_ || !spec_->request_shipping()) |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 577 | return false; |
| 578 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 579 | return selected_app_ ? !selected_app_->HandlesShippingAddress() : true; |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | bool PaymentRequestState::ShouldShowContactSection() const { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 583 | if (!spec_) |
| 584 | return false; |
| 585 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 586 | if (spec_->request_payer_name() && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 587 | (!selected_app_ || !selected_app_->HandlesPayerName())) { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 588 | return true; |
| 589 | } |
| 590 | if (spec_->request_payer_email() && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 591 | (!selected_app_ || !selected_app_->HandlesPayerEmail())) { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 592 | return true; |
| 593 | } |
| 594 | if (spec_->request_payer_phone() && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 595 | (!selected_app_ || !selected_app_->HandlesPayerPhone())) { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 596 | return true; |
| 597 | } |
| 598 | |
| 599 | return false; |
| 600 | } |
| 601 | |
Rouslan Solomakhin | 6555229 | 2019-08-29 17:28:21 | [diff] [blame] | 602 | base::WeakPtr<PaymentRequestState> PaymentRequestState::AsWeakPtr() { |
| 603 | return weak_ptr_factory_.GetWeakPtr(); |
| 604 | } |
| 605 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 606 | void PaymentRequestState::PopulateProfileCache() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 607 | std::vector<autofill::AutofillProfile*> profiles = |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 608 | personal_data_manager_->GetProfilesToSuggest(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 609 | |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 610 | std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering; |
| 611 | raw_profiles_for_filtering.reserve(profiles.size()); |
| 612 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 613 | // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 614 | // Thus, we store copies, and return a vector of pointers to these copies |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 615 | // whenever Profiles are requested. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 616 | for (size_t i = 0; i < profiles.size(); i++) { |
| 617 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 618 | std::make_unique<autofill::AutofillProfile>(*profiles[i])); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 619 | raw_profiles_for_filtering.push_back(profile_cache_.back().get()); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 620 | } |
| 621 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 622 | contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
| 623 | raw_profiles_for_filtering); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 624 | shipping_profiles_ = profile_comparator()->FilterProfilesForShipping( |
| 625 | raw_profiles_for_filtering); |
tmartino | a6eb22f | 2017-04-06 20:16:24 | [diff] [blame] | 626 | |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 627 | // Set the number of suggestions shown for the sections requested by the |
| 628 | // merchant. |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 629 | if (ShouldShowContactSection()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 630 | bool has_complete_contact = |
| 631 | contact_profiles_.empty() |
| 632 | ? false |
| 633 | : profile_comparator()->IsContactInfoComplete(contact_profiles_[0]); |
Rouslan Solomakhin | cb29c4f | 2019-08-16 22:49:39 | [diff] [blame] | 634 | is_requested_autofill_data_available_ &= has_complete_contact; |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 635 | journey_logger_->SetNumberOfSuggestionsShown( |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 636 | JourneyLogger::Section::SECTION_CONTACT_INFO, contact_profiles_.size(), |
| 637 | has_complete_contact); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 638 | } |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 639 | if (ShouldShowShippingSection()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 640 | bool has_complete_shipping = |
| 641 | shipping_profiles_.empty() |
| 642 | ? false |
| 643 | : profile_comparator()->IsShippingComplete(shipping_profiles_[0]); |
Rouslan Solomakhin | cb29c4f | 2019-08-16 22:49:39 | [diff] [blame] | 644 | is_requested_autofill_data_available_ &= has_complete_shipping; |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 645 | journey_logger_->SetNumberOfSuggestionsShown( |
| 646 | JourneyLogger::Section::SECTION_SHIPPING_ADDRESS, |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 647 | shipping_profiles_.size(), has_complete_shipping); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 648 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | void PaymentRequestState::SetDefaultProfileSelections() { |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 652 | // Contact profiles were ordered by completeness in addition to frecency; |
| 653 | // the first one is the best default selection. |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 654 | if (!contact_profiles().empty() && |
| 655 | profile_comparator()->IsContactInfoComplete(contact_profiles_[0])) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 656 | selected_contact_profile_ = contact_profiles()[0]; |
| 657 | |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 658 | // Record the missing required fields (if any) of the most complete contact |
| 659 | // profile. |
| 660 | profile_comparator()->RecordMissingFieldsOfContactProfile( |
| 661 | contact_profiles().empty() ? nullptr : contact_profiles()[0]); |
| 662 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 663 | // Sort apps. |
| 664 | PaymentApp::SortApps(&available_apps_); |
Sahel Sharify | 13331e8 | 2019-07-12 01:00:30 | [diff] [blame] | 665 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 666 | selected_app_ = nullptr; |
Sahel Sharify | 9030787 | 2019-12-02 17:28:14 | [diff] [blame] | 667 | if (!available_apps_.empty() && available_apps_[0]->CanPreselect()) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 668 | selected_app_ = available_apps_[0].get(); |
Sahel Sharify | 9be7b29 | 2020-03-13 17:37:46 | [diff] [blame] | 669 | UpdateIsReadyToPayAndNotifyObservers(); |
Sahel Sharify | 13331e8 | 2019-07-12 01:00:30 | [diff] [blame] | 670 | } |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 671 | |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 672 | // Record the missing required payment fields when no complete payment |
| 673 | // info exists. |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 674 | if (available_apps_.empty()) { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 675 | if (spec_ && spec_->supports_basic_card()) { |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 676 | // All fields are missing when basic-card is requested but no card exits. |
| 677 | base::UmaHistogramSparse("PaymentRequest.MissingPaymentFields", |
Sahel Sharify | 13331e8 | 2019-07-12 01:00:30 | [diff] [blame] | 678 | CREDIT_CARD_EXPIRED | CREDIT_CARD_NO_CARDHOLDER | |
| 679 | CREDIT_CARD_NO_NUMBER | |
| 680 | CREDIT_CARD_NO_BILLING_ADDRESS); |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 681 | } |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 682 | } else if (available_apps_[0]->type() == PaymentApp::Type::AUTOFILL) { |
| 683 | // Record the missing fields (if any) of the most complete app when |
| 684 | // it's autofill based. SW based apps are always complete. |
| 685 | static_cast<const AutofillPaymentApp*>(available_apps_[0].get()) |
| 686 | ->RecordMissingFieldsForApp(); |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 687 | } |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 688 | |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 689 | SelectDefaultShippingAddressAndNotifyObservers(); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 690 | |
| 691 | journey_logger_->SetNumberOfSuggestionsShown( |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 692 | JourneyLogger::Section::SECTION_PAYMENT_METHOD, available_apps().size(), |
| 693 | selected_app_); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { |
| 697 | is_ready_to_pay_ = |
| 698 | ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); |
| 699 | NotifyOnSelectedInformationChanged(); |
| 700 | } |
| 701 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 702 | void PaymentRequestState::NotifyOnGetAllPaymentAppsFinished() { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 703 | for (auto& observer : observers_) |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 704 | observer.OnGetAllPaymentAppsFinished(); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 705 | } |
| 706 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 707 | void PaymentRequestState::NotifyOnSelectedInformationChanged() { |
| 708 | for (auto& observer : observers_) |
| 709 | observer.OnSelectedInformationChanged(); |
| 710 | } |
| 711 | |
| 712 | bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 713 | // There is no need to check for supported networks, because only supported |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 714 | // apps are listed/created in the flow. |
| 715 | return selected_app_ != nullptr && selected_app_->IsCompleteForPayment(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
Rouslan Solomakhin | 75450a4 | 2020-09-24 18:44:22 | [diff] [blame] | 719 | if (is_waiting_for_merchant_validation_ || !spec_) |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 720 | return false; |
| 721 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 722 | if (ShouldShowShippingSection() && |
| 723 | (!spec_->selected_shipping_option() || |
| 724 | !profile_comparator()->IsShippingComplete(selected_shipping_profile_))) { |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 725 | return false; |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 726 | } |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 727 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 728 | if (ShouldShowContactSection() && |
| 729 | !profile_comparator()->IsContactInfoComplete(selected_contact_profile_)) { |
Anthony Vallee-Dubois | a881467 | 2017-06-01 18:43:20 | [diff] [blame] | 730 | return false; |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 731 | } |
Anthony Vallee-Dubois | a881467 | 2017-06-01 18:43:20 | [diff] [blame] | 732 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 733 | return true; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 734 | } |
| 735 | |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 736 | void PaymentRequestState::OnAddressNormalized( |
| 737 | bool success, |
| 738 | const autofill::AutofillProfile& normalized_profile) { |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame] | 739 | if (!delegate_) |
| 740 | return; |
| 741 | |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 742 | delegate_->OnShippingAddressSelected( |
Tommy Martino | 21d415f | 2018-01-12 20:08:22 | [diff] [blame] | 743 | data_util::GetPaymentAddressFromAutofillProfile(normalized_profile, |
| 744 | app_locale_)); |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 745 | } |
| 746 | |
Nick Burris | fca09083 | 2020-07-28 00:17:42 | [diff] [blame] | 747 | bool PaymentRequestState::IsInTwa() const { |
| 748 | return !payment_request_delegate_->GetTwaPackageName().empty(); |
| 749 | } |
| 750 | |
Rouslan Solomakhin | ebf9f16 | 2020-08-27 15:28:13 | [diff] [blame] | 751 | bool PaymentRequestState::GetCanMakePaymentValue() const { |
| 752 | return are_requested_methods_supported_ || |
| 753 | can_make_payment_even_without_apps_; |
| 754 | } |
| 755 | |
| 756 | bool PaymentRequestState::GetHasEnrolledInstrumentValue() const { |
| 757 | return has_enrolled_instrument_ || can_make_payment_even_without_apps_; |
| 758 | } |
| 759 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 760 | } // namespace payments |