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