wallet_items.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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.
4
5#ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
6#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
7
8#include <string>
9#include <vector>
10
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"
19#include "url/gurl.h"
20
21namespace base {
22class DictionaryValue;
23}
24
25namespace gfx {
26class Image;
27}
28
29namespace autofill {
30
31class AutofillType;
32
33FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest, GetInfoCreditCardExpMonth);
34FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest,
35                     GetDisplayTextEmptyWhenExpired);
36
37namespace wallet {
38
39class WalletItemsTest;
40
41enum AmexPermission {
42  AMEX_ALLOWED,
43  AMEX_DISALLOWED,
44};
45
46// WalletItems is a collection of cards and addresses that a user picks from to
47// construct a full wallet. However, it also provides a transaction ID which
48// must be used throughout all API calls being made using this data.
49// Additionally, user actions may be required before a purchase can be completed
50// using Online Wallet and those actions are present in the object as well.
51class WalletItems {
52 public:
53  // Container for all information about a credit card except for it's card
54  // verfication number (CVN) and it's complete primary account number (PAN).
55  class MaskedInstrument {
56   public:
57    enum Type {
58      AMEX,
59      DISCOVER,
60      MAESTRO,
61      MASTER_CARD,
62      SOLO,
63      SWITCH,
64      UNKNOWN,  // Catch all type.
65      VISA,
66    };
67    enum Status {
68      AMEX_NOT_SUPPORTED,
69      BILLING_INCOMPLETE,
70      DECLINED,
71      DISABLED_FOR_THIS_MERCHANT,  // Deprecated.
72      EXPIRED,
73      INAPPLICABLE,  // Catch all status.
74      PENDING,
75      UNSUPPORTED_COUNTRY,
76      VALID,
77    };
78
79    ~MaskedInstrument();
80
81    // Returns an empty scoped_ptr if input is invalid or a valid masked
82    // instrument.
83    static scoped_ptr<MaskedInstrument>
84        CreateMaskedInstrument(const base::DictionaryValue& dictionary);
85
86    bool operator==(const MaskedInstrument& other) const;
87    bool operator!=(const MaskedInstrument& other) const;
88
89    // Gets an image to display for this instrument.
90    const gfx::Image& CardIcon() const;
91
92    // Returns a pair of strings that summarizes this CC,
93    // suitable for display to the user.
94    base::string16 DisplayName() const;
95    base::string16 DisplayNameDetail() const;
96
97    // Gets info that corresponds with |type|.
98    base::string16 GetInfo(const AutofillType& type,
99                           const std::string& app_locale) const;
100
101    // Returns the display type of the and last four digits (e.g. Visa - 4444).
102    base::string16 TypeAndLastFourDigits() const;
103
104    const base::string16& descriptive_name() const { return descriptive_name_; }
105    const Type& type() const { return type_; }
106    const std::vector<base::string16>& supported_currencies() const {
107      return supported_currencies_;
108    }
109    const base::string16& last_four_digits() const { return last_four_digits_; }
110    int expiration_month() const { return expiration_month_; }
111    int expiration_year() const { return expiration_year_; }
112    const Address& address() const { return *address_; }
113    const Status& status() const { return status_; }
114    const std::string& object_id() const { return object_id_; }
115
116   private:
117    friend class WalletItemsTest;
118    friend scoped_ptr<MaskedInstrument> GetTestMaskedInstrumentWithDetails(
119        const std::string&, scoped_ptr<Address> address,
120        Type type, Status status);
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);
127
128    MaskedInstrument(const base::string16& descriptive_name,
129                     const Type& type,
130                     const std::vector<base::string16>& supported_currencies,
131                     const base::string16& last_four_digits,
132                     int expiration_month,
133                     int expiration_year,
134                     scoped_ptr<Address> address,
135                     const Status& status,
136                     const std::string& object_id);
137
138    // A user-provided description of the instrument. For example, "Google Visa
139    // Card".
140    base::string16 descriptive_name_;
141
142    // The payment network of the instrument. For example, Visa.
143    Type type_;
144
145    // |supported_currencies_| are ISO 4217 currency codes, e.g. USD.
146    std::vector<base::string16> supported_currencies_;
147
148    // The last four digits of the primary account number of the instrument.
149    base::string16 last_four_digits_;
150
151    // |expiration month_| should be 1-12.
152    int expiration_month_;
153
154    // |expiration_year_| should be a 4-digit year.
155    int expiration_year_;
156
157    // The billing address for the instrument.
158    scoped_ptr<Address> address_;
159
160    // The current status of the instrument. For example, expired or declined.
161    Status status_;
162
163    // Externalized Online Wallet id for this instrument.
164    std::string object_id_;
165
166    DISALLOW_COPY_AND_ASSIGN(MaskedInstrument);
167  };
168
169  // Class representing a legal document that the user must accept before they
170  // can use Online Wallet.
171  class LegalDocument {
172   public:
173    ~LegalDocument();
174
175    // Returns null if input is invalid or a valid legal document.
176    static scoped_ptr<LegalDocument>
177        CreateLegalDocument(const base::DictionaryValue& dictionary);
178
179    // Returns a document for the privacy policy (acceptance of which is not
180    // tracked by the server).
181    static scoped_ptr<LegalDocument> CreatePrivacyPolicyDocument();
182
183    bool operator==(const LegalDocument& other) const;
184    bool operator!=(const LegalDocument& other) const;
185
186    const std::string& id() { return id_; }
187    const GURL& url() const { return url_; }
188    const base::string16& display_name() const { return display_name_; }
189
190   private:
191    friend class WalletItemsTest;
192    FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateLegalDocument);
193    FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
194    FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentUrl);
195    FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentEmptyId);
196    LegalDocument(const std::string& id,
197                  const base::string16& display_name);
198    LegalDocument(const GURL& url,
199                  const base::string16& display_name);
200
201    // Externalized Online Wallet id for the document, or an empty string for
202    // documents not tracked by the server (such as the privacy policy).
203    std::string id_;
204    // The human-visitable URL that displays the document.
205    GURL url_;
206    // User displayable name for the document.
207    base::string16 display_name_;
208    DISALLOW_COPY_AND_ASSIGN(LegalDocument);
209  };
210
211  ~WalletItems();
212
213  // Returns null on invalid input, an empty wallet items with required
214  // actions if any are present, and a populated wallet items otherwise. Caller
215  // owns returned pointer.
216  static scoped_ptr<WalletItems>
217      CreateWalletItems(const base::DictionaryValue& dictionary);
218
219  bool operator==(const WalletItems& other) const;
220  bool operator!=(const WalletItems& other) const;
221
222  void AddInstrument(scoped_ptr<MaskedInstrument> instrument) {
223    DCHECK(instrument.get());
224    instruments_.push_back(instrument.release());
225  }
226  void AddAddress(scoped_ptr<Address> address) {
227    DCHECK(address.get());
228    addresses_.push_back(address.release());
229  }
230  void AddLegalDocument(scoped_ptr<LegalDocument> legal_document) {
231    DCHECK(legal_document.get());
232    legal_documents_.push_back(legal_document.release());
233  }
234
235  // Return the corresponding instrument for |id| or NULL if it doesn't exist.
236  const WalletItems::MaskedInstrument* GetInstrumentById(
237      const std::string& object_id) const;
238
239  // Whether or not |action| is in |required_actions_|.
240  bool HasRequiredAction(RequiredAction action) const;
241
242  // Checks whether |card_number| is supported by Wallet for this merchant and
243  // if not, fills in |message| with a user-visible explanation.
244  bool SupportsCard(const base::string16& card_number,
245                    base::string16* message) const;
246
247  const std::vector<RequiredAction>& required_actions() const {
248    return required_actions_;
249  }
250  const std::string& google_transaction_id() const {
251    return google_transaction_id_;
252  }
253  const std::vector<MaskedInstrument*>& instruments() const {
254    return instruments_.get();
255  }
256  const std::string& default_instrument_id() const {
257    return default_instrument_id_;
258  }
259  const std::vector<Address*>& addresses() const { return addresses_.get(); }
260  const std::string& default_address_id() const { return default_address_id_; }
261  const std::string& obfuscated_gaia_id() const { return obfuscated_gaia_id_; }
262  const std::vector<LegalDocument*>& legal_documents() const {
263    return legal_documents_.get();
264  }
265
266 private:
267  friend class WalletItemsTest;
268  friend scoped_ptr<WalletItems> GetTestWalletItemsWithDefaultIds(
269      const std::string&, const std::string&, AmexPermission);
270  FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
271  FRIEND_TEST_ALL_PREFIXES(WalletItemsTest,
272                           CreateWalletItemsWithRequiredActions);
273
274  WalletItems(const std::vector<RequiredAction>& required_actions,
275              const std::string& google_transaction_id,
276              const std::string& default_instrument_id,
277              const std::string& default_address_id,
278              const std::string& obfuscated_gaia_id,
279              AmexPermission amex_permission);
280
281  // Actions that must be completed by the user before a FullWallet can be
282  // issued to them by the Online Wallet service.
283  std::vector<RequiredAction> required_actions_;
284
285  // The id for this transaction issued by Google.
286  std::string google_transaction_id_;
287
288  // The id of the user's default instrument.
289  std::string default_instrument_id_;
290
291  // The id of the user's default address.
292  std::string default_address_id_;
293
294  // The externalized Gaia id of the user.
295  std::string obfuscated_gaia_id_;
296
297  // The user's backing instruments.
298  ScopedVector<MaskedInstrument> instruments_;
299
300  // The user's shipping addresses.
301  ScopedVector<Address> addresses_;
302
303  // Legal documents the user must accept before using Online Wallet.
304  ScopedVector<LegalDocument> legal_documents_;
305
306  // Whether Google Wallet allows American Express card for this merchant.
307  AmexPermission amex_permission_;
308
309  DISALLOW_COPY_AND_ASSIGN(WalletItems);
310};
311
312}  // namespace wallet
313}  // namespace autofill
314
315#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
316