blob: 3a4e8389415ee36d7f540e3ea8292b0327d08f41 [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) {
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 Bangcac8d9a02018-08-23 19:47:22210 spec()->UpdateShippingAddressErrors(std::move(errors->shipping_address));
211 spec()->UpdatePayerErrors(std::move(errors->payer));
212 display_handle_->Retry();
Jinho Bangbe463a22018-08-02 10:26:50213}
214
gogerald0a7ee6c2017-11-13 18:23:19215void PaymentRequest::AreRequestedMethodsSupportedCallback(
216 bool methods_supported) {
217 if (methods_supported) {
gogerald2ddfc64c2018-08-23 16:20:21218 if (SatisfiesSkipUIConstraints()) {
219 skipped_payment_request_ui_ = true;
Rouslan Solomakhind2cae95a2018-08-09 00:16:10220 Pay();
gogerald2ddfc64c2018-08-23 16:20:21221 }
gogerald0a7ee6c2017-11-13 18:23:19222 } else {
sebsg828269bc2017-06-09 19:11:12223 journey_logger_.SetNotShown(
224 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28225 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
226 if (observer_for_testing_)
227 observer_for_testing_->OnNotSupportedError();
228 OnConnectionTerminated();
rouslan6e3cf7c62017-04-17 21:23:28229 }
mathpf709499d2017-01-09 20:48:36230}
231
gogerald2ddfc64c2018-08-23 16:20:21232bool PaymentRequest::SatisfiesSkipUIConstraints() const {
233 return base::FeatureList::IsEnabled(features::kWebPaymentsSingleAppUiSkip) &&
234 base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps) &&
235 is_show_user_gesture_ && state()->is_get_all_instruments_finished() &&
236 state()->available_instruments().size() == 1 &&
237 spec()->stringified_method_data().size() == 1 &&
238 !spec()->request_shipping() && !spec()->request_payer_name() &&
239 !spec()->request_payer_phone() &&
240 !spec()->request_payer_email()
241 // Only allowing URL base payment apps to skip the payment sheet.
242 && spec()->url_payment_method_identifiers().size() == 1;
243}
244
mathp151bd312017-04-03 21:07:24245void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
246 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22247 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd312017-04-03 21:07:24248 LOG(ERROR) << error;
249 OnConnectionTerminated();
250 return;
251 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39252
253 if (!details->total) {
254 LOG(ERROR) << "Missing total";
255 OnConnectionTerminated();
256 return;
257 }
258
mathp151bd312017-04-03 21:07:24259 spec_->UpdateWith(std::move(details));
260}
261
Rouslan Solomakhina9ff9282017-10-31 21:58:05262void PaymentRequest::NoUpdatedPaymentDetails() {
263 spec_->RecomputeSpecForDetails();
264}
265
mathpf4bc50e2017-01-24 05:17:50266void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56267 // The API user has decided to abort. If a successful abort message is
268 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50269 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56270 // Otherwise, the abort promise is rejected and the pipe is not closed.
271 // The abort is only successful if the payment app wasn't yet invoked.
272 // TODO(crbug.com/716546): Add a merchant abort metric
273
274 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33275 if (accepting_abort)
276 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56277
mathpf4bc50e2017-01-24 05:17:50278 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56279 client_->OnAbort(accepting_abort);
280
281 if (observer_for_testing_)
282 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50283}
284
mathp218795892017-03-29 15:15:34285void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16286 if (!client_.is_bound())
287 return;
288
Rouslan Solomakhine3473192017-06-16 14:54:57289 // Failed transactions show an error. Successful and unknown-state
290 // transactions don't show an error.
291 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34292 delegate_->ShowErrorMessage();
293 } else {
sebsgfcdd13c2017-06-08 15:49:33294 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58295 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33296 has_recorded_completion_ = true;
297
anthonyvd6a43b932017-05-11 18:39:27298 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
299 true);
mathp218795892017-03-29 15:15:34300 // When the renderer closes the connection,
301 // PaymentRequest::OnConnectionTerminated will be called.
302 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22303 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34304 }
mathp4b85b582017-03-08 21:07:16305}
306
307void PaymentRequest::CanMakePayment() {
gogerald8189d522017-09-15 17:52:18308 if (observer_for_testing_)
309 observer_for_testing_->OnCanMakePaymentCalled();
Mathieu Perreaultcacb85e2018-06-06 20:40:13310
Rouslan Solomakhind2cae95a2018-08-09 00:16:10311 if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) ||
312 !state_) {
Mathieu Perreaultcacb85e2018-06-06 20:40:13313 CanMakePaymentCallback(/*can_make_payment=*/false);
314 } else {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10315 state_->CanMakePayment(
Mathieu Perreaultcacb85e2018-06-06 20:40:13316 base::BindOnce(&PaymentRequest::CanMakePaymentCallback,
317 weak_ptr_factory_.GetWeakPtr()));
318 }
gogerald8189d522017-09-15 17:52:18319}
320
mathpf1a7a3752017-03-15 11:23:37321void PaymentRequest::OnPaymentResponseAvailable(
322 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45323 journey_logger_.SetEventOccurred(
324 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
gogerald7a2b761e2017-11-09 18:30:19325
326 // Do not send invalid response to client.
327 if (response->method_name.empty() || response->stringified_details.empty()) {
328 RecordFirstAbortReason(
329 JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR);
330 delegate_->ShowErrorMessage();
331 return;
332 }
333
mathpf1a7a3752017-03-15 11:23:37334 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16335}
336
mathp151bd312017-04-03 21:07:24337void PaymentRequest::OnShippingOptionIdSelected(
338 std::string shipping_option_id) {
339 client_->OnShippingOptionChange(shipping_option_id);
340}
341
342void PaymentRequest::OnShippingAddressSelected(
343 mojom::PaymentAddressPtr address) {
344 client_->OnShippingAddressChange(std::move(address));
345}
346
mathpf4bc50e2017-01-24 05:17:50347void PaymentRequest::UserCancelled() {
348 // If |client_| is not bound, then the object is already being destroyed as
349 // a result of a renderer event.
350 if (!client_.is_bound())
351 return;
352
sebsgfcdd13c2017-06-08 15:49:33353 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17354
mathpf4bc50e2017-01-24 05:17:50355 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28356 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50357
358 // We close all bindings and ask to be destroyed.
359 client_.reset();
360 binding_.Close();
rouslanb28f4532017-05-08 15:41:47361 if (observer_for_testing_)
362 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50363 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36364}
365
sebsgd56b3e422017-10-20 18:08:08366void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument(
367 bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33368 RecordFirstAbortReason(is_user_initiated
369 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
370 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10371}
372
mathpf4bc50e2017-01-24 05:17:50373void PaymentRequest::OnConnectionTerminated() {
374 // We are here because of a browser-side error, or likely as a result of the
375 // connection_error_handler on |binding_|, which can mean that the renderer
376 // has decided to close the pipe for various reasons (see all uses of
377 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
378 // the binding and the dialog, and ask to be deleted.
379 client_.reset();
mathpf709499d2017-01-09 20:48:36380 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50381 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47382 if (observer_for_testing_)
383 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33384
385 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36386 manager_->DestroyRequest(this);
387}
388
mathpd4be8de82017-03-01 00:51:48389void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45390 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault23d25bfb82018-05-11 14:45:37391
392 // Log the correct "selected instrument" metric according to type.
393 DCHECK(state_->selected_instrument());
394 JourneyLogger::Event selected_event =
395 JourneyLogger::Event::EVENT_SELECTED_OTHER;
396 switch (state_->selected_instrument()->type()) {
397 case PaymentInstrument::Type::AUTOFILL:
398 selected_event = JourneyLogger::Event::EVENT_SELECTED_CREDIT_CARD;
399 break;
400 case PaymentInstrument::Type::SERVICE_WORKER_APP:
401 selected_event = JourneyLogger::Event::EVENT_SELECTED_OTHER;
402 break;
403 case PaymentInstrument::Type::NATIVE_MOBILE_APP:
404 NOTREACHED();
405 break;
406 }
407 journey_logger_.SetEventOccurred(selected_event);
408
mathpf1a7a3752017-03-15 11:23:37409 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48410}
411
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07412void PaymentRequest::HideIfNecessary() {
413 display_handle_.reset();
414}
415
Rouslan Solomakhind2cae95a2018-08-09 00:16:10416void PaymentRequest::RecordDialogShownEventInJourneyLogger() {
417 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
418}
419
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04420bool PaymentRequest::IsIncognito() const {
421 return delegate_->IsIncognito();
422}
423
sebsgfcdd13c2017-06-08 15:49:33424void PaymentRequest::RecordFirstAbortReason(
425 JourneyLogger::AbortReason abort_reason) {
426 if (!has_recorded_completion_) {
427 has_recorded_completion_ = true;
428 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10429 }
430}
431
Rouslan Solomakhin1804ee42017-10-03 14:27:43432void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10433 if (!spec_ || CanMakePaymentQueryFactory::GetInstance()
434 ->GetForContext(web_contents_->GetBrowserContext())
435 ->CanQuery(top_level_origin_, frame_origin_,
436 spec_->stringified_method_data())) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42437 RespondToCanMakePaymentQuery(can_make_payment, false);
Rouslan Solomakhin1804ee42017-10-03 14:27:43438 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42439 RespondToCanMakePaymentQuery(can_make_payment, true);
Rouslan Solomakhin1804ee42017-10-03 14:27:43440 } else {
441 client_->OnCanMakePayment(
442 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
443 }
444
445 if (observer_for_testing_)
446 observer_for_testing_->OnCanMakePaymentReturned();
447}
448
Rouslan Solomakhin16ee55202017-10-03 19:04:42449void PaymentRequest::RespondToCanMakePaymentQuery(bool can_make_payment,
450 bool warn_localhost_or_file) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42451 mojom::CanMakePaymentQueryResult positive =
452 warn_localhost_or_file
453 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
454 : mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT;
455 mojom::CanMakePaymentQueryResult negative =
456 warn_localhost_or_file
457 ? mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT
458 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT;
459
460 client_->OnCanMakePayment(can_make_payment ? positive : negative);
Rouslan Solomakhin1804ee42017-10-03 14:27:43461 journey_logger_.SetCanMakePaymentValue(can_make_payment);
462}
463
mathpf709499d2017-01-09 20:48:36464} // namespace payments