Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: components/payments/content/payment_request_state.cc

Issue 2757523002: [Payments] Use PaymentInstrument/AutofillPaymentInstrument throughout. (Closed)
Patch Set: Initial Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/autofill/core/browser/personal_data_manager.h"
11 #include "components/payments/content/payment_request_spec.h" 11 #include "components/payments/content/payment_request_spec.h"
12 #include "components/payments/core/autofill_payment_instrument.h" 12 #include "components/payments/core/autofill_payment_instrument.h"
13 13
14 namespace payments { 14 namespace payments {
15 15
16 namespace {
17 // Identifier for the basic card payment method in the PaymentMethodData.
18 static const char* const kBasicCardMethodName = "basic-card";
19 } // namespace
20
21 PaymentRequestState::PaymentRequestState( 16 PaymentRequestState::PaymentRequestState(
22 PaymentRequestSpec* spec, 17 PaymentRequestSpec* spec,
23 Delegate* delegate, 18 Delegate* delegate,
24 const std::string& app_locale, 19 const std::string& app_locale,
25 autofill::PersonalDataManager* personal_data_manager) 20 autofill::PersonalDataManager* personal_data_manager)
26 : is_ready_to_pay_(false), 21 : is_ready_to_pay_(false),
27 app_locale_(app_locale), 22 app_locale_(app_locale),
28 spec_(spec), 23 spec_(spec),
29 delegate_(delegate), 24 delegate_(delegate),
30 personal_data_manager_(personal_data_manager), 25 personal_data_manager_(personal_data_manager),
31 selected_shipping_profile_(nullptr), 26 selected_shipping_profile_(nullptr),
32 selected_contact_profile_(nullptr), 27 selected_contact_profile_(nullptr),
33 selected_credit_card_(nullptr), 28 selected_instrument_(nullptr),
34 selected_shipping_option_(nullptr) { 29 selected_shipping_option_(nullptr) {
35 PopulateProfileCache(); 30 PopulateProfileCache();
36 UpdateSelectedShippingOption(); 31 UpdateSelectedShippingOption();
37 SetDefaultProfileSelections(); 32 SetDefaultProfileSelections();
38 } 33 }
39 34
40 void PaymentRequestState::AddObserver(Observer* observer) { 35 void PaymentRequestState::AddObserver(Observer* observer) {
41 CHECK(observer); 36 CHECK(observer);
42 observers_.AddObserver(observer); 37 observers_.AddObserver(observer);
43 } 38 }
44 PaymentRequestState::~PaymentRequestState() {} 39 PaymentRequestState::~PaymentRequestState() {}
45 40
46 void PaymentRequestState::RemoveObserver(Observer* observer) { 41 void PaymentRequestState::RemoveObserver(Observer* observer) {
47 observers_.RemoveObserver(observer); 42 observers_.RemoveObserver(observer);
48 } 43 }
49 44
50 void PaymentRequestState::OnInstrumentDetailsReady( 45 void PaymentRequestState::OnInstrumentDetailsReady(
51 const std::string& method_name, 46 const std::string& method_name,
52 const std::string& stringified_details) { 47 const std::string& stringified_details) {
53 // TODO(mathp): Fill other fields in the PaymentResponsePtr object. 48 // TODO(mathp): Fill other fields in the PaymentResponsePtr object.
54 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); 49 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New();
55 50
56 payment_response->method_name = method_name; 51 payment_response->method_name = method_name;
57 payment_response->stringified_details = stringified_details; 52 payment_response->stringified_details = stringified_details;
58 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); 53 delegate_->OnPaymentResponseAvailable(std::move(payment_response));
59 } 54 }
60 55
61 void PaymentRequestState::GeneratePaymentResponse() { 56 void PaymentRequestState::GeneratePaymentResponse() {
62 // TODO(mathp): PaymentRequest should know about the currently selected
63 // instrument, and not |selected_credit_card_| which is too specific.
64 // TODO(mathp): The method_name should reflect what the merchant asked, and
65 // not necessarily basic-card.
66 selected_payment_instrument_.reset(new AutofillPaymentInstrument(
67 kBasicCardMethodName, *selected_credit_card_, shipping_profiles_,
68 app_locale_));
69 // Fetch the instrument details, will call back into 57 // Fetch the instrument details, will call back into
70 // PaymentRequest::OnInstrumentsDetailsReady. 58 // PaymentRequest::OnInstrumentsDetailsReady.
71 selected_payment_instrument_->InvokePaymentApp(this); 59 selected_instrument_->InvokePaymentApp(this);
72 } 60 }
73 61
74 void PaymentRequestState::SetSelectedShippingProfile( 62 void PaymentRequestState::SetSelectedShippingProfile(
75 autofill::AutofillProfile* profile) { 63 autofill::AutofillProfile* profile) {
76 selected_shipping_profile_ = profile; 64 selected_shipping_profile_ = profile;
77 UpdateIsReadyToPayAndNotifyObservers(); 65 UpdateIsReadyToPayAndNotifyObservers();
78 } 66 }
79 67
80 void PaymentRequestState::SetSelectedContactProfile( 68 void PaymentRequestState::SetSelectedContactProfile(
81 autofill::AutofillProfile* profile) { 69 autofill::AutofillProfile* profile) {
82 selected_contact_profile_ = profile; 70 selected_contact_profile_ = profile;
83 UpdateIsReadyToPayAndNotifyObservers(); 71 UpdateIsReadyToPayAndNotifyObservers();
84 } 72 }
85 73
86 void PaymentRequestState::SetSelectedCreditCard(autofill::CreditCard* card) { 74 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) {
87 selected_credit_card_ = card; 75 selected_instrument_ = instrument;
88 UpdateIsReadyToPayAndNotifyObservers(); 76 UpdateIsReadyToPayAndNotifyObservers();
89 } 77 }
90 78
91 const std::string& PaymentRequestState::GetApplicationLocale() { 79 const std::string& PaymentRequestState::GetApplicationLocale() {
92 return app_locale_; 80 return app_locale_;
93 } 81 }
94 82
95 autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { 83 autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() {
96 return personal_data_manager_; 84 return personal_data_manager_;
97 } 85 }
98 86
99 void PaymentRequestState::PopulateProfileCache() { 87 void PaymentRequestState::PopulateProfileCache() {
100 std::vector<autofill::AutofillProfile*> profiles = 88 std::vector<autofill::AutofillProfile*> profiles =
101 personal_data_manager_->GetProfilesToSuggest(); 89 personal_data_manager_->GetProfilesToSuggest();
102 90
103 // PaymentRequest may outlive the Profiles returned by the Data Manager. 91 // PaymentRequest may outlive the Profiles returned by the Data Manager.
104 // Thus, we store copies, and return a vector of pointers to these copies 92 // Thus, we store copies, and return a vector of pointers to these copies
105 // whenever Profiles are requested. The same is true for credit cards. 93 // whenever Profiles are requested. The same is true for credit cards.
106 for (size_t i = 0; i < profiles.size(); i++) { 94 for (size_t i = 0; i < profiles.size(); i++) {
107 profile_cache_.push_back( 95 profile_cache_.push_back(
108 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); 96 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
109 97
110 // TODO(tmartino): Implement deduplication rules specific to shipping and 98 // TODO(tmartino): Implement deduplication rules specific to shipping and
111 // contact profiles. 99 // contact profiles.
112 shipping_profiles_.push_back(profile_cache_[i].get()); 100 shipping_profiles_.push_back(profile_cache_[i].get());
113 contact_profiles_.push_back(profile_cache_[i].get()); 101 contact_profiles_.push_back(profile_cache_[i].get());
114 } 102 }
115 103
104 // Create the list of available instruments.
116 const std::vector<autofill::CreditCard*>& cards = 105 const std::vector<autofill::CreditCard*>& cards =
117 personal_data_manager_->GetCreditCardsToSuggest(); 106 personal_data_manager_->GetCreditCardsToSuggest();
107 const std::set<std::string>& supported_card_networks =
108 spec_->supported_card_networks_set();
118 for (autofill::CreditCard* card : cards) { 109 for (autofill::CreditCard* card : cards) {
119 card_cache_.push_back(base::MakeUnique<autofill::CreditCard>(*card)); 110 std::string basic_card_network =
120 credit_cards_.push_back(card_cache_.back().get()); 111 autofill::data_util::GetPaymentRequestData(card->type())
112 .basic_card_payment_type;
113 if (!supported_card_networks.count(basic_card_network))
114 continue;
115
116 // TODO(crbug.com/701952): Should use the method name preferred by the
117 // merchant (either "basic-card" or the basic card network e.g. "visa").
118
119 // Copy the credit cards as part of AutofillPaymentInstrument so they are
120 // indirectly owned by this object.
121 std::unique_ptr<PaymentInstrument> instrument =
122 base::MakeUnique<AutofillPaymentInstrument>(
123 basic_card_network, *card, shipping_profiles_, app_locale_);
124 instrument_cache_.push_back(std::move(instrument));
125 available_instruments_.push_back(instrument_cache_.back().get());
121 } 126 }
122 } 127 }
123 128
124 void PaymentRequestState::SetDefaultProfileSelections() { 129 void PaymentRequestState::SetDefaultProfileSelections() {
125 if (!shipping_profiles().empty()) 130 if (!shipping_profiles().empty())
126 selected_shipping_profile_ = shipping_profiles()[0]; 131 selected_shipping_profile_ = shipping_profiles()[0];
127 132
128 if (!contact_profiles().empty()) 133 if (!contact_profiles().empty())
129 selected_contact_profile_ = contact_profiles()[0]; 134 selected_contact_profile_ = contact_profiles()[0];
130 135
131 // TODO(anthonyvd): Change this code to prioritize server cards and implement 136 // TODO(crbug.com/702063): Change this code to prioritize instruments by use
132 // a way to modify this function's return value. 137 // count and other means, and implement a way to modify this function's return
133 const std::vector<autofill::CreditCard*> cards = credit_cards(); 138 // value.
134 auto first_complete_card = 139 const std::vector<PaymentInstrument*> instruments = available_instruments();
135 std::find_if(cards.begin(), cards.end(), 140 auto first_complete_instrument = std::find_if(
136 [](autofill::CreditCard* card) { return card->IsValid(); }); 141 instruments.begin(), instruments.end(),
142 [](PaymentInstrument* instrument) { return instrument->IsValid(); });
137 143
138 selected_credit_card_ = 144 selected_instrument_ = first_complete_instrument == instruments.end()
139 first_complete_card == cards.end() ? nullptr : *first_complete_card; 145 ? nullptr
146 : *first_complete_instrument;
140 147
141 UpdateIsReadyToPayAndNotifyObservers(); 148 UpdateIsReadyToPayAndNotifyObservers();
142 } 149 }
143 150
144 void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { 151 void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() {
145 is_ready_to_pay_ = 152 is_ready_to_pay_ =
146 ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); 153 ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied();
147 NotifyOnSelectedInformationChanged(); 154 NotifyOnSelectedInformationChanged();
148 } 155 }
149 156
150 void PaymentRequestState::NotifyOnSelectedInformationChanged() { 157 void PaymentRequestState::NotifyOnSelectedInformationChanged() {
151 for (auto& observer : observers_) 158 for (auto& observer : observers_)
152 observer.OnSelectedInformationChanged(); 159 observer.OnSelectedInformationChanged();
153 } 160 }
154 161
155 bool PaymentRequestState::ArePaymentDetailsSatisfied() { 162 bool PaymentRequestState::ArePaymentDetailsSatisfied() {
156 // TODO(mathp): A masked card may not satisfy IsValid(). 163 // There is no need to check for supported networks, because only supported
157 if (selected_credit_card_ == nullptr || !selected_credit_card_->IsValid()) 164 // instruments are listed/created in the flow.
158 return false; 165 // TODO(crbug.com/702063): A masked card may not satisfy IsValid().
159 166 return selected_instrument_ != nullptr && selected_instrument_->IsValid();
160 const std::string basic_card_payment_type =
161 autofill::data_util::GetPaymentRequestData(selected_credit_card_->type())
162 .basic_card_payment_type;
163 return !spec_->supported_card_networks().empty() &&
164 std::find(spec_->supported_card_networks().begin(),
165 spec_->supported_card_networks().end(),
166 basic_card_payment_type) !=
167 spec_->supported_card_networks().end();
168 } 167 }
169 168
170 bool PaymentRequestState::ArePaymentOptionsSatisfied() { 169 bool PaymentRequestState::ArePaymentOptionsSatisfied() {
171 // TODO(mathp): Have a measure of shipping address completeness. 170 // TODO(mathp): Have a measure of shipping address completeness.
172 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) 171 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr)
173 return false; 172 return false;
174 173
175 // TODO(mathp): Make an encompassing class to validate contact info. 174 // TODO(mathp): Make an encompassing class to validate contact info.
176 if (spec_->request_payer_name() && 175 if (spec_->request_payer_name() &&
177 (selected_contact_profile_ == nullptr || 176 (selected_contact_profile_ == nullptr ||
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 spec_->details().shipping_options.rend(), 209 spec_->details().shipping_options.rend(),
211 [](const payments::mojom::PaymentShippingOptionPtr& element) { 210 [](const payments::mojom::PaymentShippingOptionPtr& element) {
212 return element->selected; 211 return element->selected;
213 }); 212 });
214 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) { 213 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) {
215 selected_shipping_option_ = selected_shipping_option_it->get(); 214 selected_shipping_option_ = selected_shipping_option_it->get();
216 } 215 }
217 } 216 }
218 217
219 } // namespace payments 218 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698