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

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

Issue 2757523002: [Payments] Use PaymentInstrument/AutofillPaymentInstrument throughout. (Closed)
Patch Set: addressed nits 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 57 DCHECK(is_ready_to_pay());
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 58 // Fetch the instrument details, will call back into
70 // PaymentRequest::OnInstrumentsDetailsReady. 59 // PaymentRequest::OnInstrumentsDetailsReady.
71 selected_payment_instrument_->InvokePaymentApp(this); 60 selected_instrument_->InvokePaymentApp(this);
72 } 61 }
73 62
74 void PaymentRequestState::SetSelectedShippingProfile( 63 void PaymentRequestState::SetSelectedShippingProfile(
75 autofill::AutofillProfile* profile) { 64 autofill::AutofillProfile* profile) {
76 selected_shipping_profile_ = profile; 65 selected_shipping_profile_ = profile;
77 UpdateIsReadyToPayAndNotifyObservers(); 66 UpdateIsReadyToPayAndNotifyObservers();
78 } 67 }
79 68
80 void PaymentRequestState::SetSelectedContactProfile( 69 void PaymentRequestState::SetSelectedContactProfile(
81 autofill::AutofillProfile* profile) { 70 autofill::AutofillProfile* profile) {
82 selected_contact_profile_ = profile; 71 selected_contact_profile_ = profile;
83 UpdateIsReadyToPayAndNotifyObservers(); 72 UpdateIsReadyToPayAndNotifyObservers();
84 } 73 }
85 74
86 void PaymentRequestState::SetSelectedCreditCard(autofill::CreditCard* card) { 75 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) {
87 selected_credit_card_ = card; 76 selected_instrument_ = instrument;
88 UpdateIsReadyToPayAndNotifyObservers(); 77 UpdateIsReadyToPayAndNotifyObservers();
89 } 78 }
90 79
91 const std::string& PaymentRequestState::GetApplicationLocale() { 80 const std::string& PaymentRequestState::GetApplicationLocale() {
92 return app_locale_; 81 return app_locale_;
93 } 82 }
94 83
95 autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() { 84 autofill::PersonalDataManager* PaymentRequestState::GetPersonalDataManager() {
96 return personal_data_manager_; 85 return personal_data_manager_;
97 } 86 }
98 87
99 void PaymentRequestState::PopulateProfileCache() { 88 void PaymentRequestState::PopulateProfileCache() {
100 std::vector<autofill::AutofillProfile*> profiles = 89 std::vector<autofill::AutofillProfile*> profiles =
101 personal_data_manager_->GetProfilesToSuggest(); 90 personal_data_manager_->GetProfilesToSuggest();
102 91
103 // PaymentRequest may outlive the Profiles returned by the Data Manager. 92 // PaymentRequest may outlive the Profiles returned by the Data Manager.
104 // Thus, we store copies, and return a vector of pointers to these copies 93 // 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. 94 // whenever Profiles are requested. The same is true for credit cards.
106 for (size_t i = 0; i < profiles.size(); i++) { 95 for (size_t i = 0; i < profiles.size(); i++) {
107 profile_cache_.push_back( 96 profile_cache_.push_back(
108 base::MakeUnique<autofill::AutofillProfile>(*profiles[i])); 97 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
109 98
110 // TODO(tmartino): Implement deduplication rules specific to shipping and 99 // TODO(tmartino): Implement deduplication rules specific to shipping and
111 // contact profiles. 100 // contact profiles.
112 shipping_profiles_.push_back(profile_cache_[i].get()); 101 shipping_profiles_.push_back(profile_cache_[i].get());
113 contact_profiles_.push_back(profile_cache_[i].get()); 102 contact_profiles_.push_back(profile_cache_[i].get());
114 } 103 }
115 104
105 // Create the list of available instruments.
116 const std::vector<autofill::CreditCard*>& cards = 106 const std::vector<autofill::CreditCard*>& cards =
117 personal_data_manager_->GetCreditCardsToSuggest(); 107 personal_data_manager_->GetCreditCardsToSuggest();
108 const std::set<std::string>& supported_card_networks =
109 spec_->supported_card_networks_set();
118 for (autofill::CreditCard* card : cards) { 110 for (autofill::CreditCard* card : cards) {
119 card_cache_.push_back(base::MakeUnique<autofill::CreditCard>(*card)); 111 std::string basic_card_network =
120 credit_cards_.push_back(card_cache_.back().get()); 112 autofill::data_util::GetPaymentRequestData(card->type())
113 .basic_card_payment_type;
114 if (!supported_card_networks.count(basic_card_network))
115 continue;
116
117 // TODO(crbug.com/701952): Should use the method name preferred by the
118 // merchant (either "basic-card" or the basic card network e.g. "visa").
119
120 // Copy the credit cards as part of AutofillPaymentInstrument so they are
121 // indirectly owned by this object.
122 std::unique_ptr<PaymentInstrument> instrument =
123 base::MakeUnique<AutofillPaymentInstrument>(
124 basic_card_network, *card, shipping_profiles_, app_locale_);
125 available_instruments_.push_back(std::move(instrument));
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<std::unique_ptr<PaymentInstrument>>& instruments =
135 std::find_if(cards.begin(), cards.end(), 140 available_instruments();
136 [](autofill::CreditCard* card) { return card->IsValid(); }); 141 auto first_complete_instrument =
142 std::find_if(instruments.begin(), instruments.end(),
143 [](const std::unique_ptr<PaymentInstrument>& instrument) {
144 return instrument->IsValid();
145 });
137 146
138 selected_credit_card_ = 147 selected_instrument_ = first_complete_instrument == instruments.end()
139 first_complete_card == cards.end() ? nullptr : *first_complete_card; 148 ? nullptr
149 : first_complete_instrument->get();
140 150
141 UpdateIsReadyToPayAndNotifyObservers(); 151 UpdateIsReadyToPayAndNotifyObservers();
142 } 152 }
143 153
144 void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() { 154 void PaymentRequestState::UpdateIsReadyToPayAndNotifyObservers() {
145 is_ready_to_pay_ = 155 is_ready_to_pay_ =
146 ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied(); 156 ArePaymentDetailsSatisfied() && ArePaymentOptionsSatisfied();
147 NotifyOnSelectedInformationChanged(); 157 NotifyOnSelectedInformationChanged();
148 } 158 }
149 159
150 void PaymentRequestState::NotifyOnSelectedInformationChanged() { 160 void PaymentRequestState::NotifyOnSelectedInformationChanged() {
151 for (auto& observer : observers_) 161 for (auto& observer : observers_)
152 observer.OnSelectedInformationChanged(); 162 observer.OnSelectedInformationChanged();
153 } 163 }
154 164
155 bool PaymentRequestState::ArePaymentDetailsSatisfied() { 165 bool PaymentRequestState::ArePaymentDetailsSatisfied() {
156 // TODO(mathp): A masked card may not satisfy IsValid(). 166 // There is no need to check for supported networks, because only supported
157 if (selected_credit_card_ == nullptr || !selected_credit_card_->IsValid()) 167 // instruments are listed/created in the flow.
158 return false; 168 // TODO(crbug.com/702063): A masked card may not satisfy IsValid().
159 169 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 } 170 }
169 171
170 bool PaymentRequestState::ArePaymentOptionsSatisfied() { 172 bool PaymentRequestState::ArePaymentOptionsSatisfied() {
171 // TODO(mathp): Have a measure of shipping address completeness. 173 // TODO(mathp): Have a measure of shipping address completeness.
172 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) 174 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr)
173 return false; 175 return false;
174 176
175 // TODO(mathp): Make an encompassing class to validate contact info. 177 // TODO(mathp): Make an encompassing class to validate contact info.
176 if (spec_->request_payer_name() && 178 if (spec_->request_payer_name() &&
177 (selected_contact_profile_ == nullptr || 179 (selected_contact_profile_ == nullptr ||
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 spec_->details().shipping_options.rend(), 212 spec_->details().shipping_options.rend(),
211 [](const payments::mojom::PaymentShippingOptionPtr& element) { 213 [](const payments::mojom::PaymentShippingOptionPtr& element) {
212 return element->selected; 214 return element->selected;
213 }); 215 });
214 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) { 216 if (selected_shipping_option_it != spec_->details().shipping_options.rend()) {
215 selected_shipping_option_ = selected_shipping_option_it->get(); 217 selected_shipping_option_ = selected_shipping_option_it->get();
216 } 218 }
217 } 219 }
218 220
219 } // namespace payments 221 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/content/payment_request_state.h ('k') | components/payments/content/payment_request_state_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698