blob: cbd40a29368290b7599eebec70749e6b16d03648 [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>(
gogerald7a0cc3e2017-09-19 03:35:4895 web_contents_->GetBrowserContext(), top_level_origin_, frame_origin_,
96 spec_.get(), this, delegate_->GetApplicationLocale(),
97 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
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
mathpf1a7a3752017-03-15 11:23:37114 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33115 std::move(options), std::move(details), std::move(method_data), this,
116 delegate_->GetApplicationLocale());
117 state_ = base::MakeUnique<PaymentRequestState>(
gogerald7a0cc3e2017-09-19 03:35:48118 web_contents_->GetBrowserContext(), top_level_origin_, frame_origin_,
119 spec_.get(), this, delegate_->GetApplicationLocale(),
120 delegate_->GetPersonalDataManager(), 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
147void PaymentRequest::Show() {
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.
155 if (!manager_->CanShow(this)) {
156 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12157 journey_logger_.SetNotShown(
158 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
rouslan7d433cc22017-05-08 15:18:07159 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
160 OnConnectionTerminated();
161 return;
162 }
163
rouslan6e3cf7c62017-04-17 21:23:28164 if (!state_->AreRequestedMethodsSupported()) {
sebsg828269bc2017-06-09 19:11:12165 journey_logger_.SetNotShown(
166 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28167 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
168 if (observer_for_testing_)
169 observer_for_testing_->OnNotSupportedError();
170 OnConnectionTerminated();
171 return;
172 }
173
mathp57c8c862017-06-16 20:15:45174 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
sebsg8f4fa4d2017-06-13 15:25:45175
mathpf4bc50e2017-01-24 05:17:50176 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36177}
178
mathp151bd31e2017-04-03 21:07:24179void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
180 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22181 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd31e2017-04-03 21:07:24182 LOG(ERROR) << error;
183 OnConnectionTerminated();
184 return;
185 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39186
187 if (!details->total) {
188 LOG(ERROR) << "Missing total";
189 OnConnectionTerminated();
190 return;
191 }
192
mathp151bd31e2017-04-03 21:07:24193 spec_->UpdateWith(std::move(details));
194}
195
mathpf4bc50e2017-01-24 05:17:50196void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56197 // The API user has decided to abort. If a successful abort message is
198 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50199 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56200 // Otherwise, the abort promise is rejected and the pipe is not closed.
201 // The abort is only successful if the payment app wasn't yet invoked.
202 // TODO(crbug.com/716546): Add a merchant abort metric
203
204 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33205 if (accepting_abort)
206 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56207
mathpf4bc50e2017-01-24 05:17:50208 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56209 client_->OnAbort(accepting_abort);
210
211 if (observer_for_testing_)
212 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50213}
214
mathp218795892017-03-29 15:15:34215void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16216 if (!client_.is_bound())
217 return;
218
Rouslan Solomakhine3473192017-06-16 14:54:57219 // Failed transactions show an error. Successful and unknown-state
220 // transactions don't show an error.
221 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34222 delegate_->ShowErrorMessage();
223 } else {
sebsgfcdd13c2017-06-08 15:49:33224 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58225 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33226 has_recorded_completion_ = true;
227
anthonyvd6a43b932017-05-11 18:39:27228 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
229 true);
mathp218795892017-03-29 15:15:34230 // When the renderer closes the connection,
231 // PaymentRequest::OnConnectionTerminated will be called.
232 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22233 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34234 }
mathp4b85b582017-03-08 21:07:16235}
236
237void PaymentRequest::CanMakePayment() {
gogerald8189d522017-09-15 17:52:18238 state()->CanMakePayment(base::BindOnce(
239 &PaymentRequest::CanMakePaymentCallback, weak_ptr_factory_.GetWeakPtr()));
240
241 if (observer_for_testing_)
242 observer_for_testing_->OnCanMakePaymentCalled();
243}
244
245void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
rouslan690997682017-05-09 18:07:39246 if (delegate_->IsIncognito()) {
247 client_->OnCanMakePayment(
248 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
249 journey_logger_.SetCanMakePaymentValue(true);
250 } else if (CanMakePaymentQueryFactory::GetInstance()
251 ->GetForContext(web_contents_->GetBrowserContext())
Rouslan Solomakhin115f7232017-08-01 15:24:38252 ->CanQuery(top_level_origin_, frame_origin_,
253 spec()->stringified_method_data())) {
rouslan690997682017-05-09 18:07:39254 client_->OnCanMakePayment(
255 can_make_payment
256 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
257 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
258 journey_logger_.SetCanMakePaymentValue(can_make_payment);
Rouslan Solomakhin6e979ab2017-08-30 17:30:39259 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
rouslan690997682017-05-09 18:07:39260 client_->OnCanMakePayment(
261 can_make_payment
262 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
263 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
264 journey_logger_.SetCanMakePaymentValue(can_make_payment);
265 } else {
266 client_->OnCanMakePayment(
267 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
268 }
269
mathp300fa542017-03-27 19:29:37270 if (observer_for_testing_)
gogerald8189d522017-09-15 17:52:18271 observer_for_testing_->OnCanMakePaymentReturned();
mathp4b85b582017-03-08 21:07:16272}
273
mathpf1a7a3752017-03-15 11:23:37274void PaymentRequest::OnPaymentResponseAvailable(
275 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45276 journey_logger_.SetEventOccurred(
277 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37278 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16279}
280
mathp151bd31e2017-04-03 21:07:24281void PaymentRequest::OnShippingOptionIdSelected(
282 std::string shipping_option_id) {
283 client_->OnShippingOptionChange(shipping_option_id);
284}
285
286void PaymentRequest::OnShippingAddressSelected(
287 mojom::PaymentAddressPtr address) {
288 client_->OnShippingAddressChange(std::move(address));
289}
290
mathpf4bc50e2017-01-24 05:17:50291void PaymentRequest::UserCancelled() {
292 // If |client_| is not bound, then the object is already being destroyed as
293 // a result of a renderer event.
294 if (!client_.is_bound())
295 return;
296
sebsgfcdd13c2017-06-08 15:49:33297 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17298
mathpf4bc50e2017-01-24 05:17:50299 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28300 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50301
302 // We close all bindings and ask to be destroyed.
303 client_.reset();
304 binding_.Close();
rouslanb28f4532017-05-08 15:41:47305 if (observer_for_testing_)
306 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50307 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36308}
309
sebsg2c8558a2017-05-17 18:54:10310void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33311 RecordFirstAbortReason(is_user_initiated
312 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
313 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10314}
315
mathpf4bc50e2017-01-24 05:17:50316void PaymentRequest::OnConnectionTerminated() {
317 // We are here because of a browser-side error, or likely as a result of the
318 // connection_error_handler on |binding_|, which can mean that the renderer
319 // has decided to close the pipe for various reasons (see all uses of
320 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
321 // the binding and the dialog, and ask to be deleted.
322 client_.reset();
mathpf709499d2017-01-09 20:48:36323 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50324 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47325 if (observer_for_testing_)
326 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33327
328 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36329 manager_->DestroyRequest(this);
330}
331
mathpd4be8de82017-03-01 00:51:48332void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45333 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault49a97e52017-08-15 01:51:36334 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SELECTED_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37335 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48336}
337
sebsgfcdd13c2017-06-08 15:49:33338void PaymentRequest::RecordFirstAbortReason(
339 JourneyLogger::AbortReason abort_reason) {
340 if (!has_recorded_completion_) {
341 has_recorded_completion_ = true;
342 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10343 }
344}
345
mathpf709499d2017-01-09 20:48:36346} // namespace payments