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

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

Issue 2789093002: [Payments] Desktop: implement shipping address/option change (Closed)
Patch Set: compile fix Created 3 years, 8 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 <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 11 matching lines...) Expand all
22 Delegate* delegate, 22 Delegate* delegate,
23 const std::string& app_locale, 23 const std::string& app_locale,
24 autofill::PersonalDataManager* personal_data_manager) 24 autofill::PersonalDataManager* personal_data_manager)
25 : is_ready_to_pay_(false), 25 : is_ready_to_pay_(false),
26 app_locale_(app_locale), 26 app_locale_(app_locale),
27 spec_(spec), 27 spec_(spec),
28 delegate_(delegate), 28 delegate_(delegate),
29 personal_data_manager_(personal_data_manager), 29 personal_data_manager_(personal_data_manager),
30 selected_shipping_profile_(nullptr), 30 selected_shipping_profile_(nullptr),
31 selected_contact_profile_(nullptr), 31 selected_contact_profile_(nullptr),
32 selected_instrument_(nullptr), 32 selected_instrument_(nullptr) {
33 selected_shipping_option_(nullptr) {
34 PopulateProfileCache(); 33 PopulateProfileCache();
35 UpdateSelectedShippingOption();
36 SetDefaultProfileSelections(); 34 SetDefaultProfileSelections();
37 } 35 }
36 PaymentRequestState::~PaymentRequestState() {}
38 37
39 bool PaymentRequestState::CanMakePayment() const { 38 bool PaymentRequestState::CanMakePayment() const {
40 for (const std::unique_ptr<PaymentInstrument>& instrument : 39 for (const std::unique_ptr<PaymentInstrument>& instrument :
41 available_instruments_) { 40 available_instruments_) {
42 if (instrument.get()->IsValid() && 41 if (instrument.get()->IsValid() &&
43 spec_->supported_card_networks_set().count( 42 spec_->supported_card_networks_set().count(
44 instrument.get()->method_name())) { 43 instrument.get()->method_name())) {
45 return true; 44 return true;
46 } 45 }
47 } 46 }
48 return false; 47 return false;
49 } 48 }
50 49
51 void PaymentRequestState::AddObserver(Observer* observer) { 50 void PaymentRequestState::AddObserver(Observer* observer) {
52 CHECK(observer); 51 CHECK(observer);
53 observers_.AddObserver(observer); 52 observers_.AddObserver(observer);
54 } 53 }
55 PaymentRequestState::~PaymentRequestState() {}
56 54
57 void PaymentRequestState::RemoveObserver(Observer* observer) { 55 void PaymentRequestState::RemoveObserver(Observer* observer) {
58 observers_.RemoveObserver(observer); 56 observers_.RemoveObserver(observer);
59 } 57 }
60 58
61 // TODO(sebsg): Move this to the PaymentResponseHelper. 59 // TODO(sebsg): Move this to the PaymentResponseHelper.
62 void PaymentRequestState::OnInstrumentDetailsReady( 60 void PaymentRequestState::OnInstrumentDetailsReady(
63 const std::string& method_name, 61 const std::string& method_name,
64 const std::string& stringified_details) { 62 const std::string& stringified_details) {
65 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); 63 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New();
66 64
67 // Make sure that we return the method name that the merchant specified for 65 // 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., 66 // this instrument: cards can be either specified through their name (e.g.,
69 // "visa") or through basic-card's supportedNetworks. 67 // "visa") or through basic-card's supportedNetworks.
70 payment_response->method_name = 68 payment_response->method_name =
71 spec_->IsMethodSupportedThroughBasicCard(method_name) 69 spec_->IsMethodSupportedThroughBasicCard(method_name)
72 ? kBasicCardMethodName 70 ? kBasicCardMethodName
73 : method_name; 71 : method_name;
74 payment_response->stringified_details = stringified_details; 72 payment_response->stringified_details = stringified_details;
75 73
76 // Shipping Address section 74 // Shipping Address section
77 if (spec_->request_shipping()) { 75 if (spec_->request_shipping()) {
78 DCHECK(selected_shipping_profile_); 76 DCHECK(selected_shipping_profile_);
79 payment_response->shipping_address = 77 payment_response->shipping_address =
80 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( 78 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile(
81 selected_shipping_profile_, app_locale_); 79 selected_shipping_profile_, app_locale_);
82 80
83 DCHECK(selected_shipping_option_); 81 DCHECK(spec_->selected_shipping_option());
84 payment_response->shipping_option = selected_shipping_option_->id; 82 payment_response->shipping_option = spec_->selected_shipping_option()->id;
85 } 83 }
86 84
87 // Contact Details section. 85 // Contact Details section.
88 if (spec_->request_payer_name()) { 86 if (spec_->request_payer_name()) {
89 DCHECK(selected_contact_profile_); 87 DCHECK(selected_contact_profile_);
90 payment_response->payer_name = 88 payment_response->payer_name =
91 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( 89 base::UTF16ToUTF8(selected_contact_profile_->GetInfo(
92 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); 90 autofill::AutofillType(autofill::NAME_FULL), app_locale_));
93 } 91 }
94 if (spec_->request_payer_email()) { 92 if (spec_->request_payer_email()) {
(...skipping 13 matching lines...) Expand all
108 } 106 }
109 107
110 void PaymentRequestState::GeneratePaymentResponse() { 108 void PaymentRequestState::GeneratePaymentResponse() {
111 DCHECK(is_ready_to_pay()); 109 DCHECK(is_ready_to_pay());
112 // Fetch the instrument details, will call back into 110 // Fetch the instrument details, will call back into
113 // PaymentRequest::OnInstrumentDetailsReady. 111 // PaymentRequest::OnInstrumentDetailsReady.
114 selected_instrument_->InvokePaymentApp(this); 112 selected_instrument_->InvokePaymentApp(this);
115 } 113 }
116 114
117 void PaymentRequestState::SetSelectedShippingOption( 115 void PaymentRequestState::SetSelectedShippingOption(
118 mojom::PaymentShippingOption* option) { 116 const std::string& shipping_option_id) {
119 selected_shipping_option_ = option; 117 // This will inform the merchant and will lead to them calling updateWith with
120 UpdateIsReadyToPayAndNotifyObservers(); 118 // new PaymentDetails.
119 delegate_->OnShippingOptionIdSelected(shipping_option_id);
121 } 120 }
122 121
123 void PaymentRequestState::SetSelectedShippingProfile( 122 void PaymentRequestState::SetSelectedShippingProfile(
124 autofill::AutofillProfile* profile) { 123 autofill::AutofillProfile* profile) {
125 selected_shipping_profile_ = profile; 124 selected_shipping_profile_ = profile;
126 UpdateIsReadyToPayAndNotifyObservers(); 125 UpdateIsReadyToPayAndNotifyObservers();
126 delegate_->OnShippingAddressSelected(
127 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile(
128 selected_shipping_profile_, app_locale_));
127 } 129 }
128 130
129 void PaymentRequestState::SetSelectedContactProfile( 131 void PaymentRequestState::SetSelectedContactProfile(
130 autofill::AutofillProfile* profile) { 132 autofill::AutofillProfile* profile) {
131 selected_contact_profile_ = profile; 133 selected_contact_profile_ = profile;
132 UpdateIsReadyToPayAndNotifyObservers(); 134 UpdateIsReadyToPayAndNotifyObservers();
133 } 135 }
134 136
135 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) { 137 void PaymentRequestState::SetSelectedInstrument(PaymentInstrument* instrument) {
136 selected_instrument_ = instrument; 138 selected_instrument_ = instrument;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 // Copy the credit cards as part of AutofillPaymentInstrument so they are 182 // Copy the credit cards as part of AutofillPaymentInstrument so they are
181 // indirectly owned by this object. 183 // indirectly owned by this object.
182 std::unique_ptr<PaymentInstrument> instrument = 184 std::unique_ptr<PaymentInstrument> instrument =
183 base::MakeUnique<AutofillPaymentInstrument>( 185 base::MakeUnique<AutofillPaymentInstrument>(
184 basic_card_network, *card, shipping_profiles_, app_locale_); 186 basic_card_network, *card, shipping_profiles_, app_locale_);
185 available_instruments_.push_back(std::move(instrument)); 187 available_instruments_.push_back(std::move(instrument));
186 } 188 }
187 } 189 }
188 190
189 void PaymentRequestState::SetDefaultProfileSelections() { 191 void PaymentRequestState::SetDefaultProfileSelections() {
190 if (!shipping_profiles().empty()) 192 // Only pre-select an address if the merchant provided at least one selected
193 // shipping option.
194 if (!shipping_profiles().empty() && spec_->selected_shipping_option())
191 selected_shipping_profile_ = shipping_profiles()[0]; 195 selected_shipping_profile_ = shipping_profiles()[0];
192 196
193 if (!contact_profiles().empty()) 197 if (!contact_profiles().empty())
194 selected_contact_profile_ = contact_profiles()[0]; 198 selected_contact_profile_ = contact_profiles()[0];
195 199
196 // TODO(crbug.com/702063): Change this code to prioritize instruments by use 200 // 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 201 // count and other means, and implement a way to modify this function's return
198 // value. 202 // value.
199 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments = 203 const std::vector<std::unique_ptr<PaymentInstrument>>& instruments =
200 available_instruments(); 204 available_instruments();
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 selected_contact_profile_ 259 selected_contact_profile_
256 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), 260 ->GetInfo(autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER),
257 app_locale_) 261 app_locale_)
258 .empty())) { 262 .empty())) {
259 return false; 263 return false;
260 } 264 }
261 265
262 return true; 266 return true;
263 } 267 }
264 268
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 269 } // 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