blob: d24a672ecfd3b1fa9fb9fc66968a049ab7a7f99c [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
mathp151bd312017-04-03 21:07:24172void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
173 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28174 if (!validatePaymentDetails(details, &error)) {
mathp151bd312017-04-03 21:07:24175 LOG(ERROR) << error;
176 OnConnectionTerminated();
177 return;
178 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39179
180 if (!details->total) {
181 LOG(ERROR) << "Missing total";
182 OnConnectionTerminated();
183 return;
184 }
185
mathp151bd312017-04-03 21:07:24186 spec_->UpdateWith(std::move(details));
187}
188
mathpf4bc50e2017-01-24 05:17:50189void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56190 // The API user has decided to abort. If a successful abort message is
191 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50192 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56193 // Otherwise, the abort promise is rejected and the pipe is not closed.
194 // The abort is only successful if the payment app wasn't yet invoked.
195 // TODO(crbug.com/716546): Add a merchant abort metric
196
197 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33198 if (accepting_abort)
199 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56200
mathpf4bc50e2017-01-24 05:17:50201 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56202 client_->OnAbort(accepting_abort);
203
204 if (observer_for_testing_)
205 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50206}
207
mathp218795892017-03-29 15:15:34208void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16209 if (!client_.is_bound())
210 return;
211
Rouslan Solomakhine3473192017-06-16 14:54:57212 // Failed transactions show an error. Successful and unknown-state
213 // transactions don't show an error.
214 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34215 delegate_->ShowErrorMessage();
216 } else {
sebsgfcdd13c2017-06-08 15:49:33217 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58218 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33219 has_recorded_completion_ = true;
220
anthonyvd6a43b932017-05-11 18:39:27221 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
222 true);
mathp218795892017-03-29 15:15:34223 // When the renderer closes the connection,
224 // PaymentRequest::OnConnectionTerminated will be called.
225 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22226 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34227 }
mathp4b85b582017-03-08 21:07:16228}
229
230void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39231 bool can_make_payment = state()->CanMakePayment();
232 if (delegate_->IsIncognito()) {
233 client_->OnCanMakePayment(
234 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
235 journey_logger_.SetCanMakePaymentValue(true);
236 } else if (CanMakePaymentQueryFactory::GetInstance()
237 ->GetForContext(web_contents_->GetBrowserContext())
Rouslan Solomakhin115f7232017-08-01 15:24:38238 ->CanQuery(top_level_origin_, frame_origin_,
239 spec()->stringified_method_data())) {
rouslan690997682017-05-09 18:07:39240 client_->OnCanMakePayment(
241 can_make_payment
242 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
243 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
244 journey_logger_.SetCanMakePaymentValue(can_make_payment);
Rouslan Solomakhin115f7232017-08-01 15:24:38245 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(
246 frame_origin_.GetURL())) {
rouslan690997682017-05-09 18:07:39247 client_->OnCanMakePayment(
248 can_make_payment
249 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
250 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
251 journey_logger_.SetCanMakePaymentValue(can_make_payment);
252 } else {
253 client_->OnCanMakePayment(
254 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
255 }
256
mathp300fa542017-03-27 19:29:37257 if (observer_for_testing_)
258 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16259}
260
mathpf1a7a3752017-03-15 11:23:37261void PaymentRequest::OnPaymentResponseAvailable(
262 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45263 journey_logger_.SetEventOccurred(
264 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
mathpf1a7a3752017-03-15 11:23:37265 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16266}
267
mathp151bd312017-04-03 21:07:24268void PaymentRequest::OnShippingOptionIdSelected(
269 std::string shipping_option_id) {
270 client_->OnShippingOptionChange(shipping_option_id);
271}
272
273void PaymentRequest::OnShippingAddressSelected(
274 mojom::PaymentAddressPtr address) {
275 client_->OnShippingAddressChange(std::move(address));
276}
277
mathpf4bc50e2017-01-24 05:17:50278void PaymentRequest::UserCancelled() {
279 // If |client_| is not bound, then the object is already being destroyed as
280 // a result of a renderer event.
281 if (!client_.is_bound())
282 return;
283
sebsgfcdd13c2017-06-08 15:49:33284 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17285
mathpf4bc50e2017-01-24 05:17:50286 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28287 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50288
289 // We close all bindings and ask to be destroyed.
290 client_.reset();
291 binding_.Close();
rouslanb28f4532017-05-08 15:41:47292 if (observer_for_testing_)
293 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50294 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36295}
296
sebsg2c8558a2017-05-17 18:54:10297void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33298 RecordFirstAbortReason(is_user_initiated
299 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
300 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10301}
302
mathpf4bc50e2017-01-24 05:17:50303void PaymentRequest::OnConnectionTerminated() {
304 // We are here because of a browser-side error, or likely as a result of the
305 // connection_error_handler on |binding_|, which can mean that the renderer
306 // has decided to close the pipe for various reasons (see all uses of
307 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
308 // the binding and the dialog, and ask to be deleted.
309 client_.reset();
mathpf709499d2017-01-09 20:48:36310 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50311 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47312 if (observer_for_testing_)
313 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33314
315 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36316 manager_->DestroyRequest(this);
317}
318
mathpd4be8de82017-03-01 00:51:48319void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45320 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault49a97e52017-08-15 01:51:36321 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SELECTED_CREDIT_CARD);
mathpf1a7a3752017-03-15 11:23:37322 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48323}
324
sebsgfcdd13c2017-06-08 15:49:33325void PaymentRequest::RecordFirstAbortReason(
326 JourneyLogger::AbortReason abort_reason) {
327 if (!has_recorded_completion_) {
328 has_recorded_completion_ = true;
329 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10330 }
331}
332
mathpf709499d2017-01-09 20:48:36333} // namespace payments