OLD | NEW |
| (Empty) |
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 #include "components/autofill/content/browser/wallet/instrument.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "base/values.h" | |
12 #include "components/autofill/content/browser/wallet/wallet_address.h" | |
13 #include "components/autofill/core/browser/autofill_country.h" | |
14 #include "components/autofill/core/browser/autofill_profile.h" | |
15 #include "components/autofill/core/browser/autofill_sync_constants.h" | |
16 #include "components/autofill/core/browser/credit_card.h" | |
17 #include "components/autofill/core/browser/validation.h" | |
18 | |
19 namespace autofill { | |
20 namespace wallet { | |
21 | |
22 namespace { | |
23 | |
24 // Converts a known Autofill card type to a Instrument::FormOfPayment. | |
25 // Used for creating new Instruments. | |
26 Instrument::FormOfPayment FormOfPaymentFromCardType(const std::string& type) { | |
27 if (type == kAmericanExpressCard) | |
28 return Instrument::AMEX; | |
29 else if (type == kDiscoverCard) | |
30 return Instrument::DISCOVER; | |
31 else if (type == kMasterCard) | |
32 return Instrument::MASTER_CARD; | |
33 else if (type == kVisaCard) | |
34 return Instrument::VISA; | |
35 | |
36 return Instrument::UNKNOWN; | |
37 } | |
38 | |
39 std::string FormOfPaymentToString(Instrument::FormOfPayment form_of_payment) { | |
40 switch (form_of_payment) { | |
41 case Instrument::UNKNOWN: | |
42 return kSyncCardTypeUnknown; | |
43 case Instrument::VISA: | |
44 return kSyncCardTypeVisa; | |
45 case Instrument::MASTER_CARD: | |
46 return kSyncCardTypeMasterCard; | |
47 case Instrument::AMEX: | |
48 return kSyncCardTypeAmex; | |
49 case Instrument::DISCOVER: | |
50 return kSyncCardTypeDiscover; | |
51 case Instrument::JCB: | |
52 return kSyncCardTypeJCB; | |
53 } | |
54 NOTREACHED(); | |
55 return "NOT_POSSIBLE"; | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 Instrument::Instrument(const CreditCard& card, | |
61 const base::string16& card_verification_number, | |
62 const AutofillProfile& profile) | |
63 : primary_account_number_(card.GetRawInfo(CREDIT_CARD_NUMBER)), | |
64 card_verification_number_(card_verification_number), | |
65 expiration_month_(card.expiration_month()), | |
66 expiration_year_(card.expiration_year()), | |
67 form_of_payment_(FormOfPaymentFromCardType(card.type())), | |
68 address_(new Address(profile)) { | |
69 Init(); | |
70 } | |
71 | |
72 Instrument::Instrument(const base::string16& primary_account_number, | |
73 const base::string16& card_verification_number, | |
74 int expiration_month, | |
75 int expiration_year, | |
76 FormOfPayment form_of_payment, | |
77 scoped_ptr<Address> address) | |
78 : primary_account_number_(primary_account_number), | |
79 card_verification_number_(card_verification_number), | |
80 expiration_month_(expiration_month), | |
81 expiration_year_(expiration_year), | |
82 form_of_payment_(form_of_payment), | |
83 address_(address.Pass()) { | |
84 Init(); | |
85 } | |
86 | |
87 Instrument::Instrument(const Instrument& instrument) | |
88 : primary_account_number_(instrument.primary_account_number()), | |
89 card_verification_number_(instrument.card_verification_number()), | |
90 expiration_month_(instrument.expiration_month()), | |
91 expiration_year_(instrument.expiration_year()), | |
92 form_of_payment_(instrument.form_of_payment()), | |
93 address_(instrument.address() ? | |
94 new Address(*instrument.address()) : NULL) { | |
95 Init(); | |
96 } | |
97 | |
98 Instrument::~Instrument() {} | |
99 | |
100 scoped_ptr<base::DictionaryValue> Instrument::ToDictionary() const { | |
101 // |primary_account_number_| and |card_verification_number_| can never be | |
102 // sent the server in way that would require putting them into a dictionary. | |
103 // Never add them to this function. | |
104 | |
105 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
106 dict->SetString("type", "CREDIT_CARD"); | |
107 dict->SetInteger("credit_card.exp_month", expiration_month_); | |
108 dict->SetInteger("credit_card.exp_year", expiration_year_); | |
109 dict->SetString("credit_card.fop_type", | |
110 FormOfPaymentToString(form_of_payment_)); | |
111 dict->SetString("credit_card.last_4_digits", last_four_digits_); | |
112 dict->Set("credit_card.address", | |
113 address_.get()->ToDictionaryWithoutID().release()); | |
114 | |
115 return dict.Pass(); | |
116 } | |
117 | |
118 void Instrument::Init() { | |
119 if (primary_account_number_.size() >= 4) { | |
120 last_four_digits_ = | |
121 primary_account_number_.substr(primary_account_number_.size() - 4); | |
122 } | |
123 } | |
124 | |
125 } // namespace wallet | |
126 } // namespace autofill | |
OLD | NEW |