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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_
12 #include "base/compiler_specific.h"
13 #include "base/strings/string16.h"
14 #include "components/autofill/core/browser/autofill_data_model.h"
18 // A form group that stores credit card information.
19 class CreditCard
: public AutofillDataModel
{
22 // A card with a complete number managed by Chrome (and not representing
23 // something on the server).
26 // A card from Wallet with masked information. Such cards will only have
27 // the last 4 digits of the card number, and require an extra download to
28 // convert to a FULL_SERVER_CARD.
31 // A card from the Wallet server with full information. This card is not
36 // The status of this credit card. Only used for server cards.
42 CreditCard(const std::string
& guid
, const std::string
& origin
);
43 CreditCard(const base::string16
& card_number
,
47 // Creates a server card. The type must be MASKED_SERVER_CARD or
49 CreditCard(RecordType type
, const std::string
& server_id
);
51 // For use in STL containers.
53 CreditCard(const CreditCard
& credit_card
);
54 ~CreditCard() override
;
56 // Returns a version of |number| that has any separator characters removed.
57 static const base::string16
StripSeparators(const base::string16
& number
);
59 // The user-visible type of the card, e.g. 'Mastercard'.
60 static base::string16
TypeForDisplay(const std::string
& type
);
62 // This method is not compiled on iOS because the resources are not used and
63 // should not be shipped.
65 // The ResourceBundle ID for the appropriate credit card image.
66 static int IconResourceId(const std::string
& type
);
67 #endif // #if !defined(OS_IOS)
69 // Returns the internal representation of credit card type corresponding to
70 // the given |number|. The credit card type is determined purely according to
71 // the Issuer Identification Number (IIN), a.k.a. the "Bank Identification
72 // Number (BIN)", which is parsed from the relevant prefix of the |number|.
73 // This function performs no additional validation checks on the |number|.
74 // Hence, the returned type for both the valid card "4111-1111-1111-1111" and
75 // the invalid card "4garbage" will be Visa, which has an IIN of 4.
76 static const char* GetCreditCardType(const base::string16
& number
);
78 // Type strings are defined at the bottom of this file, e.g. kVisaCard.
79 void SetTypeForMaskedCard(const char* type
);
81 // Sets/gets the status of a server card.
82 void SetServerStatus(ServerStatus status
);
83 ServerStatus
GetServerStatus() const;
86 void GetMatchingTypes(const base::string16
& text
,
87 const std::string
& app_locale
,
88 ServerFieldTypeSet
* matching_types
) const override
;
89 base::string16
GetRawInfo(ServerFieldType type
) const override
;
90 void SetRawInfo(ServerFieldType type
, const base::string16
& value
) override
;
91 base::string16
GetInfo(const AutofillType
& type
,
92 const std::string
& app_locale
) const override
;
93 bool SetInfo(const AutofillType
& type
,
94 const base::string16
& value
,
95 const std::string
& app_locale
) override
;
97 // Credit card preview summary, for example: Visa - 1234, Exp: 01/2020
98 // Used for settings and the requestAutocomplete dialog, but not
99 // the autofill dropdown.
100 const base::string16
Label() const;
102 // Special method to set value for HTML5 month input type.
103 void SetInfoForMonthInputType(const base::string16
& value
);
105 // The last four digits of the credit card number (or possibly less if there
106 // aren't enough characters).
107 base::string16
LastFourDigits() const;
108 // The user-visible type of the card, e.g. 'Mastercard'.
109 base::string16
TypeForDisplay() const;
110 // A label for this credit card formatted as 'Cardname - 2345'.
111 base::string16
TypeAndLastFourDigits() const;
113 const std::string
& type() const { return type_
; }
115 int expiration_month() const { return expiration_month_
; }
116 int expiration_year() const { return expiration_year_
; }
118 // These setters verify that the month and year are within appropriate
119 // ranges, or 0. They take integers as an alternative to setting the inputs
120 // from strings via SetInfo().
121 void SetExpirationMonth(int expiration_month
);
122 void SetExpirationYear(int expiration_year
);
124 const std::string
& server_id() const { return server_id_
; }
126 // For use in STL containers.
127 void operator=(const CreditCard
& credit_card
);
129 // If the card numbers for |this| and |imported_card| match, and merging the
130 // two wouldn't result in unverified data overwriting verified data,
131 // overwrites |this| card's data with the data in |credit_card|.
132 // Returns true if the card numbers match, false otherwise.
133 bool UpdateFromImportedCard(const CreditCard
& imported_card
,
134 const std::string
& app_locale
) WARN_UNUSED_RESULT
;
136 // Comparison for Sync. Returns 0 if the credit card is the same as |this|,
137 // or < 0, or > 0 if it is different. The implied ordering can be used for
138 // culling duplicates. The ordering is based on collation order of the
139 // textual contents of the fields.
140 // GUIDs, origins, labels, and unique IDs are not compared, only the values of
141 // the credit cards themselves.
142 int Compare(const CreditCard
& credit_card
) const;
144 // Determines if |this| is a local version of the server card |other|.
145 bool IsLocalDuplicateOfServerCard(const CreditCard
& other
) const;
147 // Equality operators compare GUIDs, origins, and the contents.
148 // Usage metadata (use count, use date, modification date) are NOT compared.
149 bool operator==(const CreditCard
& credit_card
) const;
150 bool operator!=(const CreditCard
& credit_card
) const;
152 // How this card is stored.
153 RecordType
record_type() const { return record_type_
; }
154 void set_record_type(RecordType rt
) { record_type_
= rt
; }
156 // Returns true if there are no values (field types) set.
157 bool IsEmpty(const std::string
& app_locale
) const;
159 // Returns true if all field types have valid values set. Server masked cards
160 // will not be complete. MASKED_SERVER_CARDs will never be complete.
161 bool IsComplete() const;
163 // Returns true if all field types have valid values set and the card is not
164 // expired. MASKED_SERVER_CARDs will never be valid because the number is
166 bool IsValid() const;
168 // Returns the credit card number.
169 const base::string16
& number() const { return number_
; }
170 // Sets |number_| to |number| and computes the appropriate card |type_|.
171 void SetNumber(const base::string16
& number
);
175 void GetSupportedTypes(ServerFieldTypeSet
* supported_types
) const override
;
177 // The type of the card to fill in to the page, e.g. 'Mastercard'.
178 base::string16
TypeForFill() const;
180 // The month and year are zero if not present.
181 int Expiration4DigitYear() const { return expiration_year_
; }
182 int Expiration2DigitYear() const { return expiration_year_
% 100; }
183 base::string16
ExpirationMonthAsString() const;
184 base::string16
Expiration4DigitYearAsString() const;
185 base::string16
Expiration2DigitYearAsString() const;
187 // Sets |expiration_month_| to the integer conversion of |text|.
188 void SetExpirationMonthFromString(const base::string16
& text
,
189 const std::string
& app_locale
);
191 // Sets |expiration_year_| to the integer conversion of |text|.
192 void SetExpirationYearFromString(const base::string16
& text
);
194 // See enum definition above.
195 RecordType record_type_
;
197 // The credit card number. For MASKED_SERVER_CARDs, this number will
198 // just contain the last four digits of the card number.
199 base::string16 number_
;
201 // The cardholder's name. May be empty.
202 base::string16 name_on_card_
;
204 // The type of the card. This is one of the k...Card constants below.
207 // These members are zero if not present.
208 int expiration_month_
;
209 int expiration_year_
;
211 // For server cards (both MASKED and UNMASKED) this is the ID assigned by the
212 // server to uniquely identify this card.
213 std::string server_id_
;
215 // The status of the card, as reported by the server. Not valid for local
217 ServerStatus server_status_
;
220 // So we can compare CreditCards with EXPECT_EQ().
221 std::ostream
& operator<<(std::ostream
& os
, const CreditCard
& credit_card
);
223 // The string identifiers for credit card icon resources.
224 extern const char* const kAmericanExpressCard
;
225 extern const char* const kDinersCard
;
226 extern const char* const kDiscoverCard
;
227 extern const char* const kGenericCard
;
228 extern const char* const kJCBCard
;
229 extern const char* const kMasterCard
;
230 extern const char* const kUnionPay
;
231 extern const char* const kVisaCard
;
233 } // namespace autofill
235 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_H_