blob: 7489cf39871846e2b3f89227fa3030d64ddb2bdf [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>(
Rouslan Solomakhindbf593d92017-11-21 19:20:5798 web_contents_, top_level_origin_, frame_origin_, spec_.get(), this,
99 delegate_->GetApplicationLocale(), delegate_->GetPersonalDataManager(),
100 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>(
Rouslan Solomakhindbf593d92017-11-21 19:20:57121 web_contents_, top_level_origin_, frame_origin_, spec_.get(), this,
122 delegate_->GetApplicationLocale(), delegate_->GetPersonalDataManager(),
123 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
gogerald0a7ee6c2017-11-13 18:23:19175 // TODO(crbug.com/783811): Display a spinner when checking whether
176 // the methods are supported asynchronously for better user experience.
177 state_->AreRequestedMethodsSupported(
178 base::BindOnce(&PaymentRequest::AreRequestedMethodsSupportedCallback,
179 weak_ptr_factory_.GetWeakPtr()));
180}
181
182void PaymentRequest::AreRequestedMethodsSupportedCallback(
183 bool methods_supported) {
184 if (methods_supported) {
185 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
186
187 delegate_->ShowDialog(this);
188 } else {
sebsg828269bc2017-06-09 19:11:12189 journey_logger_.SetNotShown(
190 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28191 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
192 if (observer_for_testing_)
193 observer_for_testing_->OnNotSupportedError();
194 OnConnectionTerminated();
rouslan6e3cf7c62017-04-17 21:23:28195 }
mathpf709499d2017-01-09 20:48:36196}
197
mathp151bd312017-04-03 21:07:24198void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
199 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22200 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd312017-04-03 21:07:24201 LOG(ERROR) << error;
202 OnConnectionTerminated();
203 return;
204 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39205
206 if (!details->total) {
207 LOG(ERROR) << "Missing total";
208 OnConnectionTerminated();
209 return;
210 }
211
mathp151bd312017-04-03 21:07:24212 spec_->UpdateWith(std::move(details));
213}
214
Rouslan Solomakhina9ff9282017-10-31 21:58:05215void PaymentRequest::NoUpdatedPaymentDetails() {
216 spec_->RecomputeSpecForDetails();
217}
218
mathpf4bc50e2017-01-24 05:17:50219void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56220 // The API user has decided to abort. If a successful abort message is
221 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50222 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56223 // Otherwise, the abort promise is rejected and the pipe is not closed.
224 // The abort is only successful if the payment app wasn't yet invoked.
225 // TODO(crbug.com/716546): Add a merchant abort metric
226
227 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33228 if (accepting_abort)
229 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56230
mathpf4bc50e2017-01-24 05:17:50231 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56232 client_->OnAbort(accepting_abort);
233
234 if (observer_for_testing_)
235 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50236}
237
mathp218795892017-03-29 15:15:34238void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16239 if (!client_.is_bound())
240 return;
241
Rouslan Solomakhine3473192017-06-16 14:54:57242 // Failed transactions show an error. Successful and unknown-state
243 // transactions don't show an error.
244 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34245 delegate_->ShowErrorMessage();
246 } else {
sebsgfcdd13c2017-06-08 15:49:33247 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58248 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33249 has_recorded_completion_ = true;
250
anthonyvd6a43b932017-05-11 18:39:27251 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
252 true);
mathp218795892017-03-29 15:15:34253 // When the renderer closes the connection,
254 // PaymentRequest::OnConnectionTerminated will be called.
255 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22256 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34257 }
mathp4b85b582017-03-08 21:07:16258}
259
260void PaymentRequest::CanMakePayment() {
gogerald8189d522017-09-15 17:52:18261 state()->CanMakePayment(base::BindOnce(
262 &PaymentRequest::CanMakePaymentCallback, weak_ptr_factory_.GetWeakPtr()));
263
264 if (observer_for_testing_)
265 observer_for_testing_->OnCanMakePaymentCalled();
266}
267
mathpf1a7a3752017-03-15 11:23:37268void PaymentRequest::OnPaymentResponseAvailable(
269 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45270 journey_logger_.SetEventOccurred(
271 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
gogerald7a2b761e2017-11-09 18:30:19272
273 // Do not send invalid response to client.
274 if (response->method_name.empty() || response->stringified_details.empty()) {
275 RecordFirstAbortReason(
276 JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR);
277 delegate_->ShowErrorMessage();
278 return;
279 }
280
mathpf1a7a3752017-03-15 11:23:37281 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16282}
283
mathp151bd312017-04-03 21:07:24284void PaymentRequest::OnShippingOptionIdSelected(
285 std::string shipping_option_id) {
286 client_->OnShippingOptionChange(shipping_option_id);
287}
288
289void PaymentRequest::OnShippingAddressSelected(
290 mojom::PaymentAddressPtr address) {
291 client_->OnShippingAddressChange(std::move(address));
292}
293
mathpf4bc50e2017-01-24 05:17:50294void PaymentRequest::UserCancelled() {
295 // If |client_| is not bound, then the object is already being destroyed as
296 // a result of a renderer event.
297 if (!client_.is_bound())
298 return;
299
sebsgfcdd13c2017-06-08 15:49:33300 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17301
mathpf4bc50e2017-01-24 05:17:50302 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28303 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50304
305 // We close all bindings and ask to be destroyed.
306 client_.reset();
307 binding_.Close();
rouslanb28f4532017-05-08 15:41:47308 if (observer_for_testing_)
309 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50310 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36311}
312
sebsgd56b3e422017-10-20 18:08:08313void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument(
314 bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33315 RecordFirstAbortReason(is_user_initiated
316 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
317 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10318}
319
mathpf4bc50e2017-01-24 05:17:50320void PaymentRequest::OnConnectionTerminated() {
321 // We are here because of a browser-side error, or likely as a result of the
322 // connection_error_handler on |binding_|, which can mean that the renderer
323 // has decided to close the pipe for various reasons (see all uses of
324 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
325 // the binding and the dialog, and ask to be deleted.
326 client_.reset();
mathpf709499d2017-01-09 20:48:36327 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50328 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47329 if (observer_for_testing_)
330 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33331
332 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36333 manager_->DestroyRequest(this);
334}
335
mathpd4be8de82017-03-01 00:51:48336void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45337 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault49a97e52017-08-15 01:51:36338 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SELECTED_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37339 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48340}
341
sebsgfcdd13c2017-06-08 15:49:33342void PaymentRequest::RecordFirstAbortReason(
343 JourneyLogger::AbortReason abort_reason) {
344 if (!has_recorded_completion_) {
345 has_recorded_completion_ = true;
346 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10347 }
348}
349
Rouslan Solomakhin1804ee42017-10-03 14:27:43350void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42351 if (CanMakePaymentQueryFactory::GetInstance()
352 ->GetForContext(web_contents_->GetBrowserContext())
353 ->CanQuery(top_level_origin_, frame_origin_,
354 spec()->stringified_method_data())) {
355 RespondToCanMakePaymentQuery(can_make_payment, false);
Rouslan Solomakhin1804ee42017-10-03 14:27:43356 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42357 RespondToCanMakePaymentQuery(can_make_payment, true);
Rouslan Solomakhin1804ee42017-10-03 14:27:43358 } else {
359 client_->OnCanMakePayment(
360 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
361 }
362
363 if (observer_for_testing_)
364 observer_for_testing_->OnCanMakePaymentReturned();
365}
366
Rouslan Solomakhin16ee55202017-10-03 19:04:42367void PaymentRequest::RespondToCanMakePaymentQuery(bool can_make_payment,
368 bool warn_localhost_or_file) {
369 if (delegate_->IsIncognito()) {
370 can_make_payment =
371 spec()->HasBasicCardMethodName() ||
372 base::FeatureList::IsEnabled(features::kServiceWorkerPaymentApps);
373 }
374
375 mojom::CanMakePaymentQueryResult positive =
376 warn_localhost_or_file
377 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
378 : mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT;
379 mojom::CanMakePaymentQueryResult negative =
380 warn_localhost_or_file
381 ? mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT
382 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT;
383
384 client_->OnCanMakePayment(can_make_payment ? positive : negative);
Rouslan Solomakhin1804ee42017-10-03 14:27:43385 journey_logger_.SetCanMakePaymentValue(can_make_payment);
386}
387
mathpf709499d2017-01-09 20:48:36388} // namespace payments