mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 1 | // 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 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 5 | #include "components/payments/content/payment_request.h" |
| 6 | |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 7 | #include <string> |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 8 | #include <utility> |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 9 | |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 10 | #include "base/feature_list.h" |
Mathieu Perreault | 627b97c | 2017-08-12 00:44:22 | [diff] [blame] | 11 | #include "base/stl_util.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 12 | #include "components/payments/content/can_make_payment_query_factory.h" |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 13 | #include "components/payments/content/content_payment_request_delegate.h" |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 14 | #include "components/payments/content/origin_security_checker.h" |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 15 | #include "components/payments/content/payment_request_converter.h" |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 16 | #include "components/payments/content/payment_request_web_contents_manager.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 17 | #include "components/payments/core/can_make_payment_query.h" |
Anthony Vallee-Dubois | 968ae4d | 2018-03-15 16:56:36 | [diff] [blame] | 18 | #include "components/payments/core/features.h" |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 19 | #include "components/payments/core/payment_details.h" |
| 20 | #include "components/payments/core/payment_details_validation.h" |
Mathieu Perreault | 23d25bfb8 | 2018-05-11 14:45:37 | [diff] [blame] | 21 | #include "components/payments/core/payment_instrument.h" |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 22 | #include "components/payments/core/payment_prefs.h" |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 23 | #include "components/payments/core/payments_validators.h" |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 24 | #include "components/prefs/pref_service.h" |
Steven Holte | 2083e8bc | 2018-07-16 23:50:36 | [diff] [blame] | 25 | #include "components/ukm/content/source_url_recorder.h" |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 26 | #include "components/url_formatter/elide_url.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 28 | #include "content/public/browser/render_frame_host.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 29 | #include "content/public/browser/web_contents.h" |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 30 | #include "content/public/common/content_features.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 31 | |
| 32 | namespace payments { |
| 33 | |
| 34 | PaymentRequest::PaymentRequest( |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 35 | content::RenderFrameHost* render_frame_host, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 36 | content::WebContents* web_contents, |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 37 | std::unique_ptr<ContentPaymentRequestDelegate> delegate, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 38 | PaymentRequestWebContentsManager* manager, |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 39 | PaymentRequestDisplayManager* display_manager, |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 40 | mojo::InterfaceRequest<mojom::PaymentRequest> request, |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 41 | ObserverForTest* observer_for_testing) |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 42 | : web_contents_(web_contents), |
| 43 | delegate_(std::move(delegate)), |
| 44 | manager_(manager), |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 45 | display_manager_(display_manager), |
| 46 | display_handle_(nullptr), |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 47 | binding_(this, std::move(request)), |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 48 | top_level_origin_(url_formatter::FormatUrlForSecurityDisplay( |
| 49 | web_contents_->GetLastCommittedURL())), |
| 50 | frame_origin_(url_formatter::FormatUrlForSecurityDisplay( |
| 51 | render_frame_host->GetLastCommittedURL())), |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 52 | observer_for_testing_(observer_for_testing), |
| 53 | journey_logger_(delegate_->IsIncognito(), |
Steven Holte | 2083e8bc | 2018-07-16 23:50:36 | [diff] [blame] | 54 | ukm::GetSourceIdForWebContentsDocument(web_contents)), |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 55 | weak_ptr_factory_(this) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 56 | // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
| 57 | // will happen as a result of many renderer-side events (both successful and |
| 58 | // erroneous in nature). |
| 59 | // TODO(crbug.com/683636): Investigate using |
| 60 | // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 61 | binding_.set_connection_error_handler(base::BindOnce( |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 62 | &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr())); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | PaymentRequest::~PaymentRequest() {} |
| 66 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 67 | void PaymentRequest::Init(mojom::PaymentRequestClientPtr client, |
| 68 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 69 | mojom::PaymentDetailsPtr details, |
| 70 | mojom::PaymentOptionsPtr options) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 71 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 72 | client_ = std::move(client); |
| 73 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 74 | const GURL last_committed_url = delegate_->GetLastCommittedURL(); |
| 75 | if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 76 | LOG(ERROR) << "Not in a secure origin"; |
| 77 | OnConnectionTerminated(); |
| 78 | return; |
| 79 | } |
| 80 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 81 | bool allowed_origin = |
| 82 | OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) || |
| 83 | OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url); |
| 84 | if (!allowed_origin) { |
| 85 | LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins " |
| 86 | "allowed"; |
| 87 | } |
| 88 | |
| 89 | bool invalid_ssl = |
| 90 | OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) && |
| 91 | !delegate_->IsSslCertificateValid(); |
| 92 | if (invalid_ssl) |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 93 | LOG(ERROR) << "SSL certificate is not valid"; |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 94 | |
| 95 | if (!allowed_origin || invalid_ssl) { |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 96 | // Intentionally don't set |spec_| and |state_|. Don't show UI. Resolve |
| 97 | // .canMakepayment() with "false". Reject .show() with "NotSupportedError". |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 101 | std::string error; |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 102 | if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 103 | LOG(ERROR) << error; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 104 | OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 105 | return; |
| 106 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 107 | |
jinho.bang | fcb5ec9 | 2017-03-29 08:08:02 | [diff] [blame] | 108 | if (!details->total) { |
| 109 | LOG(ERROR) << "Missing total"; |
| 110 | OnConnectionTerminated(); |
| 111 | return; |
| 112 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 113 | |
sebsg | a70a6da | 2017-12-21 22:27:02 | [diff] [blame] | 114 | spec_ = std::make_unique<PaymentRequestSpec>( |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 115 | std::move(options), std::move(details), std::move(method_data), this, |
| 116 | delegate_->GetApplicationLocale()); |
sebsg | a70a6da | 2017-12-21 22:27:02 | [diff] [blame] | 117 | state_ = std::make_unique<PaymentRequestState>( |
Rouslan Solomakhin | dbf593d9 | 2017-11-21 19:20:57 | [diff] [blame] | 118 | web_contents_, top_level_origin_, frame_origin_, spec_.get(), this, |
| 119 | delegate_->GetApplicationLocale(), delegate_->GetPersonalDataManager(), |
| 120 | delegate_.get(), &journey_logger_); |
Mathieu Perreault | 627b97c | 2017-08-12 00:44:22 | [diff] [blame] | 121 | |
| 122 | journey_logger_.SetRequestedInformation( |
| 123 | spec_->request_shipping(), spec_->request_payer_email(), |
| 124 | spec_->request_payer_phone(), spec_->request_payer_name()); |
| 125 | |
| 126 | // Log metrics around which payment methods are requested by the merchant. |
| 127 | GURL google_pay_url(kGooglePayMethodName); |
| 128 | GURL android_pay_url(kAndroidPayMethodName); |
| 129 | // Looking for payment methods that are NOT google-related payment methods. |
| 130 | auto non_google_it = |
| 131 | std::find_if(spec_->url_payment_method_identifiers().begin(), |
| 132 | spec_->url_payment_method_identifiers().end(), |
| 133 | [google_pay_url, android_pay_url](const GURL& url) { |
| 134 | return url != google_pay_url && url != android_pay_url; |
| 135 | }); |
| 136 | journey_logger_.SetRequestedPaymentMethodTypes( |
| 137 | /*requested_basic_card=*/!spec_->supported_card_networks().empty(), |
| 138 | /*requested_method_google=*/ |
| 139 | base::ContainsValue(spec_->url_payment_method_identifiers(), |
| 140 | google_pay_url) || |
| 141 | base::ContainsValue(spec_->url_payment_method_identifiers(), |
| 142 | android_pay_url), |
| 143 | /*requested_method_other=*/non_google_it != |
| 144 | spec_->url_payment_method_identifiers().end()); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 145 | } |
| 146 | |
Rouslan Solomakhin | 833f851 | 2018-04-03 23:19:25 | [diff] [blame] | 147 | void PaymentRequest::Show(bool is_user_gesture) { |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 148 | if (!client_.is_bound() || !binding_.is_bound()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 149 | LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 150 | OnConnectionTerminated(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 151 | return; |
| 152 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 153 | |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 154 | // A tab can display only one PaymentRequest UI at a time. |
Anthony Vallee-Dubois | 8f5e7e1 | 2018-01-12 16:14:06 | [diff] [blame] | 155 | display_handle_ = display_manager_->TryShow(delegate_.get()); |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 156 | if (!display_handle_) { |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 157 | LOG(ERROR) << "A PaymentRequest UI is already showing"; |
sebsg | 828269bc | 2017-06-09 19:11:12 | [diff] [blame] | 158 | journey_logger_.SetNotShown( |
| 159 | JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS); |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 160 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
| 161 | OnConnectionTerminated(); |
| 162 | return; |
| 163 | } |
| 164 | |
Rouslan Solomakhin | 5b51043 | 2017-09-26 16:59:32 | [diff] [blame] | 165 | if (!delegate_->IsBrowserWindowActive()) { |
| 166 | LOG(ERROR) << "Cannot show PaymentRequest UI in a background tab"; |
| 167 | journey_logger_.SetNotShown(JourneyLogger::NOT_SHOWN_REASON_OTHER); |
| 168 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
| 169 | OnConnectionTerminated(); |
| 170 | return; |
| 171 | } |
| 172 | |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 173 | if (!state_) { |
| 174 | // SSL is not valid. |
| 175 | AreRequestedMethodsSupportedCallback(false); |
| 176 | return; |
| 177 | } |
| 178 | |
Rouslan Solomakhin | 833f851 | 2018-04-03 23:19:25 | [diff] [blame] | 179 | is_show_user_gesture_ = is_user_gesture; |
| 180 | |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 181 | display_handle_->Show(this); |
| 182 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 183 | state_->AreRequestedMethodsSupported( |
| 184 | base::BindOnce(&PaymentRequest::AreRequestedMethodsSupportedCallback, |
| 185 | weak_ptr_factory_.GetWeakPtr())); |
| 186 | } |
| 187 | |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 188 | void PaymentRequest::Retry(mojom::PaymentValidationErrorsPtr errors) { |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame^] | 189 | if (!client_.is_bound() || !binding_.is_bound()) { |
| 190 | DLOG(ERROR) << "Attempted Retry(), but binding(s) missing."; |
| 191 | OnConnectionTerminated(); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | if (!display_handle_) { |
| 196 | DLOG(ERROR) << "Attempted Retry(), but display_handle_ is nullptr."; |
| 197 | OnConnectionTerminated(); |
| 198 | return; |
| 199 | } |
| 200 | |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 201 | std::string error; |
| 202 | if (!PaymentsValidators::IsValidPaymentValidationErrorsFormat(errors, |
| 203 | &error)) { |
| 204 | DLOG(ERROR) << error; |
| 205 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
| 206 | OnConnectionTerminated(); |
| 207 | return; |
| 208 | } |
| 209 | |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame^] | 210 | spec()->UpdateShippingAddressErrors(std::move(errors->shipping_address)); |
| 211 | spec()->UpdatePayerErrors(std::move(errors->payer)); |
| 212 | display_handle_->Retry(); |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 213 | } |
| 214 | |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 215 | void PaymentRequest::AreRequestedMethodsSupportedCallback( |
| 216 | bool methods_supported) { |
| 217 | if (methods_supported) { |
gogerald | 2ddfc64c | 2018-08-23 16:20:21 | [diff] [blame] | 218 | if (SatisfiesSkipUIConstraints()) { |
| 219 | skipped_payment_request_ui_ = true; |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 220 | Pay(); |
gogerald | 2ddfc64c | 2018-08-23 16:20:21 | [diff] [blame] | 221 | } |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 222 | } else { |
sebsg | 828269bc | 2017-06-09 19:11:12 | [diff] [blame] | 223 | journey_logger_.SetNotShown( |
| 224 | JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 225 | client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED); |
| 226 | if (observer_for_testing_) |
| 227 | observer_for_testing_->OnNotSupportedError(); |
| 228 | OnConnectionTerminated(); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 229 | } |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 230 | } |
| 231 | |
gogerald | 2ddfc64c | 2018-08-23 16:20:21 | [diff] [blame] | 232 | bool PaymentRequest::SatisfiesSkipUIConstraints() const { |
| 233 | return base::FeatureList::IsEnabled(features::kWebPaymentsSingleAppUiSkip) && |
| 234 | base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps) && |
| 235 | is_show_user_gesture_ && state()->is_get_all_instruments_finished() && |
| 236 | state()->available_instruments().size() == 1 && |
| 237 | spec()->stringified_method_data().size() == 1 && |
| 238 | !spec()->request_shipping() && !spec()->request_payer_name() && |
| 239 | !spec()->request_payer_phone() && |
| 240 | !spec()->request_payer_email() |
| 241 | // Only allowing URL base payment apps to skip the payment sheet. |
| 242 | && spec()->url_payment_method_identifiers().size() == 1; |
| 243 | } |
| 244 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 245 | void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 246 | std::string error; |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 247 | if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) { |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 248 | LOG(ERROR) << error; |
| 249 | OnConnectionTerminated(); |
| 250 | return; |
| 251 | } |
Rouslan Solomakhin | 4cbda82 | 2017-08-23 18:50:39 | [diff] [blame] | 252 | |
| 253 | if (!details->total) { |
| 254 | LOG(ERROR) << "Missing total"; |
| 255 | OnConnectionTerminated(); |
| 256 | return; |
| 257 | } |
| 258 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 259 | spec_->UpdateWith(std::move(details)); |
| 260 | } |
| 261 | |
Rouslan Solomakhin | a9ff928 | 2017-10-31 21:58:05 | [diff] [blame] | 262 | void PaymentRequest::NoUpdatedPaymentDetails() { |
| 263 | spec_->RecomputeSpecForDetails(); |
| 264 | } |
| 265 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 266 | void PaymentRequest::Abort() { |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 267 | // The API user has decided to abort. If a successful abort message is |
| 268 | // returned to the renderer, the Mojo message pipe is closed, which triggers |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 269 | // PaymentRequest::OnConnectionTerminated, which destroys this object. |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 270 | // Otherwise, the abort promise is rejected and the pipe is not closed. |
| 271 | // The abort is only successful if the payment app wasn't yet invoked. |
| 272 | // TODO(crbug.com/716546): Add a merchant abort metric |
| 273 | |
| 274 | bool accepting_abort = !state_->IsPaymentAppInvoked(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 275 | if (accepting_abort) |
| 276 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT); |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 277 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 278 | if (client_.is_bound()) |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 279 | client_->OnAbort(accepting_abort); |
| 280 | |
| 281 | if (observer_for_testing_) |
| 282 | observer_for_testing_->OnAbortCalled(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 283 | } |
| 284 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 285 | void PaymentRequest::Complete(mojom::PaymentComplete result) { |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 286 | if (!client_.is_bound()) |
| 287 | return; |
| 288 | |
Rouslan Solomakhin | e347319 | 2017-06-16 14:54:57 | [diff] [blame] | 289 | // Failed transactions show an error. Successful and unknown-state |
| 290 | // transactions don't show an error. |
| 291 | if (result == mojom::PaymentComplete::FAIL) { |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 292 | delegate_->ShowErrorMessage(); |
| 293 | } else { |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 294 | DCHECK(!has_recorded_completion_); |
sebsg | f8272a2 | 2017-05-26 14:32:58 | [diff] [blame] | 295 | journey_logger_.SetCompleted(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 296 | has_recorded_completion_ = true; |
| 297 | |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 298 | delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted, |
| 299 | true); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 300 | // When the renderer closes the connection, |
| 301 | // PaymentRequest::OnConnectionTerminated will be called. |
| 302 | client_->OnComplete(); |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 303 | state_->RecordUseStats(); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 304 | } |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void PaymentRequest::CanMakePayment() { |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 308 | if (observer_for_testing_) |
| 309 | observer_for_testing_->OnCanMakePaymentCalled(); |
Mathieu Perreault | cacb85e | 2018-06-06 20:40:13 | [diff] [blame] | 310 | |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 311 | if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) || |
| 312 | !state_) { |
Mathieu Perreault | cacb85e | 2018-06-06 20:40:13 | [diff] [blame] | 313 | CanMakePaymentCallback(/*can_make_payment=*/false); |
| 314 | } else { |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 315 | state_->CanMakePayment( |
Mathieu Perreault | cacb85e | 2018-06-06 20:40:13 | [diff] [blame] | 316 | base::BindOnce(&PaymentRequest::CanMakePaymentCallback, |
| 317 | weak_ptr_factory_.GetWeakPtr())); |
| 318 | } |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 319 | } |
| 320 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 321 | void PaymentRequest::OnPaymentResponseAvailable( |
| 322 | mojom::PaymentResponsePtr response) { |
mathp | 57c8c86 | 2017-06-16 20:15:45 | [diff] [blame] | 323 | journey_logger_.SetEventOccurred( |
| 324 | JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS); |
gogerald | 7a2b761e | 2017-11-09 18:30:19 | [diff] [blame] | 325 | |
| 326 | // Do not send invalid response to client. |
| 327 | if (response->method_name.empty() || response->stringified_details.empty()) { |
| 328 | RecordFirstAbortReason( |
| 329 | JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR); |
| 330 | delegate_->ShowErrorMessage(); |
| 331 | return; |
| 332 | } |
| 333 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 334 | client_->OnPaymentResponse(std::move(response)); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 335 | } |
| 336 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 337 | void PaymentRequest::OnShippingOptionIdSelected( |
| 338 | std::string shipping_option_id) { |
| 339 | client_->OnShippingOptionChange(shipping_option_id); |
| 340 | } |
| 341 | |
| 342 | void PaymentRequest::OnShippingAddressSelected( |
| 343 | mojom::PaymentAddressPtr address) { |
| 344 | client_->OnShippingAddressChange(std::move(address)); |
| 345 | } |
| 346 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 347 | void PaymentRequest::UserCancelled() { |
| 348 | // If |client_| is not bound, then the object is already being destroyed as |
| 349 | // a result of a renderer event. |
| 350 | if (!client_.is_bound()) |
| 351 | return; |
| 352 | |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 353 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER); |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 354 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 355 | // This sends an error to the renderer, which informs the API user. |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 356 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 357 | |
| 358 | // We close all bindings and ask to be destroyed. |
| 359 | client_.reset(); |
| 360 | binding_.Close(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 361 | if (observer_for_testing_) |
| 362 | observer_for_testing_->OnConnectionTerminated(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 363 | manager_->DestroyRequest(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 364 | } |
| 365 | |
sebsg | d56b3e42 | 2017-10-20 18:08:08 | [diff] [blame] | 366 | void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument( |
| 367 | bool is_user_initiated) { |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 368 | RecordFirstAbortReason(is_user_initiated |
| 369 | ? JourneyLogger::ABORT_REASON_USER_NAVIGATION |
| 370 | : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 371 | } |
| 372 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 373 | void PaymentRequest::OnConnectionTerminated() { |
| 374 | // We are here because of a browser-side error, or likely as a result of the |
| 375 | // connection_error_handler on |binding_|, which can mean that the renderer |
| 376 | // has decided to close the pipe for various reasons (see all uses of |
| 377 | // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close |
| 378 | // the binding and the dialog, and ask to be deleted. |
| 379 | client_.reset(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 380 | binding_.Close(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 381 | delegate_->CloseDialog(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 382 | if (observer_for_testing_) |
| 383 | observer_for_testing_->OnConnectionTerminated(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 384 | |
| 385 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 386 | manager_->DestroyRequest(this); |
| 387 | } |
| 388 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 389 | void PaymentRequest::Pay() { |
mathp | 57c8c86 | 2017-06-16 20:15:45 | [diff] [blame] | 390 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED); |
Mathieu Perreault | 23d25bfb8 | 2018-05-11 14:45:37 | [diff] [blame] | 391 | |
| 392 | // Log the correct "selected instrument" metric according to type. |
| 393 | DCHECK(state_->selected_instrument()); |
| 394 | JourneyLogger::Event selected_event = |
| 395 | JourneyLogger::Event::EVENT_SELECTED_OTHER; |
| 396 | switch (state_->selected_instrument()->type()) { |
| 397 | case PaymentInstrument::Type::AUTOFILL: |
| 398 | selected_event = JourneyLogger::Event::EVENT_SELECTED_CREDIT_CARD; |
| 399 | break; |
| 400 | case PaymentInstrument::Type::SERVICE_WORKER_APP: |
| 401 | selected_event = JourneyLogger::Event::EVENT_SELECTED_OTHER; |
| 402 | break; |
| 403 | case PaymentInstrument::Type::NATIVE_MOBILE_APP: |
| 404 | NOTREACHED(); |
| 405 | break; |
| 406 | } |
| 407 | journey_logger_.SetEventOccurred(selected_event); |
| 408 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 409 | state_->GeneratePaymentResponse(); |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 410 | } |
| 411 | |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 412 | void PaymentRequest::HideIfNecessary() { |
| 413 | display_handle_.reset(); |
| 414 | } |
| 415 | |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 416 | void PaymentRequest::RecordDialogShownEventInJourneyLogger() { |
| 417 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN); |
| 418 | } |
| 419 | |
Anthony Vallee-Dubois | 10d131a | 2018-02-22 15:41:04 | [diff] [blame] | 420 | bool PaymentRequest::IsIncognito() const { |
| 421 | return delegate_->IsIncognito(); |
| 422 | } |
| 423 | |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 424 | void PaymentRequest::RecordFirstAbortReason( |
| 425 | JourneyLogger::AbortReason abort_reason) { |
| 426 | if (!has_recorded_completion_) { |
| 427 | has_recorded_completion_ = true; |
| 428 | journey_logger_.SetAborted(abort_reason); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 432 | void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) { |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 433 | if (!spec_ || CanMakePaymentQueryFactory::GetInstance() |
| 434 | ->GetForContext(web_contents_->GetBrowserContext()) |
| 435 | ->CanQuery(top_level_origin_, frame_origin_, |
| 436 | spec_->stringified_method_data())) { |
Rouslan Solomakhin | 16ee5520 | 2017-10-03 19:04:42 | [diff] [blame] | 437 | RespondToCanMakePaymentQuery(can_make_payment, false); |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 438 | } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) { |
Rouslan Solomakhin | 16ee5520 | 2017-10-03 19:04:42 | [diff] [blame] | 439 | RespondToCanMakePaymentQuery(can_make_payment, true); |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 440 | } else { |
| 441 | client_->OnCanMakePayment( |
| 442 | mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED); |
| 443 | } |
| 444 | |
| 445 | if (observer_for_testing_) |
| 446 | observer_for_testing_->OnCanMakePaymentReturned(); |
| 447 | } |
| 448 | |
Rouslan Solomakhin | 16ee5520 | 2017-10-03 19:04:42 | [diff] [blame] | 449 | void PaymentRequest::RespondToCanMakePaymentQuery(bool can_make_payment, |
| 450 | bool warn_localhost_or_file) { |
Rouslan Solomakhin | 16ee5520 | 2017-10-03 19:04:42 | [diff] [blame] | 451 | mojom::CanMakePaymentQueryResult positive = |
| 452 | warn_localhost_or_file |
| 453 | ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT |
| 454 | : mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT; |
| 455 | mojom::CanMakePaymentQueryResult negative = |
| 456 | warn_localhost_or_file |
| 457 | ? mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT |
| 458 | : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT; |
| 459 | |
| 460 | client_->OnCanMakePayment(can_make_payment ? positive : negative); |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 461 | journey_logger_.SetCanMakePaymentValue(can_make_payment); |
| 462 | } |
| 463 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 464 | } // namespace payments |