Skip to content

Commit

Permalink
[Payment Request] Formats phone number and credit card number in edit…
Browse files Browse the repository at this point in the history
…ors.

card number: http://imgur.com/a/ffyCF, http://imgur.com/a/pMl33
phone number: http://imgur.com/a/H7i6b, http://imgur.com/a/HpHto

BUG=602666

Review-Url: https://codereview.chromium.org/2949813003
Cr-Commit-Position: refs/heads/master@{#481246}
  • Loading branch information
mahmadi authored and Commit Bot committed Jun 21, 2017
1 parent 7f0a1be commit 8f116db
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "components/autofill/core/common/autofill_constants.h"
#include "components/payments/content/payment_request_spec.h"
#include "components/payments/content/payment_request_state.h"
#include "components/payments/core/payment_request_data_util.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/insets.h"
Expand Down Expand Up @@ -136,29 +137,6 @@ class ExpirationDateValidationDelegate : public ValidationDelegate {
DISALLOW_COPY_AND_ASSIGN(ExpirationDateValidationDelegate);
};

// Formats card number. For example, "4111111111111111" is formatted into
// "4111 1111 1111 1111".
base::string16 FormatCardNumber(const base::string16& text) {
// Do not format masked card numbers, which start with a letter.
base::string16 number = autofill::CreditCard::StripSeparators(text);
if (number.empty() || !base::IsAsciiDigit(number[0]))
return text;

std::vector<size_t> positions = {4U, 9U, 14U};
if (autofill::CreditCard::GetCardNetwork(number) ==
autofill::kAmericanExpressCard) {
positions = {4U, 11U};
}

static const base::char16 kSeparator = base::ASCIIToUTF16(" ")[0];
for (size_t i : positions) {
if (number.size() > i)
number.insert(i, 1U, kSeparator);
}

return number;
}

} // namespace

