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

需要先申请微信支付账号

对接现金红包接口,接口地址如下

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

代码如下:

openid:为授权openid

certificatePath:为证书路径,在微信商户平台生成

mch_id:商户号

wxappid:公众号id

/**
     * 公众号发送红包接口
     */
    @Override
    @Log(title = "公众号发送红包接口")
    public Map sendpost(String openid,String price,String orderid){
        Mapmap =new HashMap<>();
        Boolean flag=Boolean.FALSE;
        if (price.equals("0")){
            map.put("flag",flag);
            map.put("return_msg","金额为0");
        }
        String mch_id=wechatPayConfig.getMch_id();
        String certificatePath=wechatPayConfig.getCertificatePath();
        //随机字符串和红包金额,金额单位为分
         String nonce_str= StringUtils.getNonce_str();
         String sign=this.getSign(nonce_str,orderid,openid,price);
        //封装请求参数
        StringBuilder reqXmlStr = new StringBuilder();
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        reqXmlStr.append("");
        if (wechatPayConfig.getRisk_info() != null && !wechatPayConfig.getRisk_info().equals("")) {
            reqXmlStr.append("" + wechatPayConfig.getRisk_info() + "");
        }
        reqXmlStr.append("");
        String reqXmlStrn=reqXmlStr.toString();
        log.info("发送红包请求参数:"+reqXmlStrn);
        String transUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
        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);
                JSONObject jsonObject1=jsonObject.getJSONObject("xml");
                String return_code=jsonObject1.getString("return_code");
                String return_msg=jsonObject1.getString("return_msg");
                map.put("return_msg",return_msg);
                //解析发送红包的数据
                if (return_code!=null && return_code.equals("SUCCESS")){
                    if(return_msg!=null && return_msg.equals("发放成功")){
                        flag=true;
                    }
                    else{
                        log.info(return_msg);
                        flag=false;
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpPost.abort();
        }
        map.put("flag",flag);
        return map;
    }

  




    /**
     * 发送红包签名算法
     * @param nonce_str
     * @param orderid
     * @param openid
     * @param price
     * @return
     */
    private String getSign(String nonce_str,String orderid,String openid,String price) {

        Map paramMap = new HashMap();
        //随机字符串
        paramMap.put("nonce_str", nonce_str);
        //商户订单号
        paramMap.put("mch_billno", orderid);
        //商户号
        paramMap.put("mch_id",wechatPayConfig.getMch_id() );
        //公众账号appid
        paramMap.put("wxappid", wechatPayConfig.getWxappid());
        //商户名称
        paramMap.put("send_name", wechatPayConfig.getSend_name());
        //用户openid
        paramMap.put("re_openid", openid);
        //付款金额
        paramMap.put("total_amount", price);
        //红包发放总人数
        paramMap.put("total_num", wechatPayConfig.getTotal_num());
        //红包祝福语
        paramMap.put("wishing", wechatPayConfig.getWishing());
        //Ip地址
        paramMap.put("client_ip", wechatPayConfig.getClient_ip());
        //活动名称
        paramMap.put("act_name", wechatPayConfig.getAct_name());
        //备注
        paramMap.put("remark", wechatPayConfig.getRemark());
        //场景id 发放红包使用场景,红包金额大于200或者小于1元时必传,以外非必须。
//        paramMap.put("scene_id", model.getSceneId());
        //活动信息 非必须参数。
        if (wechatPayConfig.getRisk_info() != null && !"".equals(wechatPayConfig.getRisk_info())) {
            paramMap.put("risk_info", wechatPayConfig.getRisk_info());
        }
        String sign= WxUtils.getSign(paramMap,wechatPayConfig.getMchkey());
        return sign;
    }

你可能感兴趣的:(微信开发,java,开发语言,微信)