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