CreditCardEditorViewController::CreditCardEditorViewController(
Expand Down Expand Up @@ -368,7 +346,9 @@ base::string16 CreditCardEditorViewController::GetInitialValueForType(
base::string16 info = credit_card_to_edit_->GetInfo(
autofill::AutofillType(type), state()->GetApplicationLocale());

return type == autofill::CREDIT_CARD_NUMBER ? FormatCardNumber(info) : info;
return type == autofill::CREDIT_CARD_NUMBER
? data_util::FormatCardNumberForDisplay(info)
: info;
}

bool CreditCardEditorViewController::ValidateModelAndSave() {
Expand Down Expand Up @@ -627,7 +607,7 @@ bool CreditCardEditorViewController::CreditCardValidationDelegate::
base::string16
CreditCardEditorViewController::CreditCardValidationDelegate::Format(
const base::string16& text) {
return FormatCardNumber(text);
return data_util::FormatCardNumberForDisplay(text);
}

bool CreditCardEditorViewController::CreditCardValidationDelegate::
Expand Down
21 changes: 21 additions & 0 deletions components/payments/core/payment_request_data_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/strings/string16.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_country.h"
#include "components/autofill/core/browser/autofill_data_util.h"
Expand Down Expand Up @@ -191,6 +192,26 @@ std::string FormatPhoneForResponse(const std::string& phone_number,
PhoneNumberUtil::PhoneNumberFormat::E164);
}

base::string16 FormatCardNumberForDisplay(const base::string16& card_number) {
base::string16 number = autofill::CreditCard::StripSeparators(card_number);
if (number.empty() || !base::IsAsciiDigit(number[0]))
return card_number;

std::vector<size_t> positions = {4U, 9U, 14U};
if (autofill::CreditCard::GetCardNetwork(number) ==
autofill::kAmericanExpressCard) {
positions = {4U, 11U};
}

static const base::char16 kSeparator = base::ASCIIToUTF16(" ")[0];
for (size_t i : positions) {
if (number.size() > i)
number.insert(i, 1U, kSeparator);
}

return number;
}

std::string GetCountryCodeWithFallback(const autofill::AutofillProfile* profile,
const std::string& app_locale) {
std::string country_code =
Expand Down
5 changes: 5 additions & 0 deletions components/payments/core/payment_request_data_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ std::string FormatPhoneForDisplay(const std::string& phone_number,
std::string FormatPhoneForResponse(const std::string& phone_number,
const std::string& country_code);

// Formats |card_number| for display. For example, "4111111111111111" is
// formatted into "4111 1111 1111 1111". This method does not format masked card
// numbers, which start with a letter.
base::string16 FormatCardNumberForDisplay(const base::string16& card_number);

// Returns a country code to be used when validating this profile. If the
// profile has a valid country code set, it is returned. If not, a country code
// associated with |app_locale| is used as a fallback.
Expand Down
7 changes: 5 additions & 2 deletions ios/chrome/browser/ui/payments/address_edit_coordinator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/autofill_country.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/validation.h"
#include "components/autofill/core/common/autofill_constants.h"
Expand Down Expand Up @@ -147,8 +148,10 @@ - (void)paymentRequestEditViewController:
autofill::kSettingsOrigin);

for (EditorField* field in fields) {
address.SetRawInfo(AutofillTypeFromAutofillUIType(field.autofillUIType),
base::SysNSStringToUTF16(field.value));
address.SetInfo(autofill::AutofillType(
AutofillTypeFromAutofillUIType(field.autofillUIType)),
base::SysNSStringToUTF16(field.value),
GetApplicationContext()->GetApplicationLocale());
}

if (!self.address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/test/ios/wait_util.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/country_names.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/browser/test_region_data_loader.h"
#include "components/payments/core/payments_profile_comparator.h"
Expand Down Expand Up @@ -102,6 +103,7 @@
protected:
PaymentRequestAddressEditCoordinatorTest()
: pref_service_(autofill::test::PrefServiceForTesting()) {
autofill::CountryNames::SetLocaleString("en-US");
personal_data_manager_.SetTestingPrefService(pref_service_.get());
payment_request_ = base::MakeUnique<MockTestPaymentRequest>(
payment_request_test_util::CreateTestWebPaymentRequest(),
Expand Down Expand Up @@ -193,12 +195,12 @@ void TearDown() override {
// Expect an autofill profile to be added to the PaymentRequest.
EXPECT_CALL(*payment_request_,
AddAutofillProfile(ProfileMatches("John Doe", "CA" /* Canada */,
"Quebec", "16502111111")))
"Quebec", "1 650-211-1111")))
.Times(1);
// Expect an autofill profile to be added to the PersonalDataManager.
EXPECT_CALL(personal_data_manager_,
AddProfile(ProfileMatches("John Doe", "CA" /* Canada */, "Quebec",
"16502111111")))
"1 650-211-1111")))
.Times(1);
// No autofill profile should get updated in the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, UpdateProfile(_)).Times(0);
Expand Down Expand Up @@ -261,12 +263,12 @@ void TearDown() override {
// Expect an autofill profile to be updated in the PersonalDataManager.
EXPECT_CALL(personal_data_manager_,
UpdateProfile(ProfileMatches("John Doe", "CA" /* Canada */,
"Quebec", "16502111111")))
"Quebec", "1 650-211-1111")))
.Times(1);
// Expect an autofill profile to be invalidated in PaymentsProfileComparator.
EXPECT_CALL(*profile_comparator_,
Invalidate(ProfileMatches("John Doe", "CA" /* Canada */, "Quebec",
"16502111111")))
"1 650-211-1111")))
.Times(1);

// Call the controller delegate method.
Expand Down
18 changes: 16 additions & 2 deletions ios/chrome/browser/ui/payments/address_edit_mediator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "components/autofill/core/browser/country_combobox_model.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/payments/core/payment_request_data_util.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/payments/payment_request.h"
Expand Down Expand Up @@ -119,6 +120,15 @@ - (BOOL)shouldHideBackgroundForHeaderItem {
return NO;
}

