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

Side by Side Diff: components/autofill/core/browser/payments/payments_client.cc

Issue 2349033002: Include addresses with the getdetailsforsavecard Payments RPC. (Closed)
Patch Set: Add tests to verify whether or not address names are sent. Created 4 years, 3 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/autofill/core/browser/payments/payments_client.h" 5 #include "components/autofill/core/browser/payments/payments_client.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector>
9 10
10 #include "base/command_line.h" 11 #include "base/command_line.h"
11 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h" 19 #include "base/values.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 104
104 void AppendStringIfNotEmpty(const AutofillProfile& profile, 105 void AppendStringIfNotEmpty(const AutofillProfile& profile,
105 const ServerFieldType& type, 106 const ServerFieldType& type,
106 const std::string& app_locale, 107 const std::string& app_locale,
107 base::ListValue* list) { 108 base::ListValue* list) {
108 const base::string16 value = profile.GetInfo(AutofillType(type), app_locale); 109 const base::string16 value = profile.GetInfo(AutofillType(type), app_locale);
109 if (!value.empty()) 110 if (!value.empty())
110 list->AppendString(value); 111 list->AppendString(value);
111 } 112 }
112 113
114 // Returns a dictionary with the structure expected by Payments RPCs, containing
115 // each of the fields in |profile|, formatted according to |app_locale|. If
116 // |include_recipient_name| is false, the name in |profile| is not included.
113 std::unique_ptr<base::DictionaryValue> BuildAddressDictionary( 117 std::unique_ptr<base::DictionaryValue> BuildAddressDictionary(
114 const AutofillProfile& profile, 118 const AutofillProfile& profile,
115 const std::string& app_locale) { 119 const std::string& app_locale,
120 bool include_recipient_name) {
116 std::unique_ptr<base::DictionaryValue> postal_address( 121 std::unique_ptr<base::DictionaryValue> postal_address(
117 new base::DictionaryValue()); 122 new base::DictionaryValue());
118 123
119 SetStringIfNotEmpty(profile, NAME_FULL, app_locale, "recipient_name", 124 if (include_recipient_name) {
120 postal_address.get()); 125 SetStringIfNotEmpty(profile, NAME_FULL, app_locale,
126 PaymentsClient::kRecipientName, postal_address.get());
127 }
121 128
122 std::unique_ptr<base::ListValue> address_lines(new base::ListValue()); 129 std::unique_ptr<base::ListValue> address_lines(new base::ListValue());
123 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE1, app_locale, 130 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE1, app_locale,
124 address_lines.get()); 131 address_lines.get());
125 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE2, app_locale, 132 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE2, app_locale,
126 address_lines.get()); 133 address_lines.get());
127 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE3, app_locale, 134 AppendStringIfNotEmpty(profile, ADDRESS_HOME_LINE3, app_locale,
128 address_lines.get()); 135 address_lines.get());
129 if (!address_lines->empty()) 136 if (!address_lines->empty())
130 postal_address->Set("address_line", std::move(address_lines)); 137 postal_address->Set("address_line", std::move(address_lines));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 delegate->OnDidGetRealPan(result, real_pan_); 209 delegate->OnDidGetRealPan(result, real_pan_);
203 } 210 }
204 211
205 private: 212 private:
206 PaymentsClient::UnmaskRequestDetails request_details_; 213 PaymentsClient::UnmaskRequestDetails request_details_;
207 std::string real_pan_; 214 std::string real_pan_;
208 }; 215 };
209 216
210 class GetUploadDetailsRequest : public PaymentsRequest { 217 class GetUploadDetailsRequest : public PaymentsRequest {
211 public: 218 public:
212 GetUploadDetailsRequest(const std::string& app_locale) 219 GetUploadDetailsRequest(const std::vector<AutofillProfile>& addresses,
213 : app_locale_(app_locale) {} 220 const std::string& app_locale)
221 : addresses_(addresses), app_locale_(app_locale) {}
214 ~GetUploadDetailsRequest() override {} 222 ~GetUploadDetailsRequest() override {}
215 223
216 std::string GetRequestUrlPath() override { 224 std::string GetRequestUrlPath() override {
217 return kGetUploadDetailsRequestPath; 225 return kGetUploadDetailsRequestPath;
218 } 226 }
219 227
220 std::string GetRequestContentType() override { return "application/json"; } 228 std::string GetRequestContentType() override { return "application/json"; }
221 229
222 std::string GetRequestContent() override { 230 std::string GetRequestContent() override {
223 base::DictionaryValue request_dict; 231 base::DictionaryValue request_dict;
224 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue()); 232 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue());
225 context->SetString("language_code", app_locale_); 233 context->SetString("language_code", app_locale_);
226 request_dict.Set("context", std::move(context)); 234 request_dict.Set("context", std::move(context));
227 235
236 std::unique_ptr<base::ListValue> addresses(new base::ListValue());
237 for (const AutofillProfile& profile : addresses_) {
238 addresses->Append(BuildAddressDictionary(profile, app_locale_, false));
239 }
240 request_dict.Set("address", std::move(addresses));
241
228 std::string request_content; 242 std::string request_content;
229 base::JSONWriter::Write(request_dict, &request_content); 243 base::JSONWriter::Write(request_dict, &request_content);
230 VLOG(3) << "getdetailsforsavecard request body: " << request_content; 244 VLOG(3) << "getdetailsforsavecard request body: " << request_content;
231 return request_content; 245 return request_content;
232 } 246 }
233 247
234 void ParseResponse(std::unique_ptr<base::DictionaryValue> response) override { 248 void ParseResponse(std::unique_ptr<base::DictionaryValue> response) override {
235 response->GetString("context_token", &context_token_); 249 response->GetString("context_token", &context_token_);
236 base::DictionaryValue* unowned_legal_message; 250 base::DictionaryValue* unowned_legal_message;
237 if (response->GetDictionary("legal_message", &unowned_legal_message)) 251 if (response->GetDictionary("legal_message", &unowned_legal_message))
238 legal_message_ = unowned_legal_message->CreateDeepCopy(); 252 legal_message_ = unowned_legal_message->CreateDeepCopy();
239 } 253 }
240 254
241 bool IsResponseComplete() override { 255 bool IsResponseComplete() override {
242 return !context_token_.empty() && legal_message_; 256 return !context_token_.empty() && legal_message_;
243 } 257 }
244 258
245 void RespondToDelegate(PaymentsClientDelegate* delegate, 259 void RespondToDelegate(PaymentsClientDelegate* delegate,
246 AutofillClient::PaymentsRpcResult result) override { 260 AutofillClient::PaymentsRpcResult result) override {
247 delegate->OnDidGetUploadDetails(result, context_token_, 261 delegate->OnDidGetUploadDetails(result, context_token_,
248 std::move(legal_message_)); 262 std::move(legal_message_));
249 } 263 }
250 264
251 private: 265 private:
266 std::vector<AutofillProfile> addresses_;
252 std::string app_locale_; 267 std::string app_locale_;
253 base::string16 context_token_; 268 base::string16 context_token_;
254 std::unique_ptr<base::DictionaryValue> legal_message_; 269 std::unique_ptr<base::DictionaryValue> legal_message_;
255 }; 270 };
256 271
257 class UploadCardRequest : public PaymentsRequest { 272 class UploadCardRequest : public PaymentsRequest {
258 public: 273 public:
259 UploadCardRequest(const PaymentsClient::UploadRequestDetails& request_details) 274 UploadCardRequest(const PaymentsClient::UploadRequestDetails& request_details)
260 : request_details_(request_details) {} 275 : request_details_(request_details) {}
261 ~UploadCardRequest() override {} 276 ~UploadCardRequest() override {}
(...skipping 14 matching lines...) Expand all
276 const std::string& app_locale = request_details_.app_locale; 291 const std::string& app_locale = request_details_.app_locale;
277 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue()); 292 std::unique_ptr<base::DictionaryValue> context(new base::DictionaryValue());
278 context->SetString("language_code", app_locale); 293 context->SetString("language_code", app_locale);
279 request_dict.Set("context", std::move(context)); 294 request_dict.Set("context", std::move(context));
280 295
281 SetStringIfNotEmpty(request_details_.card, CREDIT_CARD_NAME_FULL, 296 SetStringIfNotEmpty(request_details_.card, CREDIT_CARD_NAME_FULL,
282 app_locale, "cardholder_name", &request_dict); 297 app_locale, "cardholder_name", &request_dict);
283 298
284 std::unique_ptr<base::ListValue> addresses(new base::ListValue()); 299 std::unique_ptr<base::ListValue> addresses(new base::ListValue());
285 for (const AutofillProfile& profile : request_details_.profiles) { 300 for (const AutofillProfile& profile : request_details_.profiles) {
286 addresses->Append(BuildAddressDictionary(profile, app_locale)); 301 addresses->Append(BuildAddressDictionary(profile, app_locale, true));
287 } 302 }
288 request_dict.Set("address", std::move(addresses)); 303 request_dict.Set("address", std::move(addresses));
289 304
290 request_dict.SetString("context_token", request_details_.context_token); 305 request_dict.SetString("context_token", request_details_.context_token);
291 306
292 int value = 0; 307 int value = 0;
293 const base::string16 exp_month = request_details_.card.GetInfo( 308 const base::string16 exp_month = request_details_.card.GetInfo(
294 AutofillType(CREDIT_CARD_EXP_MONTH), app_locale); 309 AutofillType(CREDIT_CARD_EXP_MONTH), app_locale);
295 const base::string16 exp_year = request_details_.card.GetInfo( 310 const base::string16 exp_year = request_details_.card.GetInfo(
296 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), app_locale); 311 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), app_locale);
(...skipping 26 matching lines...) Expand all
323 AutofillClient::PaymentsRpcResult result) override { 338 AutofillClient::PaymentsRpcResult result) override {
324 delegate->OnDidUploadCard(result); 339 delegate->OnDidUploadCard(result);
325 } 340 }
326 341
327 private: 342 private:
328 PaymentsClient::UploadRequestDetails request_details_; 343 PaymentsClient::UploadRequestDetails request_details_;
329 }; 344 };
330 345
331 } // namespace 346 } // namespace
332 347
348 const std::string PaymentsClient::kRecipientName = "recipient_name";
349
333 PaymentsClient::UnmaskRequestDetails::UnmaskRequestDetails() {} 350 PaymentsClient::UnmaskRequestDetails::UnmaskRequestDetails() {}
334 PaymentsClient::UnmaskRequestDetails::~UnmaskRequestDetails() {} 351 PaymentsClient::UnmaskRequestDetails::~UnmaskRequestDetails() {}
335 352
336 PaymentsClient::UploadRequestDetails::UploadRequestDetails() {} 353 PaymentsClient::UploadRequestDetails::UploadRequestDetails() {}
337 PaymentsClient::UploadRequestDetails::UploadRequestDetails( 354 PaymentsClient::UploadRequestDetails::UploadRequestDetails(
338 const UploadRequestDetails& other) = default; 355 const UploadRequestDetails& other) = default;
339 PaymentsClient::UploadRequestDetails::~UploadRequestDetails() {} 356 PaymentsClient::UploadRequestDetails::~UploadRequestDetails() {}
340 357
341 PaymentsClient::PaymentsClient(net::URLRequestContextGetter* context_getter, 358 PaymentsClient::PaymentsClient(net::URLRequestContextGetter* context_getter,
342 PaymentsClientDelegate* delegate) 359 PaymentsClientDelegate* delegate)
(...skipping 10 matching lines...) Expand all
353 void PaymentsClient::Prepare() { 370 void PaymentsClient::Prepare() {
354 if (access_token_.empty()) 371 if (access_token_.empty())
355 StartTokenFetch(false); 372 StartTokenFetch(false);
356 } 373 }
357 374
358 void PaymentsClient::UnmaskCard( 375 void PaymentsClient::UnmaskCard(
359 const PaymentsClient::UnmaskRequestDetails& request_details) { 376 const PaymentsClient::UnmaskRequestDetails& request_details) {
360 IssueRequest(base::MakeUnique<UnmaskCardRequest>(request_details), true); 377 IssueRequest(base::MakeUnique<UnmaskCardRequest>(request_details), true);
361 } 378 }
362 379
363 void PaymentsClient::GetUploadDetails(const std::string& app_locale) { 380 void PaymentsClient::GetUploadDetails(
364 IssueRequest(base::MakeUnique<GetUploadDetailsRequest>(app_locale), false); 381 const std::vector<AutofillProfile>& addresses,
382 const std::string& app_locale) {
383 IssueRequest(base::MakeUnique<GetUploadDetailsRequest>(addresses, app_locale),
384 false);
365 } 385 }
366 386
367 void PaymentsClient::UploadCard( 387 void PaymentsClient::UploadCard(
368 const PaymentsClient::UploadRequestDetails& request_details) { 388 const PaymentsClient::UploadRequestDetails& request_details) {
369 IssueRequest(base::MakeUnique<UploadCardRequest>(request_details), true); 389 IssueRequest(base::MakeUnique<UploadCardRequest>(request_details), true);
370 } 390 }
371 391
372 void PaymentsClient::IssueRequest(std::unique_ptr<PaymentsRequest> request, 392 void PaymentsClient::IssueRequest(std::unique_ptr<PaymentsRequest> request,
373 bool authenticate) { 393 bool authenticate) {
374 request_ = std::move(request); 394 request_ = std::move(request);
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 540
521 void PaymentsClient::SetOAuth2TokenAndStartRequest() { 541 void PaymentsClient::SetOAuth2TokenAndStartRequest() {
522 url_fetcher_->AddExtraRequestHeader(net::HttpRequestHeaders::kAuthorization + 542 url_fetcher_->AddExtraRequestHeader(net::HttpRequestHeaders::kAuthorization +
523 std::string(": Bearer ") + access_token_); 543 std::string(": Bearer ") + access_token_);
524 544
525 url_fetcher_->Start(); 545 url_fetcher_->Start();
526 } 546 }
527 547
528 } // namespace payments 548 } // namespace payments
529 } // namespace autofill 549 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698