Created
June 21, 2022 20:07
-
-
Save diefferson/fc05fc4158d251c3e7a8c6eb363499bd to your computer and use it in GitHub Desktop.
Validate Pix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CNPJ { | |
// Formatar número de CNPJ | |
static String? format(String? cnpj) { | |
if (cnpj == null) return null; | |
// Obter somente os números do CNPJ | |
final numbers = cnpj.replaceAll(RegExp(r'[^0-9]'), ''); | |
// Testar se o CNPJ possui 14 dígitos | |
if (numbers.length != 14) return cnpj; | |
// Retornar CPF formatado | |
return '${numbers.substring(0, 2)}.${numbers.substring(2, 5)}.${numbers.substring(5, 8)}/${numbers.substring(8, 12)}-${numbers.substring(12)}'; | |
} | |
static String? maskedPix(String? cnpj) { | |
if (cnpj == null) return null; | |
// Obter somente os números do CNPJ | |
final numbers = cnpj.replaceAll(RegExp(r'[^0-9]'), ''); | |
// Testar se o CNPJ possui 14 dígitos | |
if (numbers.length != 14) return cnpj; | |
// Retornar CPF formatado | |
return '**.${numbers.substring(2, 5)}.${numbers.substring(5, 8)}/${numbers.substring(8, 12)}-**'; | |
} | |
// Validar número de CNPJ | |
static bool isValid(String? cnpj) { | |
if (cnpj == null) return false; | |
// Obter somente os números do CNPJ | |
final numbers = cnpj.replaceAll(RegExp(r'[^0-9]'), ''); | |
// Testar se o CNPJ possui 14 dígitos | |
if (numbers.length != 14) return false; | |
// Testar se todos os dígitos do CNPJ são iguais | |
if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) return false; | |
// Dividir dígitos | |
final List<int> digits = | |
numbers.split('').map((String d) => int.parse(d)).toList(); | |
// Calcular o primeiro dígito verificador | |
var calcDv1 = 0; | |
var j = 0; | |
for (var i in Iterable<int>.generate(12, (i) => i < 4 ? 5 - i : 13 - i)) { | |
calcDv1 += digits[j++] * i; | |
} | |
calcDv1 %= 11; | |
final dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; | |
// Testar o primeiro dígito verificado | |
if (digits[12] != dv1) return false; | |
// Calcular o segundo dígito verificador | |
var calcDv2 = 0; | |
j = 0; | |
for (var i in Iterable<int>.generate(13, (i) => i < 5 ? 6 - i : 14 - i)) { | |
calcDv2 += digits[j++] * i; | |
} | |
calcDv2 %= 11; | |
final dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; | |
// Testar o segundo dígito verificador | |
if (digits[13] != dv2) return false; | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CPF { | |
// Formatar número de CPF | |
static String? format(String? cpf) { | |
if (cpf == null) return null; | |
// Obter somente os números do CPF | |
final numbers = cpf.replaceAll(RegExp(r'[^0-9]'), ''); | |
// Testar se o CPF possui 11 dígitos | |
if (numbers.length != 11) return cpf; | |
// Retornar CPF formatado | |
return '${numbers.substring(0, 3)}.${numbers.substring(3, 6)}.${numbers.substring(6, 9)}-${numbers.substring(9)}'; | |
} | |
static String? maskedPix(String? cpf) { | |
if (cpf == null) return null; | |
// Obter somente os números do CPF | |
final numbers = cpf.replaceAll(RegExp(r'[^0-9]'), ''); | |
// Testar se o CPF possui 11 dígitos | |
if (numbers.length != 11) return cpf; | |
// Retornar CPF formatado | |
return '***.${numbers.substring(3, 6)}.${numbers.substring(6, 9)}-**'; | |
} | |
// Validar número de CPF | |
static bool isValid(String? cpf) { | |
if (cpf == null) return false; | |
// Obter somente os números do CPF | |
final numbers = cpf.replaceAll(RegExp(r'[^0-9]'), ''); | |
// Testar se o CPF possui 11 dígitos | |
if (numbers.length != 11) return false; | |
// Testar se todos os dígitos do CPF são iguais | |
if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) return false; | |
// Dividir dígitos | |
final List<int> digits = | |
numbers.split('').map((String d) => int.parse(d)).toList(); | |
// Calcular o primeiro dígito verificador | |
var calcDv1 = 0; | |
for (var i in Iterable<int>.generate(9, (i) => 10 - i)) { | |
calcDv1 += digits[10 - i] * i; | |
} | |
calcDv1 %= 11; | |
final dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; | |
// Testar o primeiro dígito verificado | |
if (digits[9] != dv1) return false; | |
// Calcular o segundo dígito verificador | |
var calcDv2 = 0; | |
for (var i in Iterable<int>.generate(10, (i) => 11 - i)) { | |
calcDv2 += digits[11 - i] * i; | |
} | |
calcDv2 %= 11; | |
final dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; | |
// Testar o segundo dígito verificador | |
if (digits[10] != dv2) return false; | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool validatePixKey(String? key) { | |
if (validateCpfOrCnpj(key)) { | |
return true; | |
} | |
if (validatePhone(key)) { | |
return true; | |
} | |
if (validateEmail(key)) { | |
return true; | |
} | |
if (validateEvpKey(key)) { | |
return true; | |
} | |
return false; | |
} | |
bool validatePhone(String? phone) { | |
if (phone != null) { | |
final value = stringToCellPhone(phone); | |
final ddiRegex = RegExp(r'^(?:\+?)\d{2}?\s?\(?\d{2}\)?[\s-]?\d{5}-?\d{4}$'); | |
final dddRegex = RegExp(r'^\(?\d{2}\)?[\s-]?\d{5}-?\d{4}$'); | |
return ddiRegex.hasMatch(value) || dddRegex.hasMatch(value); | |
} | |
return false; | |
} | |
bool validateEmail(String? email) { | |
if (email != null) { | |
return RegExp(r'^[\w-.+]+@([\w-]+\.)+[\w-.]+$').hasMatch(email); | |
} | |
return false; | |
} | |
bool validateEvpKey(String? key) { | |
if (key != null) { | |
if (key.length == 36) { | |
return true; | |
} | |
if (key.length == 32 && !key.contains('@')) { | |
return true; | |
} | |
} | |
return false; | |
} | |
bool validateCpf(String? cpf) { | |
return CPF.isValid(cpf); | |
} | |
bool validateCnpj(String? cnpj) { | |
return CNPJ.isValid(cnpj); | |
} | |
bool validateCpfOrCnpj(String? value) { | |
if (validateCpf(value)) { | |
return true; | |
} | |
return validateCnpj(value); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment