blob: 7157a66e32aac9c33ee14c2f5473f91f9923d9ef [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
Rouslan Solomakhin1804ee42017-10-03 14:27:4310#include "base/feature_list.h"
Mathieu Perreault627b97c2017-08-12 00:44:2211#include "base/stl_util.h"
rouslan690997682017-05-09 18:07:3912#include "components/payments/content/can_make_payment_query_factory.h"
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5113#include "components/payments/content/content_payment_request_delegate.h"
rouslan6e3cf7c62017-04-17 21:23:2814#include "components/payments/content/origin_security_checker.h"
Mohamad Ahmadif5544bb2017-09-01 21:48:2215#include "components/payments/content/payment_request_converter.h"
rouslan908248c2017-02-27 21:30:2416#include "components/payments/content/payment_request_web_contents_manager.h"
rouslan690997682017-05-09 18:07:3917#include "components/payments/core/can_make_payment_query.h"
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:3618#include "components/payments/core/features.h"
Mohamad Ahmadif5544bb2017-09-01 21:48:2219#include "components/payments/core/payment_details.h"
20#include "components/payments/core/payment_details_validation.h"
Mathieu Perreault23d25bfb82018-05-11 14:45:3721#include "components/payments/core/payment_instrument.h"
anthonyvd6a43b932017-05-11 18:39:2722#include "components/payments/core/payment_prefs.h"
Jinho Bangbe463a22018-08-02 10:26:5023#include "components/payments/core/payments_validators.h"
anthonyvd6a43b932017-05-11 18:39:2724#include "components/prefs/pref_service.h"
Steven Holte2083e8bc2018-07-16 23:50:3625#include "components/ukm/content/source_url_recorder.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3926#include "components/url_formatter/elide_url.h"
mathpf709499d2017-01-09 20:48:3627#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3928#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3629#include "content/public/browser/web_contents.h"
Rouslan Solomakhin1804ee42017-10-03 14:27:4330#include "content/public/common/content_features.h"
mathpf709499d2017-01-09 20:48:3631
32namespace payments {
33
34PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3935 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3636 content::WebContents* web_contents,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5137 std::unique_ptr<ContentPaymentRequestDelegate> delegate,
mathpf709499d2017-01-09 20:48:3638 PaymentRequestWebContentsManager* manager,
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0739 PaymentRequestDisplayManager* display_manager,
rouslan6e3cf7c62017-04-17 21:23:2840 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3741 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3642 : web_contents_(web_contents),
43 delegate_(std::move(delegate)),
44 manager_(manager),
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0745 display_manager_(display_manager),
46 display_handle_(nullptr),
mathp300fa542017-03-27 19:29:3747 binding_(this, std::move(request)),
Rouslan Solomakhin6e979ab2017-08-30 17:30:3948 top_level_origin_(url_formatter::FormatUrlForSecurityDisplay(
49 web_contents_->GetLastCommittedURL())),
50 frame_origin_(url_formatter::FormatUrlForSecurityDisplay(
51 render_frame_host->GetLastCommittedURL())),
sebsg20b49d7b2017-05-04 20:23:1752 observer_for_testing_(observer_for_testing),
53 journey_logger_(delegate_->IsIncognito(),
Steven Holte2083e8bc2018-07-16 23:50:3654 ukm::GetSourceIdForWebContentsDocument(web_contents)),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1355 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5056 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
57 // will happen as a result of many renderer-side events (both successful and
58 // erroneous in nature).
59 // TODO(crbug.com/683636): Investigate using
60 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
tzik2bcf8e42018-07-31 11:22:1561 binding_.set_connection_error_handler(base::BindOnce(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1362 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3663}
64
65PaymentRequest::~PaymentRequest() {}
66
rouslan6e3cf7c62017-04-17 21:23:2867void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
68 std::vector<mojom::PaymentMethodDataPtr> method_data,
69 mojom::PaymentDetailsPtr details,
70 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3671 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2872 client_ = std::move(client);
73
rouslanb28f4532017-05-08 15:41:4774 const GURL last_committed_url = delegate_->GetLastCommittedURL();
75 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2876 LOG(ERROR) << "Not in a secure origin";
77 OnConnectionTerminated();
78 return;
79 }
80
rouslanb28f4532017-05-08 15:41:4781 bool allowed_origin =
82 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
83 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
84 if (!allowed_origin) {
85 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
86 "allowed";
87 }
88
89 bool invalid_ssl =
90 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
91 !delegate_->IsSslCertificateValid();
92 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2893 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4794
95 if (!allowed_origin || invalid_ssl) {
Rouslan Solomakhind2cae95a2018-08-09 00:16:1096 // Intentionally don't set |spec_| and |state_|. Don't show UI. Resolve
97 // .canMakepayment() with "false". Reject .show() with "NotSupportedError".
rouslan6e3cf7c62017-04-17 21:23:2898 return;
99 }
100
mathpf709499d2017-01-09 20:48:36101 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22102 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathpf709499d2017-01-09 20:48:36103 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:50104 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36105 return;
106 }
rouslan6e3cf7c62017-04-17 21:23:28107
jinho.bangfcb5ec92017-03-29 08:08:02108 if (!details->total) {
109 LOG(ERROR) << "Missing total";
110 OnConnectionTerminated();
111 return;
112 }
rouslan6e3cf7c62017-04-17 21:23:28113
sebsga70a6da2017-12-21 22:27:02114 spec_ = std::make_unique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33115 std::move(options), std::move(details), std::move(method_data), this,
116 delegate_->GetApplicationLocale());
sebsga70a6da2017-12-21 22:27:02117 state_ = std::make_unique<PaymentRequestState>(
Rouslan Solomakhindbf593d92017-11-21 19:20:57118 web_contents_, top_level_origin_, frame_origin_, spec_.get(), this,
119 delegate_->GetApplicationLocale(), delegate_->GetPersonalDataManager(),
120 delegate_.get(), &journey_logger_);
Mathieu Perreault627b97c2017-08-12 00:44:22121
122 journey_logger_.SetRequestedInformation(
123 spec_->request_shipping(), spec_->request_payer_email(),
124 spec_->request_payer_phone(), spec_->request_payer_name());
125
126 // Log metrics around which payment methods are requested by the merchant.
127 GURL google_pay_url(kGooglePayMethodName);
128 GURL android_pay_url(kAndroidPayMethodName);
129 // Looking for payment methods that are NOT google-related payment methods.
130 auto non_google_it =
131 std::find_if(spec_->url_payment_method_identifiers().begin(),
132 spec_->url_payment_method_identifiers().end(),
133 [google_pay_url, android_pay_url](const GURL& url) {
134 return url != google_pay_url && url != android_pay_url;
135 });
136 journey_logger_.SetRequestedPaymentMethodTypes(
137 /*requested_basic_card=*/!spec_->supported_card_networks().empty(),
138 /*requested_method_google=*/
139 base::ContainsValue(spec_->url_payment_method_identifiers(),
140 google_pay_url) ||
141 base::ContainsValue(spec_->url_payment_method_identifiers(),
142 android_pay_url),
143 /*requested_method_other=*/non_google_it !=
144 spec_->url_payment_method_identifiers().end());
mathpf709499d2017-01-09 20:48:36145}
146
Rouslan Solomakhin833f8512018-04-03 23:19:25147void PaymentRequest::Show(bool is_user_gesture) {
tmartino8ce922852017-01-09 22:23:10148 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50149 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
150 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10151 return;
152 }
rouslan6e3cf7c62017-04-17 21:23:28153
rouslan7d433cc22017-05-08 15:18:07154 // A tab can display only one PaymentRequest UI at a time.
Anthony Vallee-Dubois8f5e7e12018-01-12 16:14:06155 display_handle_ = display_manager_->TryShow(delegate_.get());
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07156 if (!display_handle_) {
rouslan7d433cc22017-05-08 15:18:07157 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12158 journey_logger_.SetNotShown(
159 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
Rouslan Solomakhinb2d233c42018-08-30 16:18:40160 client_->OnError(mojom::PaymentErrorReason::ALREADY_SHOWING);
rouslan7d433cc22017-05-08 15:18:07161 OnConnectionTerminated();
162 return;
163 }
164
Rouslan Solomakhin5b510432017-09-26 16:59:32165 if (!delegate_->IsBrowserWindowActive()) {
166 LOG(ERROR) << "Cannot show PaymentRequest UI in a background tab";
167 journey_logger_.SetNotShown(JourneyLogger::NOT_SHOWN_REASON_OTHER);
168 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
169 OnConnectionTerminated();
170 return;
171 }
172
Rouslan Solomakhind2cae95a2018-08-09 00:16:10173 if (!state_) {
174 // SSL is not valid.
175 AreRequestedMethodsSupportedCallback(false);
176 return;
177 }
178
Rouslan Solomakhin833f8512018-04-03 23:19:25179 is_show_user_gesture_ = is_user_gesture;
180
Rouslan Solomakhind2cae95a2018-08-09 00:16:10181 display_handle_->Show(this);
182
gogerald0a7ee6c2017-11-13 18:23:19183 state_->AreRequestedMethodsSupported(
184 base::BindOnce(&PaymentRequest::AreRequestedMethodsSupportedCallback,
185 weak_ptr_factory_.GetWeakPtr()));
186}
187
Jinho Bangbe463a22018-08-02 10:26:50188void PaymentRequest::Retry(mojom::PaymentValidationErrorsPtr errors) {
Jinho Bangcac8d9a02018-08-23 19:47:22189 if (!client_.is_bound() || !binding_.is_bound()) {
190 DLOG(ERROR) << "Attempted Retry(), but binding(s) missing.";
191 OnConnectionTerminated();
192 return;
193 }
194
195 if (!display_handle_) {
196 DLOG(ERROR) << "Attempted Retry(), but display_handle_ is nullptr.";
197 OnConnectionTerminated();
198 return;
199 }
200
Jinho Bangbe463a22018-08-02 10:26:50201 std::string error;
202 if (!PaymentsValidators::IsValidPaymentValidationErrorsFormat(errors,
203 &error)) {
204 DLOG(ERROR) << error;
205 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
206 OnConnectionTerminated();
207 return;
208 }
209
Jinho Bang092e7162018-09-06 23:41:19210 spec()->Retry(std::move(errors));
Jinho Bangcac8d9a02018-08-23 19:47:22211 display_handle_->Retry();
Jinho Bangbe463a22018-08-02 10:26:50212}
213
gogerald0a7ee6c2017-11-13 18:23:19214void PaymentRequest::AreRequestedMethodsSupportedCallback(
215 bool methods_supported) {
216 if (methods_supported) {
gogerald2ddfc64c2018-08-23 16:20:21217 if (SatisfiesSkipUIConstraints()) {
218 skipped_payment_request_ui_ = true;
Rouslan Solomakhind2cae95a2018-08-09 00:16:10219 Pay();
gogerald2ddfc64c2018-08-23 16:20:21220 }
gogerald0a7ee6c2017-11-13 18:23:19221 } else {
sebsg828269bc2017-06-09 19:11:12222 journey_logger_.SetNotShown(
223 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28224 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
225 if (observer_for_testing_)
226 observer_for_testing_->OnNotSupportedError();
227 OnConnectionTerminated();
rouslan6e3cf7c62017-04-17 21:23:28228 }
mathpf709499d2017-01-09 20:48:36229}
230
gogerald2ddfc64c2018-08-23 16:20:21231bool PaymentRequest::SatisfiesSkipUIConstraints() const {
232 return base::FeatureList::IsEnabled(features::kWebPaymentsSingleAppUiSkip) &&
233 base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps) &&
234 is_show_user_gesture_ && state()->is_get_all_instruments_finished() &&
235 state()->available_instruments().size() == 1 &&
236 spec()->stringified_method_data().size() == 1 &&
237 !spec()->request_shipping() && !spec()->request_payer_name() &&
238 !spec()->request_payer_phone() &&
239 !spec()->request_payer_email()
240 // Only allowing URL base payment apps to skip the payment sheet.
241 && spec()->url_payment_method_identifiers().size() == 1;
242}
243
mathp151bd312017-04-03 21:07:24244void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
245 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22246 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd312017-04-03 21:07:24247 LOG(ERROR) << error;
248 OnConnectionTerminated();
249 return;
250 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39251
Jinho Bang092e7162018-09-06 23:41:19252 if (details->shipping_address_errors &&
253 !PaymentsValidators::IsValidAddressErrorsFormat(
254 details->shipping_address_errors, &error)) {
255 DLOG(ERROR) << error;
256 OnConnectionTerminated();
257 return;
258 }
259
Rouslan Solomakhin4cbda822017-08-23 18:50:39260 if (!details->total) {
261 LOG(ERROR) << "Missing total";
262 OnConnectionTerminated();
263 return;
264 }
265
mathp151bd312017-04-03 21:07:24266 spec_->UpdateWith(std::move(details));
267}
268
Rouslan Solomakhina9ff9282017-10-31 21:58:05269void PaymentRequest::NoUpdatedPaymentDetails() {
270 spec_->RecomputeSpecForDetails();
271}
272
mathpf4bc50e2017-01-24 05:17:50273void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56274 // The API user has decided to abort. If a successful abort message is
275 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50276 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56277 // Otherwise, the abort promise is rejected and the pipe is not closed.
278 // The abort is only successful if the payment app wasn't yet invoked.
279 // TODO(crbug.com/716546): Add a merchant abort metric
280
281 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33282 if (accepting_abort)
283 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56284
mathpf4bc50e2017-01-24 05:17:50285 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56286 client_->OnAbort(accepting_abort);
287
288 if (observer_for_testing_)
289 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50290}
291
mathp218795892017-03-29 15:15:34292void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16293 if (!client_.is_bound())
294 return;
295
Rouslan Solomakhine3473192017-06-16 14:54:57296 // Failed transactions show an error. Successful and unknown-state
297 // transactions don't show an error.
298 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34299 delegate_->ShowErrorMessage();
300 } else {
sebsgfcdd13c2017-06-08 15:49:33301 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58302 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33303 has_recorded_completion_ = true;
304
anthonyvd6a43b932017-05-11 18:39:27305 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
306 true);
mathp218795892017-03-29 15:15:34307 // When the renderer closes the connection,
308 // PaymentRequest::OnConnectionTerminated will be called.
309 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22310 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34311 }
mathp4b85b582017-03-08 21:07:16312}
313
314void PaymentRequest::CanMakePayment() {
gogerald8189d522017-09-15 17:52:18315 if (observer_for_testing_)
316 observer_for_testing_->OnCanMakePaymentCalled();
Mathieu Perreaultcacb85e2018-06-06 20:40:13317
Rouslan Solomakhind2cae95a2018-08-09 00:16:10318 if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) ||
319 !state_) {
Mathieu Perreaultcacb85e2018-06-06 20:40:13320 CanMakePaymentCallback(/*can_make_payment=*/false);
321 } else {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10322 state_->CanMakePayment(
Mathieu Perreaultcacb85e2018-06-06 20:40:13323 base::BindOnce(&PaymentRequest::CanMakePaymentCallback,
324 weak_ptr_factory_.GetWeakPtr()));
325 }
gogerald8189d522017-09-15 17:52:18326}
327
mathpf1a7a3752017-03-15 11:23:37328void PaymentRequest::OnPaymentResponseAvailable(
329 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45330 journey_logger_.SetEventOccurred(
331 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
gogerald7a2b761e2017-11-09 18:30:19332
333 // Do not send invalid response to client.
334 if (response->method_name.empty() || response->stringified_details.empty()) {
335 RecordFirstAbortReason(
336 JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR);
337 delegate_->ShowErrorMessage();
338 return;
339 }
340
mathpf1a7a3752017-03-15 11:23:37341 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16342}
343
mathp151bd312017-04-03 21:07:24344void PaymentRequest::OnShippingOptionIdSelected(
345 std::string shipping_option_id) {
346 client_->OnShippingOptionChange(shipping_option_id);
347}
348
349void PaymentRequest::OnShippingAddressSelected(
350 mojom::PaymentAddressPtr address) {
351 client_->OnShippingAddressChange(std::move(address));
352}
353
mathpf4bc50e2017-01-24 05:17:50354void PaymentRequest::UserCancelled() {
355 // If |client_| is not bound, then the object is already being destroyed as
356 // a result of a renderer event.
357 if (!client_.is_bound())
358 return;
359
sebsgfcdd13c2017-06-08 15:49:33360 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17361
mathpf4bc50e2017-01-24 05:17:50362 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28363 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50364
365 // We close all bindings and ask to be destroyed.
366 client_.reset();
367 binding_.Close();
rouslanb28f4532017-05-08 15:41:47368 if (observer_for_testing_)
369 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50370 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36371}
372
sebsgd56b3e422017-10-20 18:08:08373void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument(
374 bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33375 RecordFirstAbortReason(is_user_initiated
376 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
377 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10378}
379
mathpf4bc50e2017-01-24 05:17:50380void PaymentRequest::OnConnectionTerminated() {
381 // We are here because of a browser-side error, or likely as a result of the
382 // connection_error_handler on |binding_|, which can mean that the renderer
383 // has decided to close the pipe for various reasons (see all uses of
384 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
385 // the binding and the dialog, and ask to be deleted.
386 client_.reset();
mathpf709499d2017-01-09 20:48:36387 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50388 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47389 if (observer_for_testing_)
390 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33391
392 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36393 manager_->DestroyRequest(this);
394}
395
mathpd4be8de82017-03-01 00:51:48396void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45397 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault23d25bfb82018-05-11 14:45:37398
399 // Log the correct "selected instrument" metric according to type.
400 DCHECK(state_->selected_instrument());
401 JourneyLogger::Event selected_event =
402 JourneyLogger::Event::EVENT_SELECTED_OTHER;
403 switch (state_->selected_instrument()->type()) {
404 case PaymentInstrument::Type::AUTOFILL:
405 selected_event = JourneyLogger::Event::EVENT_SELECTED_CREDIT_CARD;
406 break;
407 case PaymentInstrument::Type::SERVICE_WORKER_APP:
408 selected_event = JourneyLogger::Event::EVENT_SELECTED_OTHER;
409 break;
410 case PaymentInstrument::Type::NATIVE_MOBILE_APP:
411 NOTREACHED();
412 break;
413 }
414 journey_logger_.SetEventOccurred(selected_event);
415
mathpf1a7a3752017-03-15 11:23:37416 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48417}
418
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07419void PaymentRequest::HideIfNecessary() {
420 display_handle_.reset();
421}
422
Rouslan Solomakhind2cae95a2018-08-09 00:16:10423void PaymentRequest::RecordDialogShownEventInJourneyLogger() {
424 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
425}
426
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04427bool PaymentRequest::IsIncognito() const {
428 return delegate_->IsIncognito();
429}
430
sebsgfcdd13c2017-06-08 15:49:33431void PaymentRequest::RecordFirstAbortReason(
432 JourneyLogger::AbortReason abort_reason) {
433 if (!has_recorded_completion_) {
434 has_recorded_completion_ = true;
435 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10436 }
437}
438
Rouslan Solomakhin1804ee42017-10-03 14:27:43439void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10440 if (!spec_ || CanMakePaymentQueryFactory::GetInstance()
441 ->GetForContext(web_contents_->GetBrowserContext())
442 ->CanQuery(top_level_origin_, frame_origin_,
443 spec_->stringified_method_data())) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42444 RespondToCanMakePaymentQuery(can_make_payment, false);
Rouslan Solomakhin1804ee42017-10-03 14:27:43445 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42446 RespondToCanMakePaymentQuery(can_make_payment, true);
Rouslan Solomakhin1804ee42017-10-03 14:27:43447 } else {
448 client_->OnCanMakePayment(
449 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
450 }
451
452 if (observer_for_testing_)
453 observer_for_testing_->OnCanMakePaymentReturned();
454}
455
Rouslan Solomakhin16ee55202017-10-03 19:04:42456void PaymentRequest::RespondToCanMakePaymentQuery(bool can_make_payment,
457 bool warn_localhost_or_file) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42458 mojom::CanMakePaymentQueryResult positive =
459 warn_localhost_or_file
460 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
461 : mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT;
462 mojom::CanMakePaymentQueryResult negative =
463 warn_localhost_or_file
464 ? mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT
465 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT;
466
467 client_->OnCanMakePayment(can_make_payment ? positive : negative);
Rouslan Solomakhin1804ee42017-10-03 14:27:43468 journey_logger_.SetCanMakePaymentValue(can_make_payment);
469}
470
mathpf709499d2017-01-09 20:48:36471} // namespace payments