OLD | NEW |
| (Empty) |
1 // Copyright (c) 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 #include "chrome/browser/autofill/wallet/instrument.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/string_util.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/autofill/validation.h" | |
12 #include "chrome/browser/autofill/wallet/wallet_address.h" | |
13 | |
14 namespace autofill { | |
15 namespace wallet { | |
16 | |
17 namespace { | |
18 | |
19 std::string FormOfPaymentToString(Instrument::FormOfPayment form_of_payment) { | |
20 switch (form_of_payment) { | |
21 case Instrument::UNKNOWN: | |
22 return "UNKNOWN"; | |
23 case Instrument::VISA: | |
24 return "VISA"; | |
25 case Instrument::MASTER_CARD: | |
26 return "MASTER_CARD"; | |
27 case Instrument::AMEX: | |
28 return "AMEX"; | |
29 case Instrument::DISCOVER: | |
30 return "DISCOVER"; | |
31 case Instrument::JCB: | |
32 return "JCB"; | |
33 } | |
34 NOTREACHED(); | |
35 return "NOT_POSSIBLE"; | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 Instrument::Instrument(const string16& primary_account_number, | |
41 const string16& card_verification_number, | |
42 int expiration_month, | |
43 int expiration_year, | |
44 FormOfPayment form_of_payment, | |
45 scoped_ptr<Address> address) | |
46 : primary_account_number_(primary_account_number), | |
47 card_verification_number_(card_verification_number), | |
48 expiration_month_(expiration_month), | |
49 expiration_year_(expiration_year), | |
50 form_of_payment_(form_of_payment), | |
51 address_(address.Pass()) { | |
52 DCHECK(address_); | |
53 Init(); | |
54 } | |
55 | |
56 Instrument::Instrument(const Instrument& instrument) | |
57 : primary_account_number_(instrument.primary_account_number()), | |
58 card_verification_number_(instrument.card_verification_number()), | |
59 expiration_month_(instrument.expiration_month()), | |
60 expiration_year_(instrument.expiration_year()), | |
61 form_of_payment_(instrument.form_of_payment()), | |
62 address_(new Address(instrument.address())) { | |
63 Init(); | |
64 } | |
65 | |
66 Instrument::~Instrument() {} | |
67 | |
68 scoped_ptr<base::DictionaryValue> Instrument::ToDictionary() const { | |
69 // |primary_account_number_| and |card_verification_number_| can never be | |
70 // sent the server in way that would require putting them into a dictionary. | |
71 // Never add them to this function. | |
72 | |
73 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
74 dict->SetString("type", "CREDIT_CARD"); | |
75 dict->SetInteger("credit_card.exp_month", expiration_month_); | |
76 dict->SetInteger("credit_card.exp_year", expiration_year_); | |
77 dict->SetString("credit_card.fop_type", | |
78 FormOfPaymentToString(form_of_payment_)); | |
79 dict->SetString("credit_card.last_4_digits", last_four_digits_); | |
80 dict->Set("credit_card.address", | |
81 address_.get()->ToDictionaryWithoutID().release()); | |
82 | |
83 return dict.Pass(); | |
84 } | |
85 | |
86 bool Instrument::IsValid() const { | |
87 if (!IsStringASCII(primary_account_number_)) | |
88 return false; | |
89 bool primary_account_number_valid = | |
90 autofill::IsValidCreditCardNumber(primary_account_number_); | |
91 bool card_verification_number_valid = card_verification_number_.size() == 3 || | |
92 card_verification_number_.size() == 4; | |
93 bool exp_month_valid = expiration_month_ >= 1 && expiration_month_ <= 12; | |
94 bool exp_year_valid = expiration_year_ >= 2013 && expiration_year_ <= 2100; | |
95 | |
96 return primary_account_number_valid && | |
97 card_verification_number_valid && | |
98 exp_month_valid && | |
99 exp_year_valid; | |
100 } | |
101 | |
102 void Instrument::Init() { | |
103 if (primary_account_number_.size() >= 4) { | |
104 last_four_digits_ = | |
105 primary_account_number_.substr(primary_account_number_.size() - 4); | |
106 } | |
107 } | |
108 | |
109 } // namespace wallet | |
110 } // namespace autofill | |
OLD | NEW |