blob: 342e457a7176ef39b15ed9acc7c5d92503924006 [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
tmartino68c0a272017-01-19 17:44:0810#include "base/memory/ptr_util.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"
rouslan6e3cf7c62017-04-17 21:23:2813#include "components/payments/content/origin_security_checker.h"
rouslan908248c2017-02-27 21:30:2414#include "components/payments/content/payment_details_validation.h"
15#include "components/payments/content/payment_request_web_contents_manager.h"
rouslan690997682017-05-09 18:07:3916#include "components/payments/core/can_make_payment_query.h"
anthonyvd6a43b932017-05-11 18:39:2717#include "components/payments/core/payment_prefs.h"
18#include "components/prefs/pref_service.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3919#include "components/url_formatter/elide_url.h"
mathpf709499d2017-01-09 20:48:3620#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3921#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3622#include "content/public/browser/web_contents.h"
23
24namespace payments {
25
26PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3927 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3628 content::WebContents* web_contents,
29 std::unique_ptr<PaymentRequestDelegate> delegate,
30 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2831 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3732 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3633 : web_contents_(web_contents),
34 delegate_(std::move(delegate)),
35 manager_(manager),
mathp300fa542017-03-27 19:29:3736 binding_(this, std::move(request)),
Rouslan Solomakhin6e979ab2017-08-30 17:30:3937 top_level_origin_(url_formatter::FormatUrlForSecurityDisplay(
38 web_contents_->GetLastCommittedURL())),
39 frame_origin_(url_formatter::FormatUrlForSecurityDisplay(
40 render_frame_host->GetLastCommittedURL())),
sebsg20b49d7b2017-05-04 20:23:1741 observer_for_testing_(observer_for_testing),
42 journey_logger_(delegate_->IsIncognito(),
43 web_contents_->GetLastCommittedURL(),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1344 delegate_->GetUkmRecorder()),
45 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5046 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
47 // will happen as a result of many renderer-side events (both successful and
48 // erroneous in nature).
49 // TODO(crbug.com/683636): Investigate using
50 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
51 binding_.set_connection_error_handler(base::Bind(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1352 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3653}
54
55PaymentRequest::~PaymentRequest() {}
56
rouslan6e3cf7c62017-04-17 21:23:2857void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
58 std::vector<mojom::PaymentMethodDataPtr> method_data,
59 mojom::PaymentDetailsPtr details,
60 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3661 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2862 client_ = std::move(client);
63
rouslanb28f4532017-05-08 15:41:4764 const GURL last_committed_url = delegate_->GetLastCommittedURL();
65 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2866 LOG(ERROR) << "Not in a secure origin";
67 OnConnectionTerminated();
68 return;
69 }
70
rouslanb28f4532017-05-08 15:41:4771 bool allowed_origin =
72 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
73 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
74 if (!allowed_origin) {
75 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
76 "allowed";
77 }
78
79 bool invalid_ssl =
80 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
81 !delegate_->IsSslCertificateValid();
82 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2883 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4784
85 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2886 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
87 // with "NotSupportedError".
88 spec_ = base::MakeUnique<PaymentRequestSpec>(
89 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
90 std::vector<mojom::PaymentMethodDataPtr>(), this,
91 delegate_->GetApplicationLocale());
92 state_ = base::MakeUnique<PaymentRequestState>(
93 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:4794 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
rouslan6e3cf7c62017-04-17 21:23:2895 return;
96 }
97
mathpf709499d2017-01-09 20:48:3698 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2899 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:36100 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:50101 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36102 return;
103 }
rouslan6e3cf7c62017-04-17 21:23:28104
jinho.bangfcb5ec92017-03-29 08:08:02105 if (!details->total) {
106 LOG(ERROR) << "Missing total";
107 OnConnectionTerminated();
108 return;
109 }
rouslan6e3cf7c62017-04-17 21:23:28110
mathpf1a7a3752017-03-15 11:23:37111 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33112 std::move(options), std::move(details), std::move(method_data), this,
113 delegate_->GetApplicationLocale());
114 state_ = base::MakeUnique<PaymentRequestState>(
115 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:47116 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
Mathieu Perreault627b97c2017-08-12 00:44:22117
118 journey_logger_.SetRequestedInformation(
119 spec_->request_shipping(), spec_->request_payer_email(),
120 spec_->request_payer_phone(), spec_->request_payer_name());
121
122 // Log metrics around which payment methods are requested by the merchant.
123 GURL google_pay_url(kGooglePayMethodName);
124 GURL android_pay_url(kAndroidPayMethodName);
125 // Looking for payment methods that are NOT google-related payment methods.
126 auto non_google_it =
127 std::find_if(spec_->url_payment_method_identifiers().begin(),
128 spec_->url_payment_method_identifiers().end(),
129 [google_pay_url, android_pay_url](const GURL& url) {
130 return url != google_pay_url && url != android_pay_url;
131 });
132 journey_logger_.SetRequestedPaymentMethodTypes(
133 /*requested_basic_card=*/!spec_->supported_card_networks().empty(),
134 /*requested_method_google=*/
135 base::ContainsValue(spec_->url_payment_method_identifiers(),
136 google_pay_url) ||
137 base::ContainsValue(spec_->url_payment_method_identifiers(),
138 android_pay_url),
139 /*requested_method_other=*/non_google_it !=
140 spec_->url_payment_method_identifiers().end());
mathpf709499d2017-01-09 20:48:36141}
142
143void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10144 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50145 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
146 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10147 return;
148 }
rouslan6e3cf7c62017-04-17 21:23:28149
rouslan7d433cc22017-05-08 15:18:07150 // A tab can display only one PaymentRequest UI at a time.
151 if (!manager_->CanShow(this)) {
152 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12153 journey_logger_.SetNotShown(
154 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
rouslan7d433cc22017-05-08 15:18:07155 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
156 OnConnectionTerminated();
157 return;
158 }
159
rouslan6e3cf7c62017-04-17 21:23:28160 if (!state_->AreRequestedMethodsSupported()) {
sebsg828269bc2017-06-09 19:11:12161 journey_logger_.SetNotShown(
162 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28163 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
164 if (observer_for_testing_)
165 observer_for_testing_->OnNotSupportedError();
166 OnConnectionTerminated();
167 return;
168 }
169
mathp57c8c862017-06-16 20:15:45170 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
sebsg8f4fa4d2017-06-13 15:25:45171
mathpf4bc50e2017-01-24 05:17:50172 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36173}
174
mathp151bd31e2017-04-03 21:07:24175void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
176 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28177 if (!validatePaymentDetails(details, &error)) {
mathp151bd31e2017-04-03 21:07:24178 LOG(ERROR) << error;
179 OnConnectionTerminated();
180 return;
181 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39182
183 if (!details->total) {
184 LOG(ERROR) << "Missing total";
185 OnConnectionTerminated();
186 return;
187 }
188
mathp151bd31e2017-04-03 21:07:24189 spec_->UpdateWith(std::move(details));
190}
191
mathpf4bc50e2017-01-24 05:17:50192void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56193 // The API user has decided to abort. If a successful abort message is
194 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50195 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56196 // Otherwise, the abort promise is rejected and the pipe is not closed.
197 // The abort is only successful if the payment app wasn't yet invoked.
198 // TODO(crbug.com/716546): Add a merchant abort metric
199
200 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33201 if (accepting_abort)
202 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56203
mathpf4bc50e2017-01-24 05:17:50204 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56205 client_->OnAbort(accepting_abort);
206
207 if (observer_for_testing_)
208 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50209}
210
mathp218795892017-03-29 15:15:34211void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16212 if (!client_.is_bound())
213 return;
214
Rouslan Solomakhine3473192017-06-16 14:54:57215 // Failed transactions show an error. Successful and unknown-state
216 // transactions don't show an error.
217 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34218 delegate_->ShowErrorMessage();
219 } else {
sebsgfcdd13c2017-06-08 15:49:33220 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58221 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33222 has_recorded_completion_ = true;
223
anthonyvd6a43b932017-05-11 18:39:27224 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
225 true);
mathp218795892017-03-29 15:15:34226 // When the renderer closes the connection,
227 // PaymentRequest::OnConnectionTerminated will be called.
228 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22229 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34230 }
mathp4b85b582017-03-08 21:07:16231}
232
233void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39234 bool can_make_payment = state()->CanMakePayment();
235 if (delegate_->IsIncognito()) {
236 client_->OnCanMakePayment(
237 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
238 journey_logger_.SetCanMakePaymentValue(true);
239 } else if (CanMakePaymentQueryFactory::GetInstance()
240 ->GetForContext(web_contents_->GetBrowserContext())
Rouslan Solomakhin115f7232017-08-01 15:24:38241 ->CanQuery(top_level_origin_, frame_origin_,
242 spec()->stringified_method_data())) {
rouslan690997682017-05-09 18:07:39243 client_->OnCanMakePayment(
244 can_make_payment
245 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
246 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
247 journey_logger_.SetCanMakePaymentValue(can_make_payment);
Rouslan Solomakhin6e979ab2017-08-30 17:30:39248 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
rouslan690997682017-05-09 18:07:39249 client_->OnCanMakePayment(
250 can_make_payment
251 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
252 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
253 journey_logger_.SetCanMakePaymentValue(can_make_payment);
254 } else {
255 client_->OnCanMakePayment(
256 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
257 }
258
mathp300fa542017-03-27 19:29:37259 if (observer_for_testing_)
260 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16261}
262
mathpf1a7a3752017-03-15 11:23:37263void PaymentRequest::OnPaymentResponseAvailable(
264 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45265 journey_logger_.SetEventOccurred(
266 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37267 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16268}
269
mathp151bd31e2017-04-03 21:07:24270void PaymentRequest::OnShippingOptionIdSelected(
271 std::string shipping_option_id) {
272 client_->OnShippingOptionChange(shipping_option_id);
273}
274
275void PaymentRequest::OnShippingAddressSelected(
276 mojom::PaymentAddressPtr address) {
277 client_->OnShippingAddressChange(std::move(address));
278}
279
mathpf4bc50e2017-01-24 05:17:50280void PaymentRequest::UserCancelled() {
281 // If |client_| is not bound, then the object is already being destroyed as
282 // a result of a renderer event.
283 if (!client_.is_bound())
284 return;
285
sebsgfcdd13c2017-06-08 15:49:33286 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17287
mathpf4bc50e2017-01-24 05:17:50288 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28289 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50290
291 // We close all bindings and ask to be destroyed.
292 client_.reset();
293 binding_.Close();
rouslanb28f4532017-05-08 15:41:47294 if (observer_for_testing_)
295 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50296 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36297}
298
sebsg2c8558a2017-05-17 18:54:10299void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33300 RecordFirstAbortReason(is_user_initiated
301 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
302 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10303}
304
mathpf4bc50e2017-01-24 05:17:50305void PaymentRequest::OnConnectionTerminated() {
306 // We are here because of a browser-side error, or likely as a result of the
307 // connection_error_handler on |binding_|, which can mean that the renderer
308 // has decided to close the pipe for various reasons (see all uses of
309 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
310 // the binding and the dialog, and ask to be deleted.
311 client_.reset();
mathpf709499d2017-01-09 20:48:36312 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50313 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47314 if (observer_for_testing_)
315 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33316
317 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36318 manager_->DestroyRequest(this);
319}
320
mathpd4be8de82017-03-01 00:51:48321void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45322 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault49a97e52017-08-15 01:51:36323 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SELECTED_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37324 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48325}
326
sebsgfcdd13c2017-06-08 15:49:33327void PaymentRequest::RecordFirstAbortReason(
328 JourneyLogger::AbortReason abort_reason) {
329 if (!has_recorded_completion_) {
330 has_recorded_completion_ = true;
331 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10332 }
333}
334
mathpf709499d2017-01-09 20:48:36335} // namespace payments