Home  >  Article  >  WeChat Applet  >  Why does WeChat payment verification or signature fail? Attached are three solutions

Why does WeChat payment verification or signature fail? Attached are three solutions

php是最好的语言
php是最好的语言Original
2018-08-07 15:44:0547576browse

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 first solution: Set the tomcat encoding to utf-8

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

  1. ##

  2. connectionTimeout="20000"

  3. ##redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI=" true" />
  4. 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 body
String body = new String("body Chinese field value".toString().getBytes("ISO8859-1"),"UTF-8");

However: the product name returned by WeChat will be garbled

Why does WeChat payment verification or signature fail? Attached are three solutionsThe third solution: modify the signature MD5 encoding (

This solution is the best solution
)

The default encoding of tomcat in the window environment is gbk, so when performing md5 signature, set the encoding to utf-8.

This is the MD5 signature tool class I use: Why does WeChat payment verification or signature fail? Attached are three solutions

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" };

}

Related recommendations:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn