Como Validar Chave de Acesso NFC-e em Java
Validação de chaves de Nota Fiscal de Consumidor Eletrônica (NFC-e) em Java.
Introdução
NFC-e substitui o cupom fiscal. Sua validação é vital para sistemas de PDV (Ponto de Venda) em Java.
O Algoritmo
Segue o padrão nacional de chaves de acesso (44 dígitos, DV calculado por Módulo 11).
Implementação em Java
nfce-generator.jsjava
public class NfceValidator {
public static boolean validate(String key) {
if (key == null || key.length() != 44 || !key.matches("\\d+")) return false;
String baseKey = key.substring(0, 43);
int[] weights = {2, 3, 4, 5, 6, 7, 8, 9};
int total = 0;
int weightIndex = 0;
for (int i = baseKey.length() - 1; i >= 0; i--) {
total += Character.getNumericValue(baseKey.charAt(i)) * weights[weightIndex];
weightIndex = (weightIndex + 1) % 8;
}
int remainder = total % 11;
int dv = (remainder < 2) ? 0 : 11 - remainder;
return Character.getNumericValue(key.charAt(43)) == dv;
}
}Lógica encapsulada para validação de chaves NFC-e, ideal para integração em PDVs Java Desktop ou Web.
Não quer escrever código?
Use nossa ferramenta online gratuita para processar NFCE-GENERATOR instantaneamente. Perfeito para testes rápidos.
Usar Ferramenta de NFCE-GENERATOR Online