Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 16 digit Diners Club cards #1498

Merged
merged 2 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Stripe/STPBINRange.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ @implementation STPBINRange
@[@"37", @"37", @15, @(STPCardBrandAmex)],

// Diners Club
@[@"30", @"30", @14, @(STPCardBrandDinersClub)],
@[@"30", @"30", @16, @(STPCardBrandDinersClub)],
@[@"36", @"36", @14, @(STPCardBrandDinersClub)],
@[@"38", @"39", @14, @(STPCardBrandDinersClub)],
@[@"38", @"39", @16, @(STPCardBrandDinersClub)],

// Discover
@[@"60", @"60", @16, @(STPCardBrandDiscover)],
Expand Down
1 change: 1 addition & 0 deletions Stripe/STPCardValidator+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface STPCardValidator (Private)

+ (NSArray<NSNumber *> *)cardNumberFormatForBrand:(STPCardBrand)brand;
+ (NSArray<NSNumber *> *)cardNumberFormatForCardNumber:(NSString *)cardNumber;

@end

Expand Down
13 changes: 11 additions & 2 deletions Stripe/STPCardValidator+Private.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "STPCardValidator+Private.h"
#import "STPBINRange.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -17,13 +18,21 @@ @implementation STPCardValidator (Private)
switch (brand) {
case STPCardBrandAmex:
return @[@4, @6, @5];
case STPCardBrandDinersClub:
return @[@4, @6, @4];
default:
return @[@4, @4, @4, @4];
}
}

+ (NSArray<NSNumber *> *)cardNumberFormatForCardNumber:(NSString *)cardNumber
{
STPBINRange *binRange = [STPBINRange mostSpecificBINRangeForNumber:cardNumber];
if (binRange.brand == STPCardBrandDinersClub && binRange.length == 14) {
return @[@4, @6, @4];
}

return [self cardNumberFormatForBrand:binRange.brand];
}

@end

NS_ASSUME_NONNULL_END
Expand Down
3 changes: 1 addition & 2 deletions Stripe/STPFormTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ - (void)setAutoFormattingBehavior:(STPFormTextFieldAutoFormattingBehavior)autoFo
return [inputString copy];
}
NSMutableAttributedString *attributedString = [inputString mutableCopy];
STPCardBrand currentBrand = [STPCardValidator brandForNumber:attributedString.string];
NSArray<NSNumber *> *cardNumberFormat = [STPCardValidator cardNumberFormatForBrand:currentBrand];
NSArray<NSNumber *> *cardNumberFormat = [STPCardValidator cardNumberFormatForCardNumber:attributedString.string];

NSUInteger index = 0;
for (NSNumber *segmentLength in cardNumberFormat) {
Expand Down
2 changes: 1 addition & 1 deletion Stripe/STPPaymentCardTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ - (CGFloat)numberFieldCompressedWidth {
}

STPCardBrand currentBrand = [STPCardValidator brandForNumber:cardNumber];
NSArray<NSNumber *> *sortedCardNumberFormat = [[STPCardValidator cardNumberFormatForBrand:currentBrand] sortedArrayUsingSelector:@selector(unsignedIntegerValue)];
NSArray<NSNumber *> *sortedCardNumberFormat = [[STPCardValidator cardNumberFormatForCardNumber:cardNumber] sortedArrayUsingSelector:@selector(unsignedIntegerValue)];
NSUInteger fragmentLength = [STPCardValidator fragmentLengthForCardBrand:currentBrand];
NSUInteger maxLength = MAX([[sortedCardNumberFormat lastObject] unsignedIntegerValue], fragmentLength);

Expand Down
2 changes: 1 addition & 1 deletion Stripe/STPPaymentCardTextFieldViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ - (NSString *)compressedCardNumber {
}
} else {
// use the card number format
NSArray<NSNumber *> *cardNumberFormat = [STPCardValidator cardNumberFormatForBrand:currentBrand];
NSArray<NSNumber *> *cardNumberFormat = [STPCardValidator cardNumberFormatForCardNumber:cardNumber];

NSUInteger index = 0;
for (NSNumber *segment in cardNumberFormat) {
Expand Down
6 changes: 3 additions & 3 deletions Tests/Tests/STPCardValidatorTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ + (NSArray *)cardData {
@[@(STPCardBrandAmex), @"371449635398431", @(STPCardValidationStateValid)],
@[@(STPCardBrandDiscover), @"6011111111111117", @(STPCardValidationStateValid)],
@[@(STPCardBrandDiscover), @"6011000990139424", @(STPCardValidationStateValid)],
@[@(STPCardBrandDinersClub), @"30569309025904", @(STPCardValidationStateValid)],
@[@(STPCardBrandDinersClub), @"38520000023237", @(STPCardValidationStateValid)],
@[@(STPCardBrandDinersClub), @"36227206271667", @(STPCardValidationStateValid)],
@[@(STPCardBrandDinersClub), @"3056930009020004", @(STPCardValidationStateValid)],
@[@(STPCardBrandJCB), @"3530111333300000", @(STPCardValidationStateValid)],
@[@(STPCardBrandJCB), @"3566002020360505", @(STPCardValidationStateValid)],
@[@(STPCardBrandUnknown), @"1234567812345678", @(STPCardValidationStateInvalid)],
Expand Down Expand Up @@ -132,7 +132,7 @@ - (void)testLengthsForCardBrand {
@[@(STPCardBrandMasterCard), @[@16]],
@[@(STPCardBrandAmex), @[@15]],
@[@(STPCardBrandDiscover), @[@16]],
@[@(STPCardBrandDinersClub), @[@14]],
@[@(STPCardBrandDinersClub), @[@14, @16]],
@[@(STPCardBrandJCB), @[@16]],
@[@(STPCardBrandUnionPay), @[@16]],
@[@(STPCardBrandUnknown), @[@16]],
Expand Down
16 changes: 8 additions & 8 deletions Tests/Tests/STPPaymentCardTextFieldViewModelTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ - (void)testCompressedCardNumber {
self.viewModel.cardNumber = @"12";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"12");

self.viewModel.cardNumber = @"30569309025904";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"5904");
self.viewModel.cardNumber = @"3056930902590";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"590");
self.viewModel.cardNumber = @"30569309025";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"5");
self.viewModel.cardNumber = @"3056930902";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"930902");
self.viewModel.cardNumber = @"36227206271667";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"1667");
self.viewModel.cardNumber = @"3622720627166";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"166");
self.viewModel.cardNumber = @"36227206271";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"1");
self.viewModel.cardNumber = @"3622720627";
XCTAssertEqualObjects(self.viewModel.compressedCardNumber, @"720627");
}

@end