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

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: Fixed unit tests crash Created 3 years, 6 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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 PaymentRequestDialogView* dialog, 212 PaymentRequestDialogView* dialog,
213 BackNavigationType back_navigation, 213 BackNavigationType back_navigation,
214 int next_ui_tag, 214 int next_ui_tag,
215 base::OnceClosure on_edited, 215 base::OnceClosure on_edited,
216 base::OnceCallback<void(const autofill::CreditCard&)> on_added, 216 base::OnceCallback<void(const autofill::CreditCard&)> on_added,
217 autofill::CreditCard* credit_card) 217 autofill::CreditCard* credit_card)
218 : EditorViewController(spec, state, dialog, back_navigation), 218 : EditorViewController(spec, state, dialog, back_navigation),
219 on_edited_(std::move(on_edited)), 219 on_edited_(std::move(on_edited)),
220 on_added_(std::move(on_added)), 220 on_added_(std::move(on_added)),
221 credit_card_to_edit_(credit_card), 221 credit_card_to_edit_(credit_card),
222 add_billing_address_button_tag_(next_ui_tag) {} 222 add_billing_address_button_tag_(next_ui_tag) {
223 if (spec)
224 supported_card_networks_ = spec->supported_card_networks_set();
225 }
223 226
224 CreditCardEditorViewController::~CreditCardEditorViewController() {} 227 CreditCardEditorViewController::~CreditCardEditorViewController() {}
225 228
226 // Creates the "Cards accepted" view with a row of icons at the top of the 229 // Creates the "Cards accepted" view with a row of icons at the top of the
227 // credit card editor. 230 // credit card editor.
228 // +----------------------------------------------+ 231 // +----------------------------------------------+
229 // | Cards Accepted | 232 // | Cards Accepted |
230 // | | 233 // | |
231 // | | VISA | | MC | | AMEX | | 234 // | | VISA | | MC | | AMEX | |
232 // +----------------------------------------------+ 235 // +----------------------------------------------+
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 bool initially_valid = 486 bool initially_valid =
484 credit_card_to_edit_ 487 credit_card_to_edit_
485 ? !credit_card_to_edit_->IsExpired(autofill::AutofillClock::Now()) 488 ? !credit_card_to_edit_->IsExpired(autofill::AutofillClock::Now())
486 : true; 489 : true;
487 return base::MakeUnique<ExpirationDateValidationDelegate>( 490 return base::MakeUnique<ExpirationDateValidationDelegate>(
488 this, state()->GetApplicationLocale(), initially_valid); 491 this, state()->GetApplicationLocale(), initially_valid);
489 } 492 }
490 // The supported card networks for non-cc-number types are not passed to avoid 493 // The supported card networks for non-cc-number types are not passed to avoid
491 // the data copy in the delegate. 494 // the data copy in the delegate.
492 return base::MakeUnique< 495 return base::MakeUnique<
493 CreditCardEditorViewController::CreditCardValidationDelegate>( 496 CreditCardEditorViewController::CreditCardValidationDelegate>(field,
494 field, this, 497 this);
495 field.type == autofill::CREDIT_CARD_NUMBER
496 ? spec()->supported_card_networks()
497 : std::vector<std::string>());
498 } 498 }
499 499
500 std::unique_ptr<ui::ComboboxModel> 500 std::unique_ptr<ui::ComboboxModel>
501 CreditCardEditorViewController::GetComboboxModelForType( 501 CreditCardEditorViewController::GetComboboxModelForType(
502 const autofill::ServerFieldType& type) { 502 const autofill::ServerFieldType& type) {
503 switch (type) { 503 switch (type) {
504 case autofill::CREDIT_CARD_EXP_MONTH: { 504 case autofill::CREDIT_CARD_EXP_MONTH: {
505 int default_index = 0; 505 int default_index = 0;
506 std::vector<base::string16> months = 506 std::vector<base::string16> months =
507 GetExpirationMonthItems(&default_index); 507 GetExpirationMonthItems(&default_index);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 // the content view added to it yet. 549 // the content view added to it yet.
550 views::Combobox* combobox = static_cast<views::Combobox*>( 550 views::Combobox* combobox = static_cast<views::Combobox*>(
551 content_view->GetViewByID(GetInputFieldViewId(kBillingAddressType))); 551 content_view->GetViewByID(GetInputFieldViewId(kBillingAddressType)));
552 // When the combobox has a single item, it's because it has no addresses 552 // When the combobox has a single item, it's because it has no addresses
553 // (otherwise, it would have the select header, and a separator before the 553 // (otherwise, it would have the select header, and a separator before the
554 // first address to choose from). 554 // first address to choose from).
555 DCHECK(combobox); 555 DCHECK(combobox);
556 combobox->SetEnabled(combobox->GetRowCount() > 1); 556 combobox->SetEnabled(combobox->GetRowCount() > 1);
557 } 557 }
558 558
559 bool CreditCardEditorViewController::IsValidCreditCardNumber(
560 const base::string16& card_number,
561 base::string16* error_message) {
562 if (!autofill::IsValidCreditCardNumberForBasicCardNetworks(
563 card_number, supported_card_networks_, error_message)) {
564 return false;
565 }
566 // Now check if another credit card has already been created with this number.
567 // TODO(crbug.com/725604): the UI should offer to load / update the existing
568 // credit card info.
569 autofill::CreditCard* existing_card =
570 state()->GetPersonalDataManager()->GetCreditCardByNumber(
571 base::UTF16ToASCII(card_number));
572 // If a card exists, it could be the one currently edited.
573 if (!existing_card || (credit_card_to_edit_ && credit_card_to_edit_->guid() ==
574 existing_card->guid())) {
575 return true;
576 }
577 if (error_message) {
578 *error_message = l10n_util::GetStringUTF16(
579 IDS_PAYMENTS_VALIDATION_ALREADY_USED_CREDIT_CARD_NUMBER);
580 }
581 return false;
582 }
583
559 base::string16 CreditCardEditorViewController::GetSheetTitle() { 584 base::string16 CreditCardEditorViewController::GetSheetTitle() {
560 if (!credit_card_to_edit_) 585 if (!credit_card_to_edit_)
561 return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_CARD); 586 return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_CARD);
562 587
563 // Gets the completion message, or empty if nothing is missing from the card. 588 // Gets the completion message, or empty if nothing is missing from the card.
564 base::string16 title = autofill::GetCompletionMessageForCard( 589 base::string16 title = autofill::GetCompletionMessageForCard(
565 autofill::GetCompletionStatusForCard( 590 autofill::GetCompletionStatusForCard(
566 *credit_card_to_edit_, state()->GetApplicationLocale(), 591 *credit_card_to_edit_, state()->GetApplicationLocale(),
567 state()->GetPersonalDataManager()->GetProfiles())); 592 state()->GetPersonalDataManager()->GetProfiles()));
568 return title.empty() ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_CARD) 593 return title.empty() ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_CARD)
(...skipping 26 matching lines...) Expand all
595 static_cast<autofill::AddressComboboxModel*>(address_combobox->model()); 620 static_cast<autofill::AddressComboboxModel*>(address_combobox->model());
596 int index = model->AddNewProfile(profile); 621 int index = model->AddNewProfile(profile);
597 // SetSelectedIndex doesn't trigger a perform action notification, which is 622 // SetSelectedIndex doesn't trigger a perform action notification, which is
598 // needed to update the valid state. 623 // needed to update the valid state.
599 address_combobox->SetSelectedRow(index); 624 address_combobox->SetSelectedRow(index);
600 // But it needs to be blured at least once. 625 // But it needs to be blured at least once.
601 address_combobox->OnBlur(); 626 address_combobox->OnBlur();
602 } 627 }
603 628
604 CreditCardEditorViewController::CreditCardValidationDelegate:: 629 CreditCardEditorViewController::CreditCardValidationDelegate::
605 CreditCardValidationDelegate( 630 CreditCardValidationDelegate(const EditorField& field,
606 const EditorField& field, 631 CreditCardEditorViewController* controller)
607 CreditCardEditorViewController* controller, 632 : field_(field), controller_(controller) {}
608 const std::vector<std::string>& supported_card_networks)
609 : field_(field),
610 controller_(controller),
611 supported_card_networks_(supported_card_networks.begin(),
612 supported_card_networks.end()) {}
613 CreditCardEditorViewController::CreditCardValidationDelegate:: 633 CreditCardEditorViewController::CreditCardValidationDelegate::
614 ~CreditCardValidationDelegate() {} 634 ~CreditCardValidationDelegate() {}
615 635
616 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 636 bool CreditCardEditorViewController::CreditCardValidationDelegate::
617 ShouldFormat() { 637 ShouldFormat() {
618 return field_.type == autofill::CREDIT_CARD_NUMBER; 638 return field_.type == autofill::CREDIT_CARD_NUMBER;
619 } 639 }
620 640
621 base::string16 641 base::string16
622 CreditCardEditorViewController::CreditCardValidationDelegate::Format( 642 CreditCardEditorViewController::CreditCardValidationDelegate::Format(
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 base::string16 error_message; 682 base::string16 error_message;
663 bool is_valid = ValidateCombobox(combobox, nullptr); 683 bool is_valid = ValidateCombobox(combobox, nullptr);
664 controller_->DisplayErrorMessageForField(field_.type, error_message); 684 controller_->DisplayErrorMessageForField(field_.type, error_message);
665 return is_valid; 685 return is_valid;
666 } 686 }
667 687
668 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 688 bool CreditCardEditorViewController::CreditCardValidationDelegate::
669 ValidateValue(const base::string16& value, base::string16* error_message) { 689 ValidateValue(const base::string16& value, base::string16* error_message) {
670 if (!value.empty()) { 690 if (!value.empty()) {
671 base::string16 local_error_message; 691 base::string16 local_error_message;
672 bool is_valid = 692 bool is_valid = false;
673 field_.type == autofill::CREDIT_CARD_NUMBER 693 if (field_.type == autofill::CREDIT_CARD_NUMBER) {
674 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( 694 is_valid =
675 value, supported_card_networks_, &local_error_message) 695 controller_->IsValidCreditCardNumber(value, &local_error_message);
676 : autofill::IsValidForType(value, field_.type, 696 } else {
677 &local_error_message); 697 is_valid =
698 autofill::IsValidForType(value, field_.type, &local_error_message);
699 }
678 if (error_message) 700 if (error_message)
679 *error_message = local_error_message; 701 *error_message = local_error_message;
680 return is_valid; 702 return is_valid;
681 } 703 }
682 704
683 if (error_message && field_.required) { 705 if (error_message && field_.required) {
684 *error_message = l10n_util::GetStringUTF16( 706 *error_message = l10n_util::GetStringUTF16(
685 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); 707 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
686 } 708 }
687 return !field_.required; 709 return !field_.required;
(...skipping 20 matching lines...) Expand all
708 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), 730 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()),
709 error_message); 731 error_message);
710 } 732 }
711 733
712 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { 734 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) {
713 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; 735 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET;
714 return true; 736 return true;
715 } 737 }
716 738
717 } // namespace payments 739 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698