blob: b410cc720c7d2aae6b3eeeb35d681c80f1aed274 [file] [log] [blame]
mathpf709499d2017-01-09 20:48:361// 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
rouslan908248c2017-02-27 21:30:245#include "components/payments/content/payment_request.h"
6
anthonyvdd23ed702017-04-05 15:29:007#include <string>
rouslan908248c2017-02-27 21:30:248#include <utility>
mathpf709499d2017-01-09 20:48:369
Sebastien Marchand53801a32019-01-25 16:26:1110#include "base/bind.h"
Rouslan Solomakhin1804ee42017-10-03 14:27:4311#include "base/feature_list.h"
Mathieu Perreault627b97c2017-08-12 00:44:2212#include "base/stl_util.h"
rouslan690997682017-05-09 18:07:3913#include "components/payments/content/can_make_payment_query_factory.h"
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5114#include "components/payments/content/content_payment_request_delegate.h"
rouslan6e3cf7c62017-04-17 21:23:2815#include "components/payments/content/origin_security_checker.h"
Mohamad Ahmadif5544bb2017-09-01 21:48:2216#include "components/payments/content/payment_request_converter.h"
rouslan908248c2017-02-27 21:30:2417#include "components/payments/content/payment_request_web_contents_manager.h"
rouslan690997682017-05-09 18:07:3918#include "components/payments/core/can_make_payment_query.h"
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:3619#include "components/payments/core/features.h"
Mohamad Ahmadif5544bb2017-09-01 21:48:2220#include "components/payments/core/payment_details.h"
21#include "components/payments/core/payment_details_validation.h"
Mathieu Perreault23d25bfb82018-05-11 14:45:3722#include "components/payments/core/payment_instrument.h"
anthonyvd6a43b932017-05-11 18:39:2723#include "components/payments/core/payment_prefs.h"
Jinho Bangbe463a22018-08-02 10:26:5024#include "components/payments/core/payments_validators.h"
anthonyvd6a43b932017-05-11 18:39:2725#include "components/prefs/pref_service.h"
Steven Holte2083e8bc2018-07-16 23:50:3626#include "components/ukm/content/source_url_recorder.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3927#include "components/url_formatter/elide_url.h"
mathpf709499d2017-01-09 20:48:3628#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3929#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3630#include "content/public/browser/web_contents.h"
Rouslan Solomakhin1804ee42017-10-03 14:27:4331#include "content/public/common/content_features.h"
mathpf709499d2017-01-09 20:48:3632
Danyao Wangce175bf2018-12-21 22:35:5833namespace {
34
35using ::payments::mojom::HasEnrolledInstrumentQueryResult;
36
37} // namespace
38
mathpf709499d2017-01-09 20:48:3639namespace payments {
40
41PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3942 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3643 content::WebContents* web_contents,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5144 std::unique_ptr<ContentPaymentRequestDelegate> delegate,
mathpf709499d2017-01-09 20:48:3645 PaymentRequestWebContentsManager* manager,
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0746 PaymentRequestDisplayManager* display_manager,
rouslan6e3cf7c62017-04-17 21:23:2847 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3748 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3649 : web_contents_(web_contents),
Rouslan Solomakhin27064702018-12-14 21:15:3350 log_(web_contents_),
mathpf709499d2017-01-09 20:48:3651 delegate_(std::move(delegate)),
52 manager_(manager),
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0753 display_manager_(display_manager),
54 display_handle_(nullptr),
mathp300fa542017-03-27 19:29:3755 binding_(this, std::move(request)),
Rouslan Solomakhin6e979ab2017-08-30 17:30:3956 top_level_origin_(url_formatter::FormatUrlForSecurityDisplay(
57 web_contents_->GetLastCommittedURL())),
58 frame_origin_(url_formatter::FormatUrlForSecurityDisplay(
59 render_frame_host->GetLastCommittedURL())),
sebsg20b49d7b2017-05-04 20:23:1760 observer_for_testing_(observer_for_testing),
61 journey_logger_(delegate_->IsIncognito(),
Steven Holte2083e8bc2018-07-16 23:50:3662 ukm::GetSourceIdForWebContentsDocument(web_contents)),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1363 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5064 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
65 // will happen as a result of many renderer-side events (both successful and
66 // erroneous in nature).
67 // TODO(crbug.com/683636): Investigate using
68 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
tzik2bcf8e42018-07-31 11:22:1569 binding_.set_connection_error_handler(base::BindOnce(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1370 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3671}
72
73PaymentRequest::~PaymentRequest() {}
74
rouslan6e3cf7c62017-04-17 21:23:2875void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
76 std::vector<mojom::PaymentMethodDataPtr> method_data,
77 mojom::PaymentDetailsPtr details,
78 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3679 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Rouslan Solomakhin27064702018-12-14 21:15:3380
81 if (is_initialized_) {
82 log_.Error("Attempted initialization twice");
83 OnConnectionTerminated();
84 return;
85 }
86
87 is_initialized_ = true;
rouslan6e3cf7c62017-04-17 21:23:2888 client_ = std::move(client);
89
rouslanb28f4532017-05-08 15:41:4790 const GURL last_committed_url = delegate_->GetLastCommittedURL();
91 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
Rouslan Solomakhin27064702018-12-14 21:15:3392 log_.Error("Not in a secure origin");
rouslan6e3cf7c62017-04-17 21:23:2893 OnConnectionTerminated();
94 return;
95 }
96
rouslanb28f4532017-05-08 15:41:4797 bool allowed_origin =
98 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
99 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
100 if (!allowed_origin) {
Rouslan Solomakhin27064702018-12-14 21:15:33101 log_.Error(
102 "Only localhost, file://, and cryptographic scheme origins allowed");
rouslanb28f4532017-05-08 15:41:47103 }
104
105 bool invalid_ssl =
106 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
107 !delegate_->IsSslCertificateValid();
Rouslan Solomakhin27064702018-12-14 21:15:33108 if (invalid_ssl) {
109 log_.Error("SSL certificate is not valid.");
110 }
rouslanb28f4532017-05-08 15:41:47111
112 if (!allowed_origin || invalid_ssl) {
Rouslan Solomakhin27064702018-12-14 21:15:33113 // Intentionally don't set |spec_| and |state_|, so the UI is never shown.
114 log_.Error(
115 "No UI will be shown. CanMakePayment will always return false. "
116 "Show will be rejected with NotSupportedError.");
rouslan6e3cf7c62017-04-17 21:23:28117 return;
118 }
119
mathpf709499d2017-01-09 20:48:36120 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22121 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
Rouslan Solomakhin27064702018-12-14 21:15:33122 log_.Error(error);
mathpf4bc50e2017-01-24 05:17:50123 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36124 return;
125 }
rouslan6e3cf7c62017-04-17 21:23:28126
jinho.bangfcb5ec92017-03-29 08:08:02127 if (!details->total) {
Rouslan Solomakhin27064702018-12-14 21:15:33128 log_.Error("Missing total");
jinho.bangfcb5ec92017-03-29 08:08:02129 OnConnectionTerminated();
130 return;
131 }
rouslan6e3cf7c62017-04-17 21:23:28132
sebsga70a6da2017-12-21 22:27:02133 spec_ = std::make_unique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33134 std::move(options), std::move(details), std::move(method_data), this,
135 delegate_->GetApplicationLocale());
sebsga70a6da2017-12-21 22:27:02136 state_ = std::make_unique<PaymentRequestState>(
Rouslan Solomakhindbf593d92017-11-21 19:20:57137 web_contents_, top_level_origin_, frame_origin_, spec_.get(), this,
138 delegate_->GetApplicationLocale(), delegate_->GetPersonalDataManager(),
139 delegate_.get(), &journey_logger_);
Mathieu Perreault627b97c2017-08-12 00:44:22140
141 journey_logger_.SetRequestedInformation(
142 spec_->request_shipping(), spec_->request_payer_email(),
143 spec_->request_payer_phone(), spec_->request_payer_name());
144
145 // Log metrics around which payment methods are requested by the merchant.
146 GURL google_pay_url(kGooglePayMethodName);
147 GURL android_pay_url(kAndroidPayMethodName);
148 // Looking for payment methods that are NOT google-related payment methods.
149 auto non_google_it =
150 std::find_if(spec_->url_payment_method_identifiers().begin(),
151 spec_->url_payment_method_identifiers().end(),
152 [google_pay_url, android_pay_url](const GURL& url) {
153 return url != google_pay_url && url != android_pay_url;
154 });
155 journey_logger_.SetRequestedPaymentMethodTypes(
156 /*requested_basic_card=*/!spec_->supported_card_networks().empty(),
157 /*requested_method_google=*/
158 base::ContainsValue(spec_->url_payment_method_identifiers(),
159 google_pay_url) ||
160 base::ContainsValue(spec_->url_payment_method_identifiers(),
161 android_pay_url),
162 /*requested_method_other=*/non_google_it !=
163 spec_->url_payment_method_identifiers().end());
mathpf709499d2017-01-09 20:48:36164}
165
Rouslan Solomakhin833f8512018-04-03 23:19:25166void PaymentRequest::Show(bool is_user_gesture) {
Rouslan Solomakhin27064702018-12-14 21:15:33167 if (!IsInitialized()) {
168 log_.Error("Attempted show without initialization");
mathpf4bc50e2017-01-24 05:17:50169 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10170 return;
171 }
rouslan6e3cf7c62017-04-17 21:23:28172
Rouslan Solomakhin27064702018-12-14 21:15:33173 if (is_show_called_) {
174 log_.Error("Attempted show twice");
175 OnConnectionTerminated();
176 return;
177 }
178
179 is_show_called_ = true;
180
rouslan7d433cc22017-05-08 15:18:07181 // A tab can display only one PaymentRequest UI at a time.
Anthony Vallee-Dubois8f5e7e12018-01-12 16:14:06182 display_handle_ = display_manager_->TryShow(delegate_.get());
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07183 if (!display_handle_) {
Rouslan Solomakhin27064702018-12-14 21:15:33184 log_.Error("A PaymentRequest UI is already showing");
sebsg828269bc2017-06-09 19:11:12185 journey_logger_.SetNotShown(
186 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
Rouslan Solomakhinb2d233c42018-08-30 16:18:40187 client_->OnError(mojom::PaymentErrorReason::ALREADY_SHOWING);
rouslan7d433cc22017-05-08 15:18:07188 OnConnectionTerminated();
189 return;
190 }
191
Rouslan Solomakhin5b510432017-09-26 16:59:32192 if (!delegate_->IsBrowserWindowActive()) {
Rouslan Solomakhin27064702018-12-14 21:15:33193 log_.Error("Cannot show PaymentRequest UI in a background tab");
Rouslan Solomakhin5b510432017-09-26 16:59:32194 journey_logger_.SetNotShown(JourneyLogger::NOT_SHOWN_REASON_OTHER);
195 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
196 OnConnectionTerminated();
197 return;
198 }
199
Rouslan Solomakhind2cae95a2018-08-09 00:16:10200 if (!state_) {
Rouslan Solomakhin27064702018-12-14 21:15:33201 // SSL is not valid. Reject show with NotSupportedError, disconnect the
202 // mojo pipe, and destroy this object.
Rouslan Solomakhind2cae95a2018-08-09 00:16:10203 AreRequestedMethodsSupportedCallback(false);
204 return;
205 }
206
Rouslan Solomakhin833f8512018-04-03 23:19:25207 is_show_user_gesture_ = is_user_gesture;
208
Rouslan Solomakhind2cae95a2018-08-09 00:16:10209 display_handle_->Show(this);
210
gogerald0a7ee6c2017-11-13 18:23:19211 state_->AreRequestedMethodsSupported(
212 base::BindOnce(&PaymentRequest::AreRequestedMethodsSupportedCallback,
213 weak_ptr_factory_.GetWeakPtr()));
214}
215
Jinho Bangbe463a22018-08-02 10:26:50216void PaymentRequest::Retry(mojom::PaymentValidationErrorsPtr errors) {
Rouslan Solomakhin27064702018-12-14 21:15:33217 if (!IsInitialized()) {
218 log_.Error("Attempted retry without initialization");
Jinho Bangcac8d9a02018-08-23 19:47:22219 OnConnectionTerminated();
220 return;
221 }
222
Rouslan Solomakhin27064702018-12-14 21:15:33223 if (!IsThisPaymentRequestShowing()) {
224 log_.Error("Attempted retry without show");
Jinho Bangcac8d9a02018-08-23 19:47:22225 OnConnectionTerminated();
226 return;
227 }
228
Jinho Bangbe463a22018-08-02 10:26:50229 std::string error;
230 if (!PaymentsValidators::IsValidPaymentValidationErrorsFormat(errors,
231 &error)) {
Rouslan Solomakhin27064702018-12-14 21:15:33232 log_.Error(error);
Jinho Bangbe463a22018-08-02 10:26:50233 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
234 OnConnectionTerminated();
235 return;
236 }
237
Jinho Bang092e7162018-09-06 23:41:19238 spec()->Retry(std::move(errors));
Jinho Bangcac8d9a02018-08-23 19:47:22239 display_handle_->Retry();
Jinho Bangbe463a22018-08-02 10:26:50240}
241
mathp151bd312017-04-03 21:07:24242void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
Rouslan Solomakhin27064702018-12-14 21:15:33243 if (!IsInitialized()) {
244 log_.Error("Attempted updateWith without initialization");
245 OnConnectionTerminated();
246 return;
247 }
248
249 if (!IsThisPaymentRequestShowing()) {
250 log_.Error("Attempted updateWith without show");
251 OnConnectionTerminated();
252 return;
253 }
254
mathp151bd312017-04-03 21:07:24255 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22256 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
Rouslan Solomakhin27064702018-12-14 21:15:33257 log_.Error(error);
mathp151bd312017-04-03 21:07:24258 OnConnectionTerminated();
259 return;
260 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39261
Jinho Bang092e7162018-09-06 23:41:19262 if (details->shipping_address_errors &&
263 !PaymentsValidators::IsValidAddressErrorsFormat(
264 details->shipping_address_errors, &error)) {
Rouslan Solomakhin27064702018-12-14 21:15:33265 log_.Error(error);
Jinho Bang092e7162018-09-06 23:41:19266 OnConnectionTerminated();
267 return;
268 }
269
Rouslan Solomakhin4cbda822017-08-23 18:50:39270 if (!details->total) {
Rouslan Solomakhin27064702018-12-14 21:15:33271 log_.Error("Missing total");
Rouslan Solomakhin4cbda822017-08-23 18:50:39272 OnConnectionTerminated();
273 return;
274 }
275
mathp151bd312017-04-03 21:07:24276 spec_->UpdateWith(std::move(details));
277}
278
Rouslan Solomakhina9ff9282017-10-31 21:58:05279void PaymentRequest::NoUpdatedPaymentDetails() {
Rouslan Solomakhin27064702018-12-14 21:15:33280 // This Mojo call is triggered by the user of the API doing nothing in
281 // response to a shipping address update event, so the error messages cannot
282 // be more verbose.
283 if (!IsInitialized()) {
284 log_.Error("Not initialized");
285 OnConnectionTerminated();
286 return;
287 }
288
289 if (!IsThisPaymentRequestShowing()) {
290 log_.Error("Not shown");
291 OnConnectionTerminated();
292 return;
293 }
294
Rouslan Solomakhina9ff9282017-10-31 21:58:05295 spec_->RecomputeSpecForDetails();
296}
297
mathpf4bc50e2017-01-24 05:17:50298void PaymentRequest::Abort() {
Rouslan Solomakhin27064702018-12-14 21:15:33299 if (!IsInitialized()) {
300 log_.Error("Attempted abort without initialization");
301 OnConnectionTerminated();
302 return;
303 }
304
305 if (!IsThisPaymentRequestShowing()) {
306 log_.Error("Attempted abort without show");
307 OnConnectionTerminated();
308 return;
309 }
310
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56311 // The API user has decided to abort. If a successful abort message is
312 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50313 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56314 // Otherwise, the abort promise is rejected and the pipe is not closed.
315 // The abort is only successful if the payment app wasn't yet invoked.
316 // TODO(crbug.com/716546): Add a merchant abort metric
317
318 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33319 if (accepting_abort)
320 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56321
mathpf4bc50e2017-01-24 05:17:50322 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56323 client_->OnAbort(accepting_abort);
324
325 if (observer_for_testing_)
326 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50327}
328
mathp218795892017-03-29 15:15:34329void PaymentRequest::Complete(mojom::PaymentComplete result) {
Rouslan Solomakhin27064702018-12-14 21:15:33330 if (!IsInitialized()) {
331 log_.Error("Attempted complete without initialization");
332 OnConnectionTerminated();
mathp4b85b582017-03-08 21:07:16333 return;
Rouslan Solomakhin27064702018-12-14 21:15:33334 }
335
336 if (!IsThisPaymentRequestShowing()) {
337 log_.Error("Attempted complete without show");
338 OnConnectionTerminated();
339 return;
340 }
mathp4b85b582017-03-08 21:07:16341
Rouslan Solomakhine3473192017-06-16 14:54:57342 // Failed transactions show an error. Successful and unknown-state
343 // transactions don't show an error.
344 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34345 delegate_->ShowErrorMessage();
346 } else {
sebsgfcdd13c2017-06-08 15:49:33347 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58348 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33349 has_recorded_completion_ = true;
350
anthonyvd6a43b932017-05-11 18:39:27351 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
352 true);
mathp218795892017-03-29 15:15:34353 // When the renderer closes the connection,
354 // PaymentRequest::OnConnectionTerminated will be called.
355 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22356 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34357 }
mathp4b85b582017-03-08 21:07:16358}
359
360void PaymentRequest::CanMakePayment() {
Rouslan Solomakhin27064702018-12-14 21:15:33361 if (!IsInitialized()) {
362 log_.Error("Attempted canMakePayment without initialization");
363 OnConnectionTerminated();
364 return;
365 }
366
367 // It's valid to call canMakePayment() without calling show() first.
368
gogerald8189d522017-09-15 17:52:18369 if (observer_for_testing_)
370 observer_for_testing_->OnCanMakePaymentCalled();
Mathieu Perreaultcacb85e2018-06-06 20:40:13371
Rouslan Solomakhind2cae95a2018-08-09 00:16:10372 if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) ||
373 !state_) {
Mathieu Perreaultcacb85e2018-06-06 20:40:13374 CanMakePaymentCallback(/*can_make_payment=*/false);
375 } else {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10376 state_->CanMakePayment(
Mathieu Perreaultcacb85e2018-06-06 20:40:13377 base::BindOnce(&PaymentRequest::CanMakePaymentCallback,
378 weak_ptr_factory_.GetWeakPtr()));
379 }
gogerald8189d522017-09-15 17:52:18380}
381
Rouslan Solomakhin5683eb282019-01-29 18:06:03382void PaymentRequest::HasEnrolledInstrument(bool per_method_quota) {
Danyao Wangce175bf2018-12-21 22:35:58383 if (!IsInitialized()) {
384 log_.Error("Attempted hasEnrolledInstrument without initialization");
385 OnConnectionTerminated();
386 return;
387 }
388
389 // It's valid to call hasEnrolledInstrument() without calling show() first.
390
391 if (observer_for_testing_)
392 observer_for_testing_->OnHasEnrolledInstrumentCalled();
393
394 if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) ||
395 !state_) {
Rouslan Solomakhin5683eb282019-01-29 18:06:03396 HasEnrolledInstrumentCallback(per_method_quota,
397 /*has_enrolled_instrument=*/false);
Danyao Wangce175bf2018-12-21 22:35:58398 } else {
399 state_->HasEnrolledInstrument(
400 base::BindOnce(&PaymentRequest::HasEnrolledInstrumentCallback,
Rouslan Solomakhin5683eb282019-01-29 18:06:03401 weak_ptr_factory_.GetWeakPtr(), per_method_quota));
Danyao Wangce175bf2018-12-21 22:35:58402 }
403}
404
Rouslan Solomakhin27064702018-12-14 21:15:33405void PaymentRequest::AreRequestedMethodsSupportedCallback(
406 bool methods_supported) {
407 if (methods_supported) {
408 if (SatisfiesSkipUIConstraints()) {
409 skipped_payment_request_ui_ = true;
410 Pay();
411 }
412 } else {
413 journey_logger_.SetNotShown(
414 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
415 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
416 if (observer_for_testing_)
417 observer_for_testing_->OnNotSupportedError();
418 OnConnectionTerminated();
419 }
420}
421
422bool PaymentRequest::IsInitialized() const {
423 return is_initialized_ && client_ && client_.is_bound() &&
424 binding_.is_bound();
425}
426
427bool PaymentRequest::IsThisPaymentRequestShowing() const {
428 return is_show_called_ && display_handle_ && spec_ && state_;
429}
430
431bool PaymentRequest::SatisfiesSkipUIConstraints() const {
432 return base::FeatureList::IsEnabled(features::kWebPaymentsSingleAppUiSkip) &&
433 base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps) &&
434 is_show_user_gesture_ && state()->is_get_all_instruments_finished() &&
435 state()->available_instruments().size() == 1 &&
436 spec()->stringified_method_data().size() == 1 &&
437 !spec()->request_shipping() && !spec()->request_payer_name() &&
438 !spec()->request_payer_phone() &&
439 !spec()->request_payer_email()
440 // Only allowing URL base payment apps to skip the payment sheet.
441 && spec()->url_payment_method_identifiers().size() == 1;
442}
443
mathpf1a7a3752017-03-15 11:23:37444void PaymentRequest::OnPaymentResponseAvailable(
445 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45446 journey_logger_.SetEventOccurred(
447 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
gogerald7a2b761e2017-11-09 18:30:19448
449 // Do not send invalid response to client.
450 if (response->method_name.empty() || response->stringified_details.empty()) {
451 RecordFirstAbortReason(
452 JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR);
453 delegate_->ShowErrorMessage();
454 return;
455 }
456
mathpf1a7a3752017-03-15 11:23:37457 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16458}
459
mathp151bd312017-04-03 21:07:24460void PaymentRequest::OnShippingOptionIdSelected(
461 std::string shipping_option_id) {
462 client_->OnShippingOptionChange(shipping_option_id);
463}
464
465void PaymentRequest::OnShippingAddressSelected(
466 mojom::PaymentAddressPtr address) {
467 client_->OnShippingAddressChange(std::move(address));
468}
469
Jinho Bangbb178152018-09-13 09:44:43470void PaymentRequest::OnPayerInfoSelected(mojom::PayerDetailPtr payer_info) {
471 client_->OnPayerDetailChange(std::move(payer_info));
472}
473
mathpf4bc50e2017-01-24 05:17:50474void PaymentRequest::UserCancelled() {
475 // If |client_| is not bound, then the object is already being destroyed as
476 // a result of a renderer event.
477 if (!client_.is_bound())
478 return;
479
sebsgfcdd13c2017-06-08 15:49:33480 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17481
mathpf4bc50e2017-01-24 05:17:50482 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28483 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50484
485 // We close all bindings and ask to be destroyed.
486 client_.reset();
487 binding_.Close();
rouslanb28f4532017-05-08 15:41:47488 if (observer_for_testing_)
489 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50490 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36491}
492
sebsgd56b3e422017-10-20 18:08:08493void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument(
494 bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33495 RecordFirstAbortReason(is_user_initiated
496 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
497 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10498}
499
mathpf4bc50e2017-01-24 05:17:50500void PaymentRequest::OnConnectionTerminated() {
501 // We are here because of a browser-side error, or likely as a result of the
502 // connection_error_handler on |binding_|, which can mean that the renderer
503 // has decided to close the pipe for various reasons (see all uses of
504 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
505 // the binding and the dialog, and ask to be deleted.
506 client_.reset();
mathpf709499d2017-01-09 20:48:36507 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50508 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47509 if (observer_for_testing_)
510 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33511
512 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36513 manager_->DestroyRequest(this);
514}
515
mathpd4be8de82017-03-01 00:51:48516void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45517 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault23d25bfb82018-05-11 14:45:37518
519 // Log the correct "selected instrument" metric according to type.
520 DCHECK(state_->selected_instrument());
521 JourneyLogger::Event selected_event =
522 JourneyLogger::Event::EVENT_SELECTED_OTHER;
523 switch (state_->selected_instrument()->type()) {
524 case PaymentInstrument::Type::AUTOFILL:
525 selected_event = JourneyLogger::Event::EVENT_SELECTED_CREDIT_CARD;
526 break;
527 case PaymentInstrument::Type::SERVICE_WORKER_APP:
528 selected_event = JourneyLogger::Event::EVENT_SELECTED_OTHER;
529 break;
530 case PaymentInstrument::Type::NATIVE_MOBILE_APP:
531 NOTREACHED();
532 break;
533 }
534 journey_logger_.SetEventOccurred(selected_event);
535
mathpf1a7a3752017-03-15 11:23:37536 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48537}
538
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07539void PaymentRequest::HideIfNecessary() {
540 display_handle_.reset();
541}
542
Rouslan Solomakhind2cae95a2018-08-09 00:16:10543void PaymentRequest::RecordDialogShownEventInJourneyLogger() {
544 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
545}
546
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04547bool PaymentRequest::IsIncognito() const {
548 return delegate_->IsIncognito();
549}
550
sebsgfcdd13c2017-06-08 15:49:33551void PaymentRequest::RecordFirstAbortReason(
552 JourneyLogger::AbortReason abort_reason) {
553 if (!has_recorded_completion_) {
554 has_recorded_completion_ = true;
555 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10556 }
557}
558
Rouslan Solomakhin1804ee42017-10-03 14:27:43559void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
Danyao Wang4bc0606a2018-12-27 16:54:53560 client_->OnCanMakePayment(
561 can_make_payment ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
562 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
563
564 // TODO(https://crbug.com/915907): emit JourneyLogger event once the event
565 // names are updated.
Rouslan Solomakhin1804ee42017-10-03 14:27:43566
567 if (observer_for_testing_)
568 observer_for_testing_->OnCanMakePaymentReturned();
569}
570
Danyao Wangce175bf2018-12-21 22:35:58571void PaymentRequest::HasEnrolledInstrumentCallback(
Rouslan Solomakhin5683eb282019-01-29 18:06:03572 bool per_method_quota,
Danyao Wangce175bf2018-12-21 22:35:58573 bool has_enrolled_instrument) {
Rouslan Solomakhin5683eb282019-01-29 18:06:03574 if (!spec_ ||
575 CanMakePaymentQueryFactory::GetInstance()
576 ->GetForContext(web_contents_->GetBrowserContext())
577 ->CanQuery(top_level_origin_, frame_origin_,
578 spec_->stringified_method_data(), per_method_quota)) {
Danyao Wangce175bf2018-12-21 22:35:58579 RespondToHasEnrolledInstrumentQuery(has_enrolled_instrument,
580 /*warn_localhost_or_file=*/false);
581 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
582 RespondToHasEnrolledInstrumentQuery(has_enrolled_instrument,
583 /*warn_localhost_or_file=*/true);
584 } else {
585 client_->OnHasEnrolledInstrument(
586 HasEnrolledInstrumentQueryResult::QUERY_QUOTA_EXCEEDED);
587 }
588
589 if (observer_for_testing_)
590 observer_for_testing_->OnHasEnrolledInstrumentReturned();
591}
592
Danyao Wangce175bf2018-12-21 22:35:58593void PaymentRequest::RespondToHasEnrolledInstrumentQuery(
594 bool has_enrolled_instrument,
595 bool warn_localhost_or_file) {
596 HasEnrolledInstrumentQueryResult positive =
597 warn_localhost_or_file
598 ? HasEnrolledInstrumentQueryResult::WARNING_HAS_ENROLLED_INSTRUMENT
599 : HasEnrolledInstrumentQueryResult::HAS_ENROLLED_INSTRUMENT;
600 HasEnrolledInstrumentQueryResult negative =
601 warn_localhost_or_file
602 ? HasEnrolledInstrumentQueryResult::WARNING_HAS_NO_ENROLLED_INSTRUMENT
603 : HasEnrolledInstrumentQueryResult::HAS_NO_ENROLLED_INSTRUMENT;
604
605 client_->OnHasEnrolledInstrument(has_enrolled_instrument ? positive
606 : negative);
607 journey_logger_.SetCanMakePaymentValue(has_enrolled_instrument);
608}
609
mathpf709499d2017-01-09 20:48:36610} // namespace payments