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

Side by Side Diff: chrome/browser/ui/views/payments/credit_card_editor_view_controller.cc

Issue 2894943002: Prevent existing card number to be used for new credit cards in the Payment Request (Closed)
Patch Set: Merge fluke fix Created 3 years, 7 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 PaymentRequestDialogView* dialog, 164 PaymentRequestDialogView* dialog,
165 BackNavigationType back_navigation, 165 BackNavigationType back_navigation,
166 int next_ui_tag, 166 int next_ui_tag,
167 base::OnceClosure on_edited, 167 base::OnceClosure on_edited,
168 base::OnceCallback<void(const autofill::CreditCard&)> on_added, 168 base::OnceCallback<void(const autofill::CreditCard&)> on_added,
169 autofill::CreditCard* credit_card) 169 autofill::CreditCard* credit_card)
170 : EditorViewController(spec, state, dialog, back_navigation), 170 : EditorViewController(spec, state, dialog, back_navigation),
171 on_edited_(std::move(on_edited)), 171 on_edited_(std::move(on_edited)),
172 on_added_(std::move(on_added)), 172 on_added_(std::move(on_added)),
173 credit_card_to_edit_(credit_card), 173 credit_card_to_edit_(credit_card),
174 add_billing_address_button_tag_(next_ui_tag) {} 174 add_billing_address_button_tag_(next_ui_tag),
175 supported_card_networks_(spec->supported_card_networks().begin(),
176 spec->supported_card_networks().end()) {}
175 177
176 CreditCardEditorViewController::~CreditCardEditorViewController() {} 178 CreditCardEditorViewController::~CreditCardEditorViewController() {}
177 179
178 // Creates the "Cards accepted" view with a row of icons at the top of the 180 // Creates the "Cards accepted" view with a row of icons at the top of the
179 // credit card editor. 181 // credit card editor.
180 // +----------------------------------------------+ 182 // +----------------------------------------------+
181 // | Cards Accepted | 183 // | Cards Accepted |
182 // | | 184 // | |
183 // | | VISA | | MC | | AMEX | | 185 // | | VISA | | MC | | AMEX | |
184 // +----------------------------------------------+ 186 // +----------------------------------------------+
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 std::unique_ptr<ValidationDelegate> 416 std::unique_ptr<ValidationDelegate>
415 CreditCardEditorViewController::CreateValidationDelegate( 417 CreditCardEditorViewController::CreateValidationDelegate(
416 const EditorField& field) { 418 const EditorField& field) {
417 // The supported card networks for non-cc-number types are not passed to avoid 419 // The supported card networks for non-cc-number types are not passed to avoid
418 // the data copy in the delegate. 420 // the data copy in the delegate.
419 if (field.type == autofill::CREDIT_CARD_EXP_MONTH || 421 if (field.type == autofill::CREDIT_CARD_EXP_MONTH ||
420 field.type == autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR) 422 field.type == autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR)
421 return base::MakeUnique<ExpirationDateValidationDelegate>( 423 return base::MakeUnique<ExpirationDateValidationDelegate>(
422 this, state()->GetApplicationLocale()); 424 this, state()->GetApplicationLocale());
423 return base::MakeUnique< 425 return base::MakeUnique<
424 CreditCardEditorViewController::CreditCardValidationDelegate>( 426 CreditCardEditorViewController::CreditCardValidationDelegate>(field,
425 field, this, 427 this);
426 field.type == autofill::CREDIT_CARD_NUMBER
427 ? spec()->supported_card_networks()
428 : std::vector<std::string>());
429 } 428 }
430 429
431 std::unique_ptr<ui::ComboboxModel> 430 std::unique_ptr<ui::ComboboxModel>
432 CreditCardEditorViewController::GetComboboxModelForType( 431 CreditCardEditorViewController::GetComboboxModelForType(
433 const autofill::ServerFieldType& type) { 432 const autofill::ServerFieldType& type) {
434 switch (type) { 433 switch (type) {
435 case autofill::CREDIT_CARD_EXP_MONTH: { 434 case autofill::CREDIT_CARD_EXP_MONTH: {
436 int default_index = 0; 435 int default_index = 0;
437 std::vector<base::string16> months = 436 std::vector<base::string16> months =
438 GetExpirationMonthItems(&default_index); 437 GetExpirationMonthItems(&default_index);
(...skipping 24 matching lines...) Expand all
463 // the content view added to it yet. 462 // the content view added to it yet.
464 views::Combobox* combobox = static_cast<views::Combobox*>( 463 views::Combobox* combobox = static_cast<views::Combobox*>(
465 content_view->GetViewByID(GetInputFieldViewId(kBillingAddressType))); 464 content_view->GetViewByID(GetInputFieldViewId(kBillingAddressType)));
466 // When the combobox has a single item, it's because it has no addresses 465 // When the combobox has a single item, it's because it has no addresses
467 // (otherwise, it would have the select header, and a separator before the 466 // (otherwise, it would have the select header, and a separator before the
468 // first address to choose from). 467 // first address to choose from).
469 DCHECK(combobox); 468 DCHECK(combobox);
470 combobox->SetEnabled(combobox->GetRowCount() > 1); 469 combobox->SetEnabled(combobox->GetRowCount() > 1);
471 } 470 }
472 471
472 bool CreditCardEditorViewController::IsValidCreditCardNumber(
473 const base::string16& card_number,
474 base::string16* error_message) {
475 if (!autofill::IsValidCreditCardNumberForBasicCardNetworks(
476 card_number, supported_card_networks_, error_message)) {
Mathieu 2017/05/24 02:40:20 can we use spec()->supported_card_networks_set() h
MAD 2017/05/24 14:10:08 Done.
477 return false;
478 }
479 // Now check if another credit card has already been created with this number.
480 // TODO(crbug.com/725604): the UI should offer to load / update the existing
481 // credit card info.
482 autofill::CreditCard* existing_card =
483 state()->GetPersonalDataManager()->GetCreditCardByNumber(
484 base::UTF16ToASCII(card_number));
485 // If a card exists, it could be the one currently edited.
486 if (!existing_card || (credit_card_to_edit_ && credit_card_to_edit_->guid() ==
487 existing_card->guid())) {
488 return true;
489 }
490 if (error_message) {
491 *error_message = l10n_util::GetStringUTF16(
492 IDS_PAYMENTS_VALIDATION_ALREADY_USED_CREDIT_CARD_NUMBER);
493 }
494 return false;
495 }
496
473 base::string16 CreditCardEditorViewController::GetSheetTitle() { 497 base::string16 CreditCardEditorViewController::GetSheetTitle() {
474 if (!credit_card_to_edit_) 498 if (!credit_card_to_edit_)
475 return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_CARD); 499 return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_CARD);
476 500
477 // Gets the completion message, or empty if nothing is missing from the card. 501 // Gets the completion message, or empty if nothing is missing from the card.
478 base::string16 title = autofill::GetCompletionMessageForCard( 502 base::string16 title = autofill::GetCompletionMessageForCard(
479 autofill::GetCompletionStatusForCard( 503 autofill::GetCompletionStatusForCard(
480 *credit_card_to_edit_, state()->GetApplicationLocale(), 504 *credit_card_to_edit_, state()->GetApplicationLocale(),
481 state()->GetPersonalDataManager()->GetProfiles())); 505 state()->GetPersonalDataManager()->GetProfiles()));
482 return title.empty() ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_CARD) 506 return title.empty() ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_CARD)
(...skipping 25 matching lines...) Expand all
508 static_cast<autofill::AddressComboboxModel*>(address_combobox->model()); 532 static_cast<autofill::AddressComboboxModel*>(address_combobox->model());
509 int index = model->AddNewProfile(profile); 533 int index = model->AddNewProfile(profile);
510 // SetSelectedIndex doesn't trigger a perform action notification, which is 534 // SetSelectedIndex doesn't trigger a perform action notification, which is
511 // needed to update the valid state. 535 // needed to update the valid state.
512 address_combobox->SetSelectedRow(index); 536 address_combobox->SetSelectedRow(index);
513 // But it needs to be blured at least once. 537 // But it needs to be blured at least once.
514 address_combobox->OnBlur(); 538 address_combobox->OnBlur();
515 } 539 }
516 540
517 CreditCardEditorViewController::CreditCardValidationDelegate:: 541 CreditCardEditorViewController::CreditCardValidationDelegate::
518 CreditCardValidationDelegate( 542 CreditCardValidationDelegate(const EditorField& field,
519 const EditorField& field, 543 CreditCardEditorViewController* controller)
520 EditorViewController* controller, 544 : field_(field), controller_(controller) {}
521 const std::vector<std::string>& supported_card_networks)
522 : field_(field),
523 controller_(controller),
524 supported_card_networks_(supported_card_networks.begin(),
525 supported_card_networks.end()) {}
526 CreditCardEditorViewController::CreditCardValidationDelegate:: 545 CreditCardEditorViewController::CreditCardValidationDelegate::
527 ~CreditCardValidationDelegate() {} 546 ~CreditCardValidationDelegate() {}
528 547
529 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 548 bool CreditCardEditorViewController::CreditCardValidationDelegate::
530 IsValidTextfield(views::Textfield* textfield) { 549 IsValidTextfield(views::Textfield* textfield) {
531 return ValidateValue(textfield->text(), nullptr); 550 return ValidateValue(textfield->text(), nullptr);
532 } 551 }
533 552
534 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 553 bool CreditCardEditorViewController::CreditCardValidationDelegate::
535 IsValidCombobox(views::Combobox* combobox) { 554 IsValidCombobox(views::Combobox* combobox) {
(...skipping 13 matching lines...) Expand all
549 base::string16 error_message; 568 base::string16 error_message;
550 bool is_valid = ValidateCombobox(combobox, nullptr); 569 bool is_valid = ValidateCombobox(combobox, nullptr);
551 controller_->DisplayErrorMessageForField(field_.type, error_message); 570 controller_->DisplayErrorMessageForField(field_.type, error_message);
552 return is_valid; 571 return is_valid;
553 } 572 }
554 573
555 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 574 bool CreditCardEditorViewController::CreditCardValidationDelegate::
556 ValidateValue(const base::string16& value, base::string16* error_message) { 575 ValidateValue(const base::string16& value, base::string16* error_message) {
557 if (!value.empty()) { 576 if (!value.empty()) {
558 base::string16 local_error_message; 577 base::string16 local_error_message;
559 bool is_valid = 578 bool is_valid = false;
560 field_.type == autofill::CREDIT_CARD_NUMBER 579 if (field_.type == autofill::CREDIT_CARD_NUMBER) {
561 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( 580 is_valid =
562 value, supported_card_networks_, &local_error_message) 581 controller_->IsValidCreditCardNumber(value, &local_error_message);
563 : autofill::IsValidForType(value, field_.type, 582 } else {
564 &local_error_message); 583 is_valid =
584 autofill::IsValidForType(value, field_.type, &local_error_message);
585 }
565 if (error_message) 586 if (error_message)
566 *error_message = local_error_message; 587 *error_message = local_error_message;
567 return is_valid; 588 return is_valid;
568 } 589 }
569 590
570 if (error_message && field_.required) { 591 if (error_message && field_.required) {
571 *error_message = l10n_util::GetStringUTF16( 592 *error_message = l10n_util::GetStringUTF16(
572 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); 593 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
573 } 594 }
574 return !field_.required; 595 return !field_.required;
(...skipping 20 matching lines...) Expand all
595 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), 616 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()),
596 error_message); 617 error_message);
597 } 618 }
598 619
599 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { 620 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) {
600 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; 621 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET;
601 return true; 622 return true;
602 } 623 }
603 624
604 } // namespace payments 625 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698