Skip to content

Commit

Permalink
Update card validation
Browse files Browse the repository at this point in the history
(cherry picked from commit 5c71c08)
  • Loading branch information
Ali committed Feb 7, 2023
1 parent 81c84ea commit 5bb8089
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions submodules/Stripe/PublicHeaders/Stripe/STPCardValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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").
*/
+ (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).
Expand Down Expand Up @@ -109,7 +109,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear
inMonth:(NSString *)expirationMonth
inCurrentYear:(NSInteger)currentYear
currentMonth:(NSInteger)currentMonth;
currentMonth:(NSInteger)currentMonth cardBrand:(STPCardBrand)cardBrand;
+ (STPCardValidationState)validationStateForCard:(STPCardParams *)card
inCurrentYear:(NSInteger)currentYear
currentMonth:(NSInteger)currentMonth;
Expand Down
3 changes: 2 additions & 1 deletion submodules/Stripe/Sources/STPBINRange.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ @implementation STPBINRange
@[@"492937", @"492937", @13, @(STPCardBrandVisa)],
@[@"492939", @"492939", @13, @(STPCardBrandVisa)],
@[@"492960", @"492960", @13, @(STPCardBrandVisa)],
@[@"2", @"2", @16, @(STPCardBrandOther)],
@[@"8600", @"8600", @16, @(STPCardBrandOther)],
@[@"9860", @"9860", @16, @(STPCardBrandOther)],
];
NSMutableArray *binRanges = [NSMutableArray array];
for (NSArray *range in ranges) {
Expand Down
2 changes: 1 addition & 1 deletion submodules/Stripe/Sources/STPCardParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ - (BOOL)validateExpYear:(id *)ioValue error:(NSError **)outError {
NSString *ioValueString = [(NSString *)*ioValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

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 YES;
Expand Down
22 changes: 18 additions & 4 deletions submodules/Stripe/Sources/STPCardValidator.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ + (STPCardValidationState)validationStateForExpirationMonth:(NSString *)expirati
}
}

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

Expand All @@ -91,6 +91,15 @@ + (STPCardValidationState)validationStateForExpirationYear:(NSString *)expiratio
case 1:
return STPCardValidationStateIncomplete;
case 2: {
switch (cardBrand) {
case STPCardBrandVisa:
case STPCardBrandMasterCard:
case STPCardBrandOther:
return STPCardValidationStateValid;
default:
break;
}

if (sanitizedYear.integerValue == moddedYear) {
return sanitizedMonth.integerValue >= currentMonth ? STPCardValidationStateValid : STPCardValidationStateInvalid;
} else {
Expand All @@ -104,11 +113,11 @@ + (STPCardValidationState)validationStateForExpirationYear:(NSString *)expiratio


+ (STPCardValidationState)validationStateForExpirationYear:(NSString *)expirationYear
inMonth:(NSString *)expirationMonth {
inMonth:(NSString *)expirationMonth cardBrand:(STPCardBrand)cardBrand {
return [self validationStateForExpirationYear:expirationYear
inMonth:expirationMonth
inCurrentYear:[self currentYear]
currentMonth:[self currentMonth]];
currentMonth:[self currentMonth] cardBrand:cardBrand];
}


Expand Down Expand Up @@ -165,7 +174,12 @@ + (STPCardValidationState)validationStateForCard:(nonnull STPCardParams *)card i
STPCardValidationState expYearValidation = [self validationStateForExpirationYear:expYearString
inMonth:expMonthString
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];
STPCardValidationState cvcValidation = [self validationStateForCVC:card.cvc cardBrand:brand];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ - (STPCardValidationState)validationStateForField:(STPCardFieldType)fieldType {
break;
case STPCardFieldTypeExpiration: {
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) {
return STPCardValidationStateValid;
} else if (monthState == STPCardValidationStateInvalid || yearState == STPCardValidationStateInvalid) {
Expand Down

0 comments on commit 5bb8089

Please sign in to comment.