Why does iOS WeChat unified payment verification fail? I'm really anxious because my signature failed and I still can't pay. The main problem is that the body is in Chinese, which is very troublesome to solve. I searched a lot on Baidu, but I can't find any articles that completely solve it. In short, it's all kinds of troubles. I won’t go into details here, let’s get to the point:
Because the company’s projects require WeChat payment, during deployment, I found that signature errors were always reported. After investigation, it turned out to be a coding problem, so we will solve it if we find the reason. Question, this article introduces three solutions in detail. If you have limited time and don’t have time to read it, then go directly to the third solution. I hope it can help everyone.
The default encoding of tomcat in the window environment is gbk, so set the tomcat encoding to utf-8.
Step 1: Add
set JAVA_OPTS=-Xms128m -Xmx512m -XX:MaxPermSize=256m -Dfile.encoding=utf-8 -Dsun.jnu.encoding=utf-8
让java环境使用utf-8编码
to the second line of the header in catalina.bat Step 2: Add
URIEncoding="UTF-8 to server.xml " useBodyEncodingForURI="true", causes the request sent by tomcat to use utf-8, as shown in the following code
##
connectionTimeout="20000"
The console may have garbled characters, but it will be fine if changed to gbk, but the WeChat signature fails
##The second type Solution: Convert bodyThe third solution: modify the signature MD5 encoding (
This solution is the best solutionThis is the MD5 signature tool class I use:
public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } public static String MD5Encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString .getBytes())); else resultString = byteArrayToHexString(md.digest(resultString .getBytes(charsetname))); } catch (Exception exception) { } return resultString; } private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; }
WeChat app payment: payment permissions The check failed, why?
WeChat app payment: Payment permission check failed, what is the reason
The above is the detailed content of Why does WeChat payment verification or signature fail? Attached are three solutions. For more information, please follow other related articles on the PHP Chinese website!