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 <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/gtest_prod_util.h"
     14 #include "base/logging.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/scoped_vector.h"
     17 #include "base/strings/string16.h"
     18 #include "components/autofill/content/browser/wallet/required_action.h"
     19 #include "components/autofill/content/browser/wallet/wallet_address.h"
     20 #include "url/gurl.h"
     21 
     22 namespace base {
     23 class DictionaryValue;
     24 }
     25 
     26 namespace gfx {
     27 class Image;
     28 }
     29 
     30 namespace autofill {
     31 
     32 class AutofillType;
     33 
     34 FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest, GetInfoCreditCardExpMonth);
     35 FORWARD_DECLARE_TEST(WalletInstrumentWrapperTest,
     36                      GetDisplayTextEmptyWhenExpired);
     37 
     38 namespace wallet {
     39 
     40 class GaiaAccount;
     41 class WalletItemsTest;
     42 
     43 enum AmexPermission {
     44   AMEX_ALLOWED,
     45   AMEX_DISALLOWED,
     46 };
     47 
     48 // WalletItems is a collection of cards and addresses that a user picks from to
     49 // construct a full wallet. However, it also provides a transaction ID which
     50 // must be used throughout all API calls being made using this data.
     51 // Additionally, user actions may be required before a purchase can be completed
     52 // using Online Wallet and those actions are present in the object as well.
     53 class WalletItems {
     54  public:
     55   // Container for all information about a credit card except for it's card
     56   // verfication number (CVN) and it's complete primary account number (PAN).
     57   class MaskedInstrument {
     58    public:
     59     enum Type {
     60       AMEX,
     61       DISCOVER,
     62       MAESTRO,
     63       MASTER_CARD,
     64       SOLO,
     65       SWITCH,
     66       UNKNOWN,  // Catch all type.
     67       VISA,
     68     };
     69     enum Status {
     70       AMEX_NOT_SUPPORTED,
     71       BILLING_INCOMPLETE,
     72       DECLINED,
     73       DISABLED_FOR_THIS_MERCHANT,  // Deprecated.
     74       EXPIRED,
     75       INAPPLICABLE,  // Catch all status.
     76       PENDING,
     77       UNSUPPORTED_COUNTRY,
     78       VALID,
     79     };
     80 
     81     ~MaskedInstrument();
     82 
     83     // Returns an empty scoped_ptr if input is invalid or a valid masked
     84     // instrument.
     85     static scoped_ptr<MaskedInstrument>
     86         CreateMaskedInstrument(const base::DictionaryValue& dictionary);
     87 
     88     bool operator==(const MaskedInstrument& other) const;
     89     bool operator!=(const MaskedInstrument& other) const;
     90 
     91     // Gets an image to display for this instrument.
     92     const gfx::Image& CardIcon() const;
     93 
     94     // Returns a pair of strings that summarizes this CC,
     95     // suitable for display to the user.
     96     base::string16 DisplayName() const;
     97     base::string16 DisplayNameDetail() const;
     98 
     99     // Gets info that corresponds with |type|.
    100     base::string16 GetInfo(const AutofillType& type,
    101                            const std::string& app_locale) const;
    102 
    103     // Returns the display type of the and last four digits (e.g. Visa - 4444).
    104     base::string16 TypeAndLastFourDigits() const;
    105 
    106     const base::string16& descriptive_name() const { return descriptive_name_; }
    107     const Type& type() const { return type_; }
    108     const base::string16& last_four_digits() const { return last_four_digits_; }
    109     int expiration_month() const { return expiration_month_; }
    110     int expiration_year() const { return expiration_year_; }
    111     const Address& address() const { return *address_; }
    112     const Status& status() const { return status_; }
    113     const std::string& object_id() const { return object_id_; }
    114 
    115    private:
    116     friend class WalletItemsTest;
    117     friend scoped_ptr<MaskedInstrument> GetTestMaskedInstrumentWithDetails(
    118         const std::string& id,
    119         scoped_ptr<Address> address,
    120         Type type,
    121         Status status);
    122     FRIEND_TEST_ALL_PREFIXES(::autofill::WalletInstrumentWrapperTest,
    123                              GetInfoCreditCardExpMonth);
    124     FRIEND_TEST_ALL_PREFIXES(::autofill::WalletInstrumentWrapperTest,
    125                              GetDisplayTextEmptyWhenExpired);
    126     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateMaskedInstrument);
    127     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
    128 
    129     MaskedInstrument(const base::string16& descriptive_name,
    130                      const Type& type,
    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     // The last four digits of the primary account number of the instrument.
    146     base::string16 last_four_digits_;
    147 
    148     // |expiration month_| should be 1-12.
    149     int expiration_month_;
    150 
    151     // |expiration_year_| should be a 4-digit year.
    152     int expiration_year_;
    153 
    154     // The billing address for the instrument.
    155     scoped_ptr<Address> address_;
    156 
    157     // The current status of the instrument. For example, expired or declined.
    158     Status status_;
    159 
    160     // Externalized Online Wallet id for this instrument.
    161     std::string object_id_;
    162 
    163     DISALLOW_COPY_AND_ASSIGN(MaskedInstrument);
    164   };
    165 
    166   // Class representing a legal document that the user must accept before they
    167   // can use Online Wallet.
    168   class LegalDocument {
    169    public:
    170     ~LegalDocument();
    171 
    172     // Returns null if input is invalid or a valid legal document.
    173     static scoped_ptr<LegalDocument>
    174         CreateLegalDocument(const base::DictionaryValue& dictionary);
    175 
    176     // Returns a document for the privacy policy (acceptance of which is not
    177     // tracked by the server).
    178     static scoped_ptr<LegalDocument> CreatePrivacyPolicyDocument();
    179 
    180     bool operator==(const LegalDocument& other) const;
    181     bool operator!=(const LegalDocument& other) const;
    182 
    183     const std::string& id() { return id_; }
    184     const GURL& url() const { return url_; }
    185     const base::string16& display_name() const { return display_name_; }
    186 
    187    private:
    188     friend class WalletItemsTest;
    189     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateLegalDocument);
    190     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, CreateWalletItems);
    191     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentUrl);
    192     FRIEND_TEST_ALL_PREFIXES(WalletItemsTest, LegalDocumentEmptyId);
    193     LegalDocument(const std::string& id,
    194                   const base::string16& display_name);
    195     LegalDocument(const GURL& url,
    196                   const base::string16& display_name);
    197 
    198     // Externalized Online Wallet id for the document, or an empty string for
    199     // documents not tracked by the server (such as the privacy policy).
    200     std::string id_;
    201     // The human-visitable URL that displays the document.
    202     GURL url_;
    203     // User displayable name for the document.
    204     base::string16 display_name_;
    205     DISALLOW_COPY_AND_ASSIGN(LegalDocument);
    206   };
    207 
    208   ~WalletItems();
    209 
    210   // Returns null on invalid input, an empty wallet items with required
    211   // actions if any are present, and a populated wallet items otherwise. Caller
    212   // owns returned pointer.
    213   static scoped_ptr<WalletItems>
    214       CreateWalletItems(const base::DictionaryValue& dictionary);
    215 
    216   bool operator==(const WalletItems& other) const;
    217   bool operator!=(const WalletItems& other) const;
    218 
    219   void AddAccount(scoped_ptr<GaiaAccount> account);
    220   void AddInstrument(scoped_ptr<MaskedInstrument> instrument) {
    221     DCHECK(instrument);
    222     instruments_.push_back(instrument.release());
    223   }
    224   void AddAddress(scoped_ptr<Address> address) {
    225     DCHECK(address);
    226     addresses_.push_back(address.release());
    227   }
    228   void AddLegalDocument(scoped_ptr<LegalDocument> legal_document) {
    229     DCHECK(legal_document);
    230     legal_documents_.push_back(legal_document.release());
    231   }
    232   void AddAllowedShippingCountry(const std::string& country_code) {
    233     allowed_shipping_countries_.insert(country_code);
    234   }
    235 
    236   // Return the corresponding instrument for |id| or NULL if it doesn't exist.
    237   const WalletItems::MaskedInstrument* GetInstrumentById(
    238       const std::string& object_id) const;
    239 
    240   // Whether or not |action| is in |required_actions_|.
    241   bool HasRequiredAction(RequiredAction action) const;
    242 
    243   // Checks whether |card_number| is supported by Wallet for this merchant and
    244   // if not, fills in |message| with a user-visible explanation.
    245   bool SupportsCard(const base::string16& card_number,
    246                     base::string16* message) const;
    247 
    248   const std::vector<GaiaAccount*>& gaia_accounts() const {
    249     return gaia_accounts_.get();
    250   }
    251   const std::vector<RequiredAction>& required_actions() const {
    252     return required_actions_;
    253   }
    254   const std::string& google_transaction_id() const {
    255     return google_transaction_id_;
    256   }
    257   const std::vector<MaskedInstrument*>& instruments() const {
    258     return instruments_.get();
    259   }
    260   const std::string& default_instrument_id() const {
    261     return default_instrument_id_;
    262   }
    263   const std::vector<Address*>& addresses() const { return addresses_.get(); }
    264   const std::string& default_address_id() const { return default_address_id_; }
    265   // Returns the GAIA id of the active account, or an empty string if no account
    266   // is active.
    267   std::string ObfuscatedGaiaId() const;
    268   size_t active_account_index() const { return active_account_index_; }
    269   const std::vector<LegalDocument*>& legal_documents() const {
    270     return legal_documents_.get();
    271   }
    272   const std::set<std::string>& allowed_shipping_countries() const {
    273     return allowed_shipping_countries_;
    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   // Country codes for allowed Wallet shipping destinations.
    325   std::set<std::string> allowed_shipping_countries_;
    326 
    327   // Whether Google Wallet allows American Express card for this merchant.
    328   AmexPermission amex_permission_;
    329 
    330   DISALLOW_COPY_AND_ASSIGN(WalletItems);
    331 };
    332 
    333 }  // namespace wallet
    334 }  // namespace autofill
    335 
    336 #endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ITEMS_H_
    337