微信JSAPI支付 适用于小程序和微信端H5页面

  • 前几天做了关于微信JSAPI的项目有很多很多的坑,在网上也自己百度学习了很久才弄出来,现在把代码发出来大家一起研究学习.

               这些代码中也有很多借鉴了其他大神的代码 先对大神们写的文章表示感谢
    
    • 在看这段代码前,希望大家可以去看看微信的官方文档这样会对整个的流程比较清晰

      微信官方文档

    在开始JSAPI支付之前我们需要注意在开通商户号的权限后,与公众号进行绑定还需要注意API秘钥不能丢失不然会很麻烦,第一次设置后后面都看不到了.还需要在商户号中配置JS授权域名才能成功的吊起微信支付.下面就是java后台代码和前端代码:

@CrossOrigin(maxAge = 3600)
@Controller
public class WxPayController
{
    private static final Logger logger = Logger.getLogger(String.valueOf(WxPayController.class));
    private final static String UTF8 = "UTF-8";
    private HttpClient client;
    private final static int SUCCESS = 200;
    /**
     * 微信公众平台 验证token配置
     */
    @RequestMapping(value = "wechat", method = RequestMethod.GET)
    @ResponseBody
    public String check(@RequestParam(value = "signature", required = false) String signature,
                        @RequestParam(value = "nonce", required = false) String nonce,
                        @RequestParam(value = "timestamp", required = false) String timestamp,
                        @RequestParam(value = "echostr", required = false) String echostr)
            throws UnsupportedEncodingException, MalformedURLException
    {

        return echostr;
    }

    /**
     * 发起支付 只支持MD5
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/pay")
    @ResponseBody
    public String wxPayFunction(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        // 获取配置数据
        WXPayConfig config = WXPayConfigImpl.getInstance();
        String appid = config.getAppID();
        String secret = config.getAppSecret();
        String code = request.getParameter("code");
        String body = new String(request.getParameter("body").getBytes("ISO-8859-1"), "UTF-8"); // 转换编码
        String total_fee = request.getParameter("total_fee");
        // 微信返回code值,用code获取openid
        URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret
                + "&grant_type=authorization_code&code=" + code);
                   String resp = doGet(url);
      
        logger.info("调起支付:"+getSign(body, total_fee, resp));
        return getSign(body, total_fee, resp);
    }
    /**
     * 获取签名
     *
     * @param body      商品描述
     * @param total_fee 价格(分)
     * @param resp      获取openid
     * @return
     * @throws Exception
     */
    public String getSign(String body, String total_fee, String resp) throws Exception
    {
        String nonce_str = getRandomStr(12);
        String notifyUrl = "https://www.baidu.com/"; // 我这里的回调地址是随便写的,到时候需要换成处理业务的回调接口
        WXPayConfig config = WXPayConfigImpl.getInstance();
        // 根据微信支付api来设置
        Map<String, String> data = new HashMap<>();
        data.put("appid", config.getAppID());
        data.put("body", body);
        data.put("fee_type", "CNY"); // 默认人民币
        data.put("mch_id", config.getMchID()); // 商户号
        data.put("nonce_str", nonce_str); // 随机字符串小于32位
        data.put("notify_url", notifyUrl); // 回调地址
//        data.put("openid", resp.indexOf("openid") != -1 ? JSONObject.fromObject(resp).getString("openid") : "");
        data.put("openid", resp);
        data.put("out_trade_no", String.valueOf(System.currentTimeMillis())); // 交易号
        data.put("spbill_create_ip", "127.0.0.1"); // 终端ip
        data.put("total_fee", total_fee); // 订单总金额
        data.put("trade_type", "JSAPI"); // 支付场景 APP 微信app支付 JSAPI 公众号支付 NATIVE
        String s = WXPayUtil.generateSignature(data, config.getKey()); // 签名
        data.put("sign", s);
        logger.info("签名:"+s);
        /** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
        WXPay wxPay = new WXPay();
        Map<String, String> respData = wxPay.unifiedOrder(data, config.getHttpConnectTimeoutMs(),
                config.getHttpReadTimeoutMs());
        logger.info("统一下单接口返回值:"+respData);
        if (respData.get("return_code").equals("SUCCESS"))
        {
            // 调用支付
            return sendPay(config, respData.get("prepay_id"));
        }
        return "flie";
    }

    /**
     * 发起支付
     *
     * @param config
     * @param prepayId
     * @return
     * @throws Exception
     */
    public String sendPay(WXPayConfig config, String prepayId) throws Exception
    {
        SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
        parameters.put("appId", config.getAppID());
        parameters.put("nonceStr", getRandomStr(16));
        parameters.put("package", "prepay_id=" + prepayId);
        parameters.put("signType", String.valueOf(WXPayConstants.SignType.MD5));
        parameters.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));

