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 is_waiting_for_shipping_address_normalization_ = true; | |
45 | |
46 // Use the country code from the profile if it is set, otherwise infer it | |
47 // from the |app_locale_|. | |
48 std::string region_code = base::UTF16ToUTF8( | |
49 selected_shipping_profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); | |
50 if (region_code.empty()) | |
please use gerrit instead
2017/04/20 21:13:06
Can you check that it matches ^[A-Z]{2}$ regex ins
sebsg
2017/04/20 22:58:27
Done.
| |
51 region_code = | |
52 autofill::AutofillCountry::CountryCodeForLocale(app_locale_); | |
53 | |
54 payment_request_delegate_->GetAddressNormalizer() | |
55 ->StartAddressNormalization(*selected_shipping_profile, region_code, | |
56 /*timeout_seconds=*/5, this); | |
57 } | |
58 | |
39 // Start to get the instrument details. Will call back into | 59 // Start to get the instrument details. Will call back into |
40 // OnInstrumentDetailsReady. | 60 // OnInstrumentDetailsReady. |
41 selected_instrument_->InvokePaymentApp(this); | 61 selected_instrument_->InvokePaymentApp(this); |
42 }; | 62 }; |
43 | 63 |
44 PaymentResponseHelper::~PaymentResponseHelper(){}; | 64 PaymentResponseHelper::~PaymentResponseHelper(){}; |
45 | 65 |
46 // static | 66 // static |
47 mojom::PaymentAddressPtr | 67 mojom::PaymentAddressPtr |
48 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( | 68 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( |
(...skipping 27 matching lines...) Expand all Loading... | |
76 // TODO(crbug.com/705945): Format phone number according to spec. | 96 // TODO(crbug.com/705945): Format phone number according to spec. |
77 payment_address->phone = | 97 payment_address->phone = |
78 base::UTF16ToUTF8(profile->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER)); | 98 base::UTF16ToUTF8(profile->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER)); |
79 | 99 |
80 return payment_address; | 100 return payment_address; |
81 } | 101 } |
82 | 102 |
83 void PaymentResponseHelper::OnInstrumentDetailsReady( | 103 void PaymentResponseHelper::OnInstrumentDetailsReady( |
84 const std::string& method_name, | 104 const std::string& method_name, |
85 const std::string& stringified_details) { | 105 const std::string& stringified_details) { |
106 method_name_ = method_name; | |
107 stringified_details_ = stringified_details; | |
108 is_waiting_for_instrument_details_ = false; | |
109 | |
110 if (!is_waiting_for_shipping_address_normalization_) | |
111 GeneratePaymentResponse(); | |
112 } | |
113 | |
114 void PaymentResponseHelper::OnAddressNormalized( | |
115 const autofill::AutofillProfile& normalized_profile) { | |
116 if (is_waiting_for_shipping_address_normalization_) { | |
117 shipping_address_ = normalized_profile; | |
118 is_waiting_for_shipping_address_normalization_ = false; | |
119 | |
120 if (!is_waiting_for_instrument_details_) | |
121 GeneratePaymentResponse(); | |
122 } | |
123 } | |
124 | |
125 void PaymentResponseHelper::OnCouldNotNormalize( | |
126 const autofill::AutofillProfile& profile) { | |
127 // Since the phone number is formatted in either case, this profile should be | |
128 // used. | |
129 OnAddressNormalized(profile); | |
130 } | |
131 | |
132 void PaymentResponseHelper::GeneratePaymentResponse() { | |
133 DCHECK(!is_waiting_for_instrument_details_); | |
134 DCHECK(!is_waiting_for_shipping_address_normalization_); | |
135 | |
86 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); | 136 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); |
87 | 137 |
88 // Make sure that we return the method name that the merchant specified for | 138 // 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., | 139 // this instrument: cards can be either specified through their name (e.g., |
90 // "visa") or through basic-card's supportedNetworks. | 140 // "visa") or through basic-card's supportedNetworks. |
91 payment_response->method_name = | 141 payment_response->method_name = |
92 spec_->IsMethodSupportedThroughBasicCard(method_name) | 142 spec_->IsMethodSupportedThroughBasicCard(method_name_) |
93 ? kBasicCardMethodName | 143 ? kBasicCardMethodName |
94 : method_name; | 144 : method_name_; |
95 payment_response->stringified_details = stringified_details; | 145 payment_response->stringified_details = stringified_details_; |
96 | 146 |
97 // Shipping Address section | 147 // Shipping Address section |
98 if (spec_->request_shipping()) { | 148 if (spec_->request_shipping()) { |
99 DCHECK(selected_shipping_profile_); | |
100 payment_response->shipping_address = | 149 payment_response->shipping_address = |
101 GetMojomPaymentAddressFromAutofillProfile(selected_shipping_profile_, | 150 GetMojomPaymentAddressFromAutofillProfile(&shipping_address_, |
102 app_locale_); | 151 app_locale_); |
103 | |
104 DCHECK(spec_->selected_shipping_option()); | |
105 payment_response->shipping_option = spec_->selected_shipping_option()->id; | 152 payment_response->shipping_option = spec_->selected_shipping_option()->id; |
106 } | 153 } |
107 | 154 |
108 // Contact Details section. | 155 // Contact Details section. |
109 if (spec_->request_payer_name()) { | 156 if (spec_->request_payer_name()) { |
110 DCHECK(selected_contact_profile_); | 157 DCHECK(selected_contact_profile_); |
111 payment_response->payer_name = | 158 payment_response->payer_name = |
112 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( | 159 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( |
113 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); | 160 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); |
114 } | 161 } |
115 if (spec_->request_payer_email()) { | 162 if (spec_->request_payer_email()) { |
116 DCHECK(selected_contact_profile_); | 163 DCHECK(selected_contact_profile_); |
117 payment_response->payer_email = base::UTF16ToUTF8( | 164 payment_response->payer_email = base::UTF16ToUTF8( |
118 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); | 165 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); |
119 } | 166 } |
120 if (spec_->request_payer_phone()) { | 167 if (spec_->request_payer_phone()) { |
121 DCHECK(selected_contact_profile_); | 168 DCHECK(selected_contact_profile_); |
122 | 169 |
123 // Try to format the phone number to the E.164 format to send in the Payment | 170 // 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, | 171 // Response, as defined in the Payment Request spec. If it's not possible, |
125 // send the original. More info at: | 172 // send the original. More info at: |
126 // https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorit hm | 173 // 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 = | 174 const std::string original_number = |
129 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( | 175 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( |
130 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), | 176 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
131 app_locale_)); | 177 app_locale_)); |
132 i18n::phonenumbers::PhoneNumber parsed_number; | 178 |
133 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | 179 const std::string default_region_code = |
134 if (phone_number_util->Parse(original_number, "US", &parsed_number) == | 180 autofill::AutofillCountry::CountryCodeForLocale(app_locale_); |
135 ::i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { | 181 payment_response->payer_phone = |
136 std::string formatted_number; | 182 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 } | 183 } |
145 | 184 |
146 delegate_->OnPaymentResponseReady(std::move(payment_response)); | 185 delegate_->OnPaymentResponseReady(std::move(payment_response)); |
147 } | 186 } |
148 | 187 |
149 } // namespace payments | 188 } // namespace payments |
OLD | NEW |