| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/payments/content/payment_request.h" | 5 #include "components/payments/content/payment_request.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "components/autofill/core/browser/autofill_data_util.h" | 12 #include "components/autofill/core/browser/autofill_data_util.h" |
| 13 #include "components/autofill/core/browser/field_types.h" | 13 #include "components/autofill/core/browser/field_types.h" |
| 14 #include "components/autofill/core/browser/personal_data_manager.h" | 14 #include "components/autofill/core/browser/personal_data_manager.h" |
| 15 #include "components/payments/content/payment_details_validation.h" | 15 #include "components/payments/content/payment_details_validation.h" |
| 16 #include "components/payments/content/payment_request_web_contents_manager.h" | 16 #include "components/payments/content/payment_request_web_contents_manager.h" |
| 17 #include "components/payments/core/autofill_payment_instrument.h" |
| 17 #include "components/payments/core/currency_formatter.h" | 18 #include "components/payments/core/currency_formatter.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 20 | 21 |
| 21 namespace payments { | 22 namespace payments { |
| 22 | 23 |
| 24 namespace { |
| 25 // Identifier for the basic card payment method in the PaymentMethodData. |
| 26 static const char* const kBasicCardMethodName = "basic-card"; |
| 27 } // namespace |
| 28 |
| 23 PaymentRequest::PaymentRequest( | 29 PaymentRequest::PaymentRequest( |
| 24 content::WebContents* web_contents, | 30 content::WebContents* web_contents, |
| 25 std::unique_ptr<PaymentRequestDelegate> delegate, | 31 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 26 PaymentRequestWebContentsManager* manager, | 32 PaymentRequestWebContentsManager* manager, |
| 27 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) | 33 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
| 28 : web_contents_(web_contents), | 34 : web_contents_(web_contents), |
| 29 delegate_(std::move(delegate)), | 35 delegate_(std::move(delegate)), |
| 30 manager_(manager), | 36 manager_(manager), |
| 31 binding_(this, std::move(request)), | 37 binding_(this, std::move(request)), |
| 32 is_ready_to_pay_(false), | 38 is_ready_to_pay_(false), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 } | 80 } |
| 75 | 81 |
| 76 void PaymentRequest::Abort() { | 82 void PaymentRequest::Abort() { |
| 77 // The API user has decided to abort. We return a successful abort message to | 83 // The API user has decided to abort. We return a successful abort message to |
| 78 // the renderer, which closes the Mojo message pipe, which triggers | 84 // the renderer, which closes the Mojo message pipe, which triggers |
| 79 // PaymentRequest::OnConnectionTerminated, which destroys this object. | 85 // PaymentRequest::OnConnectionTerminated, which destroys this object. |
| 80 if (client_.is_bound()) | 86 if (client_.is_bound()) |
| 81 client_->OnAbort(true /* aborted_successfully */); | 87 client_->OnAbort(true /* aborted_successfully */); |
| 82 } | 88 } |
| 83 | 89 |
| 90 void PaymentRequest::Complete(payments::mojom::PaymentComplete result) { |
| 91 if (!client_.is_bound()) |
| 92 return; |
| 93 |
| 94 // TODO(mathp): Validate |result|. |
| 95 |
| 96 // When the renderer closes the connection, |
| 97 // PaymentRequest::OnConnectionTerminated will be called. |
| 98 client_->OnComplete(); |
| 99 } |
| 100 |
| 101 void PaymentRequest::CanMakePayment() { |
| 102 // TODO(mathp): Return whether we can make payment. |
| 103 client_->OnCanMakePayment(mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT); |
| 104 } |
| 105 |
| 106 void PaymentRequest::OnInstrumentDetailsReady( |
| 107 const std::string& method_name, |
| 108 const std::string& stringified_details) { |
| 109 payment_response_->method_name = method_name; |
| 110 payment_response_->stringified_details = stringified_details; |
| 111 client_->OnPaymentResponse(std::move(payment_response_)); |
| 112 } |
| 113 |
| 84 void PaymentRequest::UserCancelled() { | 114 void PaymentRequest::UserCancelled() { |
| 85 // If |client_| is not bound, then the object is already being destroyed as | 115 // If |client_| is not bound, then the object is already being destroyed as |
| 86 // a result of a renderer event. | 116 // a result of a renderer event. |
| 87 if (!client_.is_bound()) | 117 if (!client_.is_bound()) |
| 88 return; | 118 return; |
| 89 | 119 |
| 90 // This sends an error to the renderer, which informs the API user. | 120 // This sends an error to the renderer, which informs the API user. |
| 91 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); | 121 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); |
| 92 | 122 |
| 93 // We close all bindings and ask to be destroyed. | 123 // We close all bindings and ask to be destroyed. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 104 // the binding and the dialog, and ask to be deleted. | 134 // the binding and the dialog, and ask to be deleted. |
| 105 client_.reset(); | 135 client_.reset(); |
| 106 binding_.Close(); | 136 binding_.Close(); |
| 107 delegate_->CloseDialog(); | 137 delegate_->CloseDialog(); |
| 108 manager_->DestroyRequest(this); | 138 manager_->DestroyRequest(this); |
| 109 } | 139 } |
| 110 | 140 |
| 111 void PaymentRequest::Pay() { | 141 void PaymentRequest::Pay() { |
| 112 DCHECK(is_ready_to_pay_); | 142 DCHECK(is_ready_to_pay_); |
| 113 | 143 |
| 114 // TODO(mathp): Return the PaymentResponse to the |client_|. | 144 // TODO(mathp): Fill other fields in the PaymentResponsePtr object. |
| 115 OnConnectionTerminated(); | 145 payment_response_ = mojom::PaymentResponse::New(); |
| 146 |
| 147 // TODO(mathp): PaymentRequest should know about the currently selected |
| 148 // instrument, and not |selected_credit_card_| which is too specific. |
| 149 // TODO(mathp): The method_name should reflect what the merchant asked, and |
| 150 // not necessarily basic-card. |
| 151 selected_payment_instrument_.reset(new AutofillPaymentInstrument( |
| 152 kBasicCardMethodName, *selected_credit_card_, shipping_profiles_, |
| 153 delegate_->GetApplicationLocale())); |
| 154 // Fetch the instrument details, will call back into |
| 155 // PaymentRequest::OnInstrumentsDetailsReady. |
| 156 selected_payment_instrument_->InvokePaymentApp(this); |
| 116 } | 157 } |
| 117 | 158 |
| 118 void PaymentRequest::AddObserver(Observer* observer) { | 159 void PaymentRequest::AddObserver(Observer* observer) { |
| 119 CHECK(observer); | 160 CHECK(observer); |
| 120 observers_.AddObserver(observer); | 161 observers_.AddObserver(observer); |
| 121 } | 162 } |
| 122 | 163 |
| 123 void PaymentRequest::RemoveObserver(Observer* observer) { | 164 void PaymentRequest::RemoveObserver(Observer* observer) { |
| 124 observers_.RemoveObserver(observer); | 165 observers_.RemoveObserver(observer); |
| 125 } | 166 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 } | 257 } |
| 217 | 258 |
| 218 void PaymentRequest::PopulateValidatedMethodData( | 259 void PaymentRequest::PopulateValidatedMethodData( |
| 219 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) { | 260 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) { |
| 220 if (method_data.empty()) { | 261 if (method_data.empty()) { |
| 221 LOG(ERROR) << "Invalid payment methods or data"; | 262 LOG(ERROR) << "Invalid payment methods or data"; |
| 222 OnConnectionTerminated(); | 263 OnConnectionTerminated(); |
| 223 return; | 264 return; |
| 224 } | 265 } |
| 225 | 266 |
| 226 // Identifier for the basic card payment method in the PaymentMethodData. | |
| 227 const char* const kBasicCardMethodName = "basic-card"; | |
| 228 std::set<std::string> card_networks{"amex", "diners", "discover", | 267 std::set<std::string> card_networks{"amex", "diners", "discover", |
| 229 "jcb", "mastercard", "mir", | 268 "jcb", "mastercard", "mir", |
| 230 "unionpay", "visa"}; | 269 "unionpay", "visa"}; |
| 231 for (const payments::mojom::PaymentMethodDataPtr& method_data_entry : | 270 for (const payments::mojom::PaymentMethodDataPtr& method_data_entry : |
| 232 method_data) { | 271 method_data) { |
| 233 std::vector<std::string> supported_methods = | 272 std::vector<std::string> supported_methods = |
| 234 method_data_entry->supported_methods; | 273 method_data_entry->supported_methods; |
| 235 if (supported_methods.empty()) { | 274 if (supported_methods.empty()) { |
| 236 LOG(ERROR) << "Invalid payment methods or data"; | 275 LOG(ERROR) << "Invalid payment methods or data"; |
| 237 OnConnectionTerminated(); | 276 OnConnectionTerminated(); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), | 381 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
| 343 app_locale) | 382 app_locale) |
| 344 .empty())) { | 383 .empty())) { |
| 345 return false; | 384 return false; |
| 346 } | 385 } |
| 347 | 386 |
| 348 return true; | 387 return true; |
| 349 } | 388 } |
| 350 | 389 |
| 351 } // namespace payments | 390 } // namespace payments |
| OLD | NEW |