OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_request_state.h" | 5 #include "components/payments/content/payment_request_state.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "components/autofill/core/browser/autofill_country.h" | 12 #include "components/autofill/core/browser/autofill_country.h" |
13 #include "components/autofill/core/browser/autofill_data_util.h" | 13 #include "components/autofill/core/browser/autofill_data_util.h" |
14 #include "components/autofill/core/browser/autofill_profile.h" | 14 #include "components/autofill/core/browser/autofill_profile.h" |
15 #include "components/autofill/core/browser/credit_card.h" | 15 #include "components/autofill/core/browser/credit_card.h" |
16 #include "components/autofill/core/browser/personal_data_manager.h" | 16 #include "components/autofill/core/browser/personal_data_manager.h" |
17 #include "components/payments/content/payment_response_helper.h" | 17 #include "components/payments/content/payment_response_helper.h" |
18 #include "components/payments/core/autofill_payment_instrument.h" | 18 #include "components/payments/core/autofill_payment_instrument.h" |
19 #include "components/payments/core/payment_instrument.h" | 19 #include "components/payments/core/payment_instrument.h" |
20 #include "components/payments/core/payment_request_delegate.h" | 20 #include "components/payments/core/payment_request_delegate.h" |
21 #include "components/payments/core/profile_util.h" | |
22 | 21 |
23 namespace payments { | 22 namespace payments { |
24 | 23 |
25 PaymentRequestState::PaymentRequestState( | 24 PaymentRequestState::PaymentRequestState( |
26 PaymentRequestSpec* spec, | 25 PaymentRequestSpec* spec, |
27 Delegate* delegate, | 26 Delegate* delegate, |
28 const std::string& app_locale, | 27 const std::string& app_locale, |
29 autofill::PersonalDataManager* personal_data_manager, | 28 autofill::PersonalDataManager* personal_data_manager, |
30 PaymentRequestDelegate* payment_request_delegate) | 29 PaymentRequestDelegate* payment_request_delegate) |
31 : is_ready_to_pay_(false), | 30 : is_ready_to_pay_(false), |
32 is_waiting_for_merchant_validation_(false), | 31 is_waiting_for_merchant_validation_(false), |
33 app_locale_(app_locale), | 32 app_locale_(app_locale), |
34 spec_(spec), | 33 spec_(spec), |
35 delegate_(delegate), | 34 delegate_(delegate), |
36 personal_data_manager_(personal_data_manager), | 35 personal_data_manager_(personal_data_manager), |
37 selected_shipping_profile_(nullptr), | 36 selected_shipping_profile_(nullptr), |
38 selected_contact_profile_(nullptr), | 37 selected_contact_profile_(nullptr), |
39 selected_instrument_(nullptr), | 38 selected_instrument_(nullptr), |
40 payment_request_delegate_(payment_request_delegate) { | 39 payment_request_delegate_(payment_request_delegate), |
| 40 profile_comparator_(app_locale, *spec) { |
41 PopulateProfileCache(); | 41 PopulateProfileCache(); |
42 SetDefaultProfileSelections(); | 42 SetDefaultProfileSelections(); |
43 spec_->AddObserver(this); | 43 spec_->AddObserver(this); |
44 } | 44 } |
45 PaymentRequestState::~PaymentRequestState() {} | 45 PaymentRequestState::~PaymentRequestState() {} |
46 | 46 |
47 void PaymentRequestState::OnPaymentResponseReady( | 47 void PaymentRequestState::OnPaymentResponseReady( |
48 mojom::PaymentResponsePtr payment_response) { | 48 mojom::PaymentResponsePtr payment_response) { |
49 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); | 49 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
50 } | 50 } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 selected_shipping_profile_ = profile; | 156 selected_shipping_profile_ = profile; |
157 | 157 |
158 // The user should not be able to click on pay until the callback from the | 158 // The user should not be able to click on pay until the callback from the |
159 // merchant. | 159 // merchant. |
160 is_waiting_for_merchant_validation_ = true; | 160 is_waiting_for_merchant_validation_ = true; |
161 UpdateIsReadyToPayAndNotifyObservers(); | 161 UpdateIsReadyToPayAndNotifyObservers(); |
162 | 162 |
163 // Start the normalization of the shipping address. | 163 // Start the normalization of the shipping address. |
164 // Use the country code from the profile if it is set, otherwise infer it | 164 // Use the country code from the profile if it is set, otherwise infer it |
165 // from the |app_locale_|. | 165 // from the |app_locale_|. |
166 std::string country_code = base::UTF16ToUTF8( | 166 std::string country_code = data_util::GetCountryCodeWithFallback( |
167 selected_shipping_profile_->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); | 167 selected_shipping_profile_, app_locale_); |
168 if (!autofill::data_util::IsValidCountryCode(country_code)) | |
169 country_code = autofill::AutofillCountry::CountryCodeForLocale(app_locale_); | |
170 payment_request_delegate_->GetAddressNormalizer()->StartAddressNormalization( | 168 payment_request_delegate_->GetAddressNormalizer()->StartAddressNormalization( |
171 *selected_shipping_profile_, country_code, /*timeout_seconds=*/2, this); | 169 *selected_shipping_profile_, country_code, /*timeout_seconds=*/2, this); |
172 } | 170 } |
173 | 171 |
174 void PaymentRequestState::SetSelectedContactProfile( | 172 void PaymentRequestState::SetSelectedContactProfile( |
175 autofill::AutofillProfile* profile) { | 173 autofill::AutofillProfile* profile) { |
176 selected_contact_profile_ = profile; | 174 selected_contact_profile_ = profile; |
177 UpdateIsReadyToPayAndNotifyObservers(); | 175 UpdateIsReadyToPayAndNotifyObservers(); |
178 } | 176 } |
179 | 177 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 } | 209 } |
212 | 210 |
213 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering( | 211 std::vector<autofill::AutofillProfile*> raw_profiles_for_filtering( |
214 profile_cache_.size()); | 212 profile_cache_.size()); |
215 std::transform(profile_cache_.begin(), profile_cache_.end(), | 213 std::transform(profile_cache_.begin(), profile_cache_.end(), |
216 raw_profiles_for_filtering.begin(), | 214 raw_profiles_for_filtering.begin(), |
217 [](const std::unique_ptr<autofill::AutofillProfile>& p) { | 215 [](const std::unique_ptr<autofill::AutofillProfile>& p) { |
218 return p.get(); | 216 return p.get(); |
219 }); | 217 }); |
220 | 218 |
221 contact_profiles_ = profile_util::FilterProfilesForContact( | 219 contact_profiles_ = profile_comparator()->FilterProfilesForContact( |
222 raw_profiles_for_filtering, GetApplicationLocale(), *spec_); | 220 raw_profiles_for_filtering); |
223 | 221 |
224 // Create the list of available instruments. | 222 // Create the list of available instruments. |
225 const std::vector<autofill::CreditCard*>& cards = | 223 const std::vector<autofill::CreditCard*>& cards = |
226 personal_data_manager_->GetCreditCardsToSuggest(); | 224 personal_data_manager_->GetCreditCardsToSuggest(); |
227 for (autofill::CreditCard* card : cards) | 225 for (autofill::CreditCard* card : cards) |
228 AddAutofillPaymentInstrument(/*selected=*/false, *card); | 226 AddAutofillPaymentInstrument(/*selected=*/false, *card); |
229 } | 227 } |
230 | 228 |
231 void PaymentRequestState::SetDefaultProfileSelections() { | 229 void PaymentRequestState::SetDefaultProfileSelections() { |
232 // Only pre-select an address if the merchant provided at least one selected | 230 // Only pre-select an address if the merchant provided at least one selected |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 } | 265 } |
268 | 266 |
269 bool PaymentRequestState::ArePaymentDetailsSatisfied() { | 267 bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
270 // There is no need to check for supported networks, because only supported | 268 // There is no need to check for supported networks, because only supported |
271 // instruments are listed/created in the flow. | 269 // instruments are listed/created in the flow. |
272 return selected_instrument_ != nullptr && | 270 return selected_instrument_ != nullptr && |
273 selected_instrument_->IsCompleteForPayment(); | 271 selected_instrument_->IsCompleteForPayment(); |
274 } | 272 } |
275 | 273 |
276 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 274 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
277 // TODO(mathp): Have a measure of shipping address completeness. | |
278 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | |
279 return false; | |
280 | |
281 if (is_waiting_for_merchant_validation_) | 275 if (is_waiting_for_merchant_validation_) |
282 return false; | 276 return false; |
283 | 277 |
284 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); | 278 if (!profile_comparator()->IsShippingComplete(selected_shipping_profile_)) |
285 return comparator.IsContactInfoComplete(selected_contact_profile_); | 279 return false; |
| 280 |
| 281 return profile_comparator()->IsContactInfoComplete(selected_contact_profile_); |
286 } | 282 } |
287 | 283 |
288 } // namespace payments | 284 } // namespace payments |
OLD | NEW |