blob: a61af2ff9ffc829a2bb3af7629d3b9f677e87d0d [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(),
88 delegate_->GetPersonalDataManager(), delegate_.get());
89 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(),
anthonyvdd23ed702017-04-05 15:29:00110 delegate_->GetPersonalDataManager(), delegate_.get());
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();
sebsg8f4fa4d2017-06-13 15:25:45143 journey_logger_.SetRequestedInformation(
144 spec_->request_shipping(), spec_->request_payer_email(),
145 spec_->request_payer_phone(), spec_->request_payer_name());
146
mathpf4bc50e2017-01-24 05:17:50147 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36148}
149
mathp151bd312017-04-03 21:07:24150void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
151 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28152 if (!validatePaymentDetails(details, &error)) {
mathp151bd312017-04-03 21:07:24153 LOG(ERROR) << error;
154 OnConnectionTerminated();
155 return;
156 }
157 spec_->UpdateWith(std::move(details));
158}
159
mathpf4bc50e2017-01-24 05:17:50160void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56161 // The API user has decided to abort. If a successful abort message is
162 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50163 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56164 // Otherwise, the abort promise is rejected and the pipe is not closed.
165 // The abort is only successful if the payment app wasn't yet invoked.
166 // TODO(crbug.com/716546): Add a merchant abort metric
167
168 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33169 if (accepting_abort)
170 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56171
mathpf4bc50e2017-01-24 05:17:50172 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56173 client_->OnAbort(accepting_abort);
174
175 if (observer_for_testing_)
176 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50177}
178
mathp218795892017-03-29 15:15:34179void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16180 if (!client_.is_bound())
181 return;
182
Rouslan Solomakhine3473192017-06-16 14:54:57183 // Failed transactions show an error. Successful and unknown-state
184 // transactions don't show an error.
185 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34186 delegate_->ShowErrorMessage();
187 } else {
sebsgfcdd13c2017-06-08 15:49:33188 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58189 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33190 has_recorded_completion_ = true;
191
anthonyvd6a43b932017-05-11 18:39:27192 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
193 true);
mathp218795892017-03-29 15:15:34194 // When the renderer closes the connection,
195 // PaymentRequest::OnConnectionTerminated will be called.
196 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22197 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34198 }
mathp4b85b582017-03-08 21:07:16199}
200
201void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39202 bool can_make_payment = state()->CanMakePayment();
203 if (delegate_->IsIncognito()) {
204 client_->OnCanMakePayment(
205 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
206 journey_logger_.SetCanMakePaymentValue(true);
207 } else if (CanMakePaymentQueryFactory::GetInstance()
208 ->GetForContext(web_contents_->GetBrowserContext())
209 ->CanQuery(frame_origin_, spec()->stringified_method_data())) {
210 client_->OnCanMakePayment(
211 can_make_payment
212 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
213 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
214 journey_logger_.SetCanMakePaymentValue(can_make_payment);
215 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
216 client_->OnCanMakePayment(
217 can_make_payment
218 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
219 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
220 journey_logger_.SetCanMakePaymentValue(can_make_payment);
221 } else {
222 client_->OnCanMakePayment(
223 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
224 }
225
mathp300fa542017-03-27 19:29:37226 if (observer_for_testing_)
227 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16228}
229
mathpf1a7a3752017-03-15 11:23:37230void PaymentRequest::OnPaymentResponseAvailable(
231 mojom::PaymentResponsePtr response) {
232 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16233}
234
mathp151bd312017-04-03 21:07:24235void PaymentRequest::OnShippingOptionIdSelected(
236 std::string shipping_option_id) {
237 client_->OnShippingOptionChange(shipping_option_id);
238}
239
240void PaymentRequest::OnShippingAddressSelected(
241 mojom::PaymentAddressPtr address) {
242 client_->OnShippingAddressChange(std::move(address));
243}
244
mathpf4bc50e2017-01-24 05:17:50245void PaymentRequest::UserCancelled() {
246 // If |client_| is not bound, then the object is already being destroyed as
247 // a result of a renderer event.
248 if (!client_.is_bound())
249 return;
250
sebsgfcdd13c2017-06-08 15:49:33251 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17252
mathpf4bc50e2017-01-24 05:17:50253 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28254 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50255
256 // We close all bindings and ask to be destroyed.
257 client_.reset();
258 binding_.Close();
rouslanb28f4532017-05-08 15:41:47259 if (observer_for_testing_)
260 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50261 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36262}
263
sebsg2c8558a2017-05-17 18:54:10264void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33265 RecordFirstAbortReason(is_user_initiated
266 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
267 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10268}
269
mathpf4bc50e2017-01-24 05:17:50270void PaymentRequest::OnConnectionTerminated() {
271 // We are here because of a browser-side error, or likely as a result of the
272 // connection_error_handler on |binding_|, which can mean that the renderer
273 // has decided to close the pipe for various reasons (see all uses of
274 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
275 // the binding and the dialog, and ask to be deleted.
276 client_.reset();
mathpf709499d2017-01-09 20:48:36277 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50278 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47279 if (observer_for_testing_)
280 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33281
282 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36283 manager_->DestroyRequest(this);
284}
285
mathpd4be8de82017-03-01 00:51:48286void PaymentRequest::Pay() {
sebsgec8296f92017-06-05 15:14:24287 journey_logger_.SetSelectedPaymentMethod(
288 JourneyLogger::SELECTED_PAYMENT_METHOD_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37289 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48290}
291
sebsgfcdd13c2017-06-08 15:49:33292void PaymentRequest::RecordFirstAbortReason(
293 JourneyLogger::AbortReason abort_reason) {
294 if (!has_recorded_completion_) {
295 has_recorded_completion_ = true;
296 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10297 }
298}
299
mathpf709499d2017-01-09 20:48:36300} // namespace payments