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