Solved

Credit card algorithm checking in C / MFC C?

Posted on 2000-03-28
11
271 Views
Last Modified: 2011-09-20
hi guys,

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

Thanks !!!
0
Comment
Question by:Haho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 32

Expert Comment

by:jhance
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

by:wardkd
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

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
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

by:imladris
ID: 2670136
Is there any clarification you need before grading the answer?
0
 
LVL 32

Expert Comment

by:jhance
ID: 2670431
imladris,

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

by:imladris
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

by:jhance
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

by:imladris
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);
}

Does that cover your concerns?
0
 
LVL 1

Author Comment

by:Haho
ID: 2679072
guys,

does it work for XXXXXXXXXXXXXXXX ?

Thanks!
0
 
LVL 16

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question