forked from ieatlint/MagRead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carddetect.cpp
286 lines (241 loc) · 10.7 KB
/
carddetect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
This file is part of MagRead.
MagRead is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MagRead is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MagRead. If not, see <http://www.gnu.org/licenses/>.
Written by Jeffrey Malone <[email protected]>
http://blog.tehinterweb.com
*/
#include "carddetect.h"
#include <QDebug>
CardDetect::CardDetect( MagCard *_card ) {
aamvaIssuerList();
if( _card )
setCard( _card );
}
void CardDetect::setCard( MagCard *_card ) {
card = _card;
processCard();
}
void CardDetect::processCard() {
if( !card )
return;
card->type = MagCard::CARD_UNKNOWN;
/* Fill in the fields.. */
QString expDate;
int acctLen = 0;
if( card->encoding == ABA && card->charStream.startsWith( ';' ) ) {
acctLen = card->charStream.indexOf( '=' ) - 1;
if( acctLen > 0 ) {
card->accountNumber = card->charStream.mid( 1, acctLen );
expDate = card->charStream.mid( acctLen + 2, 4 );
card->miscData = card->charStream.mid( acctLen + 6 ).remove( '?' );
}
} else if( card->encoding == IATA && card->charStream.startsWith( "%B" ) ) {
acctLen = card->charStream.indexOf( '^', 1 ) - 2;
if( acctLen > 0 ) {
card->accountNumber = card->charStream.mid( 2, acctLen );
card->miscData = card->charStream.section( '^', 2 ).remove( '?' );
expDate = card->miscData.left( 4 );
card->miscData.remove( 0, 4 );
}
}
//if the above didn't yield us an account number, just copy the charStream */
if( card->accountNumber.isEmpty() ) {
card->accountNumber = card->charStream;
card->charStream.remove( ';' );
card->charStream.remove( '%' );
card->charStream.remove( '?' );
}
if( acctLen )
luhnCheck();
if( card->accountValid )
creditCardCheck();
/* This code allows partial credit card swipes to be treated as valid, as long at least 3 digits
* of extra data that passes checksum are present */
if( card->accountValid && !card->swipeValid && card->type & MagCard::CARD_CC ) {
if( 3 > card->miscData.length() || card->miscData.left( 3 ).contains( BAD_CHAR ) ) {
card->accountValid = false;
}
}
if( card->swipeValid && card->type == MagCard::CARD_UNKNOWN )
aamvaCardCheck( expDate );
if( !expDate.isEmpty() && card->type != MagCard::CARD_AAMVA ) {
card->expirationDate = QDate::fromString( expDate, "yyMM" );
if( card->expirationDate.year() < 1980 )//qdate defaults to 19nn. Adjust for this.
card->expirationDate = card->expirationDate.addYears( 100 );
if( card->expirationDate.isValid() ) {//Make it so it expires on the last day of the given month
card->expirationDate = card->expirationDate.addDays( card->expirationDate.daysInMonth() - 1);
} else {
card->type = MagCard::CARD_UNKNOWN; // must contain a valid date to be known
}
}
//test if it is a AAA card
if( card->type == MagCard::CARD_VISA ) {
if( card->encoding == ABA && card->miscData.endsWith( "004976" ) ) {
card->type = MagCard::CARD_AAA;
} else if( card->encoding == IATA && card->miscData.contains( "00497600" ) ) {
card->type = MagCard::CARD_AAA;
card->expirationDate.setDate( card->expirationDate.year(),
card->expirationDate.month(),
card->miscData.right( 4 ).left( 2 ).toInt() );
}
if( card->type == MagCard::CARD_AAA )
card->accountIssuer = "AAA";
}
}
void CardDetect::aamvaIssuerList() {
issuerList[ "636026" ] = (struct issuer) { "Arizona", "AZ", "L" };
issuerList[ "0636021" ] = (struct issuer) { "Arkansas", "AR", "" };
issuerList[ "636014" ] = (struct issuer) { "California", "CA", "L" };
issuerList[ "636020" ] = (struct issuer) { "Colorado", "CO", "NN-NNN-NNNN" };
issuerList[ "636010" ] = (struct issuer) { "Florida", "FL", "LNNN-NNN-NN-NNN-N" };
issuerList[ "636018" ] = (struct issuer) { "Iowa", "IA", "NNNLLNNNN" };
issuerList[ "636022" ] = (struct issuer) { "Kansas", "KS", "KNN-NN-NNNN" };
issuerList[ "636007" ] = (struct issuer) { "Louisiana", "LA", "" };
issuerList[ "636003" ] = (struct issuer) { "Maryland", "MD", "L-NNN-NNN-NNN-NNN" };
issuerList[ "636032" ] = (struct issuer) { "Michigan", "MI", "L NNN NNN NNN NNN" };
issuerList[ "636038" ] = (struct issuer) { "Minnesota", "MN", "L" };
issuerList[ "636030" ] = (struct issuer) { "Missouri", "MO", "L" };
issuerList[ "636039" ] = (struct issuer) { "New Hampshire", "NH", "NNLLLNNNN" };
issuerList[ "636009" ] = (struct issuer) { "New Mexico", "NM", "" };
issuerList[ "636023" ] = (struct issuer) { "Ohio", "OH", "LLNNNNNN" };
issuerList[ "636025" ] = (struct issuer) { "Pennsylvania", "PA", "NN NNN NNN" };
issuerList[ "636005" ] = (struct issuer) { "South Carolina", "SC", "" };
issuerList[ "636015" ] = (struct issuer) { "Texas", "TX", "" };
issuerList[ "636024" ] = (struct issuer) { "Vermont", "VT", "NNNNNNNL" };
issuerList[ "636031" ] = (struct issuer) { "Wisconsin", "WI", "LNNN-NNNN-NNNN-NN" };
issuerList[ "636027" ] = (struct issuer) { "State Dept (USA)", "US-DoS", "" };
/* Exceptions:
* Arkansas -- Inexplicably, they put a leading 0 on their IIN. This
* is handled below in the lookup.
* Vermont -- I'm told they have additional format to the one listed
* above that is 8 numbers (no letter). As the two
* formats have different field widths, I automatically
* handle this in the formatting algorithm.
*/
//Formatting information is not available for Canada right now
issuerList[ "636028" ] = (struct issuer) { "British Columbia", "BC", "" };
issuerList[ "636017" ] = (struct issuer) { "New Brunswick", "NB", "" };
issuerList[ "636016" ] = (struct issuer) { "Newfoundland", "NL", "" };
issuerList[ "636013" ] = (struct issuer) { "Nova Scotia", "NS", "" };
issuerList[ "636012" ] = (struct issuer) { "Ontario", "ON", "" };
issuerList[ "636044" ] = (struct issuer) { "Saskatchewan", "SK", "" };
//These may or may not have magstripes. I'm including them in case they do
issuerList[ "604427" ] = (struct issuer) { "American Samoa", "AS", "" };
issuerList[ "636019" ] = (struct issuer) { "Guam", "GU", "" };
issuerList[ "636062" ] = (struct issuer) { "US Virgin Islands", "US-VI", "" };
issuerList[ "636056" ] = (struct issuer) { "Coahuila", "MX-COA", "" };
issuerList[ "636057" ] = (struct issuer) { "Hidalgo", "MX-HID", "" };
}
void CardDetect::aamvaCardCheck( QString expDate ) {
if( card->encoding == IATA )
return; //we're only going to support ABA for now
struct issuer issuerInfo;
QString iin = card->accountNumber.left( 6 );
issuerInfo = issuerList.value( iin );
if( issuerInfo.name.isEmpty() ) {
iin = card->accountNumber.mid( 1, 6 );
issuerInfo = issuerList.value( iin );
if( issuerInfo.name.isEmpty() )
return; // this is not a known AAMVA card, abort
}
card->type = MagCard::CARD_AAMVA;
card->aamvaIssuer = iin;
card->aamvaIssuerName = issuerInfo.name;
card->aamvaIssuerAbr = issuerInfo.abbreviation;
card->accountNumber.remove( iin );
if( card->miscData.length() > 8 )
card->accountNumber.append( card->miscData.mid( 8 ) );
//format the id number if applicable
if( !issuerInfo.format.isEmpty() ) {
for( int i = 0; i < issuerInfo.format.length(); i++ ) {
if( issuerInfo.format.at( i ) == 'L' ) {
if( card->accountNumber.length() - i < 2 )
continue;
QChar letter = card->accountNumber.mid( i, 2 ).toInt() + 64;
if( letter.isLetter() )
card->accountNumber.replace( i, 2, letter );
} else if( issuerInfo.format.at( i ) != 'N' ) {
card->accountNumber.insert( i, issuerInfo.format.at( i ) );
} else if( card->accountNumber.at( i ) == '=' ) {
card->accountNumber.replace( i, 1, '0' );
}
}
}
card->accountNumber.remove( '=' );
//set the birthday
QString bday = card->miscData.left( 8 );
if( bday.mid( 4, 2 ) > "12" ) { //some (Calif) violate AAMVA standard and switch the exp and bday month values
QString exp = bday.mid( 4, 2 );
bday.replace( 4, 2, expDate.right( 2 ) );
expDate.replace( 2, 2, exp );
}
card->aamvaBirthday = QDate::fromString( bday, "yyyyMMdd" );
//set the age
card->aamvaAge = QDate::currentDate().year() - card->aamvaBirthday.year();
QDate curBday;
curBday.setDate( QDate::currentDate().year(), card->aamvaBirthday.month(), card->aamvaBirthday.day() );
if( curBday > QDate::currentDate() )
card->aamvaAge--;
//set the expiration date
if( expDate.endsWith( "99" ) ) { // expires on the birth day and month in the given year
expDate.replace( 2, 4, bday.mid( 4, 4 ) );
expDate.prepend( "20" );
card->expirationDate = QDate::fromString( expDate, "yyyyMMdd" );
} else if( !expDate.endsWith( "77" ) ) {
if( expDate.endsWith( "88" ) )// expires on last day of the month of birth in given year
expDate.replace( 2, 2, bday.mid( 4, 2 ) );
expDate.prepend( "20" );
card->expirationDate = QDate::fromString( expDate, "yyyyMM" );
card->expirationDate.addDays( card->expirationDate.daysInMonth() - 1 );
}
}
void CardDetect::creditCardCheck() {
int acctLen = card->accountNumber.length();
if( acctLen == 16 ) {
if( card->accountNumber.startsWith( '4' ) ) {
card->type = MagCard::CARD_VISA;
card->accountIssuer = "Visa";
} else if( card->accountNumber.left( 2 ) >= "51" && card->accountNumber.left( 2 ) <= "55" ) {
card->type = MagCard::CARD_MC;
card->accountIssuer = "MasterCard";
} else if( card->accountNumber.startsWith( "6011" ) || card->accountNumber.startsWith( "65" ) ) {
card->type = MagCard::CARD_DISC;
card->accountIssuer = "Discover";
}
} else if( acctLen == 15 ) {
if( card->accountNumber.startsWith( "34" ) || card->accountNumber.startsWith( "37" ) ) {
card->type = MagCard::CARD_CC | MagCard::CARD_AMEX;
card->accountIssuer = "American Express";
}
}
if( card->encoding == IATA && card->type & MagCard::CARD_CC ) {
card->accountHolder = card->charStream.section( '^', 1, 1 ).trimmed();
if( card->accountHolder.contains( '/' ) ) { // fix the formatting from "LAST/FIRST" on some cards
card->accountHolder = card->accountHolder.section( '/', 1, 1 ) + ' ' + card->accountHolder.section( '/', 0, 0 );
}
}
}
/* verifies the card->accountNumber against the luhn algorithm and sets card->accountValid */
void CardDetect::luhnCheck() {
int total = 0;
for( int i = 0; i < card->accountNumber.length(); i++ ) {
if( i & 1 ) { // if odd
total += card->accountNumber.at( i ).digitValue();
} else {
int x = card->accountNumber.at( i ).digitValue() * 2;
total += x % 10;
if( x > 9 )
total++;
}
}
card->accountValid = !( total % 10 );
}