All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.wechat.pay.util.SignUtil Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy