blob: cf0c0718e3491b5b1165f6984eeb26aad9fd180c [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"
rouslan690997682017-05-09 18:07:3911#include "components/payments/content/can_make_payment_query_factory.h"
rouslan6e3cf7c62017-04-17 21:23:2812#include "components/payments/content/origin_security_checker.h"
rouslan908248c2017-02-27 21:30:2413#include "components/payments/content/payment_details_validation.h"
14#include "components/payments/content/payment_request_web_contents_manager.h"
rouslan690997682017-05-09 18:07:3915#include "components/payments/core/can_make_payment_query.h"
anthonyvd6a43b932017-05-11 18:39:2716#include "components/payments/core/payment_prefs.h"
17#include "components/prefs/pref_service.h"
mathpf709499d2017-01-09 20:48:3618#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3919#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3620#include "content/public/browser/web_contents.h"
21
22namespace payments {
23
24PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3925 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3626 content::WebContents* web_contents,
27 std::unique_ptr<PaymentRequestDelegate> delegate,
28 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2829 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3730 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3631 : web_contents_(web_contents),
32 delegate_(std::move(delegate)),
33 manager_(manager),
mathp300fa542017-03-27 19:29:3734 binding_(this, std::move(request)),
rouslan690997682017-05-09 18:07:3935 frame_origin_(GURL(render_frame_host->GetLastCommittedURL()).GetOrigin()),
sebsg20b49d7b2017-05-04 20:23:1736 observer_for_testing_(observer_for_testing),
37 journey_logger_(delegate_->IsIncognito(),
38 web_contents_->GetLastCommittedURL(),
oysteineb068f272017-05-23 00:14:0139 delegate_->GetUkmRecorder()) {
mathpf4bc50e2017-01-24 05:17:5040 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
41 // will happen as a result of many renderer-side events (both successful and
42 // erroneous in nature).
43 // TODO(crbug.com/683636): Investigate using
44 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
45 binding_.set_connection_error_handler(base::Bind(
46 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
mathpf709499d2017-01-09 20:48:3647}
48
49PaymentRequest::~PaymentRequest() {}
50
rouslan6e3cf7c62017-04-17 21:23:2851void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
52 std::vector<mojom::PaymentMethodDataPtr> method_data,
53 mojom::PaymentDetailsPtr details,
54 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3655 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2856 client_ = std::move(client);
57
rouslanb28f4532017-05-08 15:41:4758 const GURL last_committed_url = delegate_->GetLastCommittedURL();
59 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2860 LOG(ERROR) << "Not in a secure origin";
61 OnConnectionTerminated();
62 return;
63 }
64
rouslanb28f4532017-05-08 15:41:4765 bool allowed_origin =
66 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
67 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
68 if (!allowed_origin) {
69 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
70 "allowed";
71 }
72
73 bool invalid_ssl =
74 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
75 !delegate_->IsSslCertificateValid();
76 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2877 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4778
79 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2880 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
81 // with "NotSupportedError".
82 spec_ = base::MakeUnique<PaymentRequestSpec>(
83 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
84 std::vector<mojom::PaymentMethodDataPtr>(), this,
85 delegate_->GetApplicationLocale());
86 state_ = base::MakeUnique<PaymentRequestState>(
87 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:4788 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
rouslan6e3cf7c62017-04-17 21:23:2889 return;
90 }
91
mathpf709499d2017-01-09 20:48:3692 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2893 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3694 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5095 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3696 return;
97 }
rouslan6e3cf7c62017-04-17 21:23:2898
jinho.bangfcb5ec92017-03-29 08:08:0299 if (!details->total) {
100 LOG(ERROR) << "Missing total";
101 OnConnectionTerminated();
102 return;
103 }
rouslan6e3cf7c62017-04-17 21:23:28104
mathpf1a7a3752017-03-15 11:23:37105 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33106 std::move(options), std::move(details), std::move(method_data), this,
107 delegate_->GetApplicationLocale());
108 state_ = base::MakeUnique<PaymentRequestState>(
109 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:47110 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
mathpf709499d2017-01-09 20:48:36111}
112
113void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10114 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50115 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
116 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10117 return;
118 }
rouslan6e3cf7c62017-04-17 21:23:28119
rouslan7d433cc22017-05-08 15:18:07120 // A tab can display only one PaymentRequest UI at a time.
121 if (!manager_->CanShow(this)) {
122 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12123 journey_logger_.SetNotShown(
124 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
125 has_recorded_completion_ = true;
rouslan7d433cc22017-05-08 15:18:07126 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
127 OnConnectionTerminated();
128 return;
129 }
130
rouslan6e3cf7c62017-04-17 21:23:28131 if (!state_->AreRequestedMethodsSupported()) {
sebsg828269bc2017-06-09 19:11:12132 journey_logger_.SetNotShown(
133 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
134 has_recorded_completion_ = true;
rouslan6e3cf7c62017-04-17 21:23:28135 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
136 if (observer_for_testing_)
137 observer_for_testing_->OnNotSupportedError();
138 OnConnectionTerminated();
139 return;
140 }
141
sebsg20b49d7b2017-05-04 20:23:17142 journey_logger_.SetShowCalled();
mathp57c8c862017-06-16 20:15:45143 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
sebsg8f4fa4d2017-06-13 15:25:45144 journey_logger_.SetRequestedInformation(
145 spec_->request_shipping(), spec_->request_payer_email(),
146 spec_->request_payer_phone(), spec_->request_payer_name());
147
mathpf4bc50e2017-01-24 05:17:50148 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36149}
150
mathp151bd312017-04-03 21:07:24151void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
152 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28153 if (!validatePaymentDetails(details, &error)) {
mathp151bd312017-04-03 21:07:24154 LOG(ERROR) << error;
155 OnConnectionTerminated();
156 return;
157 }
158 spec_->UpdateWith(std::move(details));
159}
160
mathpf4bc50e2017-01-24 05:17:50161void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56162 // The API user has decided to abort. If a successful abort message is
163 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50164 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56165 // Otherwise, the abort promise is rejected and the pipe is not closed.
166 // The abort is only successful if the payment app wasn't yet invoked.
167 // TODO(crbug.com/716546): Add a merchant abort metric
168
169 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33170 if (accepting_abort)
171 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56172
mathpf4bc50e2017-01-24 05:17:50173 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56174 client_->OnAbort(accepting_abort);
175
176 if (observer_for_testing_)
177 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50178}
179
mathp218795892017-03-29 15:15:34180void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16181 if (!client_.is_bound())
182 return;
183
Rouslan Solomakhine3473192017-06-16 14:54:57184 // Failed transactions show an error. Successful and unknown-state
185 // transactions don't show an error.
186 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34187 delegate_->ShowErrorMessage();
188 } else {
sebsgfcdd13c2017-06-08 15:49:33189 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58190 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33191 has_recorded_completion_ = true;
192
anthonyvd6a43b932017-05-11 18:39:27193 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
194 true);
mathp218795892017-03-29 15:15:34195 // When the renderer closes the connection,
196 // PaymentRequest::OnConnectionTerminated will be called.
197 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22198 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34199 }
mathp4b85b582017-03-08 21:07:16200}
201
202void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39203 bool can_make_payment = state()->CanMakePayment();
204 if (delegate_->IsIncognito()) {
205 client_->OnCanMakePayment(
206 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
207 journey_logger_.SetCanMakePaymentValue(true);
208 } else if (CanMakePaymentQueryFactory::GetInstance()
209 ->GetForContext(web_contents_->GetBrowserContext())
210 ->CanQuery(frame_origin_, spec()->stringified_method_data())) {
211 client_->OnCanMakePayment(
212 can_make_payment
213 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
214 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
215 journey_logger_.SetCanMakePaymentValue(can_make_payment);
216 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
217 client_->OnCanMakePayment(
218 can_make_payment
219 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
220 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
221 journey_logger_.SetCanMakePaymentValue(can_make_payment);
222 } else {
223 client_->OnCanMakePayment(
224 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
225 }
226
mathp300fa542017-03-27 19:29:37227 if (observer_for_testing_)
228 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16229}
230
mathpf1a7a3752017-03-15 11:23:37231void PaymentRequest::OnPaymentResponseAvailable(
232 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45233 journey_logger_.SetEventOccurred(
234 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37235 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16236}
237
mathp151bd312017-04-03 21:07:24238void PaymentRequest::OnShippingOptionIdSelected(
239 std::string shipping_option_id) {
240 client_->OnShippingOptionChange(shipping_option_id);
241}
242
243void PaymentRequest::OnShippingAddressSelected(
244 mojom::PaymentAddressPtr address) {
245 client_->OnShippingAddressChange(std::move(address));
246}
247
mathpf4bc50e2017-01-24 05:17:50248void PaymentRequest::UserCancelled() {
249 // If |client_| is not bound, then the object is already being destroyed as
250 // a result of a renderer event.
251 if (!client_.is_bound())
252 return;
253
sebsgfcdd13c2017-06-08 15:49:33254 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17255
mathpf4bc50e2017-01-24 05:17:50256 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28257 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50258
259 // We close all bindings and ask to be destroyed.
260 client_.reset();
261 binding_.Close();
rouslanb28f4532017-05-08 15:41:47262 if (observer_for_testing_)
263 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50264 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36265}
266
sebsg2c8558a2017-05-17 18:54:10267void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33268 RecordFirstAbortReason(is_user_initiated
269 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
270 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10271}
272
mathpf4bc50e2017-01-24 05:17:50273void PaymentRequest::OnConnectionTerminated() {
274 // We are here because of a browser-side error, or likely as a result of the
275 // connection_error_handler on |binding_|, which can mean that the renderer
276 // has decided to close the pipe for various reasons (see all uses of
277 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
278 // the binding and the dialog, and ask to be deleted.
279 client_.reset();
mathpf709499d2017-01-09 20:48:36280 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50281 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47282 if (observer_for_testing_)
283 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33284
285 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36286 manager_->DestroyRequest(this);
287}
288
mathpd4be8de82017-03-01 00:51:48289void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45290 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
sebsgec8296f92017-06-05 15:14:24291 journey_logger_.SetSelectedPaymentMethod(
292 JourneyLogger::SELECTED_PAYMENT_METHOD_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37293 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48294}
295
sebsgfcdd13c2017-06-08 15:49:33296void PaymentRequest::RecordFirstAbortReason(
297 JourneyLogger::AbortReason abort_reason) {
298 if (!has_recorded_completion_) {
299 has_recorded_completion_ = true;
300 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10301 }
302}
303
mathpf709499d2017-01-09 20:48:36304} // namespace payments