- (void)formatValueForEditorField:(EditorField*)field {
if (field.autofillUIType == AutofillUITypeProfileHomePhoneWholeNumber) {
field.value =
base::SysUTF8ToNSString(payments::data_util::FormatPhoneForDisplay(
base::SysNSStringToUTF8(field.value),
base::SysNSStringToUTF8(self.selectedCountryCode)));
}
}

- (UIImage*)iconIdentifyingEditorField:(EditorField*)field {
return nil;
}
Expand Down Expand Up @@ -299,8 +309,12 @@ - (void)loadRegions {
EditorField* field = self.fieldsMap[phoneNumberFieldKey];
if (!field) {
NSString* value =
[self fieldValueFromProfile:self.address
fieldType:autofill::PHONE_HOME_WHOLE_NUMBER];
self.address
? base::SysUTF16ToNSString(
payments::data_util::GetFormattedPhoneNumberForDisplay(
*self.address,
GetApplicationContext()->GetApplicationLocale()))
: nil;
field = [[EditorField alloc]
initWithAutofillUIType:AutofillUITypeProfileHomePhoneWholeNumber
fieldType:EditorFieldTypeTextField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/autofill_country.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/validation.h"
#include "components/autofill/core/common/autofill_constants.h"
Expand Down Expand Up @@ -136,8 +137,10 @@ - (void)paymentRequestEditViewController:
autofill::kSettingsOrigin);

for (EditorField* field in fields) {
profile.SetRawInfo(AutofillTypeFromAutofillUIType(field.autofillUIType),
base::SysNSStringToUTF16(field.value));
profile.SetInfo(autofill::AutofillType(
AutofillTypeFromAutofillUIType(field.autofillUIType)),
base::SysNSStringToUTF16(field.value),
GetApplicationContext()->GetApplicationLocale());
}

if (!self.profile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ void TearDown() override {
// Expect an autofill profile to be added to the PaymentRequest.
EXPECT_CALL(*payment_request_,
AddAutofillProfile(
ProfileMatches("John Doe", "john@doe.com", "16502111111")))
ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1);
// Expect an autofill profile to be added to the PersonalDataManager.
EXPECT_CALL(
personal_data_manager_,
AddProfile(ProfileMatches("John Doe", "john@doe.com", "16502111111")))
AddProfile(ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1);
// No autofill profile should get updated in the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, UpdateProfile(_)).Times(0);
Expand Down Expand Up @@ -250,14 +250,14 @@ void TearDown() override {
// No autofill profile should get added to the PersonalDataManager.
EXPECT_CALL(personal_data_manager_, AddProfile(_)).Times(0);
// Expect an autofill profile to be updated in the PersonalDataManager.
EXPECT_CALL(
personal_data_manager_,
UpdateProfile(ProfileMatches("John Doe", "john@doe.com", "16502111111")))
EXPECT_CALL(personal_data_manager_,
UpdateProfile(
ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1);
// Expect an autofill profile to be invalidated in PaymentsProfileComparator.
EXPECT_CALL(
*profile_comparator_,
Invalidate(ProfileMatches("John Doe", "john@doe.com", "16502111111")))
Invalidate(ProfileMatches("John Doe", "john@doe.com", "1 650-211-1111")))
.Times(1);

// Call the controller delegate method.
Expand Down
21 changes: 19 additions & 2 deletions ios/chrome/browser/ui/payments/contact_info_edit_mediator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/autofill_country.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/payments/core/payment_request_data_util.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/payments/payment_request.h"
Expand Down Expand Up @@ -74,6 +76,17 @@ - (BOOL)shouldHideBackgroundForHeaderItem {
return NO;
}

- (void)formatValueForEditorField:(EditorField*)field {
if (field.autofillUIType == AutofillUITypeProfileHomePhoneWholeNumber) {
const std::string countryCode =
autofill::AutofillCountry::CountryCodeForLocale(
GetApplicationContext()->GetApplicationLocale());
field.value =
base::SysUTF8ToNSString(payments::data_util::FormatPhoneForDisplay(
base::SysNSStringToUTF8(field.value), countryCode));
}
}

- (UIImage*)iconIdentifyingEditorField:(EditorField*)field {
return nil;
}
Expand All @@ -99,8 +112,12 @@ - (UIImage*)iconIdentifyingEditorField:(EditorField*)field {

if (_paymentRequest->request_payer_phone()) {
NSString* phone =
[self fieldValueFromProfile:self.profile
fieldType:autofill::PHONE_HOME_WHOLE_NUMBER];
self.profile
? base::SysUTF16ToNSString(
payments::data_util::GetFormattedPhoneNumberForDisplay(
*self.profile,
GetApplicationContext()->GetApplicationLocale()))
: nil;
EditorField* phoneField = [[EditorField alloc]
initWithAutofillUIType:AutofillUITypeProfileHomePhoneWholeNumber
fieldType:EditorFieldTypeTextField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
field = fields[1];
EXPECT_TRUE([field isKindOfClass:[EditorField class]]);
editor_field = base::mac::ObjCCastStrict<EditorField>(field);
EXPECT_TRUE([editor_field.value isEqualToString:@"16502111111"]);
EXPECT_TRUE([editor_field.value isEqualToString:@"+1 650-211-1111"]);

field = fields[2];
EXPECT_TRUE([field isKindOfClass:[EditorField class]]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/validation.h"
#include "components/autofill/core/common/autofill_constants.h"
#import "components/autofill/ios/browser/credit_card_util.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/payments/payment_request.h"
#import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h"
#import "ios/chrome/browser/ui/payments/credit_card_edit_mediator.h"
Expand Down Expand Up @@ -187,9 +189,10 @@ - (void)paymentRequestEditViewController:
} else if (field.autofillUIType == AutofillUITypeCreditCardBillingAddress) {
creditCard.set_billing_address_id(base::SysNSStringToUTF8(field.value));
} else {
creditCard.SetRawInfo(
AutofillTypeFromAutofillUIType(field.autofillUIType),
base::SysNSStringToUTF16(field.value));
creditCard.SetInfo(autofill::AutofillType(AutofillTypeFromAutofillUIType(
field.autofillUIType)),
base::SysNSStringToUTF16(field.value),
GetApplicationContext()->GetApplicationLocale());
}
}

Expand Down
14 changes: 13 additions & 1 deletion ios/chrome/browser/ui/payments/credit_card_edit_mediator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#import "components/autofill/ios/browser/credit_card_util.h"
#include "components/payments/core/payment_request_data_util.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/payments/payment_request.h"
Expand Down Expand Up @@ -153,6 +154,14 @@ - (BOOL)shouldHideBackgroundForHeaderItem {
return !_creditCard || autofill::IsCreditCardLocal(*_creditCard);
}

- (void)formatValueForEditorField:(EditorField*)field {
if (field.autofillUIType == AutofillUITypeCreditCardNumber) {
field.value = base::SysUTF16ToNSString(
payments::data_util::FormatCardNumberForDisplay(
base::SysNSStringToUTF16(field.value)));
}
}

- (UIImage*)iconIdentifyingEditorField:(EditorField*)field {
// Early return if the field is not the credit card number field.
if (field.autofillUIType != AutofillUITypeCreditCardNumber)
Expand Down Expand Up @@ -253,7 +262,10 @@ - (void)loadYears {

// Credit Card number field.
NSString* creditCardNumber =
_creditCard ? base::SysUTF16ToNSString(_creditCard->number()) : nil;
_creditCard ? base::SysUTF16ToNSString(
payments::data_util::FormatCardNumberForDisplay(
_creditCard->number()))
: nil;
fieldKey = [NSNumber numberWithInt:AutofillUITypeCreditCardNumber];
EditorField* creditCardNumberField = self.fieldsMap[fieldKey];
if (!creditCardNumberField) {
Expand Down
Loading

0 comments on commit 8f116db

Please sign in to comment.