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