        String characterEncoding = "UTF-8";
        // 支付签名
        String paySign = WXPayUtil.createSign(characterEncoding, parameters);
        parameters.put("paySign", paySign);
        JSONObject jsonObject = JSONObject.fromObject(parameters);
        return jsonObject.toString();
    }

    /**
     * 获取随机字符串
     *
     * @param length
     * @return
     */
    public String getRandomStr(int length)
    {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        int randomNum;
        char randomChar;
        Random random = new Random();
        // StringBuffer类型的可以append增加字符
        StringBuffer str = new StringBuffer();

        for (int i = 0; i < length; i++)
        {
            // 可生成[0,n)之间的整数,获得随机位置
            randomNum = random.nextInt(base.length());
            // 获得随机位置对应的字符
            randomChar = base.charAt(randomNum);
            // 组成一个随机字符串
            str.append(randomChar);
        }
        return str.toString().toUpperCase();
    }
    private String doGet(URL url)
    {
        @SuppressWarnings("unused")
        long beginTime = System.currentTimeMillis();
        String respStr = StringUtils.EMPTY;
        try
        {
            // System.out.println(OPERATER_NAME + "开始get通信,目标host:" + url);
            HttpMethod method = new GetMethod(url.toString());
            // 中文转码
            method.getParams().setContentCharset(UTF8);
            try
            {
                client.executeMethod(method);
            } catch (HttpException e)
            {

//                logger.error(new StringBuffer("发送HTTP GET给\r\n").append(url)
//                        .append("\r\nHTTP异常\r\n"), e);
            } catch (IOException e)
            {

//                logger.error(new StringBuffer("发送HTTP GET给\r\n").append(url)
//                        .append("\r\nIO异常\r\n"), e);
            }
            if (method.getStatusCode() == SUCCESS)
            {
                respStr = method.getResponseBodyAsString();
            }
            // 释放连接
            method.releaseConnection();

            // System.out.println(OPERATER_NAME + "通讯完成,返回码:" + method.getStatusCode());
            // System.out.println(OPERATER_NAME + "返回内容:" +
            // method.getResponseBodyAsString());
            // System.out.println(OPERATER_NAME + "结束..返回结果:" + respStr);
        } catch (Exception e)
        {
//            // System.out.println(OPERATER_NAME, e);
        }
        @SuppressWarnings("unused")
        long endTime = System.currentTimeMillis();
        // System.out.println(OPERATER_NAME + "共计耗时:" + (endTime - beginTime) + "ms");

        return respStr;
    }
}

工具类
IWXPayDomain



/**
 * 域名管理,实现主备域名自动切换
 */
public abstract interface IWXPayDomain
{
    /**
     * 上报域名网络状况
     *
     * @param domain            域名。 比如:api.mch.weixin.qq.com
     * @param elapsedTimeMillis 耗时
     * @param ex                网络请求中出现的异常。 null表示没有异常
     *                          ConnectTimeoutException,表示建立网络连接异常
     *                          UnknownHostException, 表示dns解析异常
     */
    abstract void report(final String domain, long elapsedTimeMillis, final Exception ex);

    /**
     * 获取域名
     *
     * @param config 配置
     * @return 域名
     */
    abstract DomainInfo getDomain(final WXPayConfig config);

    static class DomainInfo
    {
        public String domain; // 域名
        public boolean primaryDomain; // 该域名是否为主域名。例如:api.mch.weixin.qq.com为主域名

        public DomainInfo(String domain, boolean primaryDomain)
        {
            this.domain = domain;
            this.primaryDomain = primaryDomain;
        }

        @Override
        public String toString()
        {
            return "DomainInfo{" + "domain='" + domain + '\'' + ", primaryDomain=" + primaryDomain + '}';
        }
    }

}

SignUtil


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class SignUtil {

    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        String token = "leonjo";
        String[] array = new String[]{token, timestamp, nonce};
        Arrays.sort(array);
        String content = array[0].concat(array[1]).concat(array[2]);
        String ciphertext = null;
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(content.toString().getBytes());
            ciphertext = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return ciphertext != null ? ciphertext.equals(signature.toUpperCase()) : false;
    }

    public static String getJsSdkSign(String noncestr, String tsapiTicket, String timestamp, String url) {
        String content = "jsapi_ticket=" + tsapiTicket + "&noncestr=" + noncestr + "&timestamp" + timestamp + "&url=" + url;
        System.out.println("签名:"+content);
        String ciphertext = null;
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(content.getBytes());
            ciphertext = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return ciphertext;
    }

    public static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    public static String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];
        String s = new String(tempArr);
        return s;
    }

}

WXPayConfig


import java.io.InputStream;

public abstract class WXPayConfig
{

    /**
     * 获取 App ID
     *
     * @return App ID
     */
    public abstract String getAppID();

    /**
     * 获取 AppSecret
     *
     * @return AppSecret
     */
    public abstract String getAppSecret();

    /**
     * 获取 Mch ID
     *
     * @return Mch ID
     */
    public abstract String getMchID();

    /**
     * 获取 API 密钥
     *
     * @return API密钥
     */
    public abstract String getKey();

    /**
     * 获取商户证书内容
     *
     * @return 商户证书内容
     */
    abstract InputStream getCertStream();

    /**
     * HTTP(S) 连接超时时间,单位毫秒
     *
     * @return
     */
    public int getHttpConnectTimeoutMs()
    {
        return 6 * 1000;
    }

    /**
     * HTTP(S) 读数据超时时间,单位毫秒
     *
     * @return
     */
    public int getHttpReadTimeoutMs()
    {
        return 8 * 1000;
    }

    /**
     * 获取WXPayDomain, 用于多域名容灾自动切换
     *
     * @return
     */
    abstract IWXPayDomain getWXPayDomain();

    /**
     * 是否自动上报。 若要关闭自动上报,子类中实现该函数返回 false 即可。
     *
     * @return
     */
    public boolean shouldAutoReport()
    {
        return true;
    }

    /**
     * 进行健康上报的线程的数量
     *
     * @return
     */
    public int getReportWorkerNum()
    {
        return 6;
    }

    /**
     * 健康上报缓存消息的最大数量。会有线程去独立上报 粗略计算:加入一条消息200B,10000消息占用空间 2000 KB,约为2MB,可以接受
     *
     * @return
     */
    public int getReportQueueMaxSize()
    {
        return 10000;
    }

