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

Unified Diff: components/payments/core/address_normalizer.cc

Issue 2808983003: [Payments] Format shipping and billing phone number in normalizer. (Closed)
Patch Set: Addressed mathp's comments Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: components/payments/core/address_normalizer.cc
diff --git a/components/payments/core/address_normalizer.cc b/components/payments/core/address_normalizer.cc
index 3114c5a89229cfc2a60ca2e02ebaf7f9b13819c1..d936eba56982c7877c4139c589f4121cd79d01d9 100644
--- a/components/payments/core/address_normalizer.cc
+++ b/components/payments/core/address_normalizer.cc
@@ -22,6 +22,7 @@
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
+#include "third_party/libphonenumber/phonenumber_api.h"
namespace payments {
namespace {
@@ -29,6 +30,7 @@ namespace {
using ::autofill::AutofillProfile;
using ::i18n::addressinput::Source;
using ::i18n::addressinput::Storage;
+using ::i18n::phonenumbers::PhoneNumberUtil;
class AddressNormalizationRequest : public AddressNormalizer::Request {
public:
@@ -62,6 +64,9 @@ class AddressNormalizationRequest : public AddressNormalizer::Request {
return;
has_responded_ = true;
+ // In either case, format the phone number.
+ FormatPhoneNumberForResponse();
+
if (!success) {
delegate_->OnCouldNotNormalize(profile_);
return;
@@ -90,6 +95,28 @@ class AddressNormalizationRequest : public AddressNormalizer::Request {
}
private:
+ // Tries to format the phone number to the E.164 format to send in the Payment
+ // Response, as defined in the Payment Request spec. Keeps the original
+ // if it cannot be formatted. More info at:
+ // https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm
+ void FormatPhoneNumberForResponse() {
+ const std::string original_number = base::UTF16ToUTF8(profile_.GetInfo(
+ autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER),
+ region_code_));
+ i18n::phonenumbers::PhoneNumber parsed_number;
+ PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
+ if (phone_number_util->Parse(original_number, region_code_,
+ &parsed_number) ==
+ PhoneNumberUtil::NO_PARSING_ERROR) {
+ std::string formatted_number;
+ phone_number_util->Format(parsed_number,
+ PhoneNumberUtil::PhoneNumberFormat::E164,
+ &formatted_number);
+ profile_.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
+ base::UTF8ToUTF16(formatted_number));
+ }
+ }
+
AutofillProfile profile_;
std::string region_code_;
AddressNormalizer::Delegate* delegate_;

Powered by Google App Engine
This is Rietveld 408576698