blob: a55f9869f27f8c4f38fef5fc0162f54b8d02b704 [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"
rouslan908248c2017-02-27 21:30:2414#include "components/payments/content/payment_details_validation.h"
15#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"
anthonyvd6a43b932017-05-11 18:39:2717#include "components/payments/core/payment_prefs.h"
18#include "components/prefs/pref_service.h"
mathpf709499d2017-01-09 20:48:3619#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3920#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3621#include "content/public/browser/web_contents.h"
22
23namespace payments {
24
25PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3926 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3627 content::WebContents* web_contents,
28 std::unique_ptr<PaymentRequestDelegate> delegate,
29 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2830 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3731 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3632 : web_contents_(web_contents),
33 delegate_(std::move(delegate)),
34 manager_(manager),
mathp300fa542017-03-27 19:29:3735 binding_(this, std::move(request)),
Rouslan Solomakhin115f7232017-08-01 15:24:3836 top_level_origin_(web_contents_->GetLastCommittedURL().GetOrigin()),
37 frame_origin_(render_frame_host->GetLastCommittedURL().GetOrigin()),
sebsg20b49d7b2017-05-04 20:23:1738 observer_for_testing_(observer_for_testing),
39 journey_logger_(delegate_->IsIncognito(),
40 web_contents_->GetLastCommittedURL(),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1341 delegate_->GetUkmRecorder()),
42 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5043 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
44 // will happen as a result of many renderer-side events (both successful and
45 // erroneous in nature).
46 // TODO(crbug.com/683636): Investigate using
47 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
48 binding_.set_connection_error_handler(base::Bind(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1349 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3650}
51
52PaymentRequest::~PaymentRequest() {}
53
rouslan6e3cf7c62017-04-17 21:23:2854void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
55 std::vector<mojom::PaymentMethodDataPtr> method_data,
56 mojom::PaymentDetailsPtr details,
57 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3658 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2859 client_ = std::move(client);
60
rouslanb28f4532017-05-08 15:41:4761 const GURL last_committed_url = delegate_->GetLastCommittedURL();
62 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2863 LOG(ERROR) << "Not in a secure origin";
64 OnConnectionTerminated();
65 return;
66 }
67
rouslanb28f4532017-05-08 15:41:4768 bool allowed_origin =
69 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
70 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
71 if (!allowed_origin) {
72 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
73 "allowed";
74 }
75
76 bool invalid_ssl =
77 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
78 !delegate_->IsSslCertificateValid();
79 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2880 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4781
82 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2883 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
84 // with "NotSupportedError".
85 spec_ = base::MakeUnique<PaymentRequestSpec>(
86 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
87 std::vector<mojom::PaymentMethodDataPtr>(), this,
88 delegate_->GetApplicationLocale());
89 state_ = base::MakeUnique<PaymentRequestState>(
90 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:4791 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
rouslan6e3cf7c62017-04-17 21:23:2892 return;
93 }
94
mathpf709499d2017-01-09 20:48:3695 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2896 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3697 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5098 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3699 return;
100 }
rouslan6e3cf7c62017-04-17 21:23:28101
jinho.bangfcb5ec92017-03-29 08:08:02102 if (!details->total) {
103 LOG(ERROR) << "Missing total";
104 OnConnectionTerminated();
105 return;
106 }
rouslan6e3cf7c62017-04-17 21:23:28107
mathpf1a7a3752017-03-15 11:23:37108 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33109 std::move(options), std::move(details), std::move(method_data), this,
110 delegate_->GetApplicationLocale());
111 state_ = base::MakeUnique<PaymentRequestState>(
112 spec_.get(), this, delegate_->GetApplicationLocale(),
sebsgc6719b32017-07-05 19:54:47113 delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_);
Mathieu Perreault627b97c2017-08-12 00:44:22114
115 journey_logger_.SetRequestedInformation(
116 spec_->request_shipping(), spec_->request_payer_email(),
117 spec_->request_payer_phone(), spec_->request_payer_name());
118
119 // Log metrics around which payment methods are requested by the merchant.
120 GURL google_pay_url(kGooglePayMethodName);
121 GURL android_pay_url(kAndroidPayMethodName);
122 // Looking for payment methods that are NOT google-related payment methods.
123 auto non_google_it =
124 std::find_if(spec_->url_payment_method_identifiers().begin(),
125 spec_->url_payment_method_identifiers().end(),
126 [google_pay_url, android_pay_url](const GURL& url) {
127 return url != google_pay_url && url != android_pay_url;
128 });
129 journey_logger_.SetRequestedPaymentMethodTypes(
130 /*requested_basic_card=*/!spec_->supported_card_networks().empty(),
131 /*requested_method_google=*/
132 base::ContainsValue(spec_->url_payment_method_identifiers(),
133 google_pay_url) ||
134 base::ContainsValue(spec_->url_payment_method_identifiers(),
135 android_pay_url),
136 /*requested_method_other=*/non_google_it !=
137 spec_->url_payment_method_identifiers().end());
mathpf709499d2017-01-09 20:48:36138}
139
140void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10141 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50142 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
143 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10144 return;
145 }
rouslan6e3cf7c62017-04-17 21:23:28146
rouslan7d433cc22017-05-08 15:18:07147 // A tab can display only one PaymentRequest UI at a time.
148 if (!manager_->CanShow(this)) {
149 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12150 journey_logger_.SetNotShown(
151 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
rouslan7d433cc22017-05-08 15:18:07152 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
153 OnConnectionTerminated();
154 return;
155 }
156
rouslan6e3cf7c62017-04-17 21:23:28157 if (!state_->AreRequestedMethodsSupported()) {
sebsg828269bc2017-06-09 19:11:12158 journey_logger_.SetNotShown(
159 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28160 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
161 if (observer_for_testing_)
162 observer_for_testing_->OnNotSupportedError();
163 OnConnectionTerminated();
164 return;
165 }
166
mathp57c8c862017-06-16 20:15:45167 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
sebsg8f4fa4d2017-06-13 15:25:45168
mathpf4bc50e2017-01-24 05:17:50169 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36170}
171
mathp151bd31e2017-04-03 21:07:24172void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
173 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28174 if (!validatePaymentDetails(details, &error)) {
mathp151bd31e2017-04-03 21:07:24175 LOG(ERROR) << error;
176 OnConnectionTerminated();
177 return;
178 }
179 spec_->UpdateWith(std::move(details));
180}
181
mathpf4bc50e2017-01-24 05:17:50182void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56183 // The API user has decided to abort. If a successful abort message is
184 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50185 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56186 // Otherwise, the abort promise is rejected and the pipe is not closed.
187 // The abort is only successful if the payment app wasn't yet invoked.
188 // TODO(crbug.com/716546): Add a merchant abort metric
189
190 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33191 if (accepting_abort)
192 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56193
mathpf4bc50e2017-01-24 05:17:50194 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56195 client_->OnAbort(accepting_abort);
196
197 if (observer_for_testing_)
198 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50199}
200
mathp218795892017-03-29 15:15:34201void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16202 if (!client_.is_bound())
203 return;
204
Rouslan Solomakhine3473192017-06-16 14:54:57205 // Failed transactions show an error. Successful and unknown-state
206 // transactions don't show an error.
207 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34208 delegate_->ShowErrorMessage();
209 } else {
sebsgfcdd13c2017-06-08 15:49:33210 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58211 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33212 has_recorded_completion_ = true;
213
anthonyvd6a43b932017-05-11 18:39:27214 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
215 true);
mathp218795892017-03-29 15:15:34216 // When the renderer closes the connection,
217 // PaymentRequest::OnConnectionTerminated will be called.
218 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22219 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34220 }
mathp4b85b582017-03-08 21:07:16221}
222
223void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39224 bool can_make_payment = state()->CanMakePayment();
225 if (delegate_->IsIncognito()) {
226 client_->OnCanMakePayment(
227 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
228 journey_logger_.SetCanMakePaymentValue(true);
229 } else if (CanMakePaymentQueryFactory::GetInstance()
230 ->GetForContext(web_contents_->GetBrowserContext())
Rouslan Solomakhin115f7232017-08-01 15:24:38231 ->CanQuery(top_level_origin_, frame_origin_,
232 spec()->stringified_method_data())) {
rouslan690997682017-05-09 18:07:39233 client_->OnCanMakePayment(
234 can_make_payment
235 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
236 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
237 journey_logger_.SetCanMakePaymentValue(can_make_payment);
Rouslan Solomakhin115f7232017-08-01 15:24:38238 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(
239 frame_origin_.GetURL())) {
rouslan690997682017-05-09 18:07:39240 client_->OnCanMakePayment(
241 can_make_payment
242 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
243 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
244 journey_logger_.SetCanMakePaymentValue(can_make_payment);
245 } else {
246 client_->OnCanMakePayment(
247 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
248 }
249
mathp300fa542017-03-27 19:29:37250 if (observer_for_testing_)
251 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16252}
253
mathpf1a7a3752017-03-15 11:23:37254void PaymentRequest::OnPaymentResponseAvailable(
255 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45256 journey_logger_.SetEventOccurred(
257 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37258 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16259}
260
mathp151bd31e2017-04-03 21:07:24261void PaymentRequest::OnShippingOptionIdSelected(
262 std::string shipping_option_id) {
263 client_->OnShippingOptionChange(shipping_option_id);
264}
265
266void PaymentRequest::OnShippingAddressSelected(
267 mojom::PaymentAddressPtr address) {
268 client_->OnShippingAddressChange(std::move(address));
269}
270
mathpf4bc50e2017-01-24 05:17:50271void PaymentRequest::UserCancelled() {
272 // If |client_| is not bound, then the object is already being destroyed as
273 // a result of a renderer event.
274 if (!client_.is_bound())
275 return;
276
sebsgfcdd13c2017-06-08 15:49:33277 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17278
mathpf4bc50e2017-01-24 05:17:50279 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28280 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50281
282 // We close all bindings and ask to be destroyed.
283 client_.reset();
284 binding_.Close();
rouslanb28f4532017-05-08 15:41:47285 if (observer_for_testing_)
286 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50287 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36288}
289
sebsg2c8558a2017-05-17 18:54:10290void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33291 RecordFirstAbortReason(is_user_initiated
292 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
293 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10294}
295
mathpf4bc50e2017-01-24 05:17:50296void PaymentRequest::OnConnectionTerminated() {
297 // We are here because of a browser-side error, or likely as a result of the
298 // connection_error_handler on |binding_|, which can mean that the renderer
299 // has decided to close the pipe for various reasons (see all uses of
300 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
301 // the binding and the dialog, and ask to be deleted.
302 client_.reset();
mathpf709499d2017-01-09 20:48:36303 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50304 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47305 if (observer_for_testing_)
306 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33307
308 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36309 manager_->DestroyRequest(this);
310}
311
mathpd4be8de82017-03-01 00:51:48312void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45313 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
sebsgec8296f92017-06-05 15:14:24314 journey_logger_.SetSelectedPaymentMethod(
315 JourneyLogger::SELECTED_PAYMENT_METHOD_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37316 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48317}
318
sebsgfcdd13c2017-06-08 15:49:33319void PaymentRequest::RecordFirstAbortReason(
320 JourneyLogger::AbortReason abort_reason) {
321 if (!has_recorded_completion_) {
322 has_recorded_completion_ = true;
323 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10324 }
325}
326
mathpf709499d2017-01-09 20:48:36327} // namespace payments