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

Side by Side Diff: components/payments/core/payment_request_data_util.cc

Issue 2800713003: [Payments] Move parsing the PaymentMethodData to "core" (Closed)
Patch Set: fixed TODO 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/core/payment_request_data_util.h" 5 #include "components/payments/core/payment_request_data_util.h"
6 6
7 #include "base/strings/string16.h" 7 #include "base/strings/string16.h"
8 #include "base/strings/string_split.h" 8 #include "base/strings/string_split.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/autofill_profile.h" 10 #include "components/autofill/core/browser/autofill_profile.h"
11 #include "components/autofill/core/browser/credit_card.h" 11 #include "components/autofill/core/browser/credit_card.h"
12 #include "components/autofill/core/browser/field_types.h" 12 #include "components/autofill/core/browser/field_types.h"
13 #include "components/autofill/core/browser/personal_data_manager.h" 13 #include "components/autofill/core/browser/personal_data_manager.h"
14 #include "components/payments/core/basic_card_response.h" 14 #include "components/payments/core/basic_card_response.h"
15 #include "components/payments/core/payment_address.h" 15 #include "components/payments/core/payment_address.h"
16 #include "components/payments/core/payment_method_data.h"
16 17
17 namespace payments { 18 namespace payments {
18 namespace data_util { 19 namespace data_util {
19 20
21 namespace {
22
23 const char kBasicCardMethodName[] = "basic-card";
please use gerrit instead 2017/04/05 19:29:25 Nit: place immediately before where this is used,
Mathieu 2017/04/05 20:28:33 Done.
24
25 } // namespace
26
20 PaymentAddress GetPaymentAddressFromAutofillProfile( 27 PaymentAddress GetPaymentAddressFromAutofillProfile(
21 const autofill::AutofillProfile& profile, 28 const autofill::AutofillProfile& profile,
22 const std::string& app_locale) { 29 const std::string& app_locale) {
23 PaymentAddress address; 30 PaymentAddress address;
24 address.country = profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY); 31 address.country = profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY);
25 address.address_line = base::SplitString( 32 address.address_line = base::SplitString(
26 profile.GetInfo( 33 profile.GetInfo(
27 autofill::AutofillType(autofill::ADDRESS_HOME_STREET_ADDRESS), 34 autofill::AutofillType(autofill::ADDRESS_HOME_STREET_ADDRESS),
28 app_locale), 35 app_locale),
29 base::ASCIIToUTF16("\n"), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 36 base::ASCIIToUTF16("\n"), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( 70 autofill::PersonalDataManager::GetProfileFromProfilesByGUID(
64 card.billing_address_id(), billing_profiles); 71 card.billing_address_id(), billing_profiles);
65 DCHECK(billing_address); 72 DCHECK(billing_address);
66 response.billing_address = 73 response.billing_address =
67 GetPaymentAddressFromAutofillProfile(*billing_address, app_locale); 74 GetPaymentAddressFromAutofillProfile(*billing_address, app_locale);
68 } 75 }
69 76
70 return response; 77 return response;
71 } 78 }
72 79
80 bool ParseBasicCardSupportedNetworks(
81 const std::vector<PaymentMethodData>& method_data,
82 std::vector<std::string>* out_supported_networks,
83 std::set<std::string>* out_basic_card_specified_networks) {
84 DCHECK(out_supported_networks->empty());
85 DCHECK(out_basic_card_specified_networks->empty());
86
87 std::set<std::string> card_networks{"amex", "diners", "discover",
88 "jcb", "mastercard", "mir",
89 "unionpay", "visa"};
90 for (const PaymentMethodData& method_data_entry : method_data) {
91 if (method_data_entry.supported_methods.empty())
92 return false;
93
94 for (const std::string& method : method_data_entry.supported_methods) {
95 if (method.empty())
96 continue;
97
98 // If a card network is specified right in "supportedMethods", add it.
99 auto card_it = card_networks.find(method);
100 if (card_it != card_networks.end()) {
101 out_supported_networks->push_back(method);
102 // |method| removed from |card_networks| so that it is not doubly added
103 // to |supported_card_networks_| if "basic-card" is specified with no
104 // supported networks.
105 card_networks.erase(card_it);
106 } else if (method == kBasicCardMethodName) {
107 // For the "basic-card" method, check "supportedNetworks".
108 if (method_data_entry.supported_networks.empty()) {
109 // Empty |supported_networks| means all networks are supported.
110 out_supported_networks->insert(out_supported_networks->end(),
111 card_networks.begin(),
112 card_networks.end());
113 out_basic_card_specified_networks->insert(card_networks.begin(),
114 card_networks.end());
115 // Clear the set so that no further networks are added to
116 // |out_supported_networks|.
117 card_networks.clear();
118 } else {
119 // The merchant has specified a few basic card supported networks. Use
120 // the mapping to transform to known basic-card types.
121 for (const std::string& supported_network :
122 method_data_entry.supported_networks) {
123 // Make sure that the network was not already added to
124 // |out_supported_networks|. If it's still in |card_networks| it's
125 // fair game.
126 auto it = card_networks.find(supported_network);
127 if (it != card_networks.end()) {
128 out_supported_networks->push_back(supported_network);
129 out_basic_card_specified_networks->insert(supported_network);
130 card_networks.erase(it);
131 } else {
132 // It's invalid to specify an unsupported card network identifier.
133 return false;
134 }
135 }
136 }
137 }
138 }
139 }
140 return true;
141 }
142
73 } // namespace data_util 143 } // namespace data_util
74 } // namespace payments 144 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698