| 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 "components/autofill/core/browser/autofill_data_util.h" | 7 #include "components/autofill/core/browser/autofill_data_util.h" |
| 8 #include "components/autofill/core/browser/autofill_profile.h" | 8 #include "components/autofill/core/browser/autofill_profile.h" |
| 9 #include "components/autofill/core/browser/credit_card.h" | 9 #include "components/autofill/core/browser/credit_card.h" |
| 10 #include "components/autofill/core/browser/personal_data_manager.h" |
| 10 #include "components/payments/content/payment_request_spec.h" | 11 #include "components/payments/content/payment_request_spec.h" |
| 11 #include "components/payments/core/autofill_payment_instrument.h" | 12 #include "components/payments/core/autofill_payment_instrument.h" |
| 12 | 13 |
| 13 namespace payments { | 14 namespace payments { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 // Identifier for the basic card payment method in the PaymentMethodData. | 17 // Identifier for the basic card payment method in the PaymentMethodData. |
| 17 static const char* const kBasicCardMethodName = "basic-card"; | 18 static const char* const kBasicCardMethodName = "basic-card"; |
| 18 } // namespace | 19 } // namespace |
| 19 | 20 |
| 20 PaymentRequestState::PaymentRequestState(PaymentRequestSpec* spec, | 21 PaymentRequestState::PaymentRequestState( |
| 21 Delegate* delegate) | 22 PaymentRequestSpec* spec, |
| 23 Delegate* delegate, |
| 24 const std::string& app_locale, |
| 25 autofill::PersonalDataManager* personal_data_manager) |
| 22 : is_ready_to_pay_(false), | 26 : is_ready_to_pay_(false), |
| 27 app_locale_(app_locale), |
| 23 spec_(spec), | 28 spec_(spec), |
| 24 delegate_(delegate), | 29 delegate_(delegate), |
| 30 personal_data_manager_(personal_data_manager), |
| 25 selected_shipping_profile_(nullptr), | 31 selected_shipping_profile_(nullptr), |
| 26 selected_contact_profile_(nullptr), | 32 selected_contact_profile_(nullptr), |
| 27 selected_credit_card_(nullptr), | 33 selected_credit_card_(nullptr), |
| 28 selected_shipping_option_(nullptr) { | 34 selected_shipping_option_(nullptr) { |
| 29 PopulateProfileCache(); | 35 PopulateProfileCache(); |
| 30 UpdateSelectedShippingOption(); | 36 UpdateSelectedShippingOption(); |
| 31 SetDefaultProfileSelections(); | 37 SetDefaultProfileSelections(); |
| 32 } | 38 } |
| 33 | 39 |
| 34 void PaymentRequestState::AddObserver(Observer* observer) { | 40 void PaymentRequestState::AddObserver(Observer* observer) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 52 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); | 58 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 53 } | 59 } |
| 54 | 60 |
| 55 void PaymentRequestState::GeneratePaymentResponse() { | 61 void PaymentRequestState::GeneratePaymentResponse() { |
| 56 // TODO(mathp): PaymentRequest should know about the currently selected | 62 // TODO(mathp): PaymentRequest should know about the currently selected |
| 57 // instrument, and not |selected_credit_card_| which is too specific. | 63 // instrument, and not |selected_credit_card_| which is too specific. |
| 58 // TODO(mathp): The method_name should reflect what the merchant asked, and | 64 // TODO(mathp): The method_name should reflect what the merchant asked, and |
| 59 // not necessarily basic-card. | 65 // not necessarily basic-card. |
| 60 selected_payment_instrument_.reset(new AutofillPaymentInstrument( | 66 selected_payment_instrument_.reset(new AutofillPaymentInstrument( |
| 61 kBasicCardMethodName, *selected_credit_card_, shipping_profiles_, | 67 kBasicCardMethodName, *selected_credit_card_, shipping_profiles_, |
| 62 delegate_->GetApplicationLocale())); | 68 app_locale_)); |
| 63 // Fetch the instrument details, will call back into | 69 // Fetch the instrument details, will call back into |
| 64 // PaymentRequest::OnInstrumentsDetailsReady. | 70 // PaymentRequest::OnInstrumentsDetailsReady. |
| 65 selected_payment_instrument_->InvokePaymentApp(this); | 71 selected_payment_instrument_->InvokePaymentApp(this); |
| 66 } | 72 } |
| 67 | 73 |
| 68 void PaymentRequestState::SetSelectedShippingProfile( | 74 void PaymentRequestState::SetSelectedShippingProfile( |
| 69 autofill::AutofillProfile* profile) { | 75 autofill::AutofillProfile* profile) { |
| 70 selected_shipping_profile_ = profile; | 76 selected_shipping_profile_ = profile; |
| 71 UpdateIsReadyToPayAndNotifyObservers(); | 77 UpdateIsReadyToPayAndNotifyObservers(); |
| 72 } | 78 } |
| 73 | 79 |
| 74 void PaymentRequestState::SetSelectedContactProfile( | 80 void PaymentRequestState::SetSelectedContactProfile( |
| 75 autofill::AutofillProfile* profile) { | 81 autofill::AutofillProfile* profile) { |
| 76 selected_contact_profile_ = profile; | 82 selected_contact_profile_ = profile; |
| 77 UpdateIsReadyToPayAndNotifyObservers(); | 83 UpdateIsReadyToPayAndNotifyObservers(); |
| 78 } | 84 } |
| 79 | 85 |
| 80 void PaymentRequestState::SetSelectedCreditCard(autofill::CreditCard* card) { | 86 void PaymentRequestState::SetSelectedCreditCard(autofill::CreditCard* card) { |
| 81 selected_credit_card_ = card; | 87 selected_credit_card_ = card; |
| 82 UpdateIsReadyToPayAndNotifyObservers(); | 88 UpdateIsReadyToPayAndNotifyObservers(); |
| 83 } | 89 } |
| 84 | 90 |
| 91 const std::string& PaymentRequestState::GetApplicationLocale() { |
| 92 return app_locale_; |
| 93 } |
| 94 |
| 95 autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { |
| 96 return personal_data_manager_; |
| 97 } |
| 98 |
| 85 void PaymentRequestState::PopulateProfileCache() { | 99 void PaymentRequestState::PopulateProfileCache() { |
| 86 autofill::PersonalDataManager* personal_data_manager = | |
| 87 delegate_->GetPersonalDataManager(); | |
| 88 DCHECK(personal_data_manager); | |
| 89 std::vector<autofill::AutofillProfile*> profiles = | 100 std::vector<autofill::AutofillProfile*> profiles = |
| 90 personal_data_manager->GetProfilesToSuggest(); | 101 personal_data_manager_->GetProfilesToSuggest(); |
| 91 | 102 |
| 92 // PaymentRequest may outlive the Profiles returned by the Data Manager. | 103 // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 93 // Thus, we store copies, and return a vector of pointers to these copies | 104 // Thus, we store copies, and return a vector of pointers to these copies |
| 94 // whenever Profiles are requested. The same is true for credit cards. | 105 // whenever Profiles are requested. The same is true for credit cards. |
| 95 for (size_t i = 0; i < profiles.size(); i++) { | 106 for (size_t i = 0; i < profiles.size(); i++) { |
| 96 profile_cache_.push_back( | 107 profile_cache_.push_back( |
| 97 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); | 108 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
| 98 | 109 |
| 99 // TODO(tmartino): Implement deduplication rules specific to shipping and | 110 // TODO(tmartino): Implement deduplication rules specific to shipping and |
| 100 // contact profiles. | 111 // contact profiles. |
| 101 shipping_profiles_.push_back(profile_cache_[i].get()); | 112 shipping_profiles_.push_back(profile_cache_[i].get()); |
| 102 contact_profiles_.push_back(profile_cache_[i].get()); | 113 contact_profiles_.push_back(profile_cache_[i].get()); |
| 103 } | 114 } |
| 104 | 115 |
| 105 const std::vector<autofill::CreditCard*>& cards = | 116 const std::vector<autofill::CreditCard*>& cards = |
| 106 personal_data_manager->GetCreditCardsToSuggest(); | 117 personal_data_manager_->GetCreditCardsToSuggest(); |
| 107 for (autofill::CreditCard* card : cards) { | 118 for (autofill::CreditCard* card : cards) { |
| 108 card_cache_.push_back(base::MakeUnique<autofill::CreditCard>(*card)); | 119 card_cache_.push_back(base::MakeUnique<autofill::CreditCard>(*card)); |
| 109 credit_cards_.push_back(card_cache_.back().get()); | 120 credit_cards_.push_back(card_cache_.back().get()); |
| 110 } | 121 } |
| 111 } | 122 } |
| 112 | 123 |
| 113 void PaymentRequestState::SetDefaultProfileSelections() { | 124 void PaymentRequestState::SetDefaultProfileSelections() { |
| 114 if (!shipping_profiles().empty()) | 125 if (!shipping_profiles().empty()) |
| 115 selected_shipping_profile_ = shipping_profiles()[0]; | 126 selected_shipping_profile_ = shipping_profiles()[0]; |
| 116 | 127 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 basic_card_payment_type) != | 166 basic_card_payment_type) != |
| 156 spec_->supported_card_networks().end(); | 167 spec_->supported_card_networks().end(); |
| 157 } | 168 } |
| 158 | 169 |
| 159 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 170 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| 160 // TODO(mathp): Have a measure of shipping address completeness. | 171 // TODO(mathp): Have a measure of shipping address completeness. |
| 161 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 172 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
| 162 return false; | 173 return false; |
| 163 | 174 |
| 164 // TODO(mathp): Make an encompassing class to validate contact info. | 175 // TODO(mathp): Make an encompassing class to validate contact info. |
| 165 const std::string& app_locale = delegate_->GetApplicationLocale(); | |
| 166 if (spec_->request_payer_name() && | 176 if (spec_->request_payer_name() && |
| 167 (selected_contact_profile_ == nullptr || | 177 (selected_contact_profile_ == nullptr || |
| 168 selected_contact_profile_ | 178 selected_contact_profile_ |
| 169 ->GetInfo(autofill::AutofillType(autofill::NAME_FULL), app_locale) | 179 ->GetInfo(autofill::AutofillType(autofill::NAME_FULL), app_locale_) |
| 170 .empty())) { | 180 .empty())) { |
| 171 return false; | 181 return false; |
| 172 } | 182 } |
| 173 if (spec_->request_payer_email() && | 183 if (spec_->request_payer_email() && |
| 174 (selected_contact_profile_ == nullptr || | 184 (selected_contact_profile_ == nullptr || |
| 175 selected_contact_profile_ | 185 selected_contact_profile_ |
| 176 ->GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS), | 186 ->GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS), |
| 177 app_locale) | 187 app_locale_) |
| 178 .empty())) { | 188 .empty())) { |
| 179 return false; | 189 return false; |
| 180 } | 190 } |
| 181 if (spec_->request_payer_phone() && | 191 if (spec_->request_payer_phone() && |
| 182 (selected_contact_profile_ == nullptr || | 192 (selected_contact_profile_ == nullptr || |
| 183 selected_contact_profile_ | 193 selected_contact_profile_ |
| 184 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), | 194 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
| 185 app_locale) | 195 app_locale_) |
| 186 .empty())) { | 196 .empty())) { |
| 187 return false; | 197 return false; |
| 188 } | 198 } |
| 189 | 199 |
| 190 return true; | 200 return true; |
| 191 } | 201 } |
| 192 | 202 |
| 193 void PaymentRequestState::UpdateSelectedShippingOption() { | 203 void PaymentRequestState::UpdateSelectedShippingOption() { |
| 194 selected_shipping_option_ = nullptr; | 204 selected_shipping_option_ = nullptr; |
| 195 | 205 |
| 196 // As per the spec, the selected shipping option should initially be the last | 206 // As per the spec, the selected shipping option should initially be the last |
| 197 // one in the array that has its selected field set to true. | 207 // one in the array that has its selected field set to true. |
| 198 auto selected_shipping_option_it = std::find_if( | 208 auto selected_shipping_option_it = std::find_if( |
| 199 spec_->details().shipping_options.rbegin(), | 209 spec_->details().shipping_options.rbegin(), |
| 200 spec_->details().shipping_options.rend(), | 210 spec_->details().shipping_options.rend(), |
| 201 [](const payments::mojom::PaymentShippingOptionPtr& element) { | 211 [](const payments::mojom::PaymentShippingOptionPtr& element) { |
| 202 return element->selected; | 212 return element->selected; |
| 203 }); | 213 }); |
| 204 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) { | 214 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) { |
| 205 selected_shipping_option_ = selected_shipping_option_it->get(); | 215 selected_shipping_option_ = selected_shipping_option_it->get(); |
| 206 } | 216 } |
| 207 } | 217 } |
| 208 | 218 |
| 209 } // namespace payments | 219 } // namespace payments |
| OLD | NEW |