blob: f88555a1a86df3110155487ba78daa32b3a70292 [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"
tmartino68c0a272017-01-19 17:44:0811#include "base/memory/ptr_util.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"
Mohamad Ahmadif5544bb2017-09-01 21:48:2219#include "components/payments/core/payment_details.h"
20#include "components/payments/core/payment_details_validation.h"
anthonyvd6a43b932017-05-11 18:39:2721#include "components/payments/core/payment_prefs.h"
22#include "components/prefs/pref_service.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3923#include "components/url_formatter/elide_url.h"
mathpf709499d2017-01-09 20:48:3624#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3925#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3626#include "content/public/browser/web_contents.h"
Rouslan Solomakhin1804ee42017-10-03 14:27:4327#include "content/public/common/content_features.h"
mathpf709499d2017-01-09 20:48:3628
29namespace payments {
30
31PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3932 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3633 content::WebContents* web_contents,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5134 std::unique_ptr<ContentPaymentRequestDelegate> delegate,
mathpf709499d2017-01-09 20:48:3635 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2836 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3737 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3638 : web_contents_(web_contents),
39 delegate_(std::move(delegate)),
40 manager_(manager),
mathp300fa542017-03-27 19:29:3741 binding_(this, std::move(request)),
Rouslan Solomakhin6e979ab2017-08-30 17:30:3942 top_level_origin_(url_formatter::FormatUrlForSecurityDisplay(
43 web_contents_->GetLastCommittedURL())),
44 frame_origin_(url_formatter::FormatUrlForSecurityDisplay(
45 render_frame_host->GetLastCommittedURL())),
sebsg20b49d7b2017-05-04 20:23:1746 observer_for_testing_(observer_for_testing),
47 journey_logger_(delegate_->IsIncognito(),
48 web_contents_->GetLastCommittedURL(),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1349 delegate_->GetUkmRecorder()),
50 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5051 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
52 // will happen as a result of many renderer-side events (both successful and
53 // erroneous in nature).
54 // TODO(crbug.com/683636): Investigate using
55 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
56 binding_.set_connection_error_handler(base::Bind(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1357 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3658}
59
60PaymentRequest::~PaymentRequest() {}
61
rouslan6e3cf7c62017-04-17 21:23:2862void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
63 std::vector<mojom::PaymentMethodDataPtr> method_data,
64 mojom::PaymentDetailsPtr details,
65 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3666 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2867 client_ = std::move(client);
68
rouslanb28f4532017-05-08 15:41:4769 const GURL last_committed_url = delegate_->GetLastCommittedURL();
70 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2871 LOG(ERROR) << "Not in a secure origin";
72 OnConnectionTerminated();
73 return;
74 }
75
rouslanb28f4532017-05-08 15:41:4776 bool allowed_origin =
77 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
78 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
79 if (!allowed_origin) {
80 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
81 "allowed";
82 }
83
84 bool invalid_ssl =
85 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
86 !delegate_->IsSslCertificateValid();
87 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2888 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4789
90 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2891 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
92 // with "NotSupportedError".
93 spec_ = base::MakeUnique<PaymentRequestSpec>(
94 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
95 std::vector<mojom::PaymentMethodDataPtr>(), this,
96 delegate_->GetApplicationLocale());
97 state_ = base::MakeUnique<PaymentRequestState>(
gogerald7a0cc3e2017-09-19 03:35:4898 web_contents_->GetBrowserContext(), top_level_origin_, frame_origin_,
99 spec_.get(), this, delegate_->GetApplicationLocale(),
100 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
rouslan6e3cf7c62017-04-17 21:23:28101 return;
102 }
103
mathpf709499d2017-01-09 20:48:36104 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22105 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathpf709499d2017-01-09 20:48:36106 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:50107 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36108 return;
109 }
rouslan6e3cf7c62017-04-17 21:23:28110
jinho.bangfcb5ec92017-03-29 08:08:02111 if (!details->total) {
112 LOG(ERROR) << "Missing total";
113 OnConnectionTerminated();
114 return;
115 }
rouslan6e3cf7c62017-04-17 21:23:28116
mathpf1a7a3752017-03-15 11:23:37117 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33118 std::move(options), std::move(details), std::move(method_data), this,
119 delegate_->GetApplicationLocale());
120 state_ = base::MakeUnique<PaymentRequestState>(
gogerald7a0cc3e2017-09-19 03:35:48121 web_contents_->GetBrowserContext(), top_level_origin_, frame_origin_,
122 spec_.get(), this, delegate_->GetApplicationLocale(),
123 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
Mathieu Perreault627b97c2017-08-12 00:44:22124
125 journey_logger_.SetRequestedInformation(
126 spec_->request_shipping(), spec_->request_payer_email(),
127 spec_->request_payer_phone(), spec_->request_payer_name());
128
129 // Log metrics around which payment methods are requested by the merchant.
130 GURL google_pay_url(kGooglePayMethodName);
131 GURL android_pay_url(kAndroidPayMethodName);
132 // Looking for payment methods that are NOT google-related payment methods.
133 auto non_google_it =
134 std::find_if(spec_->url_payment_method_identifiers().begin(),
135 spec_->url_payment_method_identifiers().end(),
136 [google_pay_url, android_pay_url](const GURL& url) {
137 return url != google_pay_url && url != android_pay_url;
138 });
139 journey_logger_.SetRequestedPaymentMethodTypes(
140 /*requested_basic_card=*/!spec_->supported_card_networks().empty(),
141 /*requested_method_google=*/
142 base::ContainsValue(spec_->url_payment_method_identifiers(),
143 google_pay_url) ||
144 base::ContainsValue(spec_->url_payment_method_identifiers(),
145 android_pay_url),
146 /*requested_method_other=*/non_google_it !=
147 spec_->url_payment_method_identifiers().end());
mathpf709499d2017-01-09 20:48:36148}
149
150void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10151 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50152 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
153 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10154 return;
155 }
rouslan6e3cf7c62017-04-17 21:23:28156
rouslan7d433cc22017-05-08 15:18:07157 // A tab can display only one PaymentRequest UI at a time.
158 if (!manager_->CanShow(this)) {
159 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12160 journey_logger_.SetNotShown(
161 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
rouslan7d433cc22017-05-08 15:18:07162 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
163 OnConnectionTerminated();
164 return;
165 }
166
Rouslan Solomakhin5b510432017-09-26 16:59:32167 if (!delegate_->IsBrowserWindowActive()) {
168 LOG(ERROR) << "Cannot show PaymentRequest UI in a background tab";
169 journey_logger_.SetNotShown(JourneyLogger::NOT_SHOWN_REASON_OTHER);
170 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
171 OnConnectionTerminated();
172 return;
173 }
174
rouslan6e3cf7c62017-04-17 21:23:28175 if (!state_->AreRequestedMethodsSupported()) {
sebsg828269bc2017-06-09 19:11:12176 journey_logger_.SetNotShown(
177 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28178 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
179 if (observer_for_testing_)
180 observer_for_testing_->OnNotSupportedError();
181 OnConnectionTerminated();
182 return;
183 }
184
mathp57c8c862017-06-16 20:15:45185 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
sebsg8f4fa4d2017-06-13 15:25:45186
mathpf4bc50e2017-01-24 05:17:50187 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36188}
189
mathp151bd31e2017-04-03 21:07:24190void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
191 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22192 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd31e2017-04-03 21:07:24193 LOG(ERROR) << error;
194 OnConnectionTerminated();
195 return;
196 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39197
198 if (!details->total) {
199 LOG(ERROR) << "Missing total";
200 OnConnectionTerminated();
201 return;
202 }
203
mathp151bd31e2017-04-03 21:07:24204 spec_->UpdateWith(std::move(details));
205}
206
mathpf4bc50e2017-01-24 05:17:50207void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56208 // The API user has decided to abort. If a successful abort message is
209 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50210 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56211 // Otherwise, the abort promise is rejected and the pipe is not closed.
212 // The abort is only successful if the payment app wasn't yet invoked.
213 // TODO(crbug.com/716546): Add a merchant abort metric
214
215 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33216 if (accepting_abort)
217 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56218
mathpf4bc50e2017-01-24 05:17:50219 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56220 client_->OnAbort(accepting_abort);
221
222 if (observer_for_testing_)
223 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50224}
225
mathp218795892017-03-29 15:15:34226void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16227 if (!client_.is_bound())
228 return;
229
Rouslan Solomakhine3473192017-06-16 14:54:57230 // Failed transactions show an error. Successful and unknown-state
231 // transactions don't show an error.
232 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34233 delegate_->ShowErrorMessage();
234 } else {
sebsgfcdd13c2017-06-08 15:49:33235 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58236 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33237 has_recorded_completion_ = true;
238
anthonyvd6a43b932017-05-11 18:39:27239 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
240 true);
mathp218795892017-03-29 15:15:34241 // When the renderer closes the connection,
242 // PaymentRequest::OnConnectionTerminated will be called.
243 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22244 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34245 }
mathp4b85b582017-03-08 21:07:16246}
247
248void PaymentRequest::CanMakePayment() {
gogerald8189d522017-09-15 17:52:18249 state()->CanMakePayment(base::BindOnce(
250 &PaymentRequest::CanMakePaymentCallback, weak_ptr_factory_.GetWeakPtr()));
251
252 if (observer_for_testing_)
253 observer_for_testing_->OnCanMakePaymentCalled();
254}
255
mathpf1a7a3752017-03-15 11:23:37256void PaymentRequest::OnPaymentResponseAvailable(
257 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45258 journey_logger_.SetEventOccurred(
259 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37260 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16261}
262
mathp151bd31e2017-04-03 21:07:24263void PaymentRequest::OnShippingOptionIdSelected(
264 std::string shipping_option_id) {
265 client_->OnShippingOptionChange(shipping_option_id);
266}
267
268void PaymentRequest::OnShippingAddressSelected(
269 mojom::PaymentAddressPtr address) {
270 client_->OnShippingAddressChange(std::move(address));
271}
272
mathpf4bc50e2017-01-24 05:17:50273void PaymentRequest::UserCancelled() {
274 // If |client_| is not bound, then the object is already being destroyed as
275 // a result of a renderer event.
276 if (!client_.is_bound())
277 return;
278
sebsgfcdd13c2017-06-08 15:49:33279 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17280
mathpf4bc50e2017-01-24 05:17:50281 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28282 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50283
284 // We close all bindings and ask to be destroyed.
285 client_.reset();
286 binding_.Close();
rouslanb28f4532017-05-08 15:41:47287 if (observer_for_testing_)
288 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50289 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36290}
291
sebsgd56b3e422017-10-20 18:08:08292void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument(
293 bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33294 RecordFirstAbortReason(is_user_initiated
295 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
296 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10297}
298
mathpf4bc50e2017-01-24 05:17:50299void PaymentRequest::OnConnectionTerminated() {
300 // We are here because of a browser-side error, or likely as a result of the
301 // connection_error_handler on |binding_|, which can mean that the renderer
302 // has decided to close the pipe for various reasons (see all uses of
303 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
304 // the binding and the dialog, and ask to be deleted.
305 client_.reset();
mathpf709499d2017-01-09 20:48:36306 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50307 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47308 if (observer_for_testing_)
309 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33310
311 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36312 manager_->DestroyRequest(this);
313}
314
mathpd4be8de82017-03-01 00:51:48315void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45316 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault49a97e52017-08-15 01:51:36317 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SELECTED_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37318 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48319}
320
sebsgfcdd13c2017-06-08 15:49:33321void PaymentRequest::RecordFirstAbortReason(
322 JourneyLogger::AbortReason abort_reason) {
323 if (!has_recorded_completion_) {
324 has_recorded_completion_ = true;
325 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10326 }
327}
328
Rouslan Solomakhin1804ee42017-10-03 14:27:43329void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42330 if (CanMakePaymentQueryFactory::GetInstance()
331 ->GetForContext(web_contents_->GetBrowserContext())
332 ->CanQuery(top_level_origin_, frame_origin_,
333 spec()->stringified_method_data())) {
334 RespondToCanMakePaymentQuery(can_make_payment, false);
Rouslan Solomakhin1804ee42017-10-03 14:27:43335 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42336 RespondToCanMakePaymentQuery(can_make_payment, true);
Rouslan Solomakhin1804ee42017-10-03 14:27:43337 } else {
338 client_->OnCanMakePayment(
339 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
340 }
341
342 if (observer_for_testing_)
343 observer_for_testing_->OnCanMakePaymentReturned();
344}
345
Rouslan Solomakhin16ee55202017-10-03 19:04:42346void PaymentRequest::RespondToCanMakePaymentQuery(bool can_make_payment,
347 bool warn_localhost_or_file) {
348 if (delegate_->IsIncognito()) {
349 can_make_payment =
350 spec()->HasBasicCardMethodName() ||
351 base::FeatureList::IsEnabled(features::kServiceWorkerPaymentApps);
352 }
353
354 mojom::CanMakePaymentQueryResult positive =
355 warn_localhost_or_file
356 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
357 : mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT;
358 mojom::CanMakePaymentQueryResult negative =
359 warn_localhost_or_file
360 ? mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT
361 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT;
362
363 client_->OnCanMakePayment(can_make_payment ? positive : negative);
Rouslan Solomakhin1804ee42017-10-03 14:27:43364 journey_logger_.SetCanMakePaymentValue(can_make_payment);
365}
366
mathpf709499d2017-01-09 20:48:36367} // namespace payments