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

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

Issue 2829503002: [Payments] Normalize Shipping Address sent to merchant on Desktop. (Closed)
Patch Set: Be more strict on country code 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 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 "components/payments/content/payment_response_helper.h" 5 #include "components/payments/content/payment_response_helper.h"
6 6
7 #include "base/strings/string_split.h" 7 #include "base/strings/string_split.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "components/autofill/core/browser/autofill_profile.h" 9 #include "components/autofill/core/browser/autofill_country.h"
10 #include "components/autofill/core/browser/autofill_data_util.h"
10 #include "components/autofill/core/browser/autofill_type.h" 11 #include "components/autofill/core/browser/autofill_type.h"
11 #include "components/payments/content/payment_request_spec.h" 12 #include "components/payments/content/payment_request_spec.h"
12 #include "third_party/libphonenumber/phonenumber_api.h" 13 #include "components/payments/core/payment_request_data_util.h"
14 #include "components/payments/core/payment_request_delegate.h"
13 15
14 namespace payments { 16 namespace payments {
15 17
16 namespace {
17
18 using ::i18n::phonenumbers::PhoneNumberUtil;
19
20 } // namespace
21
22 PaymentResponseHelper::PaymentResponseHelper( 18 PaymentResponseHelper::PaymentResponseHelper(
23 const std::string& app_locale, 19 const std::string& app_locale,
24 PaymentRequestSpec* spec, 20 PaymentRequestSpec* spec,
25 PaymentInstrument* selected_instrument, 21 PaymentInstrument* selected_instrument,
22 PaymentRequestDelegate* payment_request_delegate,
26 autofill::AutofillProfile* selected_shipping_profile, 23 autofill::AutofillProfile* selected_shipping_profile,
27 autofill::AutofillProfile* selected_contact_profile, 24 autofill::AutofillProfile* selected_contact_profile,
28 Delegate* delegate) 25 Delegate* delegate)
29 : app_locale_(app_locale), 26 : app_locale_(app_locale),
27 is_waiting_for_shipping_address_normalization_(false),
28 is_waiting_for_instrument_details_(false),
30 spec_(spec), 29 spec_(spec),
31 delegate_(delegate), 30 delegate_(delegate),
32 selected_instrument_(selected_instrument), 31 selected_instrument_(selected_instrument),
33 selected_shipping_profile_(selected_shipping_profile), 32 payment_request_delegate_(payment_request_delegate),
34 selected_contact_profile_(selected_contact_profile) { 33 selected_contact_profile_(selected_contact_profile) {
35 DCHECK(spec_); 34 DCHECK(spec_);
36 DCHECK(selected_instrument_); 35 DCHECK(selected_instrument_);
37 DCHECK(delegate_); 36 DCHECK(delegate_);
38 37
38 is_waiting_for_instrument_details_ = true;
39
40 // Start to normalize the shipping address, if necessary.
41 if (spec_->request_shipping()) {
42 DCHECK(selected_shipping_profile);
43 DCHECK(spec_->selected_shipping_option());
44
45 is_waiting_for_shipping_address_normalization_ = true;
46
47 // Use the country code from the profile if it is set, otherwise infer it
48 // from the |app_locale_|.
49 std::string country_code = base::UTF16ToUTF8(
50 selected_shipping_profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
51 if (!autofill::data_util::IsValidCountryCode(country_code)) {
52 country_code =
53 autofill::AutofillCountry::CountryCodeForLocale(app_locale_);
54 }
55
56 payment_request_delegate_->GetAddressNormalizer()
57 ->StartAddressNormalization(*selected_shipping_profile, country_code,
58 /*timeout_seconds=*/5, this);
59 }
60
39 // Start to get the instrument details. Will call back into 61 // Start to get the instrument details. Will call back into
40 // OnInstrumentDetailsReady. 62 // OnInstrumentDetailsReady.
41 selected_instrument_->InvokePaymentApp(this); 63 selected_instrument_->InvokePaymentApp(this);
42 }; 64 };
43 65
44 PaymentResponseHelper::~PaymentResponseHelper(){}; 66 PaymentResponseHelper::~PaymentResponseHelper(){};
45 67
46 // static 68 // static
47 mojom::PaymentAddressPtr 69 mojom::PaymentAddressPtr
48 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( 70 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile(
(...skipping 27 matching lines...) Expand all
76 // TODO(crbug.com/705945): Format phone number according to spec. 98 // TODO(crbug.com/705945): Format phone number according to spec.
77 payment_address->phone = 99 payment_address->phone =
78 base::UTF16ToUTF8(profile->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER)); 100 base::UTF16ToUTF8(profile->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER));
79 101
80 return payment_address; 102 return payment_address;
81 } 103 }
82 104
83 void PaymentResponseHelper::OnInstrumentDetailsReady( 105 void PaymentResponseHelper::OnInstrumentDetailsReady(
84 const std::string& method_name, 106 const std::string& method_name,
85 const std::string& stringified_details) { 107 const std::string& stringified_details) {
108 method_name_ = method_name;
109 stringified_details_ = stringified_details;
110 is_waiting_for_instrument_details_ = false;
111
112 if (!is_waiting_for_shipping_address_normalization_)
113 GeneratePaymentResponse();
114 }
115
116 void PaymentResponseHelper::OnAddressNormalized(
117 const autofill::AutofillProfile& normalized_profile) {
118 if (is_waiting_for_shipping_address_normalization_) {
119 shipping_address_ = normalized_profile;
120 is_waiting_for_shipping_address_normalization_ = false;
121
122 if (!is_waiting_for_instrument_details_)
123 GeneratePaymentResponse();
124 }
125 }
126
127 void PaymentResponseHelper::OnCouldNotNormalize(
128 const autofill::AutofillProfile& profile) {
129 // Since the phone number is formatted in either case, this profile should be
130 // used.
131 OnAddressNormalized(profile);
132 }
133
134 void PaymentResponseHelper::GeneratePaymentResponse() {
135 DCHECK(!is_waiting_for_instrument_details_);
136 DCHECK(!is_waiting_for_shipping_address_normalization_);
137
86 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); 138 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New();
87 139
88 // Make sure that we return the method name that the merchant specified for 140 // Make sure that we return the method name that the merchant specified for
89 // this instrument: cards can be either specified through their name (e.g., 141 // this instrument: cards can be either specified through their name (e.g.,
90 // "visa") or through basic-card's supportedNetworks. 142 // "visa") or through basic-card's supportedNetworks.
91 payment_response->method_name = 143 payment_response->method_name =
92 spec_->IsMethodSupportedThroughBasicCard(method_name) 144 spec_->IsMethodSupportedThroughBasicCard(method_name_)
93 ? kBasicCardMethodName 145 ? kBasicCardMethodName
94 : method_name; 146 : method_name_;
95 payment_response->stringified_details = stringified_details; 147 payment_response->stringified_details = stringified_details_;
96 148
97 // Shipping Address section 149 // Shipping Address section
98 if (spec_->request_shipping()) { 150 if (spec_->request_shipping()) {
99 DCHECK(selected_shipping_profile_);
100 payment_response->shipping_address = 151 payment_response->shipping_address =
101 GetMojomPaymentAddressFromAutofillProfile(selected_shipping_profile_, 152 GetMojomPaymentAddressFromAutofillProfile(&shipping_address_,
102 app_locale_); 153 app_locale_);
103
104 DCHECK(spec_->selected_shipping_option());
105 payment_response->shipping_option = spec_->selected_shipping_option()->id; 154 payment_response->shipping_option = spec_->selected_shipping_option()->id;
106 } 155 }
107 156
108 // Contact Details section. 157 // Contact Details section.
109 if (spec_->request_payer_name()) { 158 if (spec_->request_payer_name()) {
110 DCHECK(selected_contact_profile_); 159 DCHECK(selected_contact_profile_);
111 payment_response->payer_name = 160 payment_response->payer_name =
112 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( 161 base::UTF16ToUTF8(selected_contact_profile_->GetInfo(
113 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); 162 autofill::AutofillType(autofill::NAME_FULL), app_locale_));
114 } 163 }
115 if (spec_->request_payer_email()) { 164 if (spec_->request_payer_email()) {
116 DCHECK(selected_contact_profile_); 165 DCHECK(selected_contact_profile_);
117 payment_response->payer_email = base::UTF16ToUTF8( 166 payment_response->payer_email = base::UTF16ToUTF8(
118 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); 167 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS));
119 } 168 }
120 if (spec_->request_payer_phone()) { 169 if (spec_->request_payer_phone()) {
121 DCHECK(selected_contact_profile_); 170 DCHECK(selected_contact_profile_);
122 171
123 // Try to format the phone number to the E.164 format to send in the Payment 172 // Try to format the phone number to the E.164 format to send in the Payment
124 // Response, as defined in the Payment Request spec. If it's not possible, 173 // Response, as defined in the Payment Request spec. If it's not possible,
125 // send the original. More info at: 174 // send the original. More info at:
126 // https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorit hm 175 // https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorit hm
127 // TODO(sebsg): Move this code to a reusable location.
128 const std::string original_number = 176 const std::string original_number =
129 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( 177 base::UTF16ToUTF8(selected_contact_profile_->GetInfo(
130 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), 178 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER),
131 app_locale_)); 179 app_locale_));
132 i18n::phonenumbers::PhoneNumber parsed_number; 180
133 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); 181 const std::string default_region_code =
134 if (phone_number_util->Parse(original_number, "US", &parsed_number) == 182 autofill::AutofillCountry::CountryCodeForLocale(app_locale_);
135 ::i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { 183 payment_response->payer_phone =
136 std::string formatted_number; 184 data_util::FormatPhoneForResponse(original_number, default_region_code);
137 phone_number_util->Format(parsed_number,
138 PhoneNumberUtil::PhoneNumberFormat::E164,
139 &formatted_number);
140 payment_response->payer_phone = formatted_number;
141 } else {
142 payment_response->payer_phone = original_number;
143 }
144 } 185 }
145 186
146 delegate_->OnPaymentResponseReady(std::move(payment_response)); 187 delegate_->OnPaymentResponseReady(std::move(payment_response));
147 } 188 }
148 189
149 } // namespace payments 190 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/content/payment_response_helper.h ('k') | components/payments/content/payment_response_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698