Fix the card validator.

This commit is contained in:
John Preston 2023-02-21 17:10:12 +04:00
parent b8c510ca6d
commit c327fa0d45
1 changed files with 21 additions and 6 deletions

View File

@ -193,13 +193,28 @@ CardValidationResult ValidateCard(const QString &number) {
}
const auto range = MostSpecificBinRangeForNumber(sanitized);
const auto brand = range.brand;
//if (sanitized.size() > range.length) {
// return { .state = ValidationState::Invalid, .brand = brand };
//} else if (sanitized.size() < range.length) {
// return { .state = ValidationState::Incomplete, .brand = brand };
//} else
if (!IsValidLuhn(sanitized)) {
static const auto &all = AllRanges();
static const auto compare = [](const BinRange &a, const BinRange &b) {
return a.length < b.length;
};
static const auto kMinLength = std::min_element(
begin(all),
end(all),
compare)->length;
static const auto kMaxLength = std::max_element(
begin(all),
end(all),
compare)->length;
if (sanitized.size() > kMaxLength) {
return { .state = ValidationState::Invalid, .brand = brand };
} else if (sanitized.size() < kMinLength) {
return { .state = ValidationState::Incomplete, .brand = brand };
} else if (!IsValidLuhn(sanitized)) {
return { .state = ValidationState::Invalid, .brand = brand };
} else if (sanitized.size() < kMaxLength) {
return { .state = ValidationState::Valid, .brand = brand };
}
return {
.state = ValidationState::Valid,