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, |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 64 | 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), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 76 | spec_(spec), |
| 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 | 151bd31e | 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 | |
Danyao Wang | aed5b0a9 | 2020-04-01 22:43:14 | [diff] [blame] | 110 | PaymentRequestSpec* PaymentRequestState::GetSpec() const { |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 111 | return spec_; |
| 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 { |
| 137 | return GetSpec()->method_data(); |
| 138 | } |
| 139 | |
Rouslan Solomakhin | 914686c5 | 2020-08-21 15:15:41 | [diff] [blame] | 140 | std::unique_ptr<autofill::InternalAuthenticator> |
| 141 | PaymentRequestState::CreateInternalAuthenticator() const { |
| 142 | return GetPaymentRequestDelegate()->CreateInternalAuthenticator( |
| 143 | initiator_render_frame_host_); |
| 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) || |
| 163 | !spec_->supports_basic_card(); |
| 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_) |
| 230 | std::move(can_make_payment_callback_).Run(are_requested_methods_supported_); |
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 | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 234 | std::move(has_enrolled_instrument_callback_).Run(has_enrolled_instrument_); |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 235 | |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 236 | // Fulfill the pending AreRequestedMethodsSupported call. |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 237 | if (are_requested_methods_supported_callback_) |
| 238 | CheckRequestedMethodsSupported( |
| 239 | std::move(are_requested_methods_supported_callback_)); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 240 | } |
| 241 | |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 242 | void PaymentRequestState::OnPaymentResponseReady( |
| 243 | mojom::PaymentResponsePtr payment_response) { |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame^] | 244 | if (!delegate_) |
| 245 | return; |
| 246 | |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 247 | delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 248 | } |
| 249 | |
Rouslan Solomakhin | 68429b7 | 2019-06-27 15:12:39 | [diff] [blame] | 250 | void PaymentRequestState::OnPaymentResponseError( |
| 251 | const std::string& error_message) { |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame^] | 252 | if (!delegate_) |
| 253 | return; |
| 254 | |
Rouslan Solomakhin | 68429b7 | 2019-06-27 15:12:39 | [diff] [blame] | 255 | delegate_->OnPaymentResponseError(error_message); |
| 256 | } |
| 257 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 258 | void PaymentRequestState::OnSpecUpdated() { |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 259 | autofill::AutofillProfile* selected_shipping_profile = |
| 260 | selected_shipping_profile_; |
| 261 | autofill::AutofillProfile* selected_contact_profile = |
| 262 | selected_contact_profile_; |
| 263 | |
| 264 | if (spec_->current_update_reason() == |
| 265 | PaymentRequestSpec::UpdateReason::RETRY) { |
| 266 | if (spec_->has_shipping_address_error() && selected_shipping_profile) { |
| 267 | invalid_shipping_profile_ = selected_shipping_profile; |
| 268 | selected_shipping_profile_ = nullptr; |
| 269 | } |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 270 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 271 | if (spec_->has_payer_error() && selected_contact_profile) { |
| 272 | invalid_contact_profile_ = selected_contact_profile; |
| 273 | selected_contact_profile_ = nullptr; |
| 274 | } |
| 275 | } |
| 276 | |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 277 | if (spec_->selected_shipping_option_error().empty()) { |
| 278 | selected_shipping_option_error_profile_ = nullptr; |
| 279 | } else { |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 280 | selected_shipping_option_error_profile_ = selected_shipping_profile; |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 281 | selected_shipping_profile_ = nullptr; |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 282 | if (spec_->has_shipping_address_error() && selected_shipping_profile) { |
| 283 | invalid_shipping_profile_ = selected_shipping_profile; |
| 284 | } |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 285 | } |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 286 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 287 | is_waiting_for_merchant_validation_ = false; |
| 288 | UpdateIsReadyToPayAndNotifyObservers(); |
| 289 | } |
| 290 | |
Danyao Wang | 03a4cbd | 2019-08-15 23:47:11 | [diff] [blame] | 291 | void PaymentRequestState::CanMakePayment(StatusCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 292 | if (!get_all_apps_finished_) { |
Danyao Wang | 4bc0606a | 2018-12-27 16:54:53 | [diff] [blame] | 293 | DCHECK(!can_make_payment_callback_); |
| 294 | can_make_payment_callback_ = std::move(callback); |
| 295 | return; |
| 296 | } |
| 297 | |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 298 | PostStatusCallback(std::move(callback), are_requested_methods_supported_); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void PaymentRequestState::HasEnrolledInstrument(StatusCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 302 | if (!get_all_apps_finished_) { |
Danyao Wang | 4bc0606a | 2018-12-27 16:54:53 | [diff] [blame] | 303 | DCHECK(!has_enrolled_instrument_callback_); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 304 | has_enrolled_instrument_callback_ = std::move(callback); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 308 | PostStatusCallback(std::move(callback), has_enrolled_instrument_); |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 309 | } |
| 310 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 311 | void PaymentRequestState::AreRequestedMethodsSupported( |
Rouslan Solomakhin | d5dcc32 | 2019-07-11 21:47:20 | [diff] [blame] | 312 | MethodsSupportedCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 313 | if (!get_all_apps_finished_) { |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 314 | are_requested_methods_supported_callback_ = std::move(callback); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 319 | FROM_HERE, |
| 320 | base::BindOnce(&PaymentRequestState::CheckRequestedMethodsSupported, |
| 321 | weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 322 | } |
| 323 | |
Sahel Sharify | d341822 | 2020-02-19 21:50:02 | [diff] [blame] | 324 | void PaymentRequestState::OnAbort() { |
| 325 | // Reset supported method callback when the merchant calls abort before |
| 326 | // OnDoneCreatingPaymentApps(). |
| 327 | if (are_requested_methods_supported_callback_) |
| 328 | are_requested_methods_supported_callback_.Reset(); |
| 329 | } |
| 330 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 331 | void PaymentRequestState::CheckRequestedMethodsSupported( |
Rouslan Solomakhin | d5dcc32 | 2019-07-11 21:47:20 | [diff] [blame] | 332 | MethodsSupportedCallback callback) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 333 | DCHECK(get_all_apps_finished_); |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 334 | |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 335 | // Don't modify the value of |are_requested_methods_supported_|, because it's |
| 336 | // used for canMakePayment(). |
| 337 | bool supported = are_requested_methods_supported_; |
| 338 | if (supported && is_show_user_gesture_ && |
Rouslan Solomakhin | 85b10da | 2019-11-05 20:03:17 | [diff] [blame] | 339 | base::Contains(spec_->payment_method_identifiers_set(), |
| 340 | methods::kBasicCard) && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 341 | !has_non_autofill_app_ && !has_enrolled_instrument_ && |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 342 | PaymentsExperimentalFeatures::IsEnabled( |
| 343 | features::kStrictHasEnrolledAutofillInstrument)) { |
| 344 | supported = false; |
| 345 | get_all_payment_apps_error_ = errors::kStrictBasicCardShowReject; |
| 346 | } |
| 347 | |
Liquan (Max) Gu | a3db8c07 | 2020-06-22 16:24:21 | [diff] [blame] | 348 | if (!supported && get_all_payment_apps_error_.empty() && |
| 349 | base::Contains(spec_->payment_method_identifiers_set(), |
Rouslan Solomakhin | a1ff8150 | 2020-06-26 00:56:00 | [diff] [blame] | 350 | methods::kGooglePlayBilling) && |
Nick Burris | fca09083 | 2020-07-28 00:17:42 | [diff] [blame] | 351 | !IsInTwa()) { |
Liquan (Max) Gu | a3db8c07 | 2020-06-22 16:24:21 | [diff] [blame] | 352 | get_all_payment_apps_error_ = errors::kAppStoreMethodOnlySupportedInTwa; |
| 353 | } |
| 354 | |
Rouslan Solomakhin | 1dca2a92 | 2019-09-06 22:25:07 | [diff] [blame] | 355 | std::move(callback).Run(supported, get_all_payment_apps_error_); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 356 | } |
| 357 | |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 358 | std::string PaymentRequestState::GetAuthenticatedEmail() const { |
| 359 | return payment_request_delegate_->GetAuthenticatedEmail(); |
| 360 | } |
| 361 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 362 | void PaymentRequestState::AddObserver(Observer* observer) { |
| 363 | CHECK(observer); |
| 364 | observers_.AddObserver(observer); |
| 365 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 366 | |
| 367 | void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 368 | observers_.RemoveObserver(observer); |
| 369 | } |
| 370 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 371 | void PaymentRequestState::GeneratePaymentResponse() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 372 | DCHECK(is_ready_to_pay()); |
sebsg | 695799a | 2017-04-11 16:29:06 | [diff] [blame] | 373 | |
| 374 | // Once the response is ready, will call back into OnPaymentResponseReady. |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 375 | response_helper_ = std::make_unique<PaymentResponseHelper>( |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 376 | app_locale_, spec_, selected_app_, payment_request_delegate_, |
sebsg | 8a9c234 | 2017-04-21 17:05:15 | [diff] [blame] | 377 | selected_shipping_profile_, selected_contact_profile_, this); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 378 | } |
| 379 | |
Rouslan Solomakhin | 761d65eb | 2019-04-29 20:08:01 | [diff] [blame] | 380 | void PaymentRequestState::OnPaymentAppWindowClosed() { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 381 | DCHECK(selected_app_); |
Rouslan Solomakhin | 761d65eb | 2019-04-29 20:08:01 | [diff] [blame] | 382 | response_helper_.reset(); |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 383 | selected_app_->OnPaymentAppWindowClosed(); |
Rouslan Solomakhin | 761d65eb | 2019-04-29 20:08:01 | [diff] [blame] | 384 | } |
| 385 | |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 386 | void PaymentRequestState::RecordUseStats() { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 387 | if (ShouldShowShippingSection()) { |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 388 | DCHECK(selected_shipping_profile_); |
| 389 | personal_data_manager_->RecordUseOf(*selected_shipping_profile_); |
| 390 | } |
| 391 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 392 | if (ShouldShowContactSection()) { |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 393 | DCHECK(selected_contact_profile_); |
| 394 | |
| 395 | // If the same address was used for both contact and shipping, the stats |
| 396 | // should only be updated once. |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 397 | if (!ShouldShowShippingSection() || (selected_shipping_profile_->guid() != |
| 398 | selected_contact_profile_->guid())) { |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 399 | personal_data_manager_->RecordUseOf(*selected_contact_profile_); |
| 400 | } |
| 401 | } |
| 402 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 403 | selected_app_->RecordUse(); |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 404 | } |
| 405 | |
Sahel Sharify | cf4e213 | 2020-01-24 23:59:08 | [diff] [blame] | 406 | void PaymentRequestState::SetAvailablePaymentAppForRetry() { |
| 407 | DCHECK(selected_app_); |
| 408 | base::EraseIf(available_apps_, [this](const auto& payment_app) { |
| 409 | // Remove the app if it is not selected. |
| 410 | return payment_app.get() != selected_app_; |
| 411 | }); |
| 412 | is_retry_called_ = true; |
| 413 | } |
| 414 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 415 | void PaymentRequestState::AddAutofillPaymentApp( |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 416 | bool selected, |
| 417 | const autofill::CreditCard& card) { |
Rouslan Solomakhin | 6d4c1c27 | 2019-11-22 17:09:27 | [diff] [blame] | 418 | auto app = |
| 419 | AutofillPaymentAppFactory::ConvertCardToPaymentAppIfSupportedNetwork( |
| 420 | card, weak_ptr_factory_.GetWeakPtr()); |
| 421 | if (!app) |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 422 | return; |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 423 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 424 | available_apps_.push_back(std::move(app)); |
Danyao Wang | 25f72dc | 2019-10-18 05:11:32 | [diff] [blame] | 425 | journey_logger_->SetEventOccurred( |
| 426 | JourneyLogger::EVENT_AVAILABLE_METHOD_BASIC_CARD); |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 427 | |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 428 | if (selected) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 429 | SetSelectedApp(available_apps_.back().get(), |
| 430 | SectionSelectionStatus::kAddedSelected); |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 431 | } |
mathp | 24ce4cd | 2017-04-12 20:56:42 | [diff] [blame] | 432 | } |
| 433 | |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 434 | void PaymentRequestState::AddAutofillShippingProfile( |
| 435 | bool selected, |
| 436 | const autofill::AutofillProfile& profile) { |
| 437 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 438 | std::make_unique<autofill::AutofillProfile>(profile)); |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 439 | // TODO(tmartino): Implement deduplication rules specific to shipping |
| 440 | // profiles. |
| 441 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 442 | shipping_profiles_.push_back(new_cached_profile); |
| 443 | |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 444 | if (selected) { |
| 445 | SetSelectedShippingProfile(new_cached_profile, |
| 446 | SectionSelectionStatus::kAddedSelected); |
| 447 | } |
mad | f9904ea | 2017-04-25 18:39:12 | [diff] [blame] | 448 | } |
| 449 | |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 450 | void PaymentRequestState::AddAutofillContactProfile( |
| 451 | bool selected, |
| 452 | const autofill::AutofillProfile& profile) { |
| 453 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 454 | std::make_unique<autofill::AutofillProfile>(profile)); |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 455 | autofill::AutofillProfile* new_cached_profile = profile_cache_.back().get(); |
| 456 | contact_profiles_.push_back(new_cached_profile); |
| 457 | |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 458 | if (selected) { |
| 459 | SetSelectedContactProfile(new_cached_profile, |
| 460 | SectionSelectionStatus::kAddedSelected); |
| 461 | } |
Anthony Vallee-Dubois | f1e88b4 | 2017-05-15 15:09:23 | [diff] [blame] | 462 | } |
| 463 | |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 464 | void PaymentRequestState::SetSelectedShippingOption( |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 465 | const std::string& shipping_option_id) { |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 466 | spec_->StartWaitingForUpdateWith( |
| 467 | PaymentRequestSpec::UpdateReason::SHIPPING_OPTION); |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame^] | 468 | if (delegate_) { |
| 469 | // This will inform the merchant and will lead to them calling updateWith |
| 470 | // with new PaymentDetails. |
| 471 | delegate_->OnShippingOptionIdSelected(shipping_option_id); |
| 472 | } |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 473 | } |
| 474 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 475 | void PaymentRequestState::SetSelectedShippingProfile( |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 476 | autofill::AutofillProfile* profile, |
| 477 | SectionSelectionStatus selection_status) { |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 478 | spec_->StartWaitingForUpdateWith( |
| 479 | PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 480 | selected_shipping_profile_ = profile; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 481 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 482 | // Changing the shipping address clears shipping address validation errors |
| 483 | // from retry(). |
| 484 | invalid_shipping_profile_ = nullptr; |
| 485 | |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 486 | // The user should not be able to click on pay until the callback from the |
| 487 | // merchant. |
| 488 | is_waiting_for_merchant_validation_ = true; |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 489 | |
| 490 | // Start the normalization of the shipping address. |
Mathieu Perreault | c94700f11 | 2017-10-23 20:28:39 | [diff] [blame] | 491 | payment_request_delegate_->GetAddressNormalizer()->NormalizeAddressAsync( |
Mathieu Perreault | 9b40d510 | 2017-11-21 20:51:29 | [diff] [blame] | 492 | *selected_shipping_profile_, /*timeout_seconds=*/2, |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 493 | base::BindOnce(&PaymentRequestState::OnAddressNormalized, |
| 494 | weak_ptr_factory_.GetWeakPtr())); |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 495 | IncrementSelectionStatus(JourneyLogger::Section::SECTION_SHIPPING_ADDRESS, |
| 496 | selection_status); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void PaymentRequestState::SetSelectedContactProfile( |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 500 | autofill::AutofillProfile* profile, |
| 501 | SectionSelectionStatus selection_status) { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 502 | selected_contact_profile_ = profile; |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 503 | |
| 504 | // Changing the contact information clears contact information validation |
| 505 | // errors from retry(). |
| 506 | invalid_contact_profile_ = nullptr; |
| 507 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 508 | UpdateIsReadyToPayAndNotifyObservers(); |
Jinho Bang | bb17815 | 2018-09-13 09:44:43 | [diff] [blame] | 509 | |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame^] | 510 | if (IsPaymentAppInvoked() && delegate_) { |
Jinho Bang | 21d6be9 | 2018-12-18 15:30:31 | [diff] [blame] | 511 | delegate_->OnPayerInfoSelected( |
| 512 | response_helper_->GeneratePayerDetail(profile)); |
Jinho Bang | bb17815 | 2018-09-13 09:44:43 | [diff] [blame] | 513 | } |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 514 | IncrementSelectionStatus(JourneyLogger::Section::SECTION_CONTACT_INFO, |
| 515 | selection_status); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 516 | } |
| 517 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 518 | void PaymentRequestState::SetSelectedApp( |
| 519 | PaymentApp* app, |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 520 | SectionSelectionStatus selection_status) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 521 | selected_app_ = app; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 522 | UpdateIsReadyToPayAndNotifyObservers(); |
Sahel Sharify | bc4173f | 2019-05-09 21:03:57 | [diff] [blame] | 523 | IncrementSelectionStatus(JourneyLogger::Section::SECTION_PAYMENT_METHOD, |
| 524 | selection_status); |
| 525 | } |
| 526 | |
| 527 | void PaymentRequestState::IncrementSelectionStatus( |
| 528 | JourneyLogger::Section section, |
| 529 | SectionSelectionStatus selection_status) { |
| 530 | switch (selection_status) { |
| 531 | case SectionSelectionStatus::kSelected: |
| 532 | journey_logger_->IncrementSelectionChanges(section); |
| 533 | break; |
| 534 | case SectionSelectionStatus::kEditedSelected: |
| 535 | journey_logger_->IncrementSelectionEdits(section); |
| 536 | break; |
| 537 | case SectionSelectionStatus::kAddedSelected: |
| 538 | journey_logger_->IncrementSelectionAdds(section); |
| 539 | break; |
| 540 | default: |
| 541 | NOTREACHED(); |
| 542 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 543 | } |
| 544 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 545 | const std::string& PaymentRequestState::GetApplicationLocale() { |
| 546 | return app_locale_; |
| 547 | } |
| 548 | |
| 549 | autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { |
| 550 | return personal_data_manager_; |
| 551 | } |
| 552 | |
mad | 763ed2b | 2017-04-24 20:28:47 | [diff] [blame] | 553 | autofill::RegionDataLoader* PaymentRequestState::GetRegionDataLoader() { |
| 554 | return payment_request_delegate_->GetRegionDataLoader(); |
mad | 4527193 | 2017-04-13 16:07:38 | [diff] [blame] | 555 | } |
| 556 | |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 557 | bool PaymentRequestState::IsPaymentAppInvoked() const { |
| 558 | return !!response_helper_; |
| 559 | } |
| 560 | |
Mathieu Perreault | eea046d1 | 2017-09-28 15:51:21 | [diff] [blame] | 561 | autofill::AddressNormalizer* PaymentRequestState::GetAddressNormalizer() { |
Anthony Vallee-Dubois | 71bf34946 | 2017-07-20 23:56:45 | [diff] [blame] | 562 | return payment_request_delegate_->GetAddressNormalizer(); |
| 563 | } |
| 564 | |
Rouslan Solomakhin | 48fd6e87 | 2019-02-22 17:23:10 | [diff] [blame] | 565 | bool PaymentRequestState::IsInitialized() const { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 566 | return get_all_apps_finished_; |
Rouslan Solomakhin | 48fd6e87 | 2019-02-22 17:23:10 | [diff] [blame] | 567 | } |
| 568 | |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 569 | void PaymentRequestState::SelectDefaultShippingAddressAndNotifyObservers() { |
| 570 | // Only pre-select an address if the merchant provided at least one selected |
| 571 | // shipping option, and the top profile is complete. Assumes that profiles |
| 572 | // have already been sorted for completeness and frecency. |
| 573 | if (!shipping_profiles().empty() && spec_->selected_shipping_option() && |
| 574 | profile_comparator()->IsShippingComplete(shipping_profiles_[0])) { |
| 575 | selected_shipping_profile_ = shipping_profiles()[0]; |
| 576 | } |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 577 | // Record the missing required fields (if any) of the most complete shipping |
| 578 | // profile. |
| 579 | profile_comparator()->RecordMissingFieldsOfShippingProfile( |
| 580 | shipping_profiles().empty() ? nullptr : shipping_profiles()[0]); |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 581 | UpdateIsReadyToPayAndNotifyObservers(); |
| 582 | } |
| 583 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 584 | bool PaymentRequestState::ShouldShowShippingSection() const { |
| 585 | if (!spec_->request_shipping()) |
| 586 | return false; |
| 587 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 588 | return selected_app_ ? !selected_app_->HandlesShippingAddress() : true; |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | bool PaymentRequestState::ShouldShowContactSection() const { |
| 592 | if (spec_->request_payer_name() && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 593 | (!selected_app_ || !selected_app_->HandlesPayerName())) { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 594 | return true; |
| 595 | } |
| 596 | if (spec_->request_payer_email() && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 597 | (!selected_app_ || !selected_app_->HandlesPayerEmail())) { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | if (spec_->request_payer_phone() && |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 601 | (!selected_app_ || !selected_app_->HandlesPayerPhone())) { |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 602 | return true; |
| 603 | } |
| 604 | |
| 605 | return false; |
| 606 | } |
| 607 | |
Rouslan Solomakhin | 6555229 | 2019-08-29 17:28:21 | [diff] [blame] | 608 | base::WeakPtr<PaymentRequestState> PaymentRequestState::AsWeakPtr() { |
| 609 | return weak_ptr_factory_.GetWeakPtr(); |
| 610 | } |
| 611 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 612 | void PaymentRequestState::PopulateProfileCache() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 613 | std::vector<autofill::AutofillProfile*> profiles = |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 614 | personal_data_manager_->GetProfilesToSuggest(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 615 | |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 616 | std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering; |
| 617 | raw_profiles_for_filtering.reserve(profiles.size()); |
| 618 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 619 | // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 620 | // Thus, we store copies, and return a vector of pointers to these copies |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 621 | // whenever Profiles are requested. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 622 | for (size_t i = 0; i < profiles.size(); i++) { |
| 623 | profile_cache_.push_back( |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 624 | std::make_unique<autofill::AutofillProfile>(*profiles[i])); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 625 | raw_profiles_for_filtering.push_back(profile_cache_.back().get()); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 626 | } |
| 627 | |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 628 | contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
| 629 | raw_profiles_for_filtering); |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 630 | shipping_profiles_ = profile_comparator()->FilterProfilesForShipping( |
| 631 | raw_profiles_for_filtering); |
tmartino | a6eb22f | 2017-04-06 20:16:24 | [diff] [blame] | 632 | |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 633 | // Set the number of suggestions shown for the sections requested by the |
| 634 | // merchant. |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 635 | if (ShouldShowContactSection()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 636 | bool has_complete_contact = |
| 637 | contact_profiles_.empty() |
| 638 | ? false |
| 639 | : profile_comparator()->IsContactInfoComplete(contact_profiles_[0]); |
Rouslan Solomakhin | cb29c4f | 2019-08-16 22:49:39 | [diff] [blame] | 640 | is_requested_autofill_data_available_ &= has_complete_contact; |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 641 | journey_logger_->SetNumberOfSuggestionsShown( |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 642 | JourneyLogger::Section::SECTION_CONTACT_INFO, contact_profiles_.size(), |
| 643 | has_complete_contact); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 644 | } |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 645 | if (ShouldShowShippingSection()) { |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 646 | bool has_complete_shipping = |
| 647 | shipping_profiles_.empty() |
| 648 | ? false |
| 649 | : profile_comparator()->IsShippingComplete(shipping_profiles_[0]); |
Rouslan Solomakhin | cb29c4f | 2019-08-16 22:49:39 | [diff] [blame] | 650 | is_requested_autofill_data_available_ &= has_complete_shipping; |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 651 | journey_logger_->SetNumberOfSuggestionsShown( |
| 652 | JourneyLogger::Section::SECTION_SHIPPING_ADDRESS, |
sebsg | ce700484d | 2017-07-19 23:25:38 | [diff] [blame] | 653 | shipping_profiles_.size(), has_complete_shipping); |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 654 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void PaymentRequestState::SetDefaultProfileSelections() { |
mathp | f14c1e3 | 2017-05-12 16:37:30 | [diff] [blame] | 658 | // Contact profiles were ordered by completeness in addition to frecency; |
| 659 | // the first one is the best default selection. |
tmartino | cd131b3 | 2017-05-24 19:40:59 | [diff] [blame] | 660 | if (!contact_profiles().empty() && |
| 661 | profile_comparator()->IsContactInfoComplete(contact_profiles_[0])) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 662 | selected_contact_profile_ = contact_profiles()[0]; |
| 663 | |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 664 | // Record the missing required fields (if any) of the most complete contact |
| 665 | // profile. |
| 666 | profile_comparator()->RecordMissingFieldsOfContactProfile( |
| 667 | contact_profiles().empty() ? nullptr : contact_profiles()[0]); |
| 668 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 669 | // Sort apps. |
| 670 | PaymentApp::SortApps(&available_apps_); |
Sahel Sharify | 13331e8 | 2019-07-12 01:00:30 | [diff] [blame] | 671 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 672 | selected_app_ = nullptr; |
Sahel Sharify | 9030787 | 2019-12-02 17:28:14 | [diff] [blame] | 673 | if (!available_apps_.empty() && available_apps_[0]->CanPreselect()) { |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 674 | selected_app_ = available_apps_[0].get(); |
Sahel Sharify | 9be7b29 | 2020-03-13 17:37:46 | [diff] [blame] | 675 | UpdateIsReadyToPayAndNotifyObservers(); |
Sahel Sharify | 13331e8 | 2019-07-12 01:00:30 | [diff] [blame] | 676 | } |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 677 | |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 678 | // Record the missing required payment fields when no complete payment |
| 679 | // info exists. |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 680 | if (available_apps_.empty()) { |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 681 | if (spec_->supports_basic_card()) { |
| 682 | // All fields are missing when basic-card is requested but no card exits. |
| 683 | base::UmaHistogramSparse("PaymentRequest.MissingPaymentFields", |
Sahel Sharify | 13331e8 | 2019-07-12 01:00:30 | [diff] [blame] | 684 | CREDIT_CARD_EXPIRED | CREDIT_CARD_NO_CARDHOLDER | |
| 685 | CREDIT_CARD_NO_NUMBER | |
| 686 | CREDIT_CARD_NO_BILLING_ADDRESS); |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 687 | } |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 688 | } else if (available_apps_[0]->type() == PaymentApp::Type::AUTOFILL) { |
| 689 | // Record the missing fields (if any) of the most complete app when |
| 690 | // it's autofill based. SW based apps are always complete. |
| 691 | static_cast<const AutofillPaymentApp*>(available_apps_[0].get()) |
| 692 | ->RecordMissingFieldsForApp(); |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 693 | } |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 694 | |
Sahel Sharify | 6efdfb3 | 2019-07-02 16:12:16 | [diff] [blame] | 695 | SelectDefaultShippingAddressAndNotifyObservers(); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 696 | |
| 697 | journey_logger_->SetNumberOfSuggestionsShown( |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 698 | JourneyLogger::Section::SECTION_PAYMENT_METHOD, available_apps().size(), |
| 699 | selected_app_); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { |
| 703 | is_ready_to_pay_ = |
| 704 | ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); |
| 705 | NotifyOnSelectedInformationChanged(); |
| 706 | } |
| 707 | |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 708 | void PaymentRequestState::NotifyOnGetAllPaymentAppsFinished() { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 709 | for (auto& observer : observers_) |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 710 | observer.OnGetAllPaymentAppsFinished(); |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 711 | } |
| 712 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 713 | void PaymentRequestState::NotifyOnSelectedInformationChanged() { |
| 714 | for (auto& observer : observers_) |
| 715 | observer.OnSelectedInformationChanged(); |
| 716 | } |
| 717 | |
| 718 | bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 719 | // There is no need to check for supported networks, because only supported |
Rouslan Solomakhin | 5d15cb1f | 2019-11-11 18:11:39 | [diff] [blame] | 720 | // apps are listed/created in the flow. |
| 721 | return selected_app_ != nullptr && selected_app_->IsCompleteForPayment(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
sebsg | 7e61913 | 2017-04-27 16:07:10 | [diff] [blame] | 725 | if (is_waiting_for_merchant_validation_) |
| 726 | return false; |
| 727 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 728 | if (ShouldShowShippingSection() && |
| 729 | (!spec_->selected_shipping_option() || |
| 730 | !profile_comparator()->IsShippingComplete(selected_shipping_profile_))) { |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 731 | return false; |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 732 | } |
tmartino | 01c3eb8 | 2017-04-29 15:18:39 | [diff] [blame] | 733 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 734 | if (ShouldShowContactSection() && |
| 735 | !profile_comparator()->IsContactInfoComplete(selected_contact_profile_)) { |
Anthony Vallee-Dubois | a881467 | 2017-06-01 18:43:20 | [diff] [blame] | 736 | return false; |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 737 | } |
Anthony Vallee-Dubois | a881467 | 2017-06-01 18:43:20 | [diff] [blame] | 738 | |
Sahel Sharify | fef3d92 | 2019-09-11 01:41:22 | [diff] [blame] | 739 | return true; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 740 | } |
| 741 | |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 742 | void PaymentRequestState::OnAddressNormalized( |
| 743 | bool success, |
| 744 | const autofill::AutofillProfile& normalized_profile) { |
Kyungsun Lee | cdbdade | 2020-08-25 18:06:12 | [diff] [blame^] | 745 | if (!delegate_) |
| 746 | return; |
| 747 | |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 748 | delegate_->OnShippingAddressSelected( |
Tommy Martino | 21d415f | 2018-01-12 20:08:22 | [diff] [blame] | 749 | data_util::GetPaymentAddressFromAutofillProfile(normalized_profile, |
| 750 | app_locale_)); |
Mathieu Perreault | 17c1ae9 | 2017-10-20 17:16:00 | [diff] [blame] | 751 | } |
| 752 | |
Nick Burris | fca09083 | 2020-07-28 00:17:42 | [diff] [blame] | 753 | bool PaymentRequestState::IsInTwa() const { |
| 754 | return !payment_request_delegate_->GetTwaPackageName().empty(); |
| 755 | } |
| 756 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 757 | } // namespace payments |