blob: b887d66a5819668c92296b58acbc9c558dd22425 [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"
Mohamad Ahmadif5544bb2017-09-01 21:48:2214#include "components/payments/content/payment_request_converter.h"
rouslan908248c2017-02-27 21:30:2415#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"
Mohamad Ahmadif5544bb2017-09-01 21:48:2217#include "components/payments/core/payment_details.h"
18#include "components/payments/core/payment_details_validation.h"
anthonyvd6a43b932017-05-11 18:39:2719#include "components/payments/core/payment_prefs.h"
20#include "components/prefs/pref_service.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3921#include "components/url_formatter/elide_url.h"
mathpf709499d2017-01-09 20:48:3622#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3923#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3624#include "content/public/browser/web_contents.h"
25
26namespace payments {
27
28PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3929 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3630 content::WebContents* web_contents,
31 std::unique_ptr<PaymentRequestDelegate> delegate,
32 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2833 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3734 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3635 : web_contents_(web_contents),
36 delegate_(std::move(delegate)),
37 manager_(manager),
mathp300fa542017-03-27 19:29:3738 binding_(this, std::move(request)),
Rouslan Solomakhin6e979ab2017-08-30 17:30:3939 top_level_origin_(url_formatter::FormatUrlForSecurityDisplay(
40 web_contents_->GetLastCommittedURL())),
41 frame_origin_(url_formatter::FormatUrlForSecurityDisplay(
42 render_frame_host->GetLastCommittedURL())),
sebsg20b49d7b2017-05-04 20:23:1743 observer_for_testing_(observer_for_testing),
44 journey_logger_(delegate_->IsIncognito(),
45 web_contents_->GetLastCommittedURL(),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1346 delegate_->GetUkmRecorder()),
47 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5048 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
49 // will happen as a result of many renderer-side events (both successful and
50 // erroneous in nature).
51 // TODO(crbug.com/683636): Investigate using
52 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
53 binding_.set_connection_error_handler(base::Bind(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1354 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3655}
56
57PaymentRequest::~PaymentRequest() {}
58
rouslan6e3cf7c62017-04-17 21:23:2859void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
60 std::vector<mojom::PaymentMethodDataPtr> method_data,
61 mojom::PaymentDetailsPtr details,
62 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3663 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2864 client_ = std::move(client);
65
rouslanb28f4532017-05-08 15:41:4766 const GURL last_committed_url = delegate_->GetLastCommittedURL();
67 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2868 LOG(ERROR) << "Not in a secure origin";
69 OnConnectionTerminated();
70 return;
71 }
72
rouslanb28f4532017-05-08 15:41:4773 bool allowed_origin =
74 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
75 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
76 if (!allowed_origin) {
77 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
78 "allowed";
79 }
80
81 bool invalid_ssl =
82 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
83 !delegate_->IsSslCertificateValid();
84 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2885 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4786
87 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2888 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
89 // with "NotSupportedError".
90 spec_ = base::MakeUnique<PaymentRequestSpec>(
91 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
92 std::vector<mojom::PaymentMethodDataPtr>(), this,
93 delegate_->GetApplicationLocale());
94 state_ = base::MakeUnique<PaymentRequestState>(
95 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:4796 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
rouslan6e3cf7c62017-04-17 21:23:2897 return;
98 }
99
mathpf709499d2017-01-09 20:48:36100 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22101 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathpf709499d2017-01-09 20:48:36102 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:50103 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36104 return;
105 }
rouslan6e3cf7c62017-04-17 21:23:28106
jinho.bangfcb5ec92017-03-29 08:08:02107 if (!details->total) {
108 LOG(ERROR) << "Missing total";
109 OnConnectionTerminated();
110 return;
111 }
rouslan6e3cf7c62017-04-17 21:23:28112
mathpf1a7a3752017-03-15 11:23:37113 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33114 std::move(options), std::move(details), std::move(method_data), this,
115 delegate_->GetApplicationLocale());
116 state_ = base::MakeUnique<PaymentRequestState>(
117 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:47118 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
Mathieu Perreault627b97c2017-08-12 00:44:22119
120 journey_logger_.SetRequestedInformation(
121 spec_->request_shipping(), spec_->request_payer_email(),
122 spec_->request_payer_phone(), spec_->request_payer_name());
123
124 // Log metrics around which payment methods are requested by the merchant.
125 GURL google_pay_url(kGooglePayMethodName);
126 GURL android_pay_url(kAndroidPayMethodName);
127 // Looking for payment methods that are NOT google-related payment methods.
128 auto non_google_it =
129 std::find_if(spec_->url_payment_method_identifiers().begin(),
130 spec_->url_payment_method_identifiers().end(),
131 [google_pay_url, android_pay_url](const GURL& url) {
132 return url != google_pay_url && url != android_pay_url;
133 });
134 journey_logger_.SetRequestedPaymentMethodTypes(
135 /*requested_basic_card=*/!spec_->supported_card_networks().empty(),
136 /*requested_method_google=*/
137 base::ContainsValue(spec_->url_payment_method_identifiers(),
138 google_pay_url) ||
139 base::ContainsValue(spec_->url_payment_method_identifiers(),
140 android_pay_url),
141 /*requested_method_other=*/non_google_it !=
142 spec_->url_payment_method_identifiers().end());
mathpf709499d2017-01-09 20:48:36143}
144
145void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10146 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50147 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
148 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10149 return;
150 }
rouslan6e3cf7c62017-04-17 21:23:28151
rouslan7d433cc22017-05-08 15:18:07152 // A tab can display only one PaymentRequest UI at a time.
153 if (!manager_->CanShow(this)) {
154 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12155 journey_logger_.SetNotShown(
156 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
rouslan7d433cc22017-05-08 15:18:07157 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
158 OnConnectionTerminated();
159 return;
160 }
161
rouslan6e3cf7c62017-04-17 21:23:28162 if (!state_->AreRequestedMethodsSupported()) {
sebsg828269bc2017-06-09 19:11:12163 journey_logger_.SetNotShown(
164 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28165 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
166 if (observer_for_testing_)
167 observer_for_testing_->OnNotSupportedError();
168 OnConnectionTerminated();
169 return;
170 }
171
mathp57c8c862017-06-16 20:15:45172 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
sebsg8f4fa4d2017-06-13 15:25:45173
mathpf4bc50e2017-01-24 05:17:50174 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36175}
176
mathp151bd312017-04-03 21:07:24177void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
178 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22179 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd312017-04-03 21:07:24180 LOG(ERROR) << error;
181 OnConnectionTerminated();
182 return;
183 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39184
185 if (!details->total) {
186 LOG(ERROR) << "Missing total";
187 OnConnectionTerminated();
188 return;
189 }
190
mathp151bd312017-04-03 21:07:24191 spec_->UpdateWith(std::move(details));
192}
193
mathpf4bc50e2017-01-24 05:17:50194void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56195 // The API user has decided to abort. If a successful abort message is
196 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50197 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56198 // Otherwise, the abort promise is rejected and the pipe is not closed.
199 // The abort is only successful if the payment app wasn't yet invoked.
200 // TODO(crbug.com/716546): Add a merchant abort metric
201
202 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33203 if (accepting_abort)
204 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56205
mathpf4bc50e2017-01-24 05:17:50206 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56207 client_->OnAbort(accepting_abort);
208
209 if (observer_for_testing_)
210 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50211}
212
mathp218795892017-03-29 15:15:34213void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16214 if (!client_.is_bound())
215 return;
216
Rouslan Solomakhine3473192017-06-16 14:54:57217 // Failed transactions show an error. Successful and unknown-state
218 // transactions don't show an error.
219 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34220 delegate_->ShowErrorMessage();
221 } else {
sebsgfcdd13c2017-06-08 15:49:33222 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58223 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33224 has_recorded_completion_ = true;
225
anthonyvd6a43b932017-05-11 18:39:27226 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
227 true);
mathp218795892017-03-29 15:15:34228 // When the renderer closes the connection,
229 // PaymentRequest::OnConnectionTerminated will be called.
230 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22231 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34232 }
mathp4b85b582017-03-08 21:07:16233}
234
235void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39236 bool can_make_payment = state()->CanMakePayment();
237 if (delegate_->IsIncognito()) {
238 client_->OnCanMakePayment(
239 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
240 journey_logger_.SetCanMakePaymentValue(true);
241 } else if (CanMakePaymentQueryFactory::GetInstance()
242 ->GetForContext(web_contents_->GetBrowserContext())
Rouslan Solomakhin115f7232017-08-01 15:24:38243 ->CanQuery(top_level_origin_, frame_origin_,
244 spec()->stringified_method_data())) {
rouslan690997682017-05-09 18:07:39245 client_->OnCanMakePayment(
246 can_make_payment
247 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
248 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
249 journey_logger_.SetCanMakePaymentValue(can_make_payment);
Rouslan Solomakhin6e979ab2017-08-30 17:30:39250 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
rouslan690997682017-05-09 18:07:39251 client_->OnCanMakePayment(
252 can_make_payment
253 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
254 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
255 journey_logger_.SetCanMakePaymentValue(can_make_payment);
256 } else {
257 client_->OnCanMakePayment(
258 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
259 }
260
mathp300fa542017-03-27 19:29:37261 if (observer_for_testing_)
262 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16263}
264
mathpf1a7a3752017-03-15 11:23:37265void PaymentRequest::OnPaymentResponseAvailable(
266 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45267 journey_logger_.SetEventOccurred(
268 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37269 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16270}
271
mathp151bd312017-04-03 21:07:24272void PaymentRequest::OnShippingOptionIdSelected(
273 std::string shipping_option_id) {
274 client_->OnShippingOptionChange(shipping_option_id);
275}
276
277void PaymentRequest::OnShippingAddressSelected(
278 mojom::PaymentAddressPtr address) {
279 client_->OnShippingAddressChange(std::move(address));
280}
281
mathpf4bc50e2017-01-24 05:17:50282void PaymentRequest::UserCancelled() {
283 // If |client_| is not bound, then the object is already being destroyed as
284 // a result of a renderer event.
285 if (!client_.is_bound())
286 return;
287
sebsgfcdd13c2017-06-08 15:49:33288 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17289
mathpf4bc50e2017-01-24 05:17:50290 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28291 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50292
293 // We close all bindings and ask to be destroyed.
294 client_.reset();
295 binding_.Close();
rouslanb28f4532017-05-08 15:41:47296 if (observer_for_testing_)
297 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50298 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36299}
300
sebsg2c8558a2017-05-17 18:54:10301void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33302 RecordFirstAbortReason(is_user_initiated
303 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
304 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10305}
306
mathpf4bc50e2017-01-24 05:17:50307void PaymentRequest::OnConnectionTerminated() {
308 // We are here because of a browser-side error, or likely as a result of the
309 // connection_error_handler on |binding_|, which can mean that the renderer
310 // has decided to close the pipe for various reasons (see all uses of
311 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
312 // the binding and the dialog, and ask to be deleted.
313 client_.reset();
mathpf709499d2017-01-09 20:48:36314 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50315 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47316 if (observer_for_testing_)
317 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33318
319 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36320 manager_->DestroyRequest(this);
321}
322
mathpd4be8de82017-03-01 00:51:48323void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45324 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault49a97e52017-08-15 01:51:36325 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SELECTED_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37326 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48327}
328
sebsgfcdd13c2017-06-08 15:49:33329void PaymentRequest::RecordFirstAbortReason(
330 JourneyLogger::AbortReason abort_reason) {
331 if (!has_recorded_completion_) {
332 has_recorded_completion_ = true;
333 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10334 }
335}
336
mathpf709499d2017-01-09 20:48:36337} // namespace payments