在iPhone应用程序中验证信用卡公式

时间:2010-05-19 22:18:44

标签: iphone iphone-sdk-3.0

//////////// 更新! //////////////////

所以我在找到一些公式和测试之后在网上四处看看。我发现了一个有效的公式。 1.反转卡号 2.对于每隔一个数字加倍的数字 3.对于未加倍的数字,将它们添加到新的“doubled”字符串中 4.将所有数字加在一起 ​​- 一个接一个 5.除以10,如果没有余数,则卡号有效 - 可能不是“已批准”,但数字至少是正确的顺序。

例如取卡号“4866 3060 7833 1744”

  1. reverse - 4471 3387 0603 6684
  2. 与其他人一起创建一个字符串 号码以第二个号码开头 “47376364”
  3. 现在创建一个每个的字符串 号码加倍“814617126128”
  4. 现在附加一个带数字的字符串 您没有在步骤1中使用数字加倍的字符串 “41380068”(新字符串将是 81461712612841380068)
  5. 现在使用EACH对字符串求和 个人性格(总计77)
  6. 现在将总和(77)除以10.如果 剩下的就是卡片 无效(77/10 = 7.7)无效卡
  7. 以下是此http://www.brainjar.com/js/validation/default2.asp

    的来源

    因为我爱你们所有人,并且你们在很多方面帮助了我,所以我想我会分享我为了这个公式而共同编写的代码。再次感谢Mike Crandall,他帮助我扭转了第一根弦,我从那里拿走了它。

    这是使用Luhn公式

    我的.h文件

    #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
    

    我的.m文件

    - (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;
    
    }
    

    **********原帖*******************

    在发布到API之前,是否有人可以共享任何可以验证信用卡号的代码?

    在我问的时候,如果我在我的应用程序中销售电影院的门票,苹果会不会有任何问题?

    这个应用程序与fandango非常相似,但是对于私人影院连锁店(总共约13个)。

    提前致谢!

5 个答案:

答案 0 :(得分:6)

我能够回答我自己的问题,在“更新”区域上面分享了它。

答案 1 :(得分:1)

以下是一些可能有用的代码(使用Luhn Algorithm):

-(BOOL) validateCardNumber:(NSString *)cardNumber
{

const char *str = [cardNumber UTF8String];
int n, i, alternate, sum;

n = strlen(str);

if ( n < 13 || n > 19 )
    return NO;

for ( alternate = 0, sum = 0, i = n-1; i>-1; –i) {
    if ( !isdigit(str[i]))
        return NO;

    n = str[i] – ‘0′;

    if ( alternate ) {
        n*=2;
        if ( n > 9 )
            n = ( n % 10 ) + 1;
    }

    alternate = !alternate;

    sum += n;
}

return ( sum % 10 == 0 );
}

此方法可归入Donald Bellenger

答案 2 :(得分:0)

如果你是从你的应用程序内部销售产品,除非你使用应用内销售,否则苹果很可能会拒绝它(在这种情况下,你不需要任何信用卡)验证)。

答案 3 :(得分:0)

在回答您关于应用内购买和购买电影票的问题时,即使您使用应用内购买,Apple也可能会拒绝它,因为您销售的是现实世界的商品,而不是可以在应用内使用的商品。它在审查指南中说明了这一点。此外,你想要Apple占30%吗?

答案 4 :(得分:0)

-(void)validation_check:(NSString*)pass_value
{
    NSMutableArray *character;

    unsigned long long odd_no;
    unsigned long long new_odd_no;
    unsigned long long even_no;
    unsigned long long new_even_no;
    unsigned long long multiplied_even_no;
    unsigned long long changed_even_no;
    unsigned long long final_value;
    unsigned long long revers_card_no;
    unsigned long long card_no;
    unsigned long long check_reverse;
    new_odd_no = 0;
    new_even_no = 0;

    card_no = [pass_value longLongValue];

    character = [[NSMutableArray alloc]init];

    //-------reversing order of entered card number---------
    for(int i = 0; i<[pass_value length];i++)
    {       
        check_reverse =(card_no % 10);
        card_no = (card_no / 10);

        [character addObject:[NSString stringWithFormat:@"%qu",check_reverse]];

        revers_card_no=revers_card_no*10+check_reverse;
        check_reverse=card_no;

    }   
    pass_value = [NSString stringWithFormat:@"%qu",revers_card_no];

    //--------checking for even and odd numbers--------
    for(int j=0;j<[character count];j++)
    {
        if(j % 2 == 0)
        {
            odd_no = [[character objectAtIndex:j]longLongValue];

            new_odd_no = new_odd_no+odd_no;
        }
        else 
        {     
            //------doubling the value of even no's--------
            even_no = [[character objectAtIndex:j]longLongValue];
            multiplied_even_no=even_no*2;

            NSLog(@"%qu",multiplied_even_no);

            //-------if even is a single digit--------
            if((multiplied_even_no % 10) == 0)
            {
                if(multiplied_even_no == 10)
                {
                    new_even_no = 1+new_even_no;
                }
                else 
                {
                    new_even_no = multiplied_even_no + new_even_no;
                }
            }
            //----------if there is multiple digits in even no---------
            else 
            {
                int x=(multiplied_even_no % 10);
                int y=multiplied_even_no /10;

                changed_even_no = x+y;

                new_even_no = new_even_no + changed_even_no;

            } 
        }
    }

    //--------calculating final value--------
    final_value = new_even_no + new_odd_no;

    NSLog(@"%qu",final_value);


    if((final_value % 10) == 0)
    {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Card No is valid" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

        [alert show];
        card_textField.text=nil;
    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Card No is not valid" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Try again" ,nil];

        [alert show];
        card_textField.text=nil;
    }

}