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_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" | 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 app_locale_(app_locale), | 33 app_locale_(app_locale), |
33 spec_(spec), | 34 spec_(spec), |
34 delegate_(delegate), | 35 delegate_(delegate), |
35 personal_data_manager_(personal_data_manager), | 36 personal_data_manager_(personal_data_manager), |
36 selected_shipping_profile_(nullptr), | 37 selected_shipping_profile_(nullptr), |
37 selected_contact_profile_(nullptr), | 38 selected_contact_profile_(nullptr), |
38 selected_instrument_(nullptr), | 39 selected_instrument_(nullptr), |
39 payment_request_delegate_(payment_request_delegate) { | 40 payment_request_delegate_(payment_request_delegate) { |
40 PopulateProfileCache(); | 41 PopulateProfileCache(); |
41 SetDefaultProfileSelections(); | 42 SetDefaultProfileSelections(); |
43 spec_->AddObserver(this); | |
42 } | 44 } |
43 PaymentRequestState::~PaymentRequestState() {} | 45 PaymentRequestState::~PaymentRequestState() {} |
44 | 46 |
45 void PaymentRequestState::OnPaymentResponseReady( | 47 void PaymentRequestState::OnPaymentResponseReady( |
46 mojom::PaymentResponsePtr payment_response) { | 48 mojom::PaymentResponsePtr payment_response) { |
47 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); | 49 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
48 } | 50 } |
49 | 51 |
52 void PaymentRequestState::OnAddressNormalized( | |
53 const autofill::AutofillProfile& normalized_profile) { | |
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 | |
66 void PaymentRequestState::OnSpecUpdated() { | |
67 is_waiting_for_merchant_validation_ = false; | |
Mathieu
2017/04/27 11:04:01
could this be on line 54?
sebsg
2017/04/27 14:43:23
Not really, as the merchant has not validated yet.
| |
68 UpdateIsReadyToPayAndNotifyObservers(); | |
69 } | |
70 | |
50 bool PaymentRequestState::CanMakePayment() const { | 71 bool PaymentRequestState::CanMakePayment() const { |
51 for (const std::unique_ptr<PaymentInstrument>& instrument : | 72 for (const std::unique_ptr<PaymentInstrument>& instrument : |
52 available_instruments_) { | 73 available_instruments_) { |
53 if (instrument->IsValidForCanMakePayment()) { | 74 if (instrument->IsValidForCanMakePayment()) { |
54 // AddAutofillPaymentInstrument() filters out available instruments based | 75 // AddAutofillPaymentInstrument() filters out available instruments based |
55 // on supported card networks. | 76 // on supported card networks. |
56 DCHECK(spec_->supported_card_networks_set().find( | 77 DCHECK(spec_->supported_card_networks_set().find( |
57 instrument->method_name()) != | 78 instrument->method_name()) != |
58 spec_->supported_card_networks_set().end()); | 79 spec_->supported_card_networks_set().end()); |
59 return true; | 80 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 | 147 // This will inform the merchant and will lead to them calling updateWith with |
127 // new PaymentDetails. | 148 // new PaymentDetails. |
128 delegate_->OnShippingOptionIdSelected(shipping_option_id); | 149 delegate_->OnShippingOptionIdSelected(shipping_option_id); |
129 } | 150 } |
130 | 151 |
131 void PaymentRequestState::SetSelectedShippingProfile( | 152 void PaymentRequestState::SetSelectedShippingProfile( |
132 autofill::AutofillProfile* profile) { | 153 autofill::AutofillProfile* profile) { |
133 spec_->StartWaitingForUpdateWith( | 154 spec_->StartWaitingForUpdateWith( |
134 PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); | 155 PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
135 selected_shipping_profile_ = profile; | 156 selected_shipping_profile_ = profile; |
157 | |
158 // The user should not be able to click on pay until the callback from the | |
159 // merchant. | |
160 is_waiting_for_merchant_validation_ = true; | |
Mathieu
2017/04/27 11:04:01
another option is to set |selected_shipping_profil
sebsg
2017/04/27 14:43:23
I thought about that but when we get the result fr
| |
136 UpdateIsReadyToPayAndNotifyObservers(); | 161 UpdateIsReadyToPayAndNotifyObservers(); |
137 delegate_->OnShippingAddressSelected( | 162 |
138 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( | 163 // Start the normalization of the shipping address. |
139 selected_shipping_profile_, app_locale_)); | 164 // Use the country code from the profile if it is set, otherwise infer it |
165 // from the |app_locale_|. | |
166 std::string country_code = base::UTF16ToUTF8( | |
167 selected_shipping_profile_->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); | |
168 if (!autofill::data_util::IsValidCountryCode(country_code)) | |
169 country_code = autofill::AutofillCountry::CountryCodeForLocale(app_locale_); | |
170 payment_request_delegate_->GetAddressNormalizer()->StartAddressNormalization( | |
171 *selected_shipping_profile_, country_code, /*timeout_seconds=*/2, this); | |
140 } | 172 } |
141 | 173 |
142 void PaymentRequestState::SetSelectedContactProfile( | 174 void PaymentRequestState::SetSelectedContactProfile( |
143 autofill::AutofillProfile* profile) { | 175 autofill::AutofillProfile* profile) { |
144 selected_contact_profile_ = profile; | 176 selected_contact_profile_ = profile; |
145 UpdateIsReadyToPayAndNotifyObservers(); | 177 UpdateIsReadyToPayAndNotifyObservers(); |
146 } | 178 } |
147 | 179 |
148 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { | 180 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { |
149 selected_instrument_ = instrument; | 181 selected_instrument_ = instrument; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 // instruments are listed/created in the flow. | 271 // instruments are listed/created in the flow. |
240 return selected_instrument_ != nullptr && | 272 return selected_instrument_ != nullptr && |
241 selected_instrument_->IsCompleteForPayment(); | 273 selected_instrument_->IsCompleteForPayment(); |
242 } | 274 } |
243 | 275 |
244 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 276 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
245 // TODO(mathp): Have a measure of shipping address completeness. | 277 // TODO(mathp): Have a measure of shipping address completeness. |
246 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 278 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
247 return false; | 279 return false; |
248 | 280 |
281 if (is_waiting_for_merchant_validation_) | |
282 return false; | |
283 | |
249 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); | 284 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); |
250 return comparator.IsContactInfoComplete(selected_contact_profile_); | 285 return comparator.IsContactInfoComplete(selected_contact_profile_); |
251 } | 286 } |
252 | 287 |
253 } // namespace payments | 288 } // namespace payments |
OLD | NEW |