2023-11-14 09:58:49 -07:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Class encapsulating the card's data
|
|
|
|
/// as well as validation of the data.
|
|
|
|
///
|
|
|
|
/// `CardDetails.validState == ValidState.ok`
|
|
|
|
/// when fields are filled and validated as correct.
|
2023-11-14 09:58:49 -07:00
|
|
|
class CardDetails {
|
2023-11-14 15:09:03 -07:00
|
|
|
CardDetails({
|
2023-11-17 16:17:28 -07:00
|
|
|
required String? cardNumber,
|
|
|
|
required this.securityCode,
|
2023-11-14 15:09:03 -07:00
|
|
|
required this.expirationString,
|
|
|
|
required this.postalCode,
|
|
|
|
}) : _cardNumber = cardNumber {
|
2023-11-14 09:58:49 -07:00
|
|
|
checkIsValid();
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Sets every field to null, a default
|
|
|
|
/// `CardDetails` when nothing has been entered.
|
2023-11-14 09:58:49 -07:00
|
|
|
factory CardDetails.blank() {
|
2023-11-14 15:09:03 -07:00
|
|
|
return CardDetails(cardNumber: null, securityCode: null, expirationString: null, postalCode: null);
|
2023-11-14 09:58:49 -07:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Returns the CardNumber as a `String` with the spaces removed.
|
2023-11-14 15:09:03 -07:00
|
|
|
String? get cardNumber => _cardNumber?.replaceAll(' ', '');
|
|
|
|
|
|
|
|
set cardNumber(String? num) => _cardNumber = num;
|
|
|
|
|
|
|
|
String? _cardNumber;
|
2023-11-17 16:17:28 -07:00
|
|
|
String? securityCode;
|
2023-11-14 09:58:49 -07:00
|
|
|
String? postalCode;
|
2023-11-14 15:09:03 -07:00
|
|
|
String? expirationString;
|
|
|
|
DateTime? expirationDate;
|
2023-11-14 09:58:49 -07:00
|
|
|
bool _complete = false;
|
|
|
|
ValidState _validState = ValidState.blank;
|
|
|
|
int _lastCheckHash = 0;
|
|
|
|
CardProvider? provider;
|
|
|
|
|
2023-11-17 16:17:28 -07:00
|
|
|
set overrideValidState(ValidState state) => _validState = state;
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Checks the validity of the `CardDetails` and returns the result.
|
2023-11-14 09:58:49 -07:00
|
|
|
ValidState get validState {
|
|
|
|
checkIsValid();
|
|
|
|
return _validState;
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:17:28 -07:00
|
|
|
String get expMonth => isComplete ? expirationString!.split('/').first : '';
|
|
|
|
String get expYear => isComplete ? expirationString!.split('/').last : '';
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
// TODO rename to be more clear
|
|
|
|
/// Returns true if `_cardNumber` is null, or
|
|
|
|
/// if the _cardNumber matches the detected `provider`'s
|
|
|
|
/// card lenght, defaulting to 16.
|
2023-11-14 15:09:03 -07:00
|
|
|
bool get cardNumberFilled =>
|
|
|
|
_cardNumber == null ? false : (provider?.cardLength ?? 16) == _cardNumber!.replaceAll(' ', '').length;
|
2023-11-14 09:58:49 -07:00
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Returns true if all details are complete and valid
|
|
|
|
/// otherwise, return false.
|
2023-11-14 09:58:49 -07:00
|
|
|
bool get isComplete {
|
|
|
|
checkIsValid();
|
|
|
|
return _complete;
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// The maximum length of the INN (identifier)
|
2023-11-14 17:34:11 -07:00
|
|
|
/// of a card provider.
|
2023-11-14 09:58:49 -07:00
|
|
|
int get maxINNLength => 4;
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Validates each field of the `CardDetails` object in entry order,
|
|
|
|
/// namely _cardNumber -> expirationString -> securityCode -> postalCode
|
|
|
|
///
|
|
|
|
/// If all fields are filled out and valid, `CardDetails.isComplete == true`
|
|
|
|
/// and `CardDetails.validState == ValidState.ok`.
|
2023-11-14 09:58:49 -07:00
|
|
|
void checkIsValid() {
|
|
|
|
try {
|
|
|
|
int currentHash = hash;
|
|
|
|
if (currentHash == _lastCheckHash) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastCheckHash = currentHash;
|
2023-11-14 15:09:03 -07:00
|
|
|
if (_cardNumber == null && expirationString == null && securityCode == null && postalCode == null) {
|
2023-11-14 09:58:49 -07:00
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.blank;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 15:09:03 -07:00
|
|
|
final nums = _cardNumber!
|
2023-11-14 09:58:49 -07:00
|
|
|
.replaceAll(' ', '')
|
|
|
|
.split('')
|
|
|
|
.map(
|
|
|
|
(i) => int.parse(i),
|
|
|
|
)
|
|
|
|
.toList();
|
2023-11-14 17:32:28 -07:00
|
|
|
if (!_luhnAlgorithmCheck(nums)) {
|
2023-11-14 09:58:49 -07:00
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.invalidCard;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 15:09:03 -07:00
|
|
|
if (_cardNumber == null || !cardNumberFilled) {
|
2023-11-14 09:58:49 -07:00
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.missingCard;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 15:09:03 -07:00
|
|
|
if (expirationString == null) {
|
2023-11-14 09:58:49 -07:00
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.missingDate;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 15:09:03 -07:00
|
|
|
final expSplits = expirationString!.split('/');
|
2023-11-14 09:58:49 -07:00
|
|
|
if (expSplits.length != 2 || expSplits.last == '') {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.missingDate;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 15:09:03 -07:00
|
|
|
final month = int.parse(expSplits.first[0] == '0' ? expSplits.first[1] : expSplits.first);
|
|
|
|
if (month < 1 || month > 12) {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.invalidMonth;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final year = 2000 + int.parse(expSplits.last);
|
|
|
|
final date = DateTime(year, month);
|
2023-11-14 09:58:49 -07:00
|
|
|
if (date.isBefore(DateTime.now())) {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.dateTooEarly;
|
|
|
|
return;
|
|
|
|
} else if (date.isAfter(DateTime.now().add(const Duration(days: 365 * 50)))) {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.dateTooLate;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 15:09:03 -07:00
|
|
|
expirationDate = date;
|
2023-11-14 09:58:49 -07:00
|
|
|
if (securityCode == null) {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.missingCVC;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-17 16:17:28 -07:00
|
|
|
if (provider != null && securityCode!.length != provider!.cvcLength) {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.invalidCVC;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 09:58:49 -07:00
|
|
|
if (postalCode == null) {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.missingZip;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!RegExp(r'^\d{5}(-\d{4})?$').hasMatch(postalCode!)) {
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.invalidZip;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_complete = true;
|
|
|
|
_validState = ValidState.ok;
|
|
|
|
} catch (err, st) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print('Error while validating CardDetails: $err\n$st');
|
|
|
|
}
|
|
|
|
_complete = false;
|
|
|
|
_validState = ValidState.error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Provides a hash of the CardDetails object
|
|
|
|
/// Hashes `_cardNumber`, `expirationString`,
|
|
|
|
/// `securityCode`, and `postalCode`.
|
2023-11-14 09:58:49 -07:00
|
|
|
int get hash {
|
2023-11-14 15:09:03 -07:00
|
|
|
return Object.hash(_cardNumber, expirationString, securityCode, postalCode);
|
2023-11-14 09:58:49 -07:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Iterates over the list `_providers`, detecting which
|
|
|
|
/// provider the current `_cardNumber` falls under.
|
2023-11-14 09:58:49 -07:00
|
|
|
void detectCardProvider() {
|
|
|
|
bool found = false;
|
2023-11-14 15:09:03 -07:00
|
|
|
if (_cardNumber == null) {
|
2023-11-14 09:58:49 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-11-14 17:32:28 -07:00
|
|
|
for (var cardPvd in _providers) {
|
2023-11-14 15:09:03 -07:00
|
|
|
if (cardPvd.innValidNums != null) {
|
2023-11-14 09:58:49 -07:00
|
|
|
// trim card number to correct length
|
2023-11-14 15:09:03 -07:00
|
|
|
String trimmedNum = _cardNumber!;
|
|
|
|
String innNumStr = '${cardPvd.innValidNums!.first}';
|
2023-11-14 09:58:49 -07:00
|
|
|
if (trimmedNum.length > innNumStr.length) {
|
|
|
|
trimmedNum = trimmedNum.substring(0, innNumStr.length);
|
|
|
|
}
|
|
|
|
final num = int.tryParse(trimmedNum);
|
|
|
|
if (num == null) continue;
|
|
|
|
|
2023-11-14 15:09:03 -07:00
|
|
|
if (cardPvd.innValidNums!.contains(num)) {
|
2023-11-14 09:58:49 -07:00
|
|
|
provider = cardPvd;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-11-14 15:09:03 -07:00
|
|
|
if (cardPvd.innValidRanges != null) {
|
2023-11-14 09:58:49 -07:00
|
|
|
// trim card number to correct length
|
2023-11-14 15:09:03 -07:00
|
|
|
String trimmedNum = _cardNumber!;
|
|
|
|
String innNumStr = '${cardPvd.innValidRanges!.first.low}';
|
2023-11-14 09:58:49 -07:00
|
|
|
if (trimmedNum.length > innNumStr.length) {
|
|
|
|
trimmedNum = trimmedNum.substring(0, innNumStr.length);
|
|
|
|
}
|
|
|
|
final num = int.tryParse(trimmedNum);
|
|
|
|
if (num == null) continue;
|
|
|
|
|
2023-11-14 15:09:03 -07:00
|
|
|
if (cardPvd.innValidRanges!.any((range) => range.isWithin(num))) {
|
2023-11-14 09:58:49 -07:00
|
|
|
provider = cardPvd;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) provider = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-14 15:09:03 -07:00
|
|
|
String toString() {
|
|
|
|
return 'Number: "$_cardNumber" - Exp: "$expirationString" CVC: $securityCode Zip: "$postalCode"';
|
|
|
|
}
|
2023-11-14 17:32:28 -07:00
|
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Luhn_algorithm
|
|
|
|
/// The Luhn algorithm is used in industry to check
|
|
|
|
/// for valid credit / debit card numbers
|
|
|
|
///
|
|
|
|
/// The algorithm adds together all the numbers, every
|
|
|
|
/// other number is doubled, then the sum is checked to
|
|
|
|
/// see if it is a multiple of 10.
|
|
|
|
/// https://en.wikipedia.org/wiki/Luhn_algorithm
|
|
|
|
bool _luhnAlgorithmCheck(List<int> digits) {
|
|
|
|
int sum = 0;
|
|
|
|
bool isSecond = false;
|
|
|
|
for (int i = digits.length - 1; i >= 0; i--) {
|
|
|
|
int d = digits[i];
|
|
|
|
if (isSecond) {
|
|
|
|
d *= 2;
|
|
|
|
|
|
|
|
if (d > 9) {
|
|
|
|
d -= 9;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sum += d;
|
|
|
|
isSecond = !isSecond;
|
|
|
|
}
|
|
|
|
return (sum % 10) == 0;
|
|
|
|
}
|
2023-11-14 09:58:49 -07:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Enum of validation states a `CardDetails` object can have.
|
2023-11-14 09:58:49 -07:00
|
|
|
enum ValidState {
|
|
|
|
ok,
|
|
|
|
error,
|
|
|
|
blank,
|
|
|
|
missingCard,
|
|
|
|
invalidCard,
|
|
|
|
missingDate,
|
2023-11-14 15:09:03 -07:00
|
|
|
invalidMonth,
|
2023-11-14 09:58:49 -07:00
|
|
|
dateTooEarly,
|
|
|
|
dateTooLate,
|
|
|
|
missingCVC,
|
|
|
|
invalidCVC,
|
|
|
|
missingZip,
|
|
|
|
invalidZip,
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Enum of supported U.S. Card Providers
|
2023-11-14 09:58:49 -07:00
|
|
|
enum CardProviderID {
|
2023-11-14 15:09:03 -07:00
|
|
|
americanExpress,
|
|
|
|
dinersClub,
|
|
|
|
discoverCard,
|
|
|
|
mastercard,
|
|
|
|
jcb,
|
|
|
|
visa,
|
2023-11-14 09:58:49 -07:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Encapsulates criteria for Card Providers in the U.S.
|
|
|
|
/// Used by `CardDetails.detectCardProvider()` to determine
|
2023-11-14 17:34:11 -07:00
|
|
|
/// a card's Provider.
|
2023-11-14 09:58:49 -07:00
|
|
|
class CardProvider {
|
|
|
|
CardProviderID id;
|
2023-11-14 15:09:03 -07:00
|
|
|
List<int>? innValidNums;
|
2023-11-14 17:34:11 -07:00
|
|
|
List<Range>? innValidRanges;
|
2023-11-14 09:58:49 -07:00
|
|
|
int cardLength;
|
|
|
|
int cvcLength;
|
|
|
|
|
|
|
|
CardProvider(
|
2023-11-14 15:09:03 -07:00
|
|
|
{required this.id, required this.cardLength, required this.cvcLength, this.innValidNums, this.innValidRanges}) {
|
2023-11-14 09:58:49 -07:00
|
|
|
// Must provide one or the other
|
2023-11-14 15:09:03 -07:00
|
|
|
assert(innValidNums != null || innValidRanges != null);
|
2023-11-14 09:58:49 -07:00
|
|
|
// Do not provide empty list of valid nums
|
2023-11-14 15:09:03 -07:00
|
|
|
assert(innValidNums == null || innValidNums!.isNotEmpty);
|
2023-11-14 09:58:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return id.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Object for `CardProvider` to determine valid number ranges.
|
|
|
|
/// A loose wrapper on a tuple, that provides assertion of
|
2023-11-14 17:34:11 -07:00
|
|
|
/// valid inputs and the `isWithin()` helper function.
|
|
|
|
class Range {
|
2023-11-14 09:58:49 -07:00
|
|
|
int high;
|
|
|
|
int low;
|
|
|
|
|
2023-11-14 17:34:11 -07:00
|
|
|
Range({required this.low, required this.high}) {
|
2023-11-14 09:58:49 -07:00
|
|
|
assert(low <= high);
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// Returns bool whether or not `val` is between `low` and `high`.
|
|
|
|
/// The range includes the `val`, so
|
|
|
|
/// ```dart
|
|
|
|
/// Range(low: 1, high: 3).isWithin(3);
|
|
|
|
/// ```
|
|
|
|
/// would return true.
|
2023-11-14 09:58:49 -07:00
|
|
|
bool isWithin(int val) {
|
|
|
|
return low <= val && val <= high;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:32:28 -07:00
|
|
|
/// List of CardProviders for US-based Credit / Debit Cards.
|
|
|
|
List<CardProvider> _providers = [
|
2023-11-14 09:58:49 -07:00
|
|
|
CardProvider(
|
2023-11-14 15:09:03 -07:00
|
|
|
id: CardProviderID.americanExpress,
|
2023-11-14 09:58:49 -07:00
|
|
|
cardLength: 15,
|
|
|
|
cvcLength: 4,
|
2023-11-14 15:09:03 -07:00
|
|
|
innValidNums: [34, 37],
|
2023-11-14 09:58:49 -07:00
|
|
|
),
|
|
|
|
CardProvider(
|
2023-11-14 15:09:03 -07:00
|
|
|
id: CardProviderID.dinersClub,
|
2023-11-14 09:58:49 -07:00
|
|
|
cardLength: 16,
|
|
|
|
cvcLength: 3,
|
2023-11-14 15:09:03 -07:00
|
|
|
innValidNums: [30, 36, 38, 39],
|
2023-11-14 09:58:49 -07:00
|
|
|
),
|
|
|
|
CardProvider(
|
2023-11-14 15:09:03 -07:00
|
|
|
id: CardProviderID.discoverCard,
|
2023-11-14 09:58:49 -07:00
|
|
|
cardLength: 16,
|
|
|
|
cvcLength: 3,
|
2023-11-14 15:09:03 -07:00
|
|
|
innValidNums: [60, 65],
|
2023-11-14 17:34:11 -07:00
|
|
|
innValidRanges: [Range(low: 644, high: 649)],
|
2023-11-14 09:58:49 -07:00
|
|
|
),
|
|
|
|
CardProvider(
|
2023-11-14 15:09:03 -07:00
|
|
|
id: CardProviderID.jcb,
|
2023-11-14 09:58:49 -07:00
|
|
|
cardLength: 16,
|
|
|
|
cvcLength: 3,
|
2023-11-14 15:09:03 -07:00
|
|
|
innValidNums: [35],
|
2023-11-14 09:58:49 -07:00
|
|
|
),
|
|
|
|
CardProvider(
|
2023-11-14 15:09:03 -07:00
|
|
|
id: CardProviderID.mastercard,
|
2023-11-14 09:58:49 -07:00
|
|
|
cardLength: 16,
|
|
|
|
cvcLength: 3,
|
2023-11-14 17:34:11 -07:00
|
|
|
innValidRanges: [Range(low: 22, high: 27), Range(low: 51, high: 55)],
|
2023-11-14 09:58:49 -07:00
|
|
|
),
|
|
|
|
CardProvider(
|
2023-11-14 15:09:03 -07:00
|
|
|
id: CardProviderID.visa,
|
2023-11-14 09:58:49 -07:00
|
|
|
cardLength: 16,
|
|
|
|
cvcLength: 3,
|
2023-11-14 15:09:03 -07:00
|
|
|
innValidNums: [4],
|
2023-11-14 09:58:49 -07:00
|
|
|
)
|
|
|
|
];
|