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