    /**
     * 批量上报,一次最多上报多个数据
     *
     * @return
     */
    public int getReportBatchSize()
    {
        return 10;
    }

}

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class WXPayConfigImpl extends WXPayConfig
{

    private byte[] certData;
    private static WXPayConfigImpl INSTANCE;

    private WXPayConfigImpl() throws Exception
    {
        String certPath = "apiclient_cert.p12";//安全证书的地址 退款之类的api会需要
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public static WXPayConfigImpl getInstance() throws Exception
    {
        if (INSTANCE == null)
        {
            synchronized (WXPayConfigImpl.class)
            {
                if (INSTANCE == null)
                {
                    INSTANCE = new WXPayConfigImpl();
                }
            }
        }
        return INSTANCE;
    }
    public String getAppID()
    {
        return ""; //公众号的APPID
    }

    public String getAppSecret()
    {
        return "";//公众号的secret
    }

    public String getMchID()
    {
        return ""; //商户号
    }

    public String getKey()
    {
        return ""; //商户秘钥
    }

    public InputStream getCertStream()
    {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs()
    {
        return 2000;
    }

    public int getHttpReadTimeoutMs()
    {
        return 10000;
    }

    IWXPayDomain getWXPayDomain()
    {
        return WXPayDomainSimpleImpl.instance();
    }

    public String getPrimaryDomain()
    {
        return "api.mch.weixin.qq.com";
    }

    public String getAlternateDomain()
    {
        return "api2.mch.weixin.qq.com";
    }

    @Override
    public int getReportWorkerNum()
    {
        return 1;
    }

    @Override
    public int getReportBatchSize()
    {
        return 2;
    }
}

WXPayConstants

/**
 * 常量
 */
public class WXPayConstants
{

    public enum SignType
    {
        MD5
    }

    public static final String DOMAIN_API = "api.mch.weixin.qq.com";
    public static final String DOMAIN_API2 = "api2.mch.weixin.qq.com";
    public static final String DOMAIN_APIHK = "apihk.mch.weixin.qq.com";
    public static final String DOMAIN_APIUS = "apius.mch.weixin.qq.com";

    public static final String FAIL = "FAIL";
    public static final String SUCCESS = "SUCCESS";
    //    public static final String HMACSHA256 = "HMAC-SHA256";
    public static final String MD5 = "MD5";

    public static final String FIELD_SIGN = "sign";
    public static final String FIELD_SIGN_TYPE = "sign_type";

    public static final String MICROPAY_URL_SUFFIX = "/pay/micropay";
    public static final String UNIFIEDORDER_URL_SUFFIX = "/pay/unifiedorder";
    public static final String ORDERQUERY_URL_SUFFIX = "/pay/orderquery";
    public static final String REVERSE_URL_SUFFIX = "/secapi/pay/reverse";
    public static final String CLOSEORDER_URL_SUFFIX = "/pay/closeorder";
    public static final String REFUND_URL_SUFFIX = "/secapi/pay/refund";
    public static final String REFUNDQUERY_URL_SUFFIX = "/pay/refundquery";
    public static final String DOWNLOADBILL_URL_SUFFIX = "/pay/downloadbill";
    public static final String REPORT_URL_SUFFIX = "/payitil/report";
    public static final String SHORTURL_URL_SUFFIX = "/tools/shorturl";
    public static final String AUTHCODETOOPENID_URL_SUFFIX = "/tools/authcodetoopenid";

    // sandbox
    public static final String SANDBOX_MICROPAY_URL_SUFFIX = "/sandboxnew/pay/micropay";
    public static final String SANDBOX_UNIFIEDORDER_URL_SUFFIX = "/sandboxnew/pay/unifiedorder";
    public static final String SANDBOX_ORDERQUERY_URL_SUFFIX = "/sandboxnew/pay/orderquery";
    public static final String SANDBOX_REVERSE_URL_SUFFIX = "/sandboxnew/secapi/pay/reverse";
    public static final String SANDBOX_CLOSEORDER_URL_SUFFIX = "/sandboxnew/pay/closeorder";
    public static final String SANDBOX_REFUND_URL_SUFFIX = "/sandboxnew/secapi/pay/refund";
    public static final String SANDBOX_REFUNDQUERY_URL_SUFFIX = "/sandboxnew/pay/refundquery";
    public static final String SANDBOX_DOWNLOADBILL_URL_SUFFIX = "/sandboxnew/pay/downloadbill";
    public static final String SANDBOX_REPORT_URL_SUFFIX = "/sandboxnew/payitil/report";
    public static final String SANDBOX_SHORTURL_URL_SUFFIX = "/sandboxnew/tools/shorturl";
    public static final String SANDBOX_AUTHCODETOOPENID_URL_SUFFIX = "/sandboxnew/tools/authcodetoopenid";

}

WXPayDomainSimpleImpl

import org.apache.http.conn.ConnectTimeoutException;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;


public class WXPayDomainSimpleImpl implements IWXPayDomain
{
    private WXPayDomainSimpleImpl()
    {
    }

    private static class WxpayDomainHolder
    {
        private static IWXPayDomain holder = new WXPayDomainSimpleImpl();
    }

    public static IWXPayDomain instance()
    {
        return WxpayDomainHolder.holder;
    }

    public synchronized void report(final String domain, long elapsedTimeMillis, final Exception ex)
    {
        DomainStatics info = domainData.get(domain);
        if (info == null)
        {
            info = new DomainStatics(domain);
            domainData.put(domain, info);
        }

        if (ex == null)
        { // success
            if (info.succCount >= 2)
            { // continue succ, clear error count
                info.connectTimeoutCount = info.dnsErrorCount = info.otherErrorCount = 0;
            } else
            {
                ++info.succCount;
            }
        } else if (ex instanceof ConnectTimeoutException)
        {
            info.succCount = info.dnsErrorCount = 0;
            ++info.connectTimeoutCount;
        } else if (ex instanceof UnknownHostException)
        {
            info.succCount = 0;
            ++info.dnsErrorCount;
        } else
        {
            info.succCount = 0;
            ++info.otherErrorCount;
        }
    }

    public synchronized DomainInfo getDomain(final WXPayConfig config)
    {
        DomainStatics primaryDomain = domainData.get(WXPayConstants.DOMAIN_API);
        if (primaryDomain == null || primaryDomain.isGood())
        {
            return new DomainInfo(WXPayConstants.DOMAIN_API, true);
        }

        long now = System.currentTimeMillis();
        if (switchToAlternateDomainTime == 0)
        { // first switch
            switchToAlternateDomainTime = now;
            return new DomainInfo(WXPayConstants.DOMAIN_API2, false);
        } else if (now - switchToAlternateDomainTime < MIN_SWITCH_PRIMARY_MSEC)
        {
            DomainStatics alternateDomain = domainData.get(WXPayConstants.DOMAIN_API2);
            if (alternateDomain == null || alternateDomain.isGood()
                    || alternateDomain.badCount() < primaryDomain.badCount())
            {
                return new DomainInfo(WXPayConstants.DOMAIN_API2, false);
            } else
            {
                return new DomainInfo(WXPayConstants.DOMAIN_API, true);
            }
        } else
        { // force switch back
            switchToAlternateDomainTime = 0;
            primaryDomain.resetCount();
            DomainStatics alternateDomain = domainData.get(WXPayConstants.DOMAIN_API2);
            if (alternateDomain != null)
                alternateDomain.resetCount();
            return new DomainInfo(WXPayConstants.DOMAIN_API, true);
        }
    }

    static class DomainStatics
    {
        final String domain;
        int succCount = 0;
        int connectTimeoutCount = 0;
        int dnsErrorCount = 0;
        int otherErrorCount = 0;

        DomainStatics(String domain)
        {
            this.domain = domain;
        }

        void resetCount()
        {
            succCount = connectTimeoutCount = dnsErrorCount = otherErrorCount = 0;
        }

        boolean isGood()
        {
            return connectTimeoutCount <= 2 && dnsErrorCount <= 2;
        }

        int badCount()
        {
            return connectTimeoutCount + dnsErrorCount * 5 + otherErrorCount / 4;
        }
    }

    private final int MIN_SWITCH_PRIMARY_MSEC = 3 * 60 * 1000; // 3 minutes
    private long switchToAlternateDomainTime = 0;
    private Map<String, DomainStatics> domainData = new HashMap<String, DomainStatics>();
}

WXPayReport

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;

/**
 * 交易保障
 */
public class WXPayReport
{

    public static class ReportInfo
    {

        /**
         * 布尔变量使用int。0为false, 1为true。
         */

        // 基本信息
        private String version = "v0";
        private String sdk = "wxpay java sdk v1.0";
        private String uuid; // 交易的标识
        private long timestamp; // 上报时的时间戳,单位秒
        private long elapsedTimeMillis; // 耗时,单位 毫秒

        // 针对主域名
        private String firstDomain; // 第1次请求的域名
        private boolean primaryDomain; // 是否主域名
        private int firstConnectTimeoutMillis; // 第1次请求设置的连接超时时间,单位 毫秒
        private int firstReadTimeoutMillis; // 第1次请求设置的读写超时时间,单位 毫秒
        private int firstHasDnsError; // 第1次请求是否出现dns问题
        private int firstHasConnectTimeout; // 第1次请求是否出现连接超时
        private int firstHasReadTimeout; // 第1次请求是否出现连接超时

        public ReportInfo(String uuid, long timestamp, long elapsedTimeMillis, String firstDomain,
                          boolean primaryDomain, int firstConnectTimeoutMillis, int firstReadTimeoutMillis,
                          boolean firstHasDnsError, boolean firstHasConnectTimeout, boolean firstHasReadTimeout)
        {
            this.uuid = uuid;
            this.timestamp = timestamp;
            this.elapsedTimeMillis = elapsedTimeMillis;
            this.firstDomain = firstDomain;
            this.primaryDomain = primaryDomain;
            this.firstConnectTimeoutMillis = firstConnectTimeoutMillis;
            this.firstReadTimeoutMillis = firstReadTimeoutMillis;
            this.firstHasDnsError = firstHasDnsError ? 1 : 0;
            this.firstHasConnectTimeout = firstHasConnectTimeout ? 1 : 0;
            this.firstHasReadTimeout = firstHasReadTimeout ? 1 : 0;
        }

        @Override
        public String toString()
        {
            return "ReportInfo{" + "version='" + version + '\'' + ", sdk='" + sdk + '\'' + ", uuid='" + uuid + '\''
                    + ", timestamp=" + timestamp + ", elapsedTimeMillis=" + elapsedTimeMillis + ", firstDomain='"
                    + firstDomain + '\'' + ", primaryDomain=" + primaryDomain + ", firstConnectTimeoutMillis="
                    + firstConnectTimeoutMillis + ", firstReadTimeoutMillis=" + firstReadTimeoutMillis
                    + ", firstHasDnsError=" + firstHasDnsError + ", firstHasConnectTimeout=" + firstHasConnectTimeout
                    + ", firstHasReadTimeout=" + firstHasReadTimeout + '}';
        }

        /**
         * 转换成 csv 格式
         *
         * @return
         */
        public String toLineString(String key)
        {
            String separator = ",";
            Object[] objects = new Object[]
                    { version, sdk, uuid, timestamp, elapsedTimeMillis, firstDomain, primaryDomain, firstConnectTimeoutMillis,
                            firstReadTimeoutMillis, firstHasDnsError, firstHasConnectTimeout, firstHasReadTimeout };
            StringBuffer sb = new StringBuffer();
            for (Object obj : objects)
            {
                sb.append(obj).append(separator);
            }
            try
            {
                String sign = WXPayUtil.HMACSHA256(sb.toString(), key);
                sb.append(sign);
                return sb.toString();
            } catch (Exception ex)
            {
                return null;
            }

        }

    }

    private static final String REPORT_URL = "http://report.mch.weixin.qq.com/wxpay/report/default";
    // private static final String REPORT_URL = "http://127.0.0.1:5000/test";

    private static final int DEFAULT_CONNECT_TIMEOUT_MS = 6 * 1000;
    private static final int DEFAULT_READ_TIMEOUT_MS = 8 * 1000;

    private LinkedBlockingQueue<String> reportMsgQueue = null;
    private WXPayConfig config;
    private ExecutorService executorService;

    private volatile static WXPayReport INSTANCE;

    private WXPayReport(final WXPayConfig config)
    {
        this.config = config;
        reportMsgQueue = new LinkedBlockingQueue<String>(config.getReportQueueMaxSize());

        // 添加处理线程
        executorService = Executors.newFixedThreadPool(config.getReportWorkerNum(), new ThreadFactory()
        {
            public Thread newThread(Runnable r)
            {
                Thread t = Executors.defaultThreadFactory().newThread(r);
                t.setDaemon(true);
                return t;
            }
        });

        if (config.shouldAutoReport())
        {
            WXPayUtil.getLogger().info("report worker num: {}", config.getReportWorkerNum());
            for (int i = 0; i < config.getReportWorkerNum(); ++i)
            {
                executorService.execute(new Runnable()
                {
                    public void run()
                    {
                        while (true)
                        {
                            // 先用 take 获取数据
                            try
                            {
                                StringBuffer sb = new StringBuffer();
                                String firstMsg = reportMsgQueue.take();
                                WXPayUtil.getLogger().info("get first report msg: {}", firstMsg);
                                String msg = null;
                                sb.append(firstMsg); // 会阻塞至有消息
                                int remainNum = config.getReportBatchSize() - 1;
                                for (int j = 0; j < remainNum; ++j)
                                {
                                    WXPayUtil.getLogger().info("try get remain report msg");
                                    // msg = reportMsgQueue.poll(); // 不阻塞了
                                    msg = reportMsgQueue.take();
                                    WXPayUtil.getLogger().info("get remain report msg: {}", msg);
                                    if (msg == null)
                                    {
                                        break;
                                    } else
                                    {
                                        sb.append("\n");
                                        sb.append(msg);
                                    }
                                }
                                // 上报
                                WXPayReport.httpRequest(sb.toString(), DEFAULT_CONNECT_TIMEOUT_MS,
                                        DEFAULT_READ_TIMEOUT_MS);
                            } catch (Exception ex)
                            {
                                WXPayUtil.getLogger().warn("report fail. reason: {}", ex.getMessage());
                            }
                        }
                    }
                });
            }
        }

    }

    /**
     * 单例,双重校验,请在 JDK 1.5及更高版本中使用
     *
     * @param config
     * @return
     */
    public static WXPayReport getInstance(WXPayConfig config)
    {
        if (INSTANCE == null)
        {
            synchronized (WXPayReport.class)
            {
                if (INSTANCE == null)
                {
                    INSTANCE = new WXPayReport(config);
                }
            }
        }
        return INSTANCE;
    }

    public void report(String uuid, long elapsedTimeMillis, String firstDomain, boolean primaryDomain,
                       int firstConnectTimeoutMillis, int firstReadTimeoutMillis, boolean firstHasDnsError,
                       boolean firstHasConnectTimeout, boolean firstHasReadTimeout)
    {
        long currentTimestamp = WXPayUtil.getCurrentTimestamp();
        ReportInfo reportInfo = new ReportInfo(uuid, currentTimestamp, elapsedTimeMillis, firstDomain, primaryDomain,
                firstConnectTimeoutMillis, firstReadTimeoutMillis, firstHasDnsError, firstHasConnectTimeout,
                firstHasReadTimeout);
        String data = reportInfo.toLineString(config.getKey());
        WXPayUtil.getLogger().info("report {}", data);
        if (data != null)
        {
            reportMsgQueue.offer(data);
        }
    }

    @SuppressWarnings("unused")
    @Deprecated
    private void reportSync(final String data) throws Exception
    {
        httpRequest(data, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS);
    }

    @SuppressWarnings("unused")
    @Deprecated
    private void reportAsync(final String data) throws Exception
    {
        new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    httpRequest(data, DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS);
                } catch (Exception ex)
                {
                    WXPayUtil.getLogger().warn("report fail. reason: {}", ex.getMessage());
                }
            }
        }).start();
    }

    /**
     * http 请求
     *
     * @param data
     * @param connectTimeoutMs
     * @param readTimeoutMs
     * @return
     * @throws Exception
     */
    private static String httpRequest(String data, int connectTimeoutMs, int readTimeoutMs) throws Exception
    {
        BasicHttpClientConnectionManager connManager;
        connManager = new BasicHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory()).build(), null, null, null);
        HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();

        HttpPost httpPost = new HttpPost(REPORT_URL);

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs)
                .setConnectTimeout(connectTimeoutMs).build();
        httpPost.setConfig(requestConfig);

        StringEntity postEntity = new StringEntity(data, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 "); // TODO: 很重要,用来检测 sdk 的使用情况,要不要加上商户信息?
        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity, "UTF-8");
    }

}

