Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(535)

Side by Side Diff: chrome/browser/autofill/wallet/instrument.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 if (primary_account_number_.size() >=4) {
54 last_four_digits_ =
55 primary_account_number_.substr(primary_account_number_.size() - 4);
56 }
57 }
58
59 Instrument::~Instrument() {}
60
61 scoped_ptr<base::DictionaryValue> Instrument::ToDictionary() const {
62 // |primary_account_number_| and |card_verification_number_| can never be
63 // sent the server in way that would require putting them into a dictionary.
64 // Never add them to this function.
65
66 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
67 dict->SetString("type", "CREDIT_CARD");
68 dict->SetInteger("credit_card.exp_month", expiration_month_);
69 dict->SetInteger("credit_card.exp_year", expiration_year_);
70 dict->SetString("credit_card.fop_type",
71 FormOfPaymentToString(form_of_payment_));
72 dict->SetString("credit_card.last_4_digits", last_four_digits_);
73 dict->Set("credit_card.address",
74 address_.get()->ToDictionaryWithoutID().release());
75
76 return dict.Pass();
77 }
78
79 bool Instrument::IsValid() const {
80 if (!IsStringASCII(primary_account_number_))
81 return false;
82 bool primary_account_number_valid =
83 autofill::IsValidCreditCardNumber(primary_account_number_);
84 bool card_verification_number_valid = card_verification_number_.size() == 3 ||
85 card_verification_number_.size() == 4;
86 bool exp_month_valid = expiration_month_ >= 1 && expiration_month_ <= 12;
87 bool exp_year_valid = expiration_year_ >= 2013 && expiration_year_ <= 2100;
88
89 return primary_account_number_valid &&
90 card_verification_number_valid &&
91 exp_month_valid &&
92 exp_year_valid;
93 }
94
95 } // namespace wallet
96 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698