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