com.zopen.wechat.pay.util.SignUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zopen-ato-starter Show documentation
Show all versions of zopen-ato-starter Show documentation
Alibaba Tencent And Others For Spring Boot.
package com.zopen.wechat.pay.util;
import com.zcj.util.UtilString;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.TreeMap;
public class SignUtil {
private static final Logger logger = LoggerFactory.getLogger(SignUtil.class);
public static String getSign(final Object request, final String appKey) {
Map map;
try {
map = BeanUtils.describe(request);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
return null;
}
Map treeMap = new TreeMap<>();
for (Map.Entry entry : map.entrySet()) {
if (UtilString.isNotBlank(entry.getValue())) {
treeMap.put(entry.getKey(), entry.getValue());
}
}
treeMap.remove("sign");
treeMap.remove("class");
return sign(treeMap, appKey);
}
public static String sign(final Map params, final String appKey) {
StringBuilder signing = new StringBuilder();
for (Map.Entry entry : params.entrySet()) {
if (UtilString.isNotBlank(entry.getValue())) {
signing.append(entry.getKey()).append('=').append(entry.getValue()).append("&");
}
}
signing.append("key=").append(appKey);
return UtilString.getMd5(signing.toString()).toUpperCase();
}
}