Update card validation

This commit is contained in:
Ali 2023-02-07 23:14:09 +04:00
parent 524e8f1e81
commit 5c71c08b6e
5 changed files with 24 additions and 9 deletions

View File

@ -79,7 +79,7 @@ NS_ASSUME_NONNULL_BEGIN
* @return STPCardValidationStateValid if the year is valid, STPCardValidationStateInvalid if the year is invalid, or STPCardValidationStateIncomplete if the year is a substring of a valid year (e.g. @"1" or @"2"). * @return STPCardValidationStateValid if the year is valid, STPCardValidationStateInvalid if the year is invalid, or STPCardValidationStateIncomplete if the year is a substring of a valid year (e.g. @"1" or @"2").
*/ */
+ (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear + (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear
inMonth:(NSString *)expirationMonth; inMonth:(NSString *)expirationMonth cardBrand:(STPCardBrand)cardBrand;
/** /**
* The max CVC length for a card brand (for context, American Express CVCs are 4 digits, while all others are 3). * The max CVC length for a card brand (for context, American Express CVCs are 4 digits, while all others are 3).
@ -109,7 +109,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear + (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear
inMonth:(NSString *)expirationMonth inMonth:(NSString *)expirationMonth
inCurrentYear:(NSInteger)currentYear inCurrentYear:(NSInteger)currentYear
currentMonth:(NSInteger)currentMonth; currentMonth:(NSInteger)currentMonth cardBrand:(STPCardBrand)cardBrand;
+ (STPCardValidationState)validationStateForCard:(STPCardParams *)card + (STPCardValidationState)validationStateForCard:(STPCardParams *)card
inCurrentYear:(NSInteger)currentYear inCurrentYear:(NSInteger)currentYear
currentMonth:(NSInteger)currentMonth; currentMonth:(NSInteger)currentMonth;

View File

@ -66,7 +66,8 @@
@[@"492937", @"492937", @13, @(STPCardBrandVisa)], @[@"492937", @"492937", @13, @(STPCardBrandVisa)],
@[@"492939", @"492939", @13, @(STPCardBrandVisa)], @[@"492939", @"492939", @13, @(STPCardBrandVisa)],
@[@"492960", @"492960", @13, @(STPCardBrandVisa)], @[@"492960", @"492960", @13, @(STPCardBrandVisa)],
@[@"2", @"2", @16, @(STPCardBrandOther)], @[@"8600", @"8600", @16, @(STPCardBrandOther)],
@[@"9860", @"9860", @16, @(STPCardBrandOther)],
]; ];
NSMutableArray *binRanges = [NSMutableArray array]; NSMutableArray *binRanges = [NSMutableArray array];
for (NSArray *range in ranges) { for (NSArray *range in ranges) {

View File

@ -101,7 +101,7 @@
NSString *ioValueString = [(NSString *)*ioValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSString *ioValueString = [(NSString *)*ioValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *monthString = [@(self.expMonth) stringValue]; NSString *monthString = [@(self.expMonth) stringValue];
if ([STPCardValidator validationStateForExpirationYear:ioValueString inMonth:monthString] != STPCardValidationStateValid) { if ([STPCardValidator validationStateForExpirationYear:ioValueString inMonth:monthString cardBrand:[STPCardValidator brandForNumber:self.number]] != STPCardValidationStateValid) {
return [self.class handleValidationErrorForParameter:@"expYear" error:outError]; return [self.class handleValidationErrorForParameter:@"expYear" error:outError];
} }
return YES; return YES;

View File

@ -75,7 +75,7 @@ static NSString * _Nonnull stringByRemovingCharactersFromSet(NSString * _Nonnull
} }
} }
+ (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear inMonth:(NSString *)expirationMonth inCurrentYear:(NSInteger)currentYear currentMonth:(NSInteger)currentMonth { + (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear inMonth:(NSString *)expirationMonth inCurrentYear:(NSInteger)currentYear currentMonth:(NSInteger)currentMonth cardBrand:(STPCardBrand)cardBrand {
NSInteger moddedYear = currentYear % 100; NSInteger moddedYear = currentYear % 100;
@ -91,6 +91,15 @@ static NSString * _Nonnull stringByRemovingCharactersFromSet(NSString * _Nonnull
case 1: case 1:
return STPCardValidationStateIncomplete; return STPCardValidationStateIncomplete;
case 2: { case 2: {
switch (cardBrand) {
case STPCardBrandVisa:
case STPCardBrandMasterCard:
case STPCardBrandOther:
return STPCardValidationStateValid;
default:
break;
}
if (sanitizedYear.integerValue == moddedYear) { if (sanitizedYear.integerValue == moddedYear) {
return sanitizedMonth.integerValue >= currentMonth ? STPCardValidationStateValid : STPCardValidationStateInvalid; return sanitizedMonth.integerValue >= currentMonth ? STPCardValidationStateValid : STPCardValidationStateInvalid;
} else { } else {
@ -104,11 +113,11 @@ static NSString * _Nonnull stringByRemovingCharactersFromSet(NSString * _Nonnull
+ (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear + (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear
inMonth:(NSString *)expirationMonth { inMonth:(NSString *)expirationMonth cardBrand:(STPCardBrand)cardBrand {
return [self validationStateForExpirationYear:expirationYear return [self validationStateForExpirationYear:expirationYear
inMonth:expirationMonth inMonth:expirationMonth
inCurrentYear:[self currentYear] inCurrentYear:[self currentYear]
currentMonth:[self currentMonth]]; currentMonth:[self currentMonth] cardBrand:cardBrand];
} }
@ -165,7 +174,12 @@ static NSString * _Nonnull stringByRemovingCharactersFromSet(NSString * _Nonnull
STPCardValidationState expYearValidation = [self validationStateForExpirationYear:expYearString STPCardValidationState expYearValidation = [self validationStateForExpirationYear:expYearString
inMonth:expMonthString inMonth:expMonthString
inCurrentYear:currentYear inCurrentYear:currentYear
currentMonth:currentMonth]; currentMonth:currentMonth cardBrand:[STPCardValidator brandForNumber:card.number]];
if (expMonthValidation == STPCardValidationStateInvalid || expYearValidation == STPCardValidationStateInvalid) {
expMonthValidation = STPCardValidationStateValid;
expYearValidation = STPCardValidationStateValid;
}
STPCardBrand brand = [self brandForNumber:card.number]; STPCardBrand brand = [self brandForNumber:card.number];
STPCardValidationState cvcValidation = [self validationStateForCVC:card.cvc cardBrand:brand]; STPCardValidationState cvcValidation = [self validationStateForCVC:card.cvc cardBrand:brand];

View File

@ -67,7 +67,7 @@
break; break;
case STPCardFieldTypeExpiration: { case STPCardFieldTypeExpiration: {
STPCardValidationState monthState = [STPCardValidator validationStateForExpirationMonth:self.expirationMonth]; STPCardValidationState monthState = [STPCardValidator validationStateForExpirationMonth:self.expirationMonth];
STPCardValidationState yearState = [STPCardValidator validationStateForExpirationYear:self.expirationYear inMonth:self.expirationMonth]; STPCardValidationState yearState = [STPCardValidator validationStateForExpirationYear:self.expirationYear inMonth:self.expirationMonth cardBrand:[STPCardValidator brandForNumber:self.cardNumber]];
if (monthState == STPCardValidationStateValid && yearState == STPCardValidationStateValid) { if (monthState == STPCardValidationStateValid && yearState == STPCardValidationStateValid) {
return STPCardValidationStateValid; return STPCardValidationStateValid;
} else if (monthState == STPCardValidationStateInvalid || yearState == STPCardValidationStateInvalid) { } else if (monthState == STPCardValidationStateInvalid || yearState == STPCardValidationStateInvalid) {