Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: components/payments/content/payment_request.cc

Issue 2715213005: [Payments] Add the pay button, and control its enabled state (Closed)
Patch Set: tweak Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <unordered_map> 8 #include <unordered_map>
8 #include <utility> 9 #include <utility>
9 10
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "components/autofill/core/browser/autofill_data_util.h"
13 #include "components/autofill/core/browser/field_types.h"
11 #include "components/autofill/core/browser/personal_data_manager.h" 14 #include "components/autofill/core/browser/personal_data_manager.h"
12 #include "components/payments/content/payment_details_validation.h" 15 #include "components/payments/content/payment_details_validation.h"
13 #include "components/payments/content/payment_request_web_contents_manager.h" 16 #include "components/payments/content/payment_request_web_contents_manager.h"
14 #include "components/payments/core/currency_formatter.h" 17 #include "components/payments/core/currency_formatter.h"
15 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
17 20
18 namespace payments { 21 namespace payments {
19 22
20 PaymentRequest::PaymentRequest( 23 PaymentRequest::PaymentRequest(
21 content::WebContents* web_contents, 24 content::WebContents* web_contents,
22 std::unique_ptr<PaymentRequestDelegate> delegate, 25 std::unique_ptr<PaymentRequestDelegate> delegate,
23 PaymentRequestWebContentsManager* manager, 26 PaymentRequestWebContentsManager* manager,
24 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) 27 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request)
25 : web_contents_(web_contents), 28 : web_contents_(web_contents),
26 delegate_(std::move(delegate)), 29 delegate_(std::move(delegate)),
27 manager_(manager), 30 manager_(manager),
28 binding_(this, std::move(request)), 31 binding_(this, std::move(request)),
32 is_ready_to_pay_(false),
29 selected_shipping_profile_(nullptr), 33 selected_shipping_profile_(nullptr),
30 selected_contact_profile_(nullptr), 34 selected_contact_profile_(nullptr),
31 selected_credit_card_(nullptr) { 35 selected_credit_card_(nullptr) {
32 // OnConnectionTerminated will be called when the Mojo pipe is closed. This 36 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
33 // will happen as a result of many renderer-side events (both successful and 37 // will happen as a result of many renderer-side events (both successful and
34 // erroneous in nature). 38 // erroneous in nature).
35 // TODO(crbug.com/683636): Investigate using 39 // TODO(crbug.com/683636): Investigate using
36 // set_connection_error_with_reason_handler with Binding::CloseWithReason. 40 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
37 binding_.set_connection_error_handler(base::Bind( 41 binding_.set_connection_error_handler(base::Bind(
38 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); 42 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
39 } 43 }
40 44
41 PaymentRequest::~PaymentRequest() {} 45 PaymentRequest::~PaymentRequest() {}
42 46
43 void PaymentRequest::Init( 47 void PaymentRequest::Init(
44 payments::mojom::PaymentRequestClientPtr client, 48 payments::mojom::PaymentRequestClientPtr client,
45 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, 49 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
46 payments::mojom::PaymentDetailsPtr details, 50 payments::mojom::PaymentDetailsPtr details,
47 payments::mojom::PaymentOptionsPtr options) { 51 payments::mojom::PaymentOptionsPtr options) {
48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
49 std::string error; 53 std::string error;
50 if (!payments::validatePaymentDetails(details, &error)) { 54 if (!payments::validatePaymentDetails(details, &error)) {
51 LOG(ERROR) << error; 55 LOG(ERROR) << error;
52 OnConnectionTerminated(); 56 OnConnectionTerminated();
53 return; 57 return;
54 } 58 }
55 client_ = std::move(client); 59 client_ = std::move(client);
56 details_ = std::move(details); 60 details_ = std::move(details);
61 options_ = std::move(options);
57 PopulateValidatedMethodData(method_data); 62 PopulateValidatedMethodData(method_data);
58 PopulateProfileCache(); 63 PopulateProfileCache();
59 SetDefaultProfileSelections(); 64 SetDefaultProfileSelections();
65 UpdateIsReadyToPay();
60 } 66 }
61 67
62 void PaymentRequest::Show() { 68 void PaymentRequest::Show() {
63 if (!client_.is_bound() || !binding_.is_bound()) { 69 if (!client_.is_bound() || !binding_.is_bound()) {
64 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; 70 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
65 OnConnectionTerminated(); 71 OnConnectionTerminated();
66 return; 72 return;
67 } 73 }
68 delegate_->ShowDialog(this); 74 delegate_->ShowDialog(this);
69 } 75 }
(...skipping 26 matching lines...) Expand all
96 // connection_error_handler on |binding_|, which can mean that the renderer 102 // connection_error_handler on |binding_|, which can mean that the renderer
97 // has decided to close the pipe for various reasons (see all uses of 103 // has decided to close the pipe for various reasons (see all uses of
98 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close 104 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
99 // the binding and the dialog, and ask to be deleted. 105 // the binding and the dialog, and ask to be deleted.
100 client_.reset(); 106 client_.reset();
101 binding_.Close(); 107 binding_.Close();
102 delegate_->CloseDialog(); 108 delegate_->CloseDialog();
103 manager_->DestroyRequest(this); 109 manager_->DestroyRequest(this);
104 } 110 }
105 111
112 void PaymentRequest::Pay() {
113 DCHECK(is_ready_to_pay_);
114
115 // TODO(mathp): Return the PaymentResponse to the |client_|.
116 UserCancelled();
117 }
118
106 CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter( 119 CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter(
107 const std::string& currency_code, 120 const std::string& currency_code,
108 const std::string& currency_system, 121 const std::string& currency_system,
109 const std::string& locale_name) { 122 const std::string& locale_name) {
110 if (!currency_formatter_) { 123 if (!currency_formatter_) {
111 currency_formatter_.reset( 124 currency_formatter_.reset(
112 new CurrencyFormatter(currency_code, currency_system, locale_name)); 125 new CurrencyFormatter(currency_code, currency_system, locale_name));
113 } 126 }
114 return currency_formatter_.get(); 127 return currency_formatter_.get();
115 } 128 }
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 supported_card_networks_.push_back(networks[supported_network]); 247 supported_card_networks_.push_back(networks[supported_network]);
235 card_networks.erase(card_it); 248 card_networks.erase(card_it);
236 } 249 }
237 } 250 }
238 } 251 }
239 } 252 }
240 } 253 }
241 } 254 }
242 } 255 }
243 256
257 void PaymentRequest::UpdateIsReadyToPay() {
258 is_ready_to_pay_ =
259 ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied();
260 }
261
262 bool PaymentRequest::ArePaymentDetailsSatisfied() {
263 if (selected_credit_card_ == nullptr || !selected_credit_card_->IsValid())
264 return false;
265
266 const std::string basic_card_payment_type =
267 autofill::data_util::GetPaymentRequestData(selected_credit_card_->type())
268 .basic_card_payment_type;
269 return !supported_card_networks_.empty() &&
270 std::find(supported_card_networks_.begin(),
271 supported_card_networks_.end(),
272 basic_card_payment_type) != supported_card_networks_.end();
273 }
274
275 bool PaymentRequest::ArePaymentOptionsSatisfied() {
276 // TODO(mathp): Have a measure of shipping address completeness.
277 if (options_->request_shipping && selected_shipping_profile_ == nullptr)
278 return false;
279
280 // TODO(mathp): Make an encompassing class to validate contact info.
281 const std::string& app_locale = delegate_->GetApplicationLocale();
282 if (options_->request_payer_name &&
283 (selected_contact_profile_ == nullptr ||
284 selected_contact_profile_
285 ->GetInfo(autofill::AutofillType(autofill::NAME_FULL), app_locale)
286 .empty())) {
287 return false;
288 }
289 if (options_->request_payer_email &&
290 (selected_contact_profile_ == nullptr ||
291 selected_contact_profile_
292 ->GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS),
293 app_locale)
294 .empty())) {
295 return false;
296 }
297 if (options_->request_payer_phone &&
298 (selected_contact_profile_ == nullptr ||
299 selected_contact_profile_
300 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER),
301 app_locale)
302 .empty())) {
303 return false;
304 }
305
306 return true;
307 }
308
244 } // namespace payments 309 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698