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_data_util.h" | 13 #include "components/autofill/core/browser/autofill_data_util.h" |
13 #include "components/autofill/core/browser/autofill_profile.h" | 14 #include "components/autofill/core/browser/autofill_profile.h" |
14 #include "components/autofill/core/browser/credit_card.h" | 15 #include "components/autofill/core/browser/credit_card.h" |
15 #include "components/autofill/core/browser/personal_data_manager.h" | 16 #include "components/autofill/core/browser/personal_data_manager.h" |
16 #include "components/payments/content/payment_request_spec.h" | 17 #include "components/payments/content/payment_request_spec.h" |
17 #include "components/payments/content/payment_response_helper.h" | 18 #include "components/payments/content/payment_response_helper.h" |
18 #include "components/payments/core/autofill_payment_instrument.h" | 19 #include "components/payments/core/autofill_payment_instrument.h" |
19 #include "components/payments/core/payment_instrument.h" | 20 #include "components/payments/core/payment_instrument.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 #include "components/payments/core/profile_util.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
40 PopulateProfileCache(); | 41 PopulateProfileCache(); |
41 SetDefaultProfileSelections(); | 42 SetDefaultProfileSelections(); |
42 } | 43 } |
43 PaymentRequestState::~PaymentRequestState() {} | 44 PaymentRequestState::~PaymentRequestState() {} |
44 | 45 |
45 void PaymentRequestState::OnPaymentResponseReady( | 46 void PaymentRequestState::OnPaymentResponseReady( |
46 mojom::PaymentResponsePtr payment_response) { | 47 mojom::PaymentResponsePtr payment_response) { |
47 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); | 48 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
48 } | 49 } |
49 | 50 |
51 void PaymentRequestState::OnAddressNormalized( | |
52 const autofill::AutofillProfile& normalized_profile) { | |
53 UpdateIsReadyToPayAndNotifyObservers(); | |
Mathieu
2017/04/26 13:09:49
what could have changed that may affect "is ready
sebsg
2017/04/26 19:28:53
I made a bunch of changes to this logic, to make s
| |
54 delegate_->OnShippingAddressSelected( | |
55 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( | |
56 normalized_profile, app_locale_)); | |
57 } | |
58 | |
59 void PaymentRequestState::OnCouldNotNormalize( | |
60 const autofill::AutofillProfile& profile) { | |
61 // Since the phone number is formatted in either case, this profile should be | |
62 // used. | |
63 OnAddressNormalized(profile); | |
64 } | |
65 | |
50 bool PaymentRequestState::CanMakePayment() const { | 66 bool PaymentRequestState::CanMakePayment() const { |
51 for (const std::unique_ptr<PaymentInstrument>& instrument : | 67 for (const std::unique_ptr<PaymentInstrument>& instrument : |
52 available_instruments_) { | 68 available_instruments_) { |
53 if (instrument->IsValidForCanMakePayment()) { | 69 if (instrument->IsValidForCanMakePayment()) { |
54 // AddAutofillPaymentInstrument() filters out available instruments based | 70 // AddAutofillPaymentInstrument() filters out available instruments based |
55 // on supported card networks. | 71 // on supported card networks. |
56 DCHECK(spec_->supported_card_networks_set().find( | 72 DCHECK(spec_->supported_card_networks_set().find( |
57 instrument->method_name()) != | 73 instrument->method_name()) != |
58 spec_->supported_card_networks_set().end()); | 74 spec_->supported_card_networks_set().end()); |
59 return true; | 75 return true; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 // This will inform the merchant and will lead to them calling updateWith with | 142 // This will inform the merchant and will lead to them calling updateWith with |
127 // new PaymentDetails. | 143 // new PaymentDetails. |
128 delegate_->OnShippingOptionIdSelected(shipping_option_id); | 144 delegate_->OnShippingOptionIdSelected(shipping_option_id); |
129 } | 145 } |
130 | 146 |
131 void PaymentRequestState::SetSelectedShippingProfile( | 147 void PaymentRequestState::SetSelectedShippingProfile( |
132 autofill::AutofillProfile* profile) { | 148 autofill::AutofillProfile* profile) { |
133 spec_->StartWaitingForUpdateWith( | 149 spec_->StartWaitingForUpdateWith( |
134 PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); | 150 PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
135 selected_shipping_profile_ = profile; | 151 selected_shipping_profile_ = profile; |
136 UpdateIsReadyToPayAndNotifyObservers(); | 152 |
137 delegate_->OnShippingAddressSelected( | 153 // Start the normalization of the shipping address. |
138 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( | 154 // Use the country code from the profile if it is set, otherwise infer it |
139 selected_shipping_profile_, app_locale_)); | 155 // from the |app_locale_|. |
156 std::string country_code = base::UTF16ToUTF8( | |
157 selected_shipping_profile_->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); | |
158 if (!autofill::data_util::IsValidCountryCode(country_code)) { | |
Mathieu
2017/04/26 13:09:49
no curlies
sebsg
2017/04/26 19:28:53
Done.
| |
159 country_code = autofill::AutofillCountry::CountryCodeForLocale(app_locale_); | |
160 } | |
161 payment_request_delegate_->GetAddressNormalizer()->StartAddressNormalization( | |
162 *selected_shipping_profile_, country_code, /*timeout_seconds=*/2, this); | |
140 } | 163 } |
141 | 164 |
142 void PaymentRequestState::SetSelectedContactProfile( | 165 void PaymentRequestState::SetSelectedContactProfile( |
143 autofill::AutofillProfile* profile) { | 166 autofill::AutofillProfile* profile) { |
144 selected_contact_profile_ = profile; | 167 selected_contact_profile_ = profile; |
145 UpdateIsReadyToPayAndNotifyObservers(); | 168 UpdateIsReadyToPayAndNotifyObservers(); |
146 } | 169 } |
147 | 170 |
148 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { | 171 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { |
149 selected_instrument_ = instrument; | 172 selected_instrument_ = instrument; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 267 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
245 // TODO(mathp): Have a measure of shipping address completeness. | 268 // TODO(mathp): Have a measure of shipping address completeness. |
246 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 269 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
247 return false; | 270 return false; |
248 | 271 |
249 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); | 272 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); |
250 return comparator.IsContactInfoComplete(selected_contact_profile_); | 273 return comparator.IsContactInfoComplete(selected_contact_profile_); |
251 } | 274 } |
252 | 275 |
253 } // namespace payments | 276 } // namespace payments |
OLD | NEW |