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