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_CORE_BROWSER_CREDIT_CARD_H_ 6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_ 7 8 #include <iosfwd> 9 #include <string> 10 #include <vector> 11 12 #include "base/compiler_specific.h" 13 #include "base/strings/string16.h" 14 #include "components/autofill/core/browser/autofill_data_model.h" 15 16 namespace autofill { 17 18 struct FormFieldData; 19 20 // A form group that stores credit card information. 21 class CreditCard : public AutofillDataModel { 22 public: 23 CreditCard(const std::string& guid, const std::string& origin); 24 25 // For use in STL containers. 26 CreditCard(); 27 CreditCard(const CreditCard& credit_card); 28 virtual ~CreditCard(); 29 30 // Returns a version of |number| that has any separator characters removed. 31 static const base::string16 StripSeparators(const base::string16& number); 32 33 // The user-visible type of the card, e.g. 'Mastercard'. 34 static base::string16 TypeForDisplay(const std::string& type); 35 36 // The ResourceBundle ID for the appropriate credit card image. 37 static int IconResourceId(const std::string& type); 38 39 // Returns the internal representation of credit card type corresponding to 40 // the given |number|. The credit card type is determined purely according to 41 // the Issuer Identification Number (IIN), a.k.a. the "Bank Identification 42 // Number (BIN)", which is parsed from the relevant prefix of the |number|. 43 // This function performs no additional validation checks on the |number|. 44 // Hence, the returned type for both the valid card "4111-1111-1111-1111" and 45 // the invalid card "4garbage" will be Visa, which has an IIN of 4. 46 static std::string GetCreditCardType(const base::string16& number); 47 48 // FormGroup: 49 virtual void GetMatchingTypes( 50 const base::string16& text, 51 const std::string& app_locale, 52 ServerFieldTypeSet* matching_types) const OVERRIDE; 53 virtual base::string16 GetRawInfo(ServerFieldType type) const OVERRIDE; 54 virtual void SetRawInfo(ServerFieldType type, 55 const base::string16& value) OVERRIDE; 56 virtual base::string16 GetInfo(const AutofillType& type, 57 const std::string& app_locale) const OVERRIDE; 58 virtual bool SetInfo(const AutofillType& type, 59 const base::string16& value, 60 const std::string& app_locale) OVERRIDE; 61 62 // AutofillDataModel: 63 virtual void FillFormField(const AutofillField& field, 64 size_t variant, 65 const std::string& app_locale, 66 FormFieldData* field_data) const OVERRIDE; 67 68 // Credit card preview summary, for example: ******1234, Exp: 01/2020 69 const base::string16 Label() const; 70 71 // Special method to set value for HTML5 month input type. 72 void SetInfoForMonthInputType(const base::string16& value); 73 74 // The number altered for display, for example: ******1234 75 base::string16 ObfuscatedNumber() const; 76 // The last four digits of the credit card number. 77 base::string16 LastFourDigits() const; 78 // The user-visible type of the card, e.g. 'Mastercard'. 79 base::string16 TypeForDisplay() const; 80 // A label for this credit card formatted as 'Cardname - 2345'. 81 base::string16 TypeAndLastFourDigits() const; 82 83 const std::string& type() const { return type_; } 84 85 int expiration_month() const { return expiration_month_; } 86 int expiration_year() const { return expiration_year_; } 87 88 // For use in STL containers. 89 void operator=(const CreditCard& credit_card); 90 91 // If the card numbers for |this| and |imported_card| match, and merging the 92 // two wouldn't result in unverified data overwriting verified data, 93 // overwrites |this| card's data with the data in |credit_card|. 94 // Returns true if the card numbers match, false otherwise. 95 bool UpdateFromImportedCard(const CreditCard& imported_card, 96 const std::string& app_locale) WARN_UNUSED_RESULT; 97 98 // Comparison for Sync. Returns 0 if the credit card is the same as |this|, 99 // or < 0, or > 0 if it is different. The implied ordering can be used for 100 // culling duplicates. The ordering is based on collation order of the 101 // textual contents of the fields. 102 // GUIDs, origins, labels, and unique IDs are not compared, only the values of 103 // the credit cards themselves. 104 int Compare(const CreditCard& credit_card) const; 105 106 // Used by tests. 107 bool operator==(const CreditCard& credit_card) const; 108 bool operator!=(const CreditCard& credit_card) const; 109 110 // Returns true if there are no values (field types) set. 111 bool IsEmpty(const std::string& app_locale) const; 112 113 // Returns true if all field types have valid values set. 114 bool IsComplete() const; 115 116 // Returns true if all field types have valid values set and the card is not 117 // expired. 118 bool IsValid() const; 119 120 // Returns the credit card number. 121 const base::string16& number() const { return number_; } 122 123 private: 124 // FormGroup: 125 virtual void GetSupportedTypes( 126 ServerFieldTypeSet* supported_types) const OVERRIDE; 127 128 // The month and year are zero if not present. 129 int Expiration4DigitYear() const { return expiration_year_; } 130 int Expiration2DigitYear() const { return expiration_year_ % 100; } 131 base::string16 ExpirationMonthAsString() const; 132 base::string16 Expiration4DigitYearAsString() const; 133 base::string16 Expiration2DigitYearAsString() const; 134 135 // Sets |expiration_month_| to the integer conversion of |text|. 136 void SetExpirationMonthFromString(const base::string16& text, 137 const std::string& app_locale); 138 139 // Sets |expiration_year_| to the integer conversion of |text|. 140 void SetExpirationYearFromString(const base::string16& text); 141 142 // Sets |number_| to |number| and computes the appropriate card |type_|. 143 void SetNumber(const base::string16& number); 144 145 // These setters verify that the month and year are within appropriate 146 // ranges. 147 void SetExpirationMonth(int expiration_month); 148 void SetExpirationYear(int expiration_year); 149 150 base::string16 number_; // The credit card number. 151 base::string16 name_on_card_; // The cardholder's name. 152 std::string type_; // The type of the card. 153 154 // These members are zero if not present. 155 int expiration_month_; 156 int expiration_year_; 157 }; 158 159 // So we can compare CreditCards with EXPECT_EQ(). 160 std::ostream& operator<<(std::ostream& os, const CreditCard& credit_card); 161 162 // The string identifiers for credit card icon resources. 163 extern const char* const kAmericanExpressCard; 164 extern const char* const kDinersCard; 165 extern const char* const kDiscoverCard; 166 extern const char* const kGenericCard; 167 extern const char* const kJCBCard; 168 extern const char* const kMasterCard; 169 extern const char* const kUnionPay; 170 extern const char* const kVisaCard; 171 172 } // namespace autofill 173 174 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_ 175