Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Credit Card Formula in Iphone App

//////////// UPDATE! //////////////////

So I looked around on the net and after finding a few formulas and testing. I found a formula that works. 1. Reverse the card number 2. For every other digit double the number 3. For the digits that you didn't double, add them in the new "doubled" string 4. Add all the numbers up together - ONE BY ONE 5. Divide by ten, if there is no remainder, the card number is valid - might not be "approved" but the numbers are atleast in the correct sequence.

For example take card number "4866 3060 7833 1744"

  1. reverse - 4471 3387 0603 6684
  2. create a string with every other number starting with the 2nd number "47376364"
  3. now create a string with every number doubled "814617126128"
  4. now append a string with the digits that you did not use in step 1 to the string with the numbers doubled "41380068" (new string will be 81461712612841380068 )
  5. now sum the string, using EACH INDIVIDUAL character (totals 77 )
  6. now divide the sum (77) by 10. If there is a remainder, the card is invalid (77/10 = 7.7)INVALID CARD

Here is the source for this http://www.brainjar.com/js/validation/default2.asp

Because I love you all, and you have all helped me in so many ways, I thought Id share the code I put together to do this formula. Thanks to Mike Crandall again, who helped me reverse the first string and I took it from there.

This is using the Luhn Formula

My .h File

#import <UIKit/UIKit.h>

@interface CCValidatorViewController : UIViewController {

    NSString * ccNumber;
    NSString * validCard;
    NSString * isAMEX;


}
@property (nonatomic, retain)NSString * ccNumber;
@property (nonatomic, retain)NSString * isAMEX;
@property (nonatomic, retain)NSString * validCard;
- (NSString *) validateCard:(NSString *)ccNumberString;

@end

My .m file

- (void)viewDidLoad {
    [super viewDidLoad];
    ccNumber = @"4866306078331744"; //invalid card number
    ccNumber = [self validateCard:ccNumber];
    NSLog(@"%@",validCard);
}

- (NSString *) validateCard:(NSString *)ccNumberString{
    validCard = @"";
    NSString * ccNumberReversed = @"";
    NSString * doubleNumbers = @"";
    NSString * everyOtherNumber = @"";
    NSString * lastChar = @"";
    NSString * intDoubled;
    NSString * stringToSum;
    NSUInteger count = [ccNumberString length];
    NSUInteger len = 1;
    NSRange r;

    //since American Express is American Express....., we have to do something special for them.... assholes....
    r = NSMakeRange( 0, 1);
    lastChar = [ccNumberString substringWithRange:r];
    if ([lastChar compare:@"3"] ==0) {

        isAMEX = @"YES";

    }
    else {
        isAMEX = @"NO";
    }



    //reverse the string

    for ( int i=0; i<count; i++){
        r = NSMakeRange( count-i-1, len);
        lastChar = [ccNumberString substringWithRange:r];
        ccNumberReversed = [ccNumberReversed stringByAppendingString:lastChar];
    }

    //double every other number

    int loc = 1;
    int ttr = ccNumberReversed.length/2;
    for ( int i=0; i<ttr; i++){

        r = NSMakeRange( loc, len);
        loc = loc+2;
        lastChar = [ccNumberReversed substringWithRange:r];
        int dv = [lastChar intValue];
        dv = (dv * 2);
        intDoubled = [NSString stringWithFormat:@"%d",dv];
        doubleNumbers = [doubleNumbers stringByAppendingString:intDoubled];
    }

    // get every other number starting at index 0
    loc = 0;
    if ([isAMEX compare:@"YES"] ==0) {
    ttr = ccNumber.length/2+1;
    }
    else {
        ttr = ccNumber.length/2;
    }


    for ( int i=0; i<ttr; i++){

        r = NSMakeRange( loc, len);
        loc = loc+2;
        lastChar = [ccNumberReversed substringWithRange:r];
        everyOtherNumber = [everyOtherNumber stringByAppendingString:lastChar];
    }

    //combine both strings so we can sum them up
    stringToSum = [doubleNumbers stringByAppendingString:everyOtherNumber];

    // add all the numbers up one by one and divide by 10... if no remainder - its a valid card

    loc = 0;
    ttr = stringToSum.length;
    int stringSum = 0;
    for ( int i=0; i<ttr; i++){

        r = NSMakeRange( loc, len);
        lastChar = [stringToSum substringWithRange:r];
        int cc = [lastChar intValue];
        stringSum = stringSum+cc;

        loc ++;
    }

    if (stringSum%10 == 0) {

        validCard = @"YES";
    }
    else {

        validCard = @"NO";
    }

    return validCard;

}

********** Original Post *******************

Does anyone have any code they can share that will validate a credit card number before posting to API?

While im asking, are there going to be any issues from apple if I am selling tickets to a movie theater within my app?

This app is very similar to fandango but for a private chain of theaters (about 13 in all).

Thanks in advance!

like image 829
Louie Avatar asked May 19 '10 22:05

Louie


People also ask

How do I set up AutoFill credit card on iPhone?

Set up AutoFill You can save your personal information or credit card number on your iPhone to speed up filling in online forms and making purchases. Go to Settings > Safari > AutoFill.


1 Answers

I was able to answer my own question, shared it above in the "updated" area.

like image 64
Louie Avatar answered Dec 01 '22 23:12

Louie