WXPayRequest

import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.SecureRandom;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

public class WXPayRequest
{
    private WXPayConfig config;

    public WXPayRequest(WXPayConfig config) throws Exception
    {

        this.config = config;
    }

    /**
     * 请求,只请求一次,不做重试
     *
     * @param domain
     * @param urlSuffix
     * @param uuid
     * @param data
     * @param connectTimeoutMs
     * @param readTimeoutMs
     * @param useCert          是否使用证书,针对退款、撤销等操作
     * @return
     * @throws Exception
     */
    private String requestOnce(final String domain, String urlSuffix, String uuid, String data, int connectTimeoutMs,
                               int readTimeoutMs, boolean useCert) throws Exception
    {
        BasicHttpClientConnectionManager connManager;
        if (useCert)
        {
            // 证书
            char[] password = config.getMchID().toCharArray();
            InputStream certStream = config.getCertStream();
            KeyStore ks = KeyStore.getInstance("PKCS12");
            ks.load(certStream, password);

            // 实例化密钥库 & 初始化密钥工厂
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks, password);

            // 创建 SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());

            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                    new String[]
                            { "TLSv1" }, null, new DefaultHostnameVerifier());

            connManager = new BasicHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslConnectionSocketFactory).build(), null, null, null);
        } else
        {
            connManager = new BasicHttpClientConnectionManager(
                    RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
                            .register("https", SSLConnectionSocketFactory.getSocketFactory()).build(),
                    null, null, null);
        }

        HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();

        String url = "https://" + domain + urlSuffix;
        HttpPost httpPost = new HttpPost(url);

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs)
                .setConnectTimeout(connectTimeoutMs).build();
        httpPost.setConfig(requestConfig);

        StringEntity postEntity = new StringEntity(data, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 " + config.getMchID()); // TODO: 很重要,用来检测 sdk
        // 的使用情况,要不要加上商户信息?
        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity, "UTF-8");

    }

    private String request(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs,
                           boolean useCert, boolean autoReport) throws Exception
    {
        Exception exception = null;
        long elapsedTimeMillis = 0;
        long startTimestampMs = WXPayUtil.getCurrentTimestampMs();
        boolean firstHasDnsErr = false;
        boolean firstHasConnectTimeout = false;
        boolean firstHasReadTimeout = false;
        IWXPayDomain.DomainInfo domainInfo = config.getWXPayDomain().getDomain(config);
        if (domainInfo == null)
        {
            throw new Exception("WXPayConfig.getWXPayDomain().getDomain() is empty or null");
        }
        try
        {
            String result = requestOnce(domainInfo.domain, urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs,
                    useCert);
            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs() - startTimestampMs;
            config.getWXPayDomain().report(domainInfo.domain, elapsedTimeMillis, null);
            WXPayReport.getInstance(config).report(uuid, elapsedTimeMillis, domainInfo.domain, domainInfo.primaryDomain,
                    connectTimeoutMs, readTimeoutMs, firstHasDnsErr, firstHasConnectTimeout, firstHasReadTimeout);
            return result;
        } catch (UnknownHostException ex)
        { // dns 解析错误,或域名不存在
            exception = ex;
            firstHasDnsErr = true;
            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs() - startTimestampMs;
            WXPayUtil.getLogger().warn("UnknownHostException for domainInfo {}", domainInfo);
            WXPayReport.getInstance(config).report(uuid, elapsedTimeMillis, domainInfo.domain, domainInfo.primaryDomain,
                    connectTimeoutMs, readTimeoutMs, firstHasDnsErr, firstHasConnectTimeout, firstHasReadTimeout);
        } catch (ConnectTimeoutException ex)
        {
            exception = ex;
            firstHasConnectTimeout = true;
            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs() - startTimestampMs;
            WXPayUtil.getLogger().warn("connect timeout happened for domainInfo {}", domainInfo);
            WXPayReport.getInstance(config).report(uuid, elapsedTimeMillis, domainInfo.domain, domainInfo.primaryDomain,
                    connectTimeoutMs, readTimeoutMs, firstHasDnsErr, firstHasConnectTimeout, firstHasReadTimeout);
        } catch (SocketTimeoutException ex)
        {
            exception = ex;
            firstHasReadTimeout = true;
            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs() - startTimestampMs;
            WXPayUtil.getLogger().warn("timeout happened for domainInfo {}", domainInfo);
            WXPayReport.getInstance(config).report(uuid, elapsedTimeMillis, domainInfo.domain, domainInfo.primaryDomain,
                    connectTimeoutMs, readTimeoutMs, firstHasDnsErr, firstHasConnectTimeout, firstHasReadTimeout);
        } catch (Exception ex)
        {
            exception = ex;
            elapsedTimeMillis = WXPayUtil.getCurrentTimestampMs() - startTimestampMs;
            WXPayReport.getInstance(config).report(uuid, elapsedTimeMillis, domainInfo.domain, domainInfo.primaryDomain,
                    connectTimeoutMs, readTimeoutMs, firstHasDnsErr, firstHasConnectTimeout, firstHasReadTimeout);
        }
        config.getWXPayDomain().report(domainInfo.domain, elapsedTimeMillis, exception);
        throw exception;
    }

    /**
     * 可重试的,非双向认证的请求
     *
     * @param urlSuffix
     * @param uuid
     * @param data
     * @return
     */
    public String requestWithoutCert(String urlSuffix, String uuid, String data, boolean autoReport) throws Exception
    {
        return this.request(urlSuffix, uuid, data, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(),
                false, autoReport);
        // return requestWithoutCert(urlSuffix, uuid, data,
        // config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(), autoReport);
    }

    /**
     * 可重试的,非双向认证的请求
     *
     * @param urlSuffix
     * @param uuid
     * @param data
     * @param connectTimeoutMs
     * @param readTimeoutMs
     * @return
     */
    public String requestWithoutCert(String urlSuffix, String uuid, String data, int connectTimeoutMs,
                                     int readTimeoutMs, boolean autoReport) throws Exception
    {
        return this.request(urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, false, autoReport);

        /*
         * String result; Exception exception; boolean shouldRetry = false;
         *
         * boolean useCert = false; try { result = requestOnce(domain, urlSuffix, uuid,
         * data, connectTimeoutMs, readTimeoutMs, useCert); return result; } catch
         * (UnknownHostException ex) { // dns 解析错误,或域名不存在 exception = ex;
         * WXPayUtil.getLogger().
         * warn("UnknownHostException for domain {}, try to use {}", domain,
         * this.primaryDomain); shouldRetry = true; } catch (ConnectTimeoutException ex)
         * { exception = ex; WXPayUtil.getLogger().
         * warn("connect timeout happened for domain {}, try to use {}", domain,
         * this.primaryDomain); shouldRetry = true; } catch (SocketTimeoutException ex)
         * { exception = ex; shouldRetry = false; } catch (Exception ex) { exception =
         * ex; shouldRetry = false; }
         *
         * if (shouldRetry) { result = requestOnce(this.primaryDomain, urlSuffix, uuid,
         * data, connectTimeoutMs, readTimeoutMs, useCert); return result; } else {
         * throw exception; }
         */
    }

    /**
     * 可重试的,双向认证的请求
     *
     * @param urlSuffix
     * @param uuid
     * @param data
     * @return
     */
    public String requestWithCert(String urlSuffix, String uuid, String data, boolean autoReport) throws Exception
    {
        return this.request(urlSuffix, uuid, data, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(),
                true, autoReport);
        // return requestWithCert(urlSuffix, uuid, data,
        // config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs(), autoReport);
    }

    /**
     * 可重试的,双向认证的请求
     *
     * @param urlSuffix
     * @param uuid
     * @param data
     * @param connectTimeoutMs
     * @param readTimeoutMs
     * @return
     */
    public String requestWithCert(String urlSuffix, String uuid, String data, int connectTimeoutMs, int readTimeoutMs,
                                  boolean autoReport) throws Exception
    {
        return this.request(urlSuffix, uuid, data, connectTimeoutMs, readTimeoutMs, true, autoReport);

        /*
         * String result; Exception exception; boolean shouldRetry = false;
         *
         * boolean useCert = true; try { result = requestOnce(domain, urlSuffix, uuid,
         * data, connectTimeoutMs, readTimeoutMs, useCert); return result; } catch
         * (ConnectTimeoutException ex) { exception = ex;
         * WXPayUtil.getLogger().warn(String.
         * format("connect timeout happened for domain {}, try to use {}", domain,
         * this.primaryDomain)); shouldRetry = true; } catch (SocketTimeoutException ex)
         * { exception = ex; shouldRetry = false; } catch (Exception ex) { exception =
         * ex; shouldRetry = false; }
         *
         * if (shouldRetry && this.primaryDomain != null) { result =
         * requestOnce(this.primaryDomain, urlSuffix, uuid, data, connectTimeoutMs,
         * readTimeoutMs, useCert, autoReport); return result; } else { throw exception;
         * }
         */
    }
}

