blob: 63598655bfec6be2d1ebd9fbf80490b14aee5294 [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)),
Rouslan Solomakhin115f7232017-08-01 15:24:3835 top_level_origin_(web_contents_->GetLastCommittedURL().GetOrigin()),
36 frame_origin_(render_frame_host->GetLastCommittedURL().GetOrigin()),
sebsg20b49d7b2017-05-04 20:23:1737 observer_for_testing_(observer_for_testing),
38 journey_logger_(delegate_->IsIncognito(),
39 web_contents_->GetLastCommittedURL(),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1340 delegate_->GetUkmRecorder()),
41 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5042 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
43 // will happen as a result of many renderer-side events (both successful and
44 // erroneous in nature).
45 // TODO(crbug.com/683636): Investigate using
46 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
47 binding_.set_connection_error_handler(base::Bind(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1348 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3649}
50
51PaymentRequest::~PaymentRequest() {}
52
rouslan6e3cf7c62017-04-17 21:23:2853void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
54 std::vector<mojom::PaymentMethodDataPtr> method_data,
55 mojom::PaymentDetailsPtr details,
56 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3657 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2858 client_ = std::move(client);
59
rouslanb28f4532017-05-08 15:41:4760 const GURL last_committed_url = delegate_->GetLastCommittedURL();
61 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2862 LOG(ERROR) << "Not in a secure origin";
63 OnConnectionTerminated();
64 return;
65 }
66
rouslanb28f4532017-05-08 15:41:4767 bool allowed_origin =
68 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
69 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
70 if (!allowed_origin) {
71 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
72 "allowed";
73 }
74
75 bool invalid_ssl =
76 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
77 !delegate_->IsSslCertificateValid();
78 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2879 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4780
81 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2882 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
83 // with "NotSupportedError".
84 spec_ = base::MakeUnique<PaymentRequestSpec>(
85 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
86 std::vector<mojom::PaymentMethodDataPtr>(), this,
87 delegate_->GetApplicationLocale());
88 state_ = base::MakeUnique<PaymentRequestState>(
89 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:4790 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
rouslan6e3cf7c62017-04-17 21:23:2891 return;
92 }
93
mathpf709499d2017-01-09 20:48:3694 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2895 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3696 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5097 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3698 return;
99 }
rouslan6e3cf7c62017-04-17 21:23:28100
jinho.bangfcb5ec92017-03-29 08:08:02101 if (!details->total) {
102 LOG(ERROR) << "Missing total";
103 OnConnectionTerminated();
104 return;
105 }
rouslan6e3cf7c62017-04-17 21:23:28106
mathpf1a7a3752017-03-15 11:23:37107 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33108 std::move(options), std::move(details), std::move(method_data), this,
109 delegate_->GetApplicationLocale());
110 state_ = base::MakeUnique<PaymentRequestState>(
111 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:47112 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
mathpf709499d2017-01-09 20:48:36113}
114
115void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10116 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50117 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
118 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10119 return;
120 }
rouslan6e3cf7c62017-04-17 21:23:28121
rouslan7d433cc22017-05-08 15:18:07122 // A tab can display only one PaymentRequest UI at a time.
123 if (!manager_->CanShow(this)) {
124 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12125 journey_logger_.SetNotShown(
126 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
127 has_recorded_completion_ = true;
rouslan7d433cc22017-05-08 15:18:07128 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
129 OnConnectionTerminated();
130 return;
131 }
132
rouslan6e3cf7c62017-04-17 21:23:28133 if (!state_->AreRequestedMethodsSupported()) {
sebsg828269bc2017-06-09 19:11:12134 journey_logger_.SetNotShown(
135 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
136 has_recorded_completion_ = true;
rouslan6e3cf7c62017-04-17 21:23:28137 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
138 if (observer_for_testing_)
139 observer_for_testing_->OnNotSupportedError();
140 OnConnectionTerminated();
141 return;
142 }
143
mathp57c8c862017-06-16 20:15:45144 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
sebsg8f4fa4d2017-06-13 15:25:45145 journey_logger_.SetRequestedInformation(
146 spec_->request_shipping(), spec_->request_payer_email(),
147 spec_->request_payer_phone(), spec_->request_payer_name());
148
mathpf4bc50e2017-01-24 05:17:50149 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36150}
151
mathp151bd312017-04-03 21:07:24152void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
153 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28154 if (!validatePaymentDetails(details, &error)) {
mathp151bd312017-04-03 21:07:24155 LOG(ERROR) << error;
156 OnConnectionTerminated();
157 return;
158 }
159 spec_->UpdateWith(std::move(details));
160}
161
mathpf4bc50e2017-01-24 05:17:50162void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56163 // The API user has decided to abort. If a successful abort message is
164 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50165 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56166 // Otherwise, the abort promise is rejected and the pipe is not closed.
167 // The abort is only successful if the payment app wasn't yet invoked.
168 // TODO(crbug.com/716546): Add a merchant abort metric
169
170 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33171 if (accepting_abort)
172 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56173
mathpf4bc50e2017-01-24 05:17:50174 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56175 client_->OnAbort(accepting_abort);
176
177 if (observer_for_testing_)
178 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50179}
180
mathp218795892017-03-29 15:15:34181void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16182 if (!client_.is_bound())
183 return;
184
Rouslan Solomakhine3473192017-06-16 14:54:57185 // Failed transactions show an error. Successful and unknown-state
186 // transactions don't show an error.
187 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34188 delegate_->ShowErrorMessage();
189 } else {
sebsgfcdd13c2017-06-08 15:49:33190 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58191 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33192 has_recorded_completion_ = true;
193
anthonyvd6a43b932017-05-11 18:39:27194 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
195 true);
mathp218795892017-03-29 15:15:34196 // When the renderer closes the connection,
197 // PaymentRequest::OnConnectionTerminated will be called.
198 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22199 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34200 }
mathp4b85b582017-03-08 21:07:16201}
202
203void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39204 bool can_make_payment = state()->CanMakePayment();
205 if (delegate_->IsIncognito()) {
206 client_->OnCanMakePayment(
207 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
208 journey_logger_.SetCanMakePaymentValue(true);
209 } else if (CanMakePaymentQueryFactory::GetInstance()
210 ->GetForContext(web_contents_->GetBrowserContext())
Rouslan Solomakhin115f7232017-08-01 15:24:38211 ->CanQuery(top_level_origin_, frame_origin_,
212 spec()->stringified_method_data())) {
rouslan690997682017-05-09 18:07:39213 client_->OnCanMakePayment(
214 can_make_payment
215 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
216 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
217 journey_logger_.SetCanMakePaymentValue(can_make_payment);
Rouslan Solomakhin115f7232017-08-01 15:24:38218 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(
219 frame_origin_.GetURL())) {
rouslan690997682017-05-09 18:07:39220 client_->OnCanMakePayment(
221 can_make_payment
222 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
223 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
224 journey_logger_.SetCanMakePaymentValue(can_make_payment);
225 } else {
226 client_->OnCanMakePayment(
227 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
228 }
229
mathp300fa542017-03-27 19:29:37230 if (observer_for_testing_)
231 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16232}
233
mathpf1a7a3752017-03-15 11:23:37234void PaymentRequest::OnPaymentResponseAvailable(
235 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45236 journey_logger_.SetEventOccurred(
237 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37238 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16239}
240
mathp151bd312017-04-03 21:07:24241void PaymentRequest::OnShippingOptionIdSelected(
242 std::string shipping_option_id) {
243 client_->OnShippingOptionChange(shipping_option_id);
244}
245
246void PaymentRequest::OnShippingAddressSelected(
247 mojom::PaymentAddressPtr address) {
248 client_->OnShippingAddressChange(std::move(address));
249}
250
mathpf4bc50e2017-01-24 05:17:50251void PaymentRequest::UserCancelled() {
252 // If |client_| is not bound, then the object is already being destroyed as
253 // a result of a renderer event.
254 if (!client_.is_bound())
255 return;
256
sebsgfcdd13c2017-06-08 15:49:33257 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17258
mathpf4bc50e2017-01-24 05:17:50259 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28260 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50261
262 // We close all bindings and ask to be destroyed.
263 client_.reset();
264 binding_.Close();
rouslanb28f4532017-05-08 15:41:47265 if (observer_for_testing_)
266 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50267 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36268}
269
sebsg2c8558a2017-05-17 18:54:10270void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33271 RecordFirstAbortReason(is_user_initiated
272 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
273 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10274}
275
mathpf4bc50e2017-01-24 05:17:50276void PaymentRequest::OnConnectionTerminated() {
277 // We are here because of a browser-side error, or likely as a result of the
278 // connection_error_handler on |binding_|, which can mean that the renderer
279 // has decided to close the pipe for various reasons (see all uses of
280 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
281 // the binding and the dialog, and ask to be deleted.
282 client_.reset();
mathpf709499d2017-01-09 20:48:36283 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50284 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47285 if (observer_for_testing_)
286 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33287
288 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36289 manager_->DestroyRequest(this);
290}
291
mathpd4be8de82017-03-01 00:51:48292void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45293 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
sebsgec8296f92017-06-05 15:14:24294 journey_logger_.SetSelectedPaymentMethod(
295 JourneyLogger::SELECTED_PAYMENT_METHOD_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37296 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48297}
298
sebsgfcdd13c2017-06-08 15:49:33299void PaymentRequest::RecordFirstAbortReason(
300 JourneyLogger::AbortReason abort_reason) {
301 if (!has_recorded_completion_) {
302 has_recorded_completion_ = true;
303 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10304 }
305}
306
mathpf709499d2017-01-09 20:48:36307} // namespace payments