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