detectCardProvider method

void detectCardProvider()

Iterates over the list _providers, detecting which provider the current _cardNumber falls under.

Implementation

void detectCardProvider() {
  bool found = false;
  if (_cardNumber == null) {
    return;
  }
  for (var cardPvd in _providers) {
    if (cardPvd.innValidNums != null) {
      // trim card number to correct length
      String trimmedNum = _cardNumber!;
      String innNumStr = '${cardPvd.innValidNums!.first}';
      if (trimmedNum.length > innNumStr.length) {
        trimmedNum = trimmedNum.substring(0, innNumStr.length);
      }
      final num = int.tryParse(trimmedNum);
      if (num == null) continue;

      if (cardPvd.innValidNums!.contains(num)) {
        provider = cardPvd;
        found = true;
        break;
      }
    }
    if (cardPvd.innValidRanges != null) {
      // trim card number to correct length
      String trimmedNum = _cardNumber!;
      String innNumStr = '${cardPvd.innValidRanges!.first.low}';
      if (trimmedNum.length > innNumStr.length) {
        trimmedNum = trimmedNum.substring(0, innNumStr.length);
      }
      final num = int.tryParse(trimmedNum);
      if (num == null) continue;

      if (cardPvd.innValidRanges!.any((range) => range.isWithin(num))) {
        provider = cardPvd;
        found = true;
        break;
      }
    }
  }
  if (!found) provider = null;
}