mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/payments/content/payment_request_state.h" |
| 6 | |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 7 | #include <set> |
| 8 | |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame^] | 9 | #include "base/strings/utf_string_conversions.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 10 | #include "components/autofill/core/browser/autofill_data_util.h" |
| 11 | #include "components/autofill/core/browser/autofill_profile.h" |
| 12 | #include "components/autofill/core/browser/credit_card.h" |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 13 | #include "components/autofill/core/browser/personal_data_manager.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 14 | #include "components/payments/content/payment_request_spec.h" |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame^] | 15 | #include "components/payments/content/payment_response_helper.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 16 | #include "components/payments/core/autofill_payment_instrument.h" |
| 17 | |
| 18 | namespace payments { |
| 19 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 20 | PaymentRequestState::PaymentRequestState( |
| 21 | PaymentRequestSpec* spec, |
| 22 | Delegate* delegate, |
| 23 | const std::string& app_locale, |
| 24 | autofill::PersonalDataManager* personal_data_manager) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 25 | : is_ready_to_pay_(false), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 26 | app_locale_(app_locale), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 27 | spec_(spec), |
| 28 | delegate_(delegate), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 29 | personal_data_manager_(personal_data_manager), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 30 | selected_shipping_profile_(nullptr), |
| 31 | selected_contact_profile_(nullptr), |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 32 | selected_instrument_(nullptr), |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 33 | selected_shipping_option_(nullptr) { |
| 34 | PopulateProfileCache(); |
| 35 | UpdateSelectedShippingOption(); |
| 36 | SetDefaultProfileSelections(); |
| 37 | } |
| 38 | |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 39 | bool PaymentRequestState::CanMakePayment() const { |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 40 | for (const std::unique_ptr<PaymentInstrument>& instrument : |
| 41 | available_instruments_) { |
| 42 | if (instrument.get()->IsValid() && |
| 43 | spec_->supported_card_networks_set().count( |
| 44 | instrument.get()->method_name())) { |
| 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 51 | void PaymentRequestState::AddObserver(Observer* observer) { |
| 52 | CHECK(observer); |
| 53 | observers_.AddObserver(observer); |
| 54 | } |
| 55 | PaymentRequestState::~PaymentRequestState() {} |
| 56 | |
| 57 | void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 58 | observers_.RemoveObserver(observer); |
| 59 | } |
| 60 | |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame^] | 61 | // TODO(sebsg): Move this to the PaymentResponseHelper. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 62 | void PaymentRequestState::OnInstrumentDetailsReady( |
| 63 | const std::string& method_name, |
| 64 | const std::string& stringified_details) { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 65 | mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); |
| 66 | |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 67 | // Make sure that we return the method name that the merchant specified for |
| 68 | // this instrument: cards can be either specified through their name (e.g., |
| 69 | // "visa") or through basic-card's supportedNetworks. |
| 70 | payment_response->method_name = |
| 71 | spec_->IsMethodSupportedThroughBasicCard(method_name) |
| 72 | ? kBasicCardMethodName |
| 73 | : method_name; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 74 | payment_response->stringified_details = stringified_details; |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame^] | 75 | |
| 76 | // Shipping Address section |
| 77 | if (spec_->request_shipping()) { |
| 78 | DCHECK(selected_shipping_profile_); |
| 79 | payment_response->shipping_address = |
| 80 | PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( |
| 81 | selected_shipping_profile_, app_locale_); |
| 82 | |
| 83 | DCHECK(selected_shipping_option_); |
| 84 | payment_response->shipping_option = selected_shipping_option_->id; |
| 85 | } |
| 86 | |
| 87 | // Contact Details section. |
| 88 | if (spec_->request_payer_name()) { |
| 89 | DCHECK(selected_contact_profile_); |
| 90 | payment_response->payer_name = |
| 91 | base::UTF16ToUTF8(selected_contact_profile_->GetInfo( |
| 92 | autofill::AutofillType(autofill::NAME_FULL), app_locale_)); |
| 93 | } |
| 94 | if (spec_->request_payer_email()) { |
| 95 | DCHECK(selected_contact_profile_); |
| 96 | payment_response->payer_email = base::UTF16ToUTF8( |
| 97 | selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); |
| 98 | } |
| 99 | if (spec_->request_payer_phone()) { |
| 100 | DCHECK(selected_contact_profile_); |
| 101 | // TODO(crbug.com/705945): Format phone number according to spec. |
| 102 | payment_response->payer_phone = |
| 103 | base::UTF16ToUTF8(selected_contact_profile_->GetRawInfo( |
| 104 | autofill::PHONE_HOME_WHOLE_NUMBER)); |
| 105 | } |
| 106 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 107 | delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 108 | } |
| 109 | |
| 110 | void PaymentRequestState::GeneratePaymentResponse() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 111 | DCHECK(is_ready_to_pay()); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 112 | // Fetch the instrument details, will call back into |
sebsg | ad86bd00 | 2017-03-29 16:39:12 | [diff] [blame^] | 113 | // PaymentRequest::OnInstrumentDetailsReady. |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 114 | selected_instrument_->InvokePaymentApp(this); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 115 | } |
| 116 | |
anthonyvd | 0116ce33 | 2017-03-21 21:29:01 | [diff] [blame] | 117 | void PaymentRequestState::SetSelectedShippingOption( |
| 118 | mojom::PaymentShippingOption* option) { |
| 119 | selected_shipping_option_ = option; |
| 120 | UpdateIsReadyToPayAndNotifyObservers(); |
| 121 | } |
| 122 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 123 | void PaymentRequestState::SetSelectedShippingProfile( |
| 124 | autofill::AutofillProfile* profile) { |
| 125 | selected_shipping_profile_ = profile; |
| 126 | UpdateIsReadyToPayAndNotifyObservers(); |
| 127 | } |
| 128 | |
| 129 | void PaymentRequestState::SetSelectedContactProfile( |
| 130 | autofill::AutofillProfile* profile) { |
| 131 | selected_contact_profile_ = profile; |
| 132 | UpdateIsReadyToPayAndNotifyObservers(); |
| 133 | } |
| 134 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 135 | void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { |
| 136 | selected_instrument_ = instrument; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 137 | UpdateIsReadyToPayAndNotifyObservers(); |
| 138 | } |
| 139 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 140 | const std::string& PaymentRequestState::GetApplicationLocale() { |
| 141 | return app_locale_; |
| 142 | } |
| 143 | |
| 144 | autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { |
| 145 | return personal_data_manager_; |
| 146 | } |
| 147 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 148 | void PaymentRequestState::PopulateProfileCache() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 149 | std::vector<autofill::AutofillProfile*> profiles = |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 150 | personal_data_manager_->GetProfilesToSuggest(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 151 | |
| 152 | // PaymentRequest may outlive the Profiles returned by the Data Manager. |
| 153 | // Thus, we store copies, and return a vector of pointers to these copies |
| 154 | // whenever Profiles are requested. The same is true for credit cards. |
| 155 | for (size_t i = 0; i < profiles.size(); i++) { |
| 156 | profile_cache_.push_back( |
| 157 | base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); |
| 158 | |
| 159 | // TODO(tmartino): Implement deduplication rules specific to shipping and |
| 160 | // contact profiles. |
| 161 | shipping_profiles_.push_back(profile_cache_[i].get()); |
| 162 | contact_profiles_.push_back(profile_cache_[i].get()); |
| 163 | } |
| 164 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 165 | // Create the list of available instruments. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 166 | const std::vector<autofill::CreditCard*>& cards = |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 167 | personal_data_manager_->GetCreditCardsToSuggest(); |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 168 | const std::set<std::string>& supported_card_networks = |
| 169 | spec_->supported_card_networks_set(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 170 | for (autofill::CreditCard* card : cards) { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 171 | std::string basic_card_network = |
| 172 | autofill::data_util::GetPaymentRequestData(card->type()) |
| 173 | .basic_card_payment_type; |
| 174 | if (!supported_card_networks.count(basic_card_network)) |
| 175 | continue; |
| 176 | |
| 177 | // TODO(crbug.com/701952): Should use the method name preferred by the |
| 178 | // merchant (either "basic-card" or the basic card network e.g. "visa"). |
| 179 | |
| 180 | // Copy the credit cards as part of AutofillPaymentInstrument so they are |
| 181 | // indirectly owned by this object. |
| 182 | std::unique_ptr<PaymentInstrument> instrument = |
| 183 | base::MakeUnique<AutofillPaymentInstrument>( |
| 184 | basic_card_network, *card, shipping_profiles_, app_locale_); |
| 185 | available_instruments_.push_back(std::move(instrument)); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | void PaymentRequestState::SetDefaultProfileSelections() { |
| 190 | if (!shipping_profiles().empty()) |
| 191 | selected_shipping_profile_ = shipping_profiles()[0]; |
| 192 | |
| 193 | if (!contact_profiles().empty()) |
| 194 | selected_contact_profile_ = contact_profiles()[0]; |
| 195 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 196 | // TODO(crbug.com/702063): Change this code to prioritize instruments by use |
| 197 | // count and other means, and implement a way to modify this function's return |
| 198 | // value. |
| 199 | const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = |
| 200 | available_instruments(); |
| 201 | auto first_complete_instrument = |
| 202 | std::find_if(instruments.begin(), instruments.end(), |
| 203 | [](const std::unique_ptr<PaymentInstrument>& instrument) { |
| 204 | return instrument->IsValid(); |
| 205 | }); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 206 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 207 | selected_instrument_ = first_complete_instrument == instruments.end() |
| 208 | ? nullptr |
| 209 | : first_complete_instrument->get(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 210 | |
| 211 | UpdateIsReadyToPayAndNotifyObservers(); |
| 212 | } |
| 213 | |
| 214 | void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { |
| 215 | is_ready_to_pay_ = |
| 216 | ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); |
| 217 | NotifyOnSelectedInformationChanged(); |
| 218 | } |
| 219 | |
| 220 | void PaymentRequestState::NotifyOnSelectedInformationChanged() { |
| 221 | for (auto& observer : observers_) |
| 222 | observer.OnSelectedInformationChanged(); |
| 223 | } |
| 224 | |
| 225 | bool PaymentRequestState::ArePaymentDetailsSatisfied() { |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 226 | // There is no need to check for supported networks, because only supported |
| 227 | // instruments are listed/created in the flow. |
| 228 | // TODO(crbug.com/702063): A masked card may not satisfy IsValid(). |
| 229 | return selected_instrument_ != nullptr && selected_instrument_->IsValid(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| 233 | // TODO(mathp): Have a measure of shipping address completeness. |
| 234 | if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
| 235 | return false; |
| 236 | |
| 237 | // TODO(mathp): Make an encompassing class to validate contact info. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 238 | if (spec_->request_payer_name() && |
| 239 | (selected_contact_profile_ == nullptr || |
| 240 | selected_contact_profile_ |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 241 | ->GetInfo(autofill::AutofillType(autofill::NAME_FULL), app_locale_) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 242 | .empty())) { |
| 243 | return false; |
| 244 | } |
| 245 | if (spec_->request_payer_email() && |
| 246 | (selected_contact_profile_ == nullptr || |
| 247 | selected_contact_profile_ |
| 248 | ->GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 249 | app_locale_) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 250 | .empty())) { |
| 251 | return false; |
| 252 | } |
| 253 | if (spec_->request_payer_phone() && |
| 254 | (selected_contact_profile_ == nullptr || |
| 255 | selected_contact_profile_ |
| 256 | ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 257 | app_locale_) |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 258 | .empty())) { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | void PaymentRequestState::UpdateSelectedShippingOption() { |
| 266 | selected_shipping_option_ = nullptr; |
| 267 | |
| 268 | // As per the spec, the selected shipping option should initially be the last |
| 269 | // one in the array that has its selected field set to true. |
| 270 | auto selected_shipping_option_it = std::find_if( |
| 271 | spec_->details().shipping_options.rbegin(), |
| 272 | spec_->details().shipping_options.rend(), |
| 273 | [](const payments::mojom::PaymentShippingOptionPtr& element) { |
| 274 | return element->selected; |
| 275 | }); |
| 276 | if (selected_shipping_option_it != spec_->details().shipping_options.rend()) { |
| 277 | selected_shipping_option_ = selected_shipping_option_it->get(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | } // namespace payments |