| 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 <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" | 10 #include "components/autofill/core/browser/autofill_data_util.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); | 109 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 110 } | 110 } |
| 111 | 111 |
| 112 void PaymentRequestState::GeneratePaymentResponse() { | 112 void PaymentRequestState::GeneratePaymentResponse() { |
| 113 DCHECK(is_ready_to_pay()); | 113 DCHECK(is_ready_to_pay()); |
| 114 // Fetch the instrument details, will call back into | 114 // Fetch the instrument details, will call back into |
| 115 // PaymentRequest::OnInstrumentDetailsReady. | 115 // PaymentRequest::OnInstrumentDetailsReady. |
| 116 selected_instrument_->InvokePaymentApp(this); | 116 selected_instrument_->InvokePaymentApp(this); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void PaymentRequestState::AddAutofillPaymentInstrument( |
| 120 bool selected, |
| 121 const autofill::CreditCard& card) { |
| 122 std::string basic_card_network = |
| 123 autofill::data_util::GetPaymentRequestData(card.type()) |
| 124 .basic_card_payment_type; |
| 125 if (!spec_->supported_card_networks_set().count(basic_card_network)) |
| 126 return; |
| 127 |
| 128 // AutofillPaymentInstrument makes a copy of |card| so it is effectively |
| 129 // owned by this object. |
| 130 std::unique_ptr<PaymentInstrument> instrument = |
| 131 base::MakeUnique<AutofillPaymentInstrument>( |
| 132 basic_card_network, card, shipping_profiles_, app_locale_, |
| 133 payment_request_delegate_); |
| 134 available_instruments_.push_back(std::move(instrument)); |
| 135 |
| 136 if (selected) |
| 137 SetSelectedInstrument(available_instruments_.back().get()); |
| 138 } |
| 139 |
| 119 void PaymentRequestState::SetSelectedShippingOption( | 140 void PaymentRequestState::SetSelectedShippingOption( |
| 120 const std::string& shipping_option_id) { | 141 const std::string& shipping_option_id) { |
| 121 // 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 |
| 122 // new PaymentDetails. | 143 // new PaymentDetails. |
| 123 delegate_->OnShippingOptionIdSelected(shipping_option_id); | 144 delegate_->OnShippingOptionIdSelected(shipping_option_id); |
| 124 } | 145 } |
| 125 | 146 |
| 126 void PaymentRequestState::SetSelectedShippingProfile( | 147 void PaymentRequestState::SetSelectedShippingProfile( |
| 127 autofill::AutofillProfile* profile) { | 148 autofill::AutofillProfile* profile) { |
| 128 selected_shipping_profile_ = profile; | 149 selected_shipping_profile_ = profile; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 [](const std::unique_ptr<autofill::AutofillProfile>& p) { | 195 [](const std::unique_ptr<autofill::AutofillProfile>& p) { |
| 175 return p.get(); | 196 return p.get(); |
| 176 }); | 197 }); |
| 177 | 198 |
| 178 contact_profiles_ = profile_util::FilterProfilesForContact( | 199 contact_profiles_ = profile_util::FilterProfilesForContact( |
| 179 raw_profiles_for_filtering, GetApplicationLocale(), *spec_); | 200 raw_profiles_for_filtering, GetApplicationLocale(), *spec_); |
| 180 | 201 |
| 181 // Create the list of available instruments. | 202 // Create the list of available instruments. |
| 182 const std::vector<autofill::CreditCard*>& cards = | 203 const std::vector<autofill::CreditCard*>& cards = |
| 183 personal_data_manager_->GetCreditCardsToSuggest(); | 204 personal_data_manager_->GetCreditCardsToSuggest(); |
| 184 const std::set<std::string>& supported_card_networks = | 205 for (autofill::CreditCard* card : cards) |
| 185 spec_->supported_card_networks_set(); | 206 AddAutofillPaymentInstrument(/*selected=*/false, *card); |
| 186 for (autofill::CreditCard* card : cards) { | |
| 187 std::string basic_card_network = | |
| 188 autofill::data_util::GetPaymentRequestData(card->type()) | |
| 189 .basic_card_payment_type; | |
| 190 if (!supported_card_networks.count(basic_card_network)) | |
| 191 continue; | |
| 192 | |
| 193 // Copy the credit cards as part of AutofillPaymentInstrument so they are | |
| 194 // indirectly owned by this object. | |
| 195 std::unique_ptr<PaymentInstrument> instrument = | |
| 196 base::MakeUnique<AutofillPaymentInstrument>( | |
| 197 basic_card_network, *card, shipping_profiles_, app_locale_, | |
| 198 payment_request_delegate_); | |
| 199 available_instruments_.push_back(std::move(instrument)); | |
| 200 } | |
| 201 } | 207 } |
| 202 | 208 |
| 203 void PaymentRequestState::SetDefaultProfileSelections() { | 209 void PaymentRequestState::SetDefaultProfileSelections() { |
| 204 // Only pre-select an address if the merchant provided at least one selected | 210 // Only pre-select an address if the merchant provided at least one selected |
| 205 // shipping option. | 211 // shipping option. |
| 206 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) | 212 if (!shipping_profiles().empty() && spec_->selected_shipping_option()) |
| 207 selected_shipping_profile_ = shipping_profiles()[0]; | 213 selected_shipping_profile_ = shipping_profiles()[0]; |
| 208 | 214 |
| 209 if (!contact_profiles().empty()) | 215 if (!contact_profiles().empty()) |
| 210 selected_contact_profile_ = contact_profiles()[0]; | 216 selected_contact_profile_ = contact_profiles()[0]; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 254 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| 249 // TODO(mathp): Have a measure of shipping address completeness. | 255 // TODO(mathp): Have a measure of shipping address completeness. |
| 250 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 256 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
| 251 return false; | 257 return false; |
| 252 | 258 |
| 253 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); | 259 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); |
| 254 return comparator.IsContactInfoComplete(selected_contact_profile_); | 260 return comparator.IsContactInfoComplete(selected_contact_profile_); |
| 255 } | 261 } |
| 256 | 262 |
| 257 } // namespace payments | 263 } // namespace payments |
| OLD | NEW |