//////////// 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"
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!
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.
I was able to answer my own question, shared it above in the "updated" area.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With