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