便捷开票二维码应用规范中的那些坑
因为最近需要给一个公众号做一个发票导入的功能,直接将微信抬头中的信息导入到微信公众号中!生成对应的抬头信息和二维码。
首先贴上国税总局给的二维码格式规范:
起始符+版本号+base64(名称</>纳税人识别号</>地址电话</>开户行及账号</>CRC)+结束符
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
s = Base64.encodeBase64String(b).replaceAll("[\\s*\t\n\r]", "");
}
return s;
}
因为规范中地址和电话之间没有分隔符,所以生成的二维码解析后地址和电话连在一起,找了一些资料也为发现怎么解决,后来才发现在拼接的时候地址与电话之间需要拼接上"|",这是一个坑,请各位看官注意。
附上CRC生成代码
static int a=0x0000;
static int crc16=0x8005;
private static void div(byte input) {//算法
int temp=0;
int data = input;
for (int i = 0; i < 8; i ++) {
temp = a & 0x8000;
a = a << 1;
a = a & 0x0000ffff;
int numIn = data & 0x80;
numIn = numIn >> 7;
a = a ^ numIn;
if (temp == 0x8000) {
a = a ^ crc16;
}
data = data << 1;
a = a & 0x0000ffff ;
}
}
调用生成CRC,注意一定要转换成GBK
public static String getCode(String input) {
try {
byte[] inputs = input.getBytes("GBK");
for (int i = 0; i < inputs.length; i ++) {
div(inputs[i]);
}
byte r = 0;
div(r);
div(r);
String crc3=Integer.toHexString(a);
String jm=getBase64(input+crc3.toUpperCase());//base64加密
String s="$01"+jm+"$";
return s;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
通过使用微信小程序国家发票助手扫码解析出来的内容。