Solved

# Credit card algorithm checking in C / MFC C?

Posted on 2000-03-28
261 Views
hi guys,

can anybody share the credit card algorithm checking function written in C / MFC C?

Thanks !!!
0
Question by:Haho
• 5
• 3
• 2
• +1

LVL 32

Expert Comment

ID: 2664079
See:

http://www.binary.net/dturley/js/ccver.html

It's in Javascript but can be translated to C pretty easily.
0

Expert Comment

ID: 2664109
This is dependant on the type of card, but the Standard ISO is as follows:
19      18      17      16      15      14      13      12      11      10      9      8      7      6      5      4      3      2      1
5      4      0      1      5      2      1      9      0      3      0      7      8      3      1      8      OK
Weights      1      2      1      2      1      2      1      2      1      2      1      2      1      2      1      2      1      2      1

Products      0      0      0      10      4      0      1      10      2      2      9      0      3      0      7      16      3      2      8

Digits      0      0      0      1      0      0      0      1      0      0      0      0      0      0      0      1      0      0      0      50      Sum of digits
0      0      0      0      4      0      1      0      2      2      9      0      3      0      7      6      3      2      8
10      Modulus
0

Expert Comment

ID: 2664138
Please ignore my previous comment. This is pasted from a spreadsheet, which hasn't worked too well ;)
0

LVL 16

Accepted Solution

ID: 2665437
That JavaScript is an awful piece of code. I hope it is merely the constraints of that pseudo language. Here is what we use. The argument is a 19 character string representing the credit card number, right justified:

ccnvalid(ccn)
char *ccn;
{
int i,cs,j,d;

d=cs=0;
for(i=18; i>=0;  --i)
{      if(ccn[i]!=' ')
{      j=(ccn[i]-'0')*(d+1);
cs+=j%10;
cs+=j/10;
d=!d;
}
}
return(cs%10==0);
}
0

LVL 16

Expert Comment

ID: 2670136
0

LVL 32

Expert Comment

ID: 2670431

There seem to be problems with your code:

1) It fails if the number is not formatted like: XXXX XXXX XXXX XXXX

2) It fails for valid 13 digit Visa card numbers: XXXX XXX XXX XXX

3) It fails with AMEX #s which are all 15-digit.

4) I didn't check it with Discover #s but I think it fails there as well.
0

LVL 16

Expert Comment

ID: 2670828
jhance,

This is production code, running in thousands of sites throughout North America.
It may be that I have not explained the parameter in enough detail.

In fact, I would expect it to fail if there are embedded spaces in the number (notice that it stops processing when it encounters a blank). If the number is shorter than 19 characters, it should have leading blanks.

I'm not clear on why you are quoting the length as being a problem in case 2 and 3, there is no specific reference to length in the algorithm or the code. It simply processes digits, right to left, until it encounters a blank.

If you could provide a specific failure case, perhaps we could discuss it in detail.
0

LVL 32

Expert Comment

ID: 2670873
I hate to be critical but if this is production code, I think it's a miracle it works at all.

For example consider:

for(i=18; i>=0;  --i)
{ if(ccn[i]!=' ')

What happens if ccn is < 18 chars long?

See my point? Any old garbage in the string after the terminating NULL will be considered.  In my case, I fed it a 16 digit CCN of the form:

1234123412341234

Well ccn[18] comes 3 chars to the _right_ of the last "4".  I don't know what's in the space after the 4.  It might be 0 or it might be junk...

Now perhaps in the context of the application where you're running this code, you are massaging the CCN BEFORE it gets to this function.  In that case, things might be better but it's dangerous code as posted.

0

LVL 16

Expert Comment

ID: 2671057
You're correct in that in our code the parameter is guaranteed to be 19 characters before it gets to the function. You'll note that I originally specified that the parameter is a 19 character string. If one would want to be more general than that, you could provide one level of generality up by providing a sprintf in it, along the lines of:

ccnvalid(cca)
char *cca;
{
int i,cs,j,d;
char ccn[20];

sprintf(ccn,"%19s",cca);
d=cs=0;
for(i=18; i>=0;  --i)
{ if(ccn[i]!=' ')
{ j=(ccn[i]-'0')*(d+1);
cs+=j%10;
cs+=j/10;
d=!d;
}
}
return(cs%10==0);
}

0

LVL 1

Author Comment

ID: 2679072
guys,

does it work for XXXXXXXXXXXXXXXX ?

Thanks!
0

LVL 16

Expert Comment

ID: 2680486
Absolutely. In fact, the function I provided assumes there are no blanks embedded in the credit card number string.
0