微信开发之---查询公众号发送的红包

查询公众号发送的现金红包接口地址:

【微信支付】现金红包开发者文档

 /**
     * 红包查询接口
     */
    @Override
    public JSONObject getwxhb(String openid,String orderid){
        JSONObject rejsonObject=new JSONObject();
        String mch_id=wechatPayConfig.getMch_id();
        String certificatePath=wechatPayConfig.getCertificatePath();
        //随机字符串和红包金额,金额单位为分
        String nonce_str= StringUtils.getNonce_str();
        String sign=this.getWxsearchSign(nonce_str,orderid);
        //封装请求参数
        StringBuilder reqXmlStr = new StringBuilder();
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        String reqXmlStrn=reqXmlStr.toString();
        log.info("微信红包查询接口:"+reqXmlStrn);
        String transUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo";
        HttpPost httpPost = new HttpPost(transUrl);

        try {
            // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
            StringEntity postEntity = new StringEntity(reqXmlStrn, "UTF-8");
            httpPost.addHeader("Content-Type", "text/xml");
            httpPost.setEntity(postEntity);

            //根初始化requestConfig 连接超时时间,默认10秒 传输超时时间,默认30秒
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(30000).build();
            httpPost.setConfig(requestConfig);
            CloseableHttpClient httpClient=WxUtils.loadCertificate(mch_id,certificatePath);
            HttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8");
                log.info(result);
                //解析是否发送成功,成功则返回成功标志,
                String jsonst= XmlUtils.xmlToJson(result);
                JSONObject jsonObject=JSONObject.parseObject(jsonst);
                rejsonObject=jsonObject.getJSONObject("xml");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpPost.abort();
        }
        return rejsonObject;
    }







/**
     * 查询红包发送结果签名算法
     * @param nonce_str
     * @param orderid
     * @return
     */
    private String getWxsearchSign(String nonce_str,String orderid) {

        Map paramMap = new HashMap();
        //随机字符串
        paramMap.put("nonce_str", nonce_str);
        //商户订单号
        paramMap.put("mch_billno", String.valueOf(orderid));
        //商户号
        paramMap.put("mch_id",wechatPayConfig.getMch_id() );
        //公众账号appid
        paramMap.put("appid", wechatPayConfig.getWxappid());
        //备注
        paramMap.put("bill_type", "MCHT");
        String sign= WxUtils.getSign(paramMap,wechatPayConfig.getMchkey());
        return sign;
    }

你可能感兴趣的:(微信开发,github)