WXPayUtil

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.util.*;


public class WXPayUtil
{

    /**
     * XML格式字符串转换为Map
     *
     * @param strXML XML字符串
     * @return XML数据转换后的Map
     * @throws Exception
     */
    public static Map<String, String> xmlToMap(String strXML) throws Exception
    {
        try
        {
            Map<String, String> data = new HashMap<String, String>();
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
            org.w3c.dom.Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();
            for (int idx = 0; idx < nodeList.getLength(); ++idx)
            {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == Node.ELEMENT_NODE)
                {
                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }
            try
            {
                stream.close();
            } catch (Exception ex)
            {
                // do nothing
            }
            return data;
        } catch (Exception ex)
        {
            WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}",
                    ex.getMessage(), strXML);
            throw ex;
        }

    }

    /**
     * 将Map转换为XML格式的字符串
     *
     * @param data Map类型数据
     * @return XML格式的字符串
     * @throws Exception
     */
    public static String mapToXml(Map<String, String> data) throws Exception
    {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document document = documentBuilder.newDocument();
        org.w3c.dom.Element root = document.createElement("xml");
        document.appendChild(root);
        for (String key : data.keySet())
        {
            String value = data.get(key);
            if (value == null)
            {
                value = "";
            }
            value = value.trim();
            org.w3c.dom.Element filed = document.createElement(key);
            filed.appendChild(document.createTextNode(value));
            root.appendChild(filed);
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        String output = writer.getBuffer().toString(); // .replaceAll("\n|\r", "");
        try
        {
            writer.close();
        } catch (Exception ex)
        {
        }
        return output;
    }

    /**
     * 生成带有 sign 的 XML 格式字符串
     *
     * @param data Map类型数据
     * @param key  API密钥
     * @return 含有sign字段的XML
     */
    public static String generateSignedXml(final Map<String, String> data, String key) throws Exception
    {
        return generateSignedXml(data, key, WXPayConstants.SignType.MD5);
    }

    /**
     * 生成带有 sign 的 XML 格式字符串
     *
     * @param data     Map类型数据
     * @param key      API密钥
     * @param signType 签名类型
     * @return 含有sign字段的XML
     */
    public static String generateSignedXml(final Map<String, String> data, String key, WXPayConstants.SignType signType)
            throws Exception
    {
        String sign = generateSignature(data, key, signType);
        data.put(WXPayConstants.FIELD_SIGN, sign);
        return mapToXml(data);
    }

    /**
     * 判断签名是否正确
     *
     * @param xmlStr XML格式数据
     * @param key    API密钥
     * @return 签名是否正确
     * @throws Exception
     */
    public static boolean isSignatureValid(String xmlStr, String key) throws Exception
    {
        Map<String, String> data = xmlToMap(xmlStr);
        if (!data.containsKey(WXPayConstants.FIELD_SIGN))
        {
            return false;
        }
        String sign = data.get(WXPayConstants.FIELD_SIGN);
        return generateSignature(data, key).equals(sign);
    }

    /**
     * 判断签名是否正确,必须包含sign字段,否则返回false。使用MD5签名。
     *
     * @param data Map类型数据
     * @param key  API密钥
     * @return 签名是否正确
     * @throws Exception
     */
    public static boolean isSignatureValid(Map<String, String> data, String key) throws Exception
    {
        return isSignatureValid(data, key, WXPayConstants.SignType.MD5);
    }

    /**
     * 判断签名是否正确,必须包含sign字段,否则返回false。
     *
     * @param data     Map类型数据
     * @param key      API密钥
     * @param signType 签名方式
     * @return 签名是否正确
     * @throws Exception
     */
    public static boolean isSignatureValid(Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception
    {
        if (!data.containsKey(WXPayConstants.FIELD_SIGN))
        {
            return false;
        }
        String sign = data.get(WXPayConstants.FIELD_SIGN);
        return generateSignature(data, key, signType).equals(sign);
    }

    /**
     * 生成签名
     *
     * @param data 待签名数据
     * @param key  API密钥
     * @return 签名
     */
    public static String generateSignature(final Map<String, String> data, String key) throws Exception
    {
        return generateSignature(data, key, WXPayConstants.SignType.MD5);
    }

    /**
     * 生成签名. 注意,若含有sign_type字段,必须和signType参数保持一致。
     *
     * @param data     待签名数据
     * @param key      API密钥
     * @param signType 签名方式
     * @return 签名
     */
    public static String generateSignature(final Map<String, String> data, String key, WXPayConstants.SignType signType)
            throws Exception
    {
//    	System.out.println("signType:  " + signType);
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray)
        {
            if (k.equals(WXPayConstants.FIELD_SIGN))
            {
                continue;
            }
            if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
                sb.append(k).append("=").append(data.get(k).trim()).append("&");
        }
        sb.append("key=").append(key);
        if (WXPayConstants.SignType.MD5.equals(signType))
        {
            return MD5(sb.toString()).toUpperCase();
        } /*
         * else if (SignType.HMACSHA256.equals(signType)) { return
         * HMACSHA256(sb.toString(), key); }
         */
        else
        {
            throw new Exception(String.format("Invalid sign_type: %s", signType));
        }
    }

    /**
     * 获取随机字符串 Nonce Str
     *
     * @return String 随机字符串
     */
    public static String generateNonceStr()
    {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
    }

    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) throws Exception
    {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array)
        {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 生成 HMACSHA256
     *
     * @param data 待处理数据
     * @param key  密钥
     * @return 加密结果
     * @throws Exception
     */
    public static String HMACSHA256(String data, String key) throws Exception
    {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array)
        {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 日志
     *
     * @return
     */
    public static Logger getLogger()
    {
        Logger logger = LoggerFactory.getLogger("wxpay java sdk");
        return logger;
    }

    /**
     * 获取当前时间戳,单位秒
     *
     * @return
     */
    public static long getCurrentTimestamp()
    {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 获取当前时间戳,单位毫秒
     *
     * @return
     */
    public static long getCurrentTimestampMs()
    {
        return System.currentTimeMillis();
    }

    /**
     * 生成 uuid, 即用来标识一笔单,也用做 nonce_str
     *
     * @return
     */
    public static String generateUUID()
    {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
    }

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

    /**
     * 微信支付签名算法sign
     *
     * @param characterEncoding
     * @param parameters
     * @return
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
    public static String createSign(String characterEncoding, SortedMap<Object, Object> parameters) throws Exception
    {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序(升序)
        Iterator it = es.iterator();
        while (it.hasNext())
        {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k))
            {
                sb.append(k + "=" + v + "&");
            }
        }
        // 获取配置数据
        WXPayConfig config = WXPayConfigImpl.getInstance();
        sb.append("key=" + config.getKey());
//        System.out.println("sb.toString()" + sb.toString());
        String sign = MD5(sb.toString()).toUpperCase();
//        System.out.println("sign-----:" + sign);
        return sign;
    }
}

前端代码

 //微信支付
    var v1;
    var appId, timeStamp, nonceStr, package, signType, paySign;

    function btn() {
$.ajax({
                                url: url + "pay", 
                                data: {
                                    "body": , //支付名称
                                    "code": , //用户id
                                    "total_fee":  //总价钱
                                },
                                async:false,
                                type: "post",
                                dataType: "JSON",
                                success: function (data) {
                                    appId = data.appId;
                                    nonceStr = data.nonceStr;
                                    package = data.package;
                                    paySign = data.paySign;
                                    signType = data.signType;
                                    timeStamp = data.timeStamp;
                                    console.log(data)
                                    if (typeof WeixinJSBridge == "undefined") {
                                        // alert(1);
                                        if (document.addEventListener) {
                                            // alert(123)
                                            document.addEventListener('WeixinJSBridgeReady',
                                                onBridgeReady(appId, timeStamp, nonceStr, package, signType, paySign), false);
                                        } else if (document.attachEvent) {
                                            document.attachEvent('WeixinJSBridgeReady',
                                                onBridgeReady(appId, timeStamp, nonceStr, package, signType, paySign));
                                            document.attachEvent('onWeixinJSBridgeReady',
                                                onBridgeReady(appId, timeStamp, nonceStr, package, signType, paySign));
                                        }
                                    } else {
                                        onBridgeReady(appId, timeStamp, nonceStr, package, signType, paySign);
                                    }

                                    //微信支付回调

                                    function onBridgeReady(appId, timeStamp, nonceStr, package, signType, paySign) {
                                        // console.log(appId, timeStamp, nonceStr, package, signType, paySign)
                                        WeixinJSBridge.invoke('getBrandWCPayRequest', {
                                            "appId": appId, //公众号名称,由商户传入
                                            "timeStamp": timeStamp, //时间戳,自1970年以来的秒数
                                            "nonceStr": nonceStr, //随机串
                                            "package": package,
                                            "signType": signType, //微信签名方式:
                                            "paySign": paySign
                                            //微信签名
                                        }, function (res) {
                                            //alert(JSON.stringify(res));
                                            if (res.err_msg == "get_brand_wcpay_request:ok") {
                                                // console.log('支付成功');
                                                //支付成功后跳转的页面
                                                //支付成功后跳转的页面
                                                
                                            } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                                // console.log('支付取消');
                                            } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                                                // console.log('支付失败');
                                                WeixinJSBridge.call('closeWindow');
                                            } //使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
                                        });
                                    }
                                },
                                error: function () {
                                    alert("请求失败!")
                                }
                            })
       }

大家看前端代码可以发现我是在前端的JS中再调用自己的接口来达到支付成功的效果,但是这样的话会有一个bug就是如果在微信支付完成后不点击完成回到当前页面就会有我们自己的回调函数不执行的情况发生.正确的逻辑应该是在回调函数中写自己的逻辑,但是微信支付的回调函数 我还没有研究出来 欢迎有研究过的大佬指导一下我.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值