1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/strings/string16.h"
17 #include "components/autofill/content/browser/wallet/required_action.h"
18 #include "components/autofill/content/browser/wallet/wallet_address.h"
22 class DictionaryValue;
33 FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest, GetInfoCreditCardExpMonth);
34 FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest,
35 GetDisplayTextEmptyWhenExpired);
40 class WalletItemsTest;
47 // WalletItems is a collection of cards and addresses that a user picks from to
48 // construct a full wallet. However, it also provides a transaction ID which
49 // must be used throughout all API calls being made using this data.
50 // Additionally, user actions may be required before a purchase can be completed
51 // using Online Wallet and those actions are present in the object as well.
54 // Container for all information about a credit card except for it's card
55 // verfication number (CVN) and it's complete primary account number (PAN).
56 class MaskedInstrument {
65 UNKNOWN, // Catch all type.
72 DISABLED_FOR_THIS_MERCHANT, // Deprecated.
74 INAPPLICABLE, // Catch all status.
82 // Returns an empty scoped_ptr if input is invalid or a valid masked
84 static scoped_ptr<MaskedInstrument>
85 CreateMaskedInstrument(const base::DictionaryValue& dictionary);
87 bool operator==(const MaskedInstrument& other) const;
88 bool operator!=(const MaskedInstrument& other) const;
90 // Gets an image to display for this instrument.
91 const gfx::Image& CardIcon() const;
93 // Returns a pair of strings that summarizes this CC,
94 // suitable for display to the user.
95 base::string16 DisplayName() const;
96 base::string16 DisplayNameDetail() const;
98 // Gets info that corresponds with |type|.
99 base::string16 GetInfo(const AutofillType& type,
100 const std::string& app_locale) const;
102 // Returns the display type of the and last four digits (e.g. Visa - 4444).
103 base::string16 TypeAndLastFourDigits() const;
105 const base::string16& descriptive_name() const { return descriptive_name_; }
106 const Type& type() const { return type_; }
107 const base::string16& last_four_digits() const { return last_four_digits_; }
108 int expiration_month() const { return expiration_month_; }
109 int expiration_year() const { return expiration_year_; }
110 const Address& address() const { return *address_; }
111 const Status& status() const { return status_; }
112 const std::string& object_id() const { return object_id_; }
115 friend class WalletItemsTest;
116 friend scoped_ptr<MaskedInstrument> GetTestMaskedInstrumentWithDetails(
117 const std::string& id,
118 scoped_ptr<Address> address,
121 FRIEND_TEST_ALL_PREFIXES(::autofill::WalletInstrumentWrapperTest,
122 GetInfoCreditCardExpMonth);
123 FRIEND_TEST_ALL_PREFIXES(::autofill::WalletInstrumentWrapperTest,
124 GetDisplayTextEmptyWhenExpired);
125 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateMaskedInstrument);
126 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
128 MaskedInstrument(const base::string16& descriptive_name,
130 const base::string16& last_four_digits,
131 int expiration_month,
133 scoped_ptr<Address> address,
134 const Status& status,
135 const std::string& object_id);
137 // A user-provided description of the instrument. For example, "Google Visa
139 base::string16 descriptive_name_;
141 // The payment network of the instrument. For example, Visa.
144 // The last four digits of the primary account number of the instrument.
145 base::string16 last_four_digits_;
147 // |expiration month_| should be 1-12.
148 int expiration_month_;
150 // |expiration_year_| should be a 4-digit year.
151 int expiration_year_;
153 // The billing address for the instrument.
154 scoped_ptr<Address> address_;
156 // The current status of the instrument. For example, expired or declined.
159 // Externalized Online Wallet id for this instrument.
160 std::string object_id_;
162 DISALLOW_COPY_AND_ASSIGN(MaskedInstrument);
165 // Class representing a legal document that the user must accept before they
166 // can use Online Wallet.
167 class LegalDocument {
171 // Returns null if input is invalid or a valid legal document.
172 static scoped_ptr<LegalDocument>
173 CreateLegalDocument(const base::DictionaryValue& dictionary);
175 // Returns a document for the privacy policy (acceptance of which is not
176 // tracked by the server).
177 static scoped_ptr<LegalDocument> CreatePrivacyPolicyDocument();
179 bool operator==(const LegalDocument& other) const;
180 bool operator!=(const LegalDocument& other) const;
182 const std::string& id() { return id_; }
183 const GURL& url() const { return url_; }
184 const base::string16& display_name() const { return display_name_; }
187 friend class WalletItemsTest;
188 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateLegalDocument);
189 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
190 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentUrl);
191 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentEmptyId);
192 LegalDocument(const std::string& id,
193 const base::string16& display_name);
194 LegalDocument(const GURL& url,
195 const base::string16& display_name);
197 // Externalized Online Wallet id for the document, or an empty string for
198 // documents not tracked by the server (such as the privacy policy).
200 // The human-visitable URL that displays the document.
202 // User displayable name for the document.
203 base::string16 display_name_;
204 DISALLOW_COPY_AND_ASSIGN(LegalDocument);
209 // Returns null on invalid input, an empty wallet items with required
210 // actions if any are present, and a populated wallet items otherwise. Caller
211 // owns returned pointer.
212 static scoped_ptr<WalletItems>
213 CreateWalletItems(const base::DictionaryValue& dictionary);
215 bool operator==(const WalletItems& other) const;
216 bool operator!=(const WalletItems& other) const;
218 void AddAccount(scoped_ptr<GaiaAccount> account);
219 void AddInstrument(scoped_ptr<MaskedInstrument> instrument) {
221 instruments_.push_back(instrument.release());
223 void AddAddress(scoped_ptr<Address> address) {
225 addresses_.push_back(address.release());
227 void AddLegalDocument(scoped_ptr<LegalDocument> legal_document) {
228 DCHECK(legal_document);
229 legal_documents_.push_back(legal_document.release());
232 // Return the corresponding instrument for |id| or NULL if it doesn't exist.
233 const WalletItems::MaskedInstrument* GetInstrumentById(
234 const std::string& object_id) const;
236 // Whether or not |action| is in |required_actions_|.
237 bool HasRequiredAction(RequiredAction action) const;
239 // Checks whether |card_number| is supported by Wallet for this merchant and
240 // if not, fills in |message| with a user-visible explanation.
241 bool SupportsCard(const base::string16& card_number,
242 base::string16* message) const;
244 const std::vector<GaiaAccount*>& gaia_accounts() const {
245 return gaia_accounts_.get();
247 const std::vector<RequiredAction>& required_actions() const {
248 return required_actions_;
250 const std::string& google_transaction_id() const {
251 return google_transaction_id_;
253 const std::vector<MaskedInstrument*>& instruments() const {
254 return instruments_.get();
256 const std::string& default_instrument_id() const {
257 return default_instrument_id_;
259 const std::vector<Address*>& addresses() const { return addresses_.get(); }
260 const std::string& default_address_id() const { return default_address_id_; }
261 // Returns the GAIA id of the active account, or an empty string if no account
263 std::string ObfuscatedGaiaId() const;
264 size_t active_account_index() const { return active_account_index_; }
265 const std::vector<LegalDocument*>& legal_documents() const {
266 return legal_documents_.get();
270 friend class WalletItemsTest;
271 friend scoped_ptr<WalletItems> GetTestWalletItemsWithDetails(
272 const std::vector<RequiredAction>& required_actions,
273 const std::string& default_instrument_id,
274 const std::string& default_address_id,
275 AmexPermission amex_permission);
276 friend scoped_ptr<WalletItems> GetTestWalletItemsWithDefaultIds(
277 RequiredAction action);
278 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
279 FRIEND_TEST_ALL_PREFIXES(WalletItemsTest,
280 CreateWalletItemsWithRequiredActions);
282 WalletItems(const std::vector<RequiredAction>& required_actions,
283 const std::string& google_transaction_id,
284 const std::string& default_instrument_id,
285 const std::string& default_address_id,
286 AmexPermission amex_permission);
288 // Actions that must be completed by the user before a FullWallet can be
289 // issued to them by the Online Wallet service.
290 std::vector<RequiredAction> required_actions_;
292 // The id for this transaction issued by Google.
293 std::string google_transaction_id_;
295 // The id of the user's default instrument.
296 std::string default_instrument_id_;
298 // The id of the user's default address.
299 std::string default_address_id_;
301 // The index into |gaia_accounts_| of the account for which this object
303 size_t active_account_index_;
305 // The complete set of logged in GAIA accounts.
306 ScopedVector<GaiaAccount> gaia_accounts_;
308 // The user's backing instruments.
309 ScopedVector<MaskedInstrument> instruments_;
311 // The user's shipping addresses.
312 ScopedVector<Address> addresses_;
314 // Legal documents the user must accept before using Online Wallet.
315 ScopedVector<LegalDocument> legal_documents_;
317 // Whether Google Wallet allows American Express card for this merchant.
318 AmexPermission amex_permission_;
320 DISALLOW_COPY_AND_ASSIGN(WalletItems);
323 } // namespace wallet
324 } // namespace autofill
326 #endif // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_