UNPKG

@starley/validacao-pix

Version:

Valida se a chavePix inserida tem um formato valido ou nao

252 lines 9.29 kB
import { Injectable } from "@angular/core"; import * as i0 from "@angular/core"; /** * Valixar chave pix - passando somente o valor da chave * @author Starley Cazorla */ var ValidacaoPix = /** @class */ (function () { function ValidacaoPix() { } /** * Validar cpf * @author Starley Cazorla * @param cpf * @returns true if cpf */ ValidacaoPix.prototype.validarCPF = function (cpf) { var soma = 0; var resto; if (cpf === '00000000000') { return false; } for (var i = 1; i <= 9; i++) { soma = soma + parseInt(cpf.substring(i - 1, i), 10) * (11 - i); } resto = (soma * 10) % 11; if (resto === 10 || resto === 11) { resto = 0; } if (resto !== parseInt(cpf.substring(9, 10), 10)) { return false; } soma = 0; for (var i = 1; i <= 10; i++) { soma = soma + parseInt(cpf.substring(i - 1, i), 10) * (12 - i); } resto = (soma * 10) % 11; if (resto === 10 || resto === 11) { resto = 0; } if (resto !== parseInt(cpf.substring(10, 11), 10)) { return false; } return true; }; /** * Validar cnpj * @author Starley Cazorla * @param cnpj * @returns true if cnpj */ ValidacaoPix.prototype.validarCNPJ = function (cnpj) { // Remover caracteres não numéricos cnpj = cnpj.replace(/[^\d]/g, ''); // Verificar se o CNPJ possui 14 dígitos if (cnpj.length !== 14) { return false; } // Verificar se todos os dígitos são iguais (CNPJ inválido) if (/^(\d)\1+$/.test(cnpj)) { return false; } // Calcular os dígitos verificadores var tamanho = cnpj.length - 2; var numeros = cnpj.substring(0, tamanho); var digitos = cnpj.substring(tamanho); var soma = 0; var pos = tamanho - 7; for (var i = tamanho; i >= 1; i--) { soma += parseInt(numeros.charAt(tamanho - i), 10) * pos--; if (pos < 2) { pos = 9; } } var resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11); // Verificar o primeiro dígito verificador if (resultado !== parseInt(digitos.charAt(0), 10)) { return false; } tamanho++; numeros = cnpj.substring(0, tamanho); soma = 0; pos = tamanho - 7; for (var i = tamanho; i >= 1; i--) { soma += parseInt(numeros.charAt(tamanho - i), 10) * pos--; if (pos < 2) { pos = 9; } } resultado = soma % 11 < 2 ? 0 : 11 - (soma % 11); // Verificar o segundo dígito verificador if (resultado !== parseInt(digitos.charAt(1), 10)) { return false; } // CNPJ válido return true; }; /** * Validar ddd de todos os estados brasileiros * @author Starley Cazorla * @param ddd * @returns true if ddd */ ValidacaoPix.prototype.validarDDD = function (ddd) { var dddsValidos = [ '11', '12', '13', '14', '15', '16', '17', '18', '19', '21', '22', '24', '27', '28', '31', '32', '33', '34', '35', '37', '38', '41', '42', '43', '44', '45', '46', '47', '48', '49', '51', '53', '54', '55', '61', '62', '64', '63', '65', '66', '67', '68', '69', '71', '73', '74', '75', '77', '79', '81', '87', '82', '83', '84', '85', '88', '86', '89', '91', '93', '94', '92', '97', '95', '96', '98', '99', // MA ]; return dddsValidos.includes(ddd); }; /** * Contems sequencia repetida * @author Starley Cazorla * @param numero * @returns true if sequencia repetida */ ValidacaoPix.prototype.contemSequenciaRepetida = function (numero) { var sequenciasRepetidas = [ '0000000000', '1111111111', '2222222222', '3333333333', '4444444444', '5555555555', '6666666666', '7777777777', '8888888888', '9999999999' ]; return sequenciasRepetidas.some(function (sequencia) { return numero.includes(sequencia); }); }; /** * Validar numero telefone * @author Starley Cazorla * @param telefone * @returns true if numero telefone */ ValidacaoPix.prototype.validarNumeroTelefone = function (telefone) { // Remover caracteres não numéricos telefone = telefone.replace(/\D/g, ''); if (this.contemSequenciaRepetida(telefone)) { return false; } // Verificar o formato do número de telefone fixo (10 dígitos) com DDD // const formatoFixo = /^\d{10}$/; // if (formatoFixo.test(telefone)) { // const ddd = telefone.substring(0, 2); // const numero = telefone.substring(2); // return this.validarDDD(ddd) && numero.length === 8; // } // Verificar o formato do número de telefone celular (11 dígitos) com DDD var formatoCelular = /^\d{11}$/; if (formatoCelular.test(telefone)) { var ddd = telefone.substring(0, 2); var numero = telefone.substring(2); return this.validarDDD(ddd) && numero.length === 9; } // Número de telefone inválido return false; }; /** * Validar chave pix * @author Starley Cazorla * @param inputPix * @returns {chavePix, tipoChave, isValid} */ ValidacaoPix.prototype.validarChavePIX = function (inputPix) { var chavePIX = inputPix; var regexCPF = /^\d{11}$/; var regexCNPJ = /^\d{14}$/; var regexCelular = /^\d{10,11}$/; var regexEmail = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{1,3}$/; var regexChaveAleatoria = /^[a-zA-Z0-9]{32}$/; var regexChaveEMV = /[bB][rR]\.[gG][oO][vV]\.[bB][cC][bB]\.[pP][iI][xX]/; var chaveSemMascara = chavePIX.replace(/\W/g, ''); var boraFazerUmPix = { chavePIX: '', tipoChave: '', isValid: false }; if (regexChaveEMV.test(chavePIX)) { // Chave é uma chave EMV válida boraFazerUmPix.chavePIX = chavePIX; boraFazerUmPix.tipoChave = 'EMV'; } else if ((regexCelular.test(chaveSemMascara) && this.validarNumeroTelefone(chaveSemMascara)) && !this.validarCPF(chaveSemMascara)) { // Chave é um número de celular válido boraFazerUmPix.chavePIX = "(".concat(chaveSemMascara.substr(0, 2), ") ").concat(chaveSemMascara.substr(2, 5), "-").concat(chaveSemMascara.substr(7)); boraFazerUmPix.tipoChave = 'CELULAR'; } else if (regexEmail.test(chavePIX)) { // Chave é um email válido boraFazerUmPix.chavePIX = chavePIX; boraFazerUmPix.tipoChave = 'EMAIL'; } else if (regexCPF.test(chaveSemMascara) && this.validarCPF(chaveSemMascara)) { // Chave é um CPF válido boraFazerUmPix.chavePIX = "".concat(chaveSemMascara.substr(0, 3), ".").concat(chaveSemMascara.substr(3, 3), ".").concat(chaveSemMascara.substr(6, 3), "-").concat(chaveSemMascara.substr(9)); boraFazerUmPix.tipoChave = 'CPF'; } else if (regexCNPJ.test(chaveSemMascara) && this.validarCNPJ(chaveSemMascara)) { // Chave é um CNPJ válido boraFazerUmPix.chavePIX = "".concat(chaveSemMascara.substr(0, 2), ".").concat(chaveSemMascara.substr(2, 3), ".").concat(chaveSemMascara.substr(5, 3), "/").concat(chaveSemMascara.substr(8, 4), "-").concat(chaveSemMascara.substr(12)); boraFazerUmPix.tipoChave = 'CNPJ'; } else if (regexChaveAleatoria.test(chaveSemMascara) && chaveSemMascara.length === 32) { // Chave é uma chave aleatória válida boraFazerUmPix.chavePIX = chavePIX; boraFazerUmPix.tipoChave = 'CHAVE ALEATÓRIA'; } else { // Chave não corresponde a nenhum formato válido boraFazerUmPix.chavePIX = chavePIX; boraFazerUmPix.tipoChave = 'INVÁLIDA'; } boraFazerUmPix.isValid = boraFazerUmPix.tipoChave !== 'INVÁLIDA'; return boraFazerUmPix; }; ValidacaoPix.ɵfac = function ValidacaoPix_Factory(t) { return new (t || ValidacaoPix)(); }; ValidacaoPix.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: ValidacaoPix, factory: ValidacaoPix.ɵfac, providedIn: 'root' }); return ValidacaoPix; }()); export { ValidacaoPix }; (function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ValidacaoPix, [{ type: Injectable, args: [{ providedIn: 'root' }] }], function () { return []; }, null); })(); //# sourceMappingURL=validacao-pix-provider.js.map