blob: dbc6518774a6e327c9c02c30d59242d1e98e1212 [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);
rouslan7d433cc22017-05-08 15:18:07160 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
161 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) {
189 std::string error;
190 if (!PaymentsValidators::IsValidPaymentValidationErrorsFormat(errors,
191 &error)) {
192 DLOG(ERROR) << error;
193 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
194 OnConnectionTerminated();
195 return;
196 }
197
198 // TODO(zino): Should implement this method (including updating UI part).
199 // Please see https://crbug.com/861704
200 NOTIMPLEMENTED();
201}
202
gogerald0a7ee6c2017-11-13 18:23:19203void PaymentRequest::AreRequestedMethodsSupportedCallback(
204 bool methods_supported) {
205 if (methods_supported) {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10206 if (SatisfiesSkipUIConstraints())
207 Pay();
gogerald0a7ee6c2017-11-13 18:23:19208 } else {
sebsg828269bc2017-06-09 19:11:12209 journey_logger_.SetNotShown(
210 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28211 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
212 if (observer_for_testing_)
213 observer_for_testing_->OnNotSupportedError();
214 OnConnectionTerminated();
rouslan6e3cf7c62017-04-17 21:23:28215 }
mathpf709499d2017-01-09 20:48:36216}
217
mathp151bd312017-04-03 21:07:24218void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
219 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22220 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd312017-04-03 21:07:24221 LOG(ERROR) << error;
222 OnConnectionTerminated();
223 return;
224 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39225
226 if (!details->total) {
227 LOG(ERROR) << "Missing total";
228 OnConnectionTerminated();
229 return;
230 }
231
mathp151bd312017-04-03 21:07:24232 spec_->UpdateWith(std::move(details));
233}
234
Rouslan Solomakhina9ff9282017-10-31 21:58:05235void PaymentRequest::NoUpdatedPaymentDetails() {
236 spec_->RecomputeSpecForDetails();
237}
238
mathpf4bc50e2017-01-24 05:17:50239void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56240 // The API user has decided to abort. If a successful abort message is
241 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50242 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56243 // Otherwise, the abort promise is rejected and the pipe is not closed.
244 // The abort is only successful if the payment app wasn't yet invoked.
245 // TODO(crbug.com/716546): Add a merchant abort metric
246
247 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33248 if (accepting_abort)
249 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56250
mathpf4bc50e2017-01-24 05:17:50251 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56252 client_->OnAbort(accepting_abort);
253
254 if (observer_for_testing_)
255 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50256}
257
mathp218795892017-03-29 15:15:34258void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16259 if (!client_.is_bound())
260 return;
261
Rouslan Solomakhine3473192017-06-16 14:54:57262 // Failed transactions show an error. Successful and unknown-state
263 // transactions don't show an error.
264 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34265 delegate_->ShowErrorMessage();
266 } else {
sebsgfcdd13c2017-06-08 15:49:33267 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58268 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33269 has_recorded_completion_ = true;
270
anthonyvd6a43b932017-05-11 18:39:27271 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
272 true);
mathp218795892017-03-29 15:15:34273 // When the renderer closes the connection,
274 // PaymentRequest::OnConnectionTerminated will be called.
275 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22276 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34277 }
mathp4b85b582017-03-08 21:07:16278}
279
280void PaymentRequest::CanMakePayment() {
gogerald8189d522017-09-15 17:52:18281 if (observer_for_testing_)
282 observer_for_testing_->OnCanMakePaymentCalled();
Mathieu Perreaultcacb85e2018-06-06 20:40:13283
Rouslan Solomakhind2cae95a2018-08-09 00:16:10284 if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) ||
285 !state_) {
Mathieu Perreaultcacb85e2018-06-06 20:40:13286 CanMakePaymentCallback(/*can_make_payment=*/false);
287 } else {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10288 state_->CanMakePayment(
Mathieu Perreaultcacb85e2018-06-06 20:40:13289 base::BindOnce(&PaymentRequest::CanMakePaymentCallback,
290 weak_ptr_factory_.GetWeakPtr()));
291 }
gogerald8189d522017-09-15 17:52:18292}
293
mathpf1a7a3752017-03-15 11:23:37294void PaymentRequest::OnPaymentResponseAvailable(
295 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45296 journey_logger_.SetEventOccurred(
297 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
gogerald7a2b761e2017-11-09 18:30:19298
299 // Do not send invalid response to client.
300 if (response->method_name.empty() || response->stringified_details.empty()) {
301 RecordFirstAbortReason(
302 JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR);
303 delegate_->ShowErrorMessage();
304 return;
305 }
306
mathpf1a7a3752017-03-15 11:23:37307 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16308}
309
mathp151bd312017-04-03 21:07:24310void PaymentRequest::OnShippingOptionIdSelected(
311 std::string shipping_option_id) {
312 client_->OnShippingOptionChange(shipping_option_id);
313}
314
315void PaymentRequest::OnShippingAddressSelected(
316 mojom::PaymentAddressPtr address) {
317 client_->OnShippingAddressChange(std::move(address));
318}
319
mathpf4bc50e2017-01-24 05:17:50320void PaymentRequest::UserCancelled() {
321 // If |client_| is not bound, then the object is already being destroyed as
322 // a result of a renderer event.
323 if (!client_.is_bound())
324 return;
325
sebsgfcdd13c2017-06-08 15:49:33326 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17327
mathpf4bc50e2017-01-24 05:17:50328 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28329 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50330
331 // We close all bindings and ask to be destroyed.
332 client_.reset();
333 binding_.Close();
rouslanb28f4532017-05-08 15:41:47334 if (observer_for_testing_)
335 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50336 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36337}
338
sebsgd56b3e422017-10-20 18:08:08339void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument(
340 bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33341 RecordFirstAbortReason(is_user_initiated
342 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
343 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10344}
345
mathpf4bc50e2017-01-24 05:17:50346void PaymentRequest::OnConnectionTerminated() {
347 // We are here because of a browser-side error, or likely as a result of the
348 // connection_error_handler on |binding_|, which can mean that the renderer
349 // has decided to close the pipe for various reasons (see all uses of
350 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
351 // the binding and the dialog, and ask to be deleted.
352 client_.reset();
mathpf709499d2017-01-09 20:48:36353 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50354 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47355 if (observer_for_testing_)
356 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33357
358 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36359 manager_->DestroyRequest(this);
360}
361
mathpd4be8de82017-03-01 00:51:48362void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45363 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault23d25bfb82018-05-11 14:45:37364
365 // Log the correct "selected instrument" metric according to type.
366 DCHECK(state_->selected_instrument());
367 JourneyLogger::Event selected_event =
368 JourneyLogger::Event::EVENT_SELECTED_OTHER;
369 switch (state_->selected_instrument()->type()) {
370 case PaymentInstrument::Type::AUTOFILL:
371 selected_event = JourneyLogger::Event::EVENT_SELECTED_CREDIT_CARD;
372 break;
373 case PaymentInstrument::Type::SERVICE_WORKER_APP:
374 selected_event = JourneyLogger::Event::EVENT_SELECTED_OTHER;
375 break;
376 case PaymentInstrument::Type::NATIVE_MOBILE_APP:
377 NOTREACHED();
378 break;
379 }
380 journey_logger_.SetEventOccurred(selected_event);
381
mathpf1a7a3752017-03-15 11:23:37382 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48383}
384
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07385void PaymentRequest::HideIfNecessary() {
386 display_handle_.reset();
387}
388
Rouslan Solomakhind2cae95a2018-08-09 00:16:10389void PaymentRequest::RecordDialogShownEventInJourneyLogger() {
390 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
391}
392
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04393bool PaymentRequest::IsIncognito() const {
394 return delegate_->IsIncognito();
395}
396
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:36397bool PaymentRequest::SatisfiesSkipUIConstraints() const {
398 return base::FeatureList::IsEnabled(features::kWebPaymentsSingleAppUiSkip) &&
399 base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps) &&
Rouslan Solomakhin833f8512018-04-03 23:19:25400 is_show_user_gesture_ && state()->is_get_all_instruments_finished() &&
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:36401 state()->available_instruments().size() == 1 &&
402 spec()->stringified_method_data().size() == 1 &&
403 !spec()->request_shipping() && !spec()->request_payer_name() &&
404 !spec()->request_payer_phone() &&
405 !spec()->request_payer_email()
406 // Only allowing URL base payment apps to skip the payment sheet.
407 && spec()->url_payment_method_identifiers().size() == 1;
408}
409
sebsgfcdd13c2017-06-08 15:49:33410void PaymentRequest::RecordFirstAbortReason(
411 JourneyLogger::AbortReason abort_reason) {
412 if (!has_recorded_completion_) {
413 has_recorded_completion_ = true;
414 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10415 }
416}
417
Rouslan Solomakhin1804ee42017-10-03 14:27:43418void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10419 if (!spec_ || CanMakePaymentQueryFactory::GetInstance()
420 ->GetForContext(web_contents_->GetBrowserContext())
421 ->CanQuery(top_level_origin_, frame_origin_,
422 spec_->stringified_method_data())) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42423 RespondToCanMakePaymentQuery(can_make_payment, false);
Rouslan Solomakhin1804ee42017-10-03 14:27:43424 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42425 RespondToCanMakePaymentQuery(can_make_payment, true);
Rouslan Solomakhin1804ee42017-10-03 14:27:43426 } else {
427 client_->OnCanMakePayment(
428 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
429 }
430
431 if (observer_for_testing_)
432 observer_for_testing_->OnCanMakePaymentReturned();
433}
434
Rouslan Solomakhin16ee55202017-10-03 19:04:42435void PaymentRequest::RespondToCanMakePaymentQuery(bool can_make_payment,
436 bool warn_localhost_or_file) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42437 mojom::CanMakePaymentQueryResult positive =
438 warn_localhost_or_file
439 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
440 : mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT;
441 mojom::CanMakePaymentQueryResult negative =
442 warn_localhost_or_file
443 ? mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT
444 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT;
445
446 client_->OnCanMakePayment(can_make_payment ? positive : negative);
Rouslan Solomakhin1804ee42017-10-03 14:27:43447 journey_logger_.SetCanMakePaymentValue(can_make_payment);
448}
449
mathpf709499d2017-01-09 20:48:36450} // namespace payments