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 | |
Sebastien Marchand | 53801a3 | 2019-01-25 16:26:11 | [diff] [blame] | 10 | #include "base/bind.h" |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 11 | #include "base/feature_list.h" |
Mathieu Perreault | 627b97c | 2017-08-12 00:44:22 | [diff] [blame] | 12 | #include "base/stl_util.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 13 | #include "components/payments/content/can_make_payment_query_factory.h" |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 14 | #include "components/payments/content/content_payment_request_delegate.h" |
Rouslan Solomakhin | cf9093f | 2019-05-20 15:32:17 | [diff] [blame] | 15 | #include "components/payments/content/payment_details_converter.h" |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 16 | #include "components/payments/content/payment_request_converter.h" |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 17 | #include "components/payments/content/payment_request_web_contents_manager.h" |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 18 | #include "components/payments/content/service_worker_payment_instrument.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 19 | #include "components/payments/core/can_make_payment_query.h" |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 20 | #include "components/payments/core/error_strings.h" |
Anthony Vallee-Dubois | 968ae4d | 2018-03-15 16:56:36 | [diff] [blame] | 21 | #include "components/payments/core/features.h" |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 22 | #include "components/payments/core/payment_details.h" |
| 23 | #include "components/payments/core/payment_details_validation.h" |
Mathieu Perreault | 23d25bfb8 | 2018-05-11 14:45:37 | [diff] [blame] | 24 | #include "components/payments/core/payment_instrument.h" |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 25 | #include "components/payments/core/payment_prefs.h" |
Danyao Wang | 50ccb9f | 2019-05-09 23:28:03 | [diff] [blame] | 26 | #include "components/payments/core/payments_experimental_features.h" |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 27 | #include "components/payments/core/payments_validators.h" |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 28 | #include "components/payments/core/url_util.h" |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 29 | #include "components/prefs/pref_service.h" |
Steven Holte | 2083e8bc | 2018-07-16 23:50:36 | [diff] [blame] | 30 | #include "components/ukm/content/source_url_recorder.h" |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 31 | #include "components/url_formatter/elide_url.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 32 | #include "content/public/browser/browser_thread.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 33 | #include "content/public/browser/render_frame_host.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 34 | #include "content/public/browser/web_contents.h" |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 35 | #include "content/public/common/content_features.h" |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 36 | #include "content/public/common/origin_util.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 37 | |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 38 | namespace payments { |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 39 | namespace { |
| 40 | |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 41 | using ::payments::mojom::CanMakePaymentQueryResult; |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 42 | using ::payments::mojom::HasEnrolledInstrumentQueryResult; |
| 43 | |
Sahel Sharify | 2688438 | 2019-05-07 16:23:51 | [diff] [blame] | 44 | bool IsGooglePaymentMethodInstrumentSelected(const std::string& method_name) { |
| 45 | return method_name == kGooglePayMethodName || |
| 46 | method_name == kAndroidPayMethodName; |
| 47 | } |
| 48 | |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 49 | } // namespace |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 50 | |
| 51 | PaymentRequest::PaymentRequest( |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 52 | content::RenderFrameHost* render_frame_host, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 53 | content::WebContents* web_contents, |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 54 | std::unique_ptr<ContentPaymentRequestDelegate> delegate, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 55 | PaymentRequestWebContentsManager* manager, |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 56 | PaymentRequestDisplayManager* display_manager, |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 57 | mojo::InterfaceRequest<mojom::PaymentRequest> request, |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 58 | ObserverForTest* observer_for_testing) |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 59 | : web_contents_(web_contents), |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 60 | log_(web_contents_), |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 61 | delegate_(std::move(delegate)), |
| 62 | manager_(manager), |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 63 | display_manager_(display_manager), |
| 64 | display_handle_(nullptr), |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 65 | binding_(this, std::move(request)), |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 66 | payment_handler_host_(this), |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 67 | top_level_origin_(url_formatter::FormatUrlForSecurityDisplay( |
| 68 | web_contents_->GetLastCommittedURL())), |
| 69 | frame_origin_(url_formatter::FormatUrlForSecurityDisplay( |
| 70 | render_frame_host->GetLastCommittedURL())), |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 71 | observer_for_testing_(observer_for_testing), |
| 72 | journey_logger_(delegate_->IsIncognito(), |
Steven Holte | 2083e8bc | 2018-07-16 23:50:36 | [diff] [blame] | 73 | ukm::GetSourceIdForWebContentsDocument(web_contents)), |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 74 | weak_ptr_factory_(this) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 75 | // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
| 76 | // will happen as a result of many renderer-side events (both successful and |
| 77 | // erroneous in nature). |
| 78 | // TODO(crbug.com/683636): Investigate using |
| 79 | // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 80 | binding_.set_connection_error_handler(base::BindOnce( |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 81 | &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr())); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | PaymentRequest::~PaymentRequest() {} |
| 85 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 86 | void PaymentRequest::Init(mojom::PaymentRequestClientPtr client, |
| 87 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 88 | mojom::PaymentDetailsPtr details, |
| 89 | mojom::PaymentOptionsPtr options) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 90 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 91 | |
| 92 | if (is_initialized_) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 93 | log_.Error(errors::kAttemptedInitializationTwice); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 94 | OnConnectionTerminated(); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | is_initialized_ = true; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 99 | client_ = std::move(client); |
| 100 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 101 | const GURL last_committed_url = delegate_->GetLastCommittedURL(); |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 102 | if (!content::IsOriginSecure(last_committed_url)) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 103 | log_.Error(errors::kNotInASecureOrigin); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 104 | OnConnectionTerminated(); |
| 105 | return; |
| 106 | } |
| 107 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 108 | bool allowed_origin = |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 109 | UrlUtil::IsOriginAllowedToUseWebPaymentApis(last_committed_url); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 110 | if (!allowed_origin) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 111 | log_.Error(errors::kProhibitedOrigin); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 112 | } |
| 113 | |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 114 | bool invalid_ssl = last_committed_url.SchemeIsCryptographic() && |
| 115 | !delegate_->IsSslCertificateValid(); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 116 | if (invalid_ssl) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 117 | log_.Error(errors::kInvalidSslCertificate); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 118 | } |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 119 | |
| 120 | if (!allowed_origin || invalid_ssl) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 121 | // Intentionally don't set |spec_| and |state_|, so the UI is never shown. |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 122 | log_.Error(errors::kProhibitedOriginOrInvalidSslExplanation); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 126 | std::string error; |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 127 | if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 128 | log_.Error(error); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 129 | OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 130 | return; |
| 131 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 132 | |
jinho.bang | fcb5ec9 | 2017-03-29 08:08:02 | [diff] [blame] | 133 | if (!details->total) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 134 | log_.Error(errors::kTotalRequired); |
jinho.bang | fcb5ec9 | 2017-03-29 08:08:02 | [diff] [blame] | 135 | OnConnectionTerminated(); |
| 136 | return; |
| 137 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 138 | |
sebsg | a70a6da | 2017-12-21 22:27:02 | [diff] [blame] | 139 | spec_ = std::make_unique<PaymentRequestSpec>( |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 140 | std::move(options), std::move(details), std::move(method_data), this, |
| 141 | delegate_->GetApplicationLocale()); |
sebsg | a70a6da | 2017-12-21 22:27:02 | [diff] [blame] | 142 | state_ = std::make_unique<PaymentRequestState>( |
Rouslan Solomakhin | dbf593d9 | 2017-11-21 19:20:57 | [diff] [blame] | 143 | web_contents_, top_level_origin_, frame_origin_, spec_.get(), this, |
| 144 | delegate_->GetApplicationLocale(), delegate_->GetPersonalDataManager(), |
| 145 | delegate_.get(), &journey_logger_); |
Mathieu Perreault | 627b97c | 2017-08-12 00:44:22 | [diff] [blame] | 146 | |
| 147 | journey_logger_.SetRequestedInformation( |
| 148 | spec_->request_shipping(), spec_->request_payer_email(), |
| 149 | spec_->request_payer_phone(), spec_->request_payer_name()); |
| 150 | |
| 151 | // Log metrics around which payment methods are requested by the merchant. |
| 152 | GURL google_pay_url(kGooglePayMethodName); |
| 153 | GURL android_pay_url(kAndroidPayMethodName); |
| 154 | // Looking for payment methods that are NOT google-related payment methods. |
| 155 | auto non_google_it = |
| 156 | std::find_if(spec_->url_payment_method_identifiers().begin(), |
| 157 | spec_->url_payment_method_identifiers().end(), |
| 158 | [google_pay_url, android_pay_url](const GURL& url) { |
| 159 | return url != google_pay_url && url != android_pay_url; |
| 160 | }); |
| 161 | journey_logger_.SetRequestedPaymentMethodTypes( |
| 162 | /*requested_basic_card=*/!spec_->supported_card_networks().empty(), |
| 163 | /*requested_method_google=*/ |
| 164 | base::ContainsValue(spec_->url_payment_method_identifiers(), |
| 165 | google_pay_url) || |
| 166 | base::ContainsValue(spec_->url_payment_method_identifiers(), |
| 167 | android_pay_url), |
| 168 | /*requested_method_other=*/non_google_it != |
| 169 | spec_->url_payment_method_identifiers().end()); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 170 | } |
| 171 | |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 172 | void PaymentRequest::Show(bool is_user_gesture, bool wait_for_updated_details) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 173 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 174 | log_.Error(errors::kCannotShowWithoutInit); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 175 | OnConnectionTerminated(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 176 | return; |
| 177 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 178 | |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 179 | if (is_show_called_) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 180 | log_.Error(errors::kCannotShowTwice); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 181 | OnConnectionTerminated(); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | is_show_called_ = true; |
| 186 | |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 187 | // A tab can display only one PaymentRequest UI at a time. |
Anthony Vallee-Dubois | 8f5e7e1 | 2018-01-12 16:14:06 | [diff] [blame] | 188 | display_handle_ = display_manager_->TryShow(delegate_.get()); |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 189 | if (!display_handle_) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 190 | log_.Error(errors::kAnotherUiShowing); |
sebsg | 828269bc | 2017-06-09 19:11:12 | [diff] [blame] | 191 | journey_logger_.SetNotShown( |
| 192 | JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS); |
Rouslan Solomakhin | b2d233c4 | 2018-08-30 16:18:40 | [diff] [blame] | 193 | client_->OnError(mojom::PaymentErrorReason::ALREADY_SHOWING); |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 194 | OnConnectionTerminated(); |
| 195 | return; |
| 196 | } |
| 197 | |
Rouslan Solomakhin | 5b51043 | 2017-09-26 16:59:32 | [diff] [blame] | 198 | if (!delegate_->IsBrowserWindowActive()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 199 | log_.Error(errors::kCannotShowInBackgroundTab); |
Rouslan Solomakhin | 5b51043 | 2017-09-26 16:59:32 | [diff] [blame] | 200 | journey_logger_.SetNotShown(JourneyLogger::NOT_SHOWN_REASON_OTHER); |
| 201 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
| 202 | OnConnectionTerminated(); |
| 203 | return; |
| 204 | } |
| 205 | |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 206 | if (!state_) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 207 | // SSL is not valid. Reject show with NotSupportedError, disconnect the |
| 208 | // mojo pipe, and destroy this object. |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 209 | AreRequestedMethodsSupportedCallback(false); |
| 210 | return; |
| 211 | } |
| 212 | |
Rouslan Solomakhin | 833f851 | 2018-04-03 23:19:25 | [diff] [blame] | 213 | is_show_user_gesture_ = is_user_gesture; |
| 214 | |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 215 | if (wait_for_updated_details) { |
| 216 | // Put |spec_| into uninitialized state, so the UI knows to show a spinner. |
| 217 | // This method does not block. |
| 218 | spec_->StartWaitingForUpdateWith( |
| 219 | PaymentRequestSpec::UpdateReason::INITIAL_PAYMENT_DETAILS); |
| 220 | } |
Takashi Sakamoto | 48a2970 | 2019-04-08 05:06:32 | [diff] [blame] | 221 | |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 222 | display_handle_->Show(this); |
gogerald | 0a7ee6c | 2017-11-13 18:23:19 | [diff] [blame] | 223 | state_->AreRequestedMethodsSupported( |
| 224 | base::BindOnce(&PaymentRequest::AreRequestedMethodsSupportedCallback, |
| 225 | weak_ptr_factory_.GetWeakPtr())); |
| 226 | } |
| 227 | |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 228 | void PaymentRequest::Retry(mojom::PaymentValidationErrorsPtr errors) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 229 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 230 | log_.Error(errors::kCannotRetryWithoutInit); |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 231 | OnConnectionTerminated(); |
| 232 | return; |
| 233 | } |
| 234 | |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 235 | if (!IsThisPaymentRequestShowing()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 236 | log_.Error(errors::kCannotRetryWithoutShow); |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 237 | OnConnectionTerminated(); |
| 238 | return; |
| 239 | } |
| 240 | |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 241 | std::string error; |
| 242 | if (!PaymentsValidators::IsValidPaymentValidationErrorsFormat(errors, |
| 243 | &error)) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 244 | log_.Error(error); |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 245 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
| 246 | OnConnectionTerminated(); |
| 247 | return; |
| 248 | } |
| 249 | |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 250 | spec()->Retry(std::move(errors)); |
Jinho Bang | cac8d9a0 | 2018-08-23 19:47:22 | [diff] [blame] | 251 | display_handle_->Retry(); |
Jinho Bang | be463a2 | 2018-08-02 10:26:50 | [diff] [blame] | 252 | } |
| 253 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 254 | void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 255 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 256 | log_.Error(errors::kCannotUpdateWithoutInit); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 257 | OnConnectionTerminated(); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (!IsThisPaymentRequestShowing()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 262 | log_.Error(errors::kCannotUpdateWithoutShow); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 263 | OnConnectionTerminated(); |
| 264 | return; |
| 265 | } |
| 266 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 267 | std::string error; |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame] | 268 | if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 269 | log_.Error(error); |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 270 | OnConnectionTerminated(); |
| 271 | return; |
| 272 | } |
Rouslan Solomakhin | 4cbda82 | 2017-08-23 18:50:39 | [diff] [blame] | 273 | |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 274 | if (details->shipping_address_errors && |
| 275 | !PaymentsValidators::IsValidAddressErrorsFormat( |
| 276 | details->shipping_address_errors, &error)) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 277 | log_.Error(error); |
Jinho Bang | 092e716 | 2018-09-06 23:41:19 | [diff] [blame] | 278 | OnConnectionTerminated(); |
| 279 | return; |
| 280 | } |
| 281 | |
Rouslan Solomakhin | cf9093f | 2019-05-20 15:32:17 | [diff] [blame] | 282 | if (state()->selected_instrument() && state()->IsPaymentAppInvoked() && |
| 283 | payment_handler_host_.is_changing_payment_method()) { |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 284 | payment_handler_host_.UpdateWith( |
Rouslan Solomakhin | cf9093f | 2019-05-20 15:32:17 | [diff] [blame] | 285 | PaymentDetailsConverter::ConvertToPaymentMethodChangeResponse( |
| 286 | details, base::BindRepeating( |
| 287 | &PaymentInstrument::IsValidForPaymentMethodIdentifier, |
| 288 | base::Unretained(state()->selected_instrument())))); |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 289 | } |
| 290 | |
Rouslan Solomakhin | 6ba46fd | 2019-04-11 23:44:01 | [diff] [blame] | 291 | bool is_resolving_promise_passed_into_show_method = !spec_->IsInitialized(); |
| 292 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 293 | spec_->UpdateWith(std::move(details)); |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 294 | |
| 295 | if (is_resolving_promise_passed_into_show_method) { |
| 296 | if (SatisfiesSkipUIConstraints()) { |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 297 | Pay(); |
| 298 | } else if (spec_->request_shipping()) { |
| 299 | state_->SelectDefaultShippingAddressAndNotifyObservers(); |
| 300 | } |
| 301 | } |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 302 | } |
| 303 | |
Rouslan Solomakhin | a9ff928 | 2017-10-31 21:58:05 | [diff] [blame] | 304 | void PaymentRequest::NoUpdatedPaymentDetails() { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 305 | // This Mojo call is triggered by the user of the API doing nothing in |
| 306 | // response to a shipping address update event, so the error messages cannot |
| 307 | // be more verbose. |
| 308 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 309 | log_.Error(errors::kNotInitialized); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 310 | OnConnectionTerminated(); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | if (!IsThisPaymentRequestShowing()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 315 | log_.Error(errors::kNotShown); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 316 | OnConnectionTerminated(); |
| 317 | return; |
| 318 | } |
| 319 | |
Rouslan Solomakhin | a9ff928 | 2017-10-31 21:58:05 | [diff] [blame] | 320 | spec_->RecomputeSpecForDetails(); |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 321 | |
Rouslan Solomakhin | cf9093f | 2019-05-20 15:32:17 | [diff] [blame] | 322 | if (state()->IsPaymentAppInvoked() && |
| 323 | payment_handler_host_.is_changing_payment_method()) { |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 324 | payment_handler_host_.NoUpdatedPaymentDetails(); |
Rouslan Solomakhin | cf9093f | 2019-05-20 15:32:17 | [diff] [blame] | 325 | } |
Rouslan Solomakhin | a9ff928 | 2017-10-31 21:58:05 | [diff] [blame] | 326 | } |
| 327 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 328 | void PaymentRequest::Abort() { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 329 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 330 | log_.Error(errors::kCannotAbortWithoutInit); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 331 | OnConnectionTerminated(); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | if (!IsThisPaymentRequestShowing()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 336 | log_.Error(errors::kCannotAbortWithoutShow); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 337 | OnConnectionTerminated(); |
| 338 | return; |
| 339 | } |
| 340 | |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 341 | // The API user has decided to abort. If a successful abort message is |
| 342 | // returned to the renderer, the Mojo message pipe is closed, which triggers |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 343 | // PaymentRequest::OnConnectionTerminated, which destroys this object. |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 344 | // Otherwise, the abort promise is rejected and the pipe is not closed. |
| 345 | // The abort is only successful if the payment app wasn't yet invoked. |
| 346 | // TODO(crbug.com/716546): Add a merchant abort metric |
| 347 | |
| 348 | bool accepting_abort = !state_->IsPaymentAppInvoked(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 349 | if (accepting_abort) |
| 350 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT); |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 351 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 352 | if (client_.is_bound()) |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 353 | client_->OnAbort(accepting_abort); |
| 354 | |
| 355 | if (observer_for_testing_) |
| 356 | observer_for_testing_->OnAbortCalled(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 357 | } |
| 358 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 359 | void PaymentRequest::Complete(mojom::PaymentComplete result) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 360 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 361 | log_.Error(errors::kCannotCompleteWithoutInit); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 362 | OnConnectionTerminated(); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 363 | return; |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (!IsThisPaymentRequestShowing()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 367 | log_.Error(errors::kCannotAbortWithoutShow); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 368 | OnConnectionTerminated(); |
| 369 | return; |
| 370 | } |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 371 | |
Rouslan Solomakhin | e347319 | 2017-06-16 14:54:57 | [diff] [blame] | 372 | // Failed transactions show an error. Successful and unknown-state |
| 373 | // transactions don't show an error. |
| 374 | if (result == mojom::PaymentComplete::FAIL) { |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 375 | delegate_->ShowErrorMessage(); |
| 376 | } else { |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 377 | DCHECK(!has_recorded_completion_); |
sebsg | f8272a2 | 2017-05-26 14:32:58 | [diff] [blame] | 378 | journey_logger_.SetCompleted(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 379 | has_recorded_completion_ = true; |
| 380 | |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 381 | delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted, |
| 382 | true); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 383 | // When the renderer closes the connection, |
| 384 | // PaymentRequest::OnConnectionTerminated will be called. |
| 385 | client_->OnComplete(); |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 386 | state_->RecordUseStats(); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 387 | } |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 388 | } |
| 389 | |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 390 | void PaymentRequest::CanMakePayment(bool legacy_mode) { |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 391 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 392 | log_.Error(errors::kCannotCallCanMakePaymentWithoutInit); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 393 | OnConnectionTerminated(); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | // It's valid to call canMakePayment() without calling show() first. |
| 398 | |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 399 | if (observer_for_testing_) |
| 400 | observer_for_testing_->OnCanMakePaymentCalled(); |
Mathieu Perreault | cacb85e | 2018-06-06 20:40:13 | [diff] [blame] | 401 | |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 402 | if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) || |
| 403 | !state_) { |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 404 | CanMakePaymentCallback(legacy_mode, /*can_make_payment=*/false); |
Mathieu Perreault | cacb85e | 2018-06-06 20:40:13 | [diff] [blame] | 405 | } else { |
Rouslan Solomakhin | d2cae95a | 2018-08-09 00:16:10 | [diff] [blame] | 406 | state_->CanMakePayment( |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 407 | legacy_mode, |
Mathieu Perreault | cacb85e | 2018-06-06 20:40:13 | [diff] [blame] | 408 | base::BindOnce(&PaymentRequest::CanMakePaymentCallback, |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 409 | weak_ptr_factory_.GetWeakPtr(), legacy_mode)); |
Mathieu Perreault | cacb85e | 2018-06-06 20:40:13 | [diff] [blame] | 410 | } |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 411 | } |
| 412 | |
Rouslan Solomakhin | 5683eb28 | 2019-01-29 18:06:03 | [diff] [blame] | 413 | void PaymentRequest::HasEnrolledInstrument(bool per_method_quota) { |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 414 | if (!IsInitialized()) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 415 | log_.Error(errors::kCannotCallHasEnrolledInstrumentWithoutInit); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 416 | OnConnectionTerminated(); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | // It's valid to call hasEnrolledInstrument() without calling show() first. |
| 421 | |
| 422 | if (observer_for_testing_) |
| 423 | observer_for_testing_->OnHasEnrolledInstrumentCalled(); |
| 424 | |
| 425 | if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) || |
| 426 | !state_) { |
Rouslan Solomakhin | 5683eb28 | 2019-01-29 18:06:03 | [diff] [blame] | 427 | HasEnrolledInstrumentCallback(per_method_quota, |
| 428 | /*has_enrolled_instrument=*/false); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 429 | } else { |
| 430 | state_->HasEnrolledInstrument( |
| 431 | base::BindOnce(&PaymentRequest::HasEnrolledInstrumentCallback, |
Rouslan Solomakhin | 5683eb28 | 2019-01-29 18:06:03 | [diff] [blame] | 432 | weak_ptr_factory_.GetWeakPtr(), per_method_quota)); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 436 | bool PaymentRequest::ChangePaymentMethod(const std::string& method_name, |
| 437 | const std::string& stringified_data) { |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 438 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 439 | DCHECK(!method_name.empty()); |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 440 | |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 441 | if (!state_ || !state_->IsPaymentAppInvoked() || !client_) |
| 442 | return false; |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 443 | |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 444 | client_->OnPaymentMethodChange(method_name, stringified_data); |
| 445 | return true; |
Rouslan Solomakhin | a480efa | 2019-05-06 15:37:22 | [diff] [blame] | 446 | } |
| 447 | |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 448 | void PaymentRequest::AreRequestedMethodsSupportedCallback( |
| 449 | bool methods_supported) { |
| 450 | if (methods_supported) { |
Sahel Sharify | d3f1bc8 | 2019-05-21 18:48:46 | [diff] [blame] | 451 | if (SatisfiesSkipUIConstraints()) |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 452 | Pay(); |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 453 | } else { |
| 454 | journey_logger_.SetNotShown( |
| 455 | JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD); |
| 456 | client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED); |
| 457 | if (observer_for_testing_) |
| 458 | observer_for_testing_->OnNotSupportedError(); |
| 459 | OnConnectionTerminated(); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | bool PaymentRequest::IsInitialized() const { |
| 464 | return is_initialized_ && client_ && client_.is_bound() && |
| 465 | binding_.is_bound(); |
| 466 | } |
| 467 | |
| 468 | bool PaymentRequest::IsThisPaymentRequestShowing() const { |
| 469 | return is_show_called_ && display_handle_ && spec_ && state_; |
| 470 | } |
| 471 | |
Sahel Sharify | d3f1bc8 | 2019-05-21 18:48:46 | [diff] [blame] | 472 | bool PaymentRequest::SatisfiesSkipUIConstraints() { |
Rouslan Solomakhin | 9788d4b | 2019-04-09 13:10:23 | [diff] [blame] | 473 | // Only allowing URL base payment apps to skip the payment sheet. |
Sahel Sharify | d3f1bc8 | 2019-05-21 18:48:46 | [diff] [blame] | 474 | skipped_payment_request_ui_ = |
| 475 | (spec()->url_payment_method_identifiers().size() == 1 || |
| 476 | skip_ui_for_non_url_payment_method_identifiers_for_test_) && |
| 477 | base::FeatureList::IsEnabled(features::kWebPaymentsSingleAppUiSkip) && |
| 478 | base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps) && |
| 479 | is_show_user_gesture_ && state()->IsInitialized() && |
| 480 | spec()->IsInitialized() && state()->available_instruments().size() == 1 && |
| 481 | spec()->stringified_method_data().size() == 1 && |
| 482 | !spec()->request_shipping() && !spec()->request_payer_name() && |
| 483 | !spec()->request_payer_phone() && !spec()->request_payer_email(); |
| 484 | if (skipped_payment_request_ui_) { |
| 485 | DCHECK(state()->IsInitialized() && spec()->IsInitialized()); |
| 486 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SKIPPED_SHOW); |
| 487 | } else if (state()->IsInitialized() && spec()->IsInitialized()) { |
| 488 | // Set EVENT_SHOWN only after state() and spec() initialization. |
| 489 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN); |
| 490 | } |
| 491 | return skipped_payment_request_ui_; |
Rouslan Solomakhin | 2706470 | 2018-12-14 21:15:33 | [diff] [blame] | 492 | } |
| 493 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 494 | void PaymentRequest::OnPaymentResponseAvailable( |
| 495 | mojom::PaymentResponsePtr response) { |
mathp | 57c8c86 | 2017-06-16 20:15:45 | [diff] [blame] | 496 | journey_logger_.SetEventOccurred( |
| 497 | JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS); |
gogerald | 7a2b761e | 2017-11-09 18:30:19 | [diff] [blame] | 498 | |
| 499 | // Do not send invalid response to client. |
| 500 | if (response->method_name.empty() || response->stringified_details.empty()) { |
| 501 | RecordFirstAbortReason( |
| 502 | JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR); |
| 503 | delegate_->ShowErrorMessage(); |
| 504 | return; |
| 505 | } |
| 506 | |
Sahel Sharify | 2688438 | 2019-05-07 16:23:51 | [diff] [blame] | 507 | // Log the correct "selected instrument" metric according to its type and |
| 508 | // the method name in response. |
| 509 | DCHECK(state_->selected_instrument()); |
| 510 | JourneyLogger::Event selected_event = |
| 511 | JourneyLogger::Event::EVENT_SELECTED_OTHER; |
| 512 | switch (state_->selected_instrument()->type()) { |
| 513 | case PaymentInstrument::Type::AUTOFILL: |
| 514 | selected_event = JourneyLogger::Event::EVENT_SELECTED_CREDIT_CARD; |
| 515 | break; |
| 516 | case PaymentInstrument::Type::SERVICE_WORKER_APP: { |
| 517 | selected_event = |
| 518 | IsGooglePaymentMethodInstrumentSelected(response->method_name) |
| 519 | ? JourneyLogger::Event::EVENT_SELECTED_GOOGLE |
| 520 | : JourneyLogger::Event::EVENT_SELECTED_OTHER; |
| 521 | break; |
| 522 | } |
| 523 | case PaymentInstrument::Type::NATIVE_MOBILE_APP: |
| 524 | NOTREACHED(); |
| 525 | break; |
| 526 | } |
| 527 | journey_logger_.SetEventOccurred(selected_event); |
| 528 | |
Rouslan Solomakhin | 02d086ec | 2019-01-31 23:10:39 | [diff] [blame] | 529 | // If currently interactive, show the processing spinner. Autofill payment |
| 530 | // instruments request a CVC, so they are always interactive at this point. A |
| 531 | // payment handler may elect to be non-interactive by not showing a |
| 532 | // confirmation page to the user. |
| 533 | if (delegate_->IsInteractive()) |
| 534 | delegate_->ShowProcessingSpinner(); |
| 535 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 536 | client_->OnPaymentResponse(std::move(response)); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 537 | } |
| 538 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 539 | void PaymentRequest::OnShippingOptionIdSelected( |
| 540 | std::string shipping_option_id) { |
| 541 | client_->OnShippingOptionChange(shipping_option_id); |
| 542 | } |
| 543 | |
| 544 | void PaymentRequest::OnShippingAddressSelected( |
| 545 | mojom::PaymentAddressPtr address) { |
Danyao Wang | 50ccb9f | 2019-05-09 23:28:03 | [diff] [blame] | 546 | // Redact shipping address before exposing it in ShippingAddressChangeEvent. |
| 547 | // https://w3c.github.io/payment-request/#shipping-address-changed-algorithm |
| 548 | if (PaymentsExperimentalFeatures::IsEnabled( |
| 549 | features::kWebPaymentsRedactShippingAddress)) { |
| 550 | address->organization.clear(); |
| 551 | address->phone.clear(); |
| 552 | address->recipient.clear(); |
| 553 | address->address_line.clear(); |
| 554 | } |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 555 | client_->OnShippingAddressChange(std::move(address)); |
| 556 | } |
| 557 | |
Jinho Bang | bb17815 | 2018-09-13 09:44:43 | [diff] [blame] | 558 | void PaymentRequest::OnPayerInfoSelected(mojom::PayerDetailPtr payer_info) { |
| 559 | client_->OnPayerDetailChange(std::move(payer_info)); |
| 560 | } |
| 561 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 562 | void PaymentRequest::UserCancelled() { |
| 563 | // If |client_| is not bound, then the object is already being destroyed as |
| 564 | // a result of a renderer event. |
| 565 | if (!client_.is_bound()) |
| 566 | return; |
| 567 | |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 568 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER); |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 569 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 570 | // This sends an error to the renderer, which informs the API user. |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 571 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 572 | |
| 573 | // We close all bindings and ask to be destroyed. |
| 574 | client_.reset(); |
| 575 | binding_.Close(); |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 576 | payment_handler_host_.Disconnect(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 577 | if (observer_for_testing_) |
| 578 | observer_for_testing_->OnConnectionTerminated(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 579 | manager_->DestroyRequest(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 580 | } |
| 581 | |
sebsg | d56b3e42 | 2017-10-20 18:08:08 | [diff] [blame] | 582 | void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument( |
| 583 | bool is_user_initiated) { |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 584 | RecordFirstAbortReason(is_user_initiated |
| 585 | ? JourneyLogger::ABORT_REASON_USER_NAVIGATION |
| 586 | : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 587 | } |
| 588 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 589 | void PaymentRequest::OnConnectionTerminated() { |
| 590 | // We are here because of a browser-side error, or likely as a result of the |
| 591 | // connection_error_handler on |binding_|, which can mean that the renderer |
| 592 | // has decided to close the pipe for various reasons (see all uses of |
| 593 | // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close |
| 594 | // the binding and the dialog, and ask to be deleted. |
| 595 | client_.reset(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 596 | binding_.Close(); |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 597 | payment_handler_host_.Disconnect(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 598 | delegate_->CloseDialog(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 599 | if (observer_for_testing_) |
| 600 | observer_for_testing_->OnConnectionTerminated(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 601 | |
| 602 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 603 | manager_->DestroyRequest(this); |
| 604 | } |
| 605 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 606 | void PaymentRequest::Pay() { |
mathp | 57c8c86 | 2017-06-16 20:15:45 | [diff] [blame] | 607 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED); |
Mathieu Perreault | 23d25bfb8 | 2018-05-11 14:45:37 | [diff] [blame] | 608 | DCHECK(state_->selected_instrument()); |
Sahel Sharify | 2688438 | 2019-05-07 16:23:51 | [diff] [blame] | 609 | if (state_->selected_instrument()->type() == |
| 610 | PaymentInstrument::Type::SERVICE_WORKER_APP) { |
Rouslan Solomakhin | 8e9f149b2 | 2019-05-10 17:43:02 | [diff] [blame] | 611 | static_cast<ServiceWorkerPaymentInstrument*>(state_->selected_instrument()) |
| 612 | ->set_payment_handler_host(payment_handler_host_.Bind()); |
Mathieu Perreault | 23d25bfb8 | 2018-05-11 14:45:37 | [diff] [blame] | 613 | } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 614 | state_->GeneratePaymentResponse(); |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 615 | } |
| 616 | |
Anthony Vallee-Dubois | c7ae733 | 2017-12-19 20:44:07 | [diff] [blame] | 617 | void PaymentRequest::HideIfNecessary() { |
| 618 | display_handle_.reset(); |
| 619 | } |
| 620 | |
Anthony Vallee-Dubois | 10d131a | 2018-02-22 15:41:04 | [diff] [blame] | 621 | bool PaymentRequest::IsIncognito() const { |
| 622 | return delegate_->IsIncognito(); |
| 623 | } |
| 624 | |
Sahel Sharify | d3f1bc8 | 2019-05-21 18:48:46 | [diff] [blame] | 625 | void PaymentRequest::SetSkipUiForNonUrlPaymentMethodIdentifiersForTest() { |
| 626 | journey_logger_.set_skip_ui_for_non_url_payment_method_identifiers_for_test(); |
| 627 | skip_ui_for_non_url_payment_method_identifiers_for_test_ = true; |
| 628 | } |
| 629 | |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 630 | void PaymentRequest::RecordFirstAbortReason( |
| 631 | JourneyLogger::AbortReason abort_reason) { |
| 632 | if (!has_recorded_completion_) { |
| 633 | has_recorded_completion_ = true; |
| 634 | journey_logger_.SetAborted(abort_reason); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 638 | void PaymentRequest::CanMakePaymentCallback(bool legacy_mode, |
| 639 | bool can_make_payment) { |
| 640 | // Only need to enforce query quota in legacy mode. Per-method quota not |
| 641 | // supported. |
| 642 | if (legacy_mode && spec_ && |
| 643 | !CanMakePaymentQueryFactory::GetInstance() |
| 644 | ->GetForContext(web_contents_->GetBrowserContext()) |
| 645 | ->CanQuery(top_level_origin_, frame_origin_, |
| 646 | spec_->stringified_method_data(), |
| 647 | /*per_method_quota=*/false)) { |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 648 | if (UrlUtil::IsLocalDevelopmentUrl(frame_origin_)) { |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 649 | client_->OnCanMakePayment( |
| 650 | can_make_payment |
| 651 | ? CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT |
| 652 | : CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT); |
| 653 | } else { |
| 654 | client_->OnCanMakePayment( |
| 655 | CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED); |
| 656 | } |
| 657 | } else { |
| 658 | client_->OnCanMakePayment( |
| 659 | can_make_payment |
| 660 | ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT |
| 661 | : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT); |
| 662 | } |
Danyao Wang | 4bc0606a | 2018-12-27 16:54:53 | [diff] [blame] | 663 | |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 664 | journey_logger_.SetCanMakePaymentValue(can_make_payment); |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 665 | |
| 666 | if (observer_for_testing_) |
| 667 | observer_for_testing_->OnCanMakePaymentReturned(); |
| 668 | } |
| 669 | |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 670 | void PaymentRequest::HasEnrolledInstrumentCallback( |
Rouslan Solomakhin | 5683eb28 | 2019-01-29 18:06:03 | [diff] [blame] | 671 | bool per_method_quota, |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 672 | bool has_enrolled_instrument) { |
Rouslan Solomakhin | 5683eb28 | 2019-01-29 18:06:03 | [diff] [blame] | 673 | if (!spec_ || |
| 674 | CanMakePaymentQueryFactory::GetInstance() |
| 675 | ->GetForContext(web_contents_->GetBrowserContext()) |
| 676 | ->CanQuery(top_level_origin_, frame_origin_, |
| 677 | spec_->stringified_method_data(), per_method_quota)) { |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 678 | RespondToHasEnrolledInstrumentQuery(has_enrolled_instrument, |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 679 | /*warn_local_development=*/false); |
| 680 | } else if (UrlUtil::IsLocalDevelopmentUrl(frame_origin_)) { |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 681 | RespondToHasEnrolledInstrumentQuery(has_enrolled_instrument, |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 682 | /*warn_local_development=*/true); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 683 | } else { |
| 684 | client_->OnHasEnrolledInstrument( |
| 685 | HasEnrolledInstrumentQueryResult::QUERY_QUOTA_EXCEEDED); |
| 686 | } |
| 687 | |
| 688 | if (observer_for_testing_) |
| 689 | observer_for_testing_->OnHasEnrolledInstrumentReturned(); |
| 690 | } |
| 691 | |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 692 | void PaymentRequest::RespondToHasEnrolledInstrumentQuery( |
| 693 | bool has_enrolled_instrument, |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 694 | bool warn_local_development) { |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 695 | HasEnrolledInstrumentQueryResult positive = |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 696 | warn_local_development |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 697 | ? HasEnrolledInstrumentQueryResult::WARNING_HAS_ENROLLED_INSTRUMENT |
| 698 | : HasEnrolledInstrumentQueryResult::HAS_ENROLLED_INSTRUMENT; |
| 699 | HasEnrolledInstrumentQueryResult negative = |
Rouslan Solomakhin | 77a7e1a | 2019-05-23 17:37:58 | [diff] [blame^] | 700 | warn_local_development |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 701 | ? HasEnrolledInstrumentQueryResult::WARNING_HAS_NO_ENROLLED_INSTRUMENT |
| 702 | : HasEnrolledInstrumentQueryResult::HAS_NO_ENROLLED_INSTRUMENT; |
| 703 | |
| 704 | client_->OnHasEnrolledInstrument(has_enrolled_instrument ? positive |
| 705 | : negative); |
Danyao Wang | 57aa044 | 2019-01-31 04:06:41 | [diff] [blame] | 706 | journey_logger_.SetHasEnrolledInstrumentValue(has_enrolled_instrument); |
Danyao Wang | ce175bf | 2018-12-21 22:35:58 | [diff] [blame] | 707 | } |
| 708 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 709 | } // namespace payments |