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

需要先申请微信支付账号

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

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

代码如下:

openid:为授权openid

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

mch_id:商户号

wxappid:公众号id

/**
     * 公众号发送红包接口
     */
    @Override
    @Log(title = "公众号发送红包接口")
    public Map<String,Object> sendpost(String openid,String price,String orderid){
        Map<String,Object>map =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("<xml>");
        reqXmlStr.append("<sign><![CDATA["+sign+"]]></sign>");
        reqXmlStr.append("<mch_billno><![CDATA["+ orderid +"]]></mch_billno>");
        reqXmlStr.append("<mch_id><![CDATA["+wechatPayConfig.getMch_id()+"]]></mch_id>");
        reqXmlStr.append("<wxappid><![CDATA["+wechatPayConfig.getWxappid()+"]]></wxappid>");
        reqXmlStr.append("<send_name><![CDATA["+wechatPayConfig.getSend_name()+"]]></send_name>");
        reqXmlStr.append("<re_openid><![CDATA["+openid+"]]></re_openid>");
        reqXmlStr.append("<total_amount><![CDATA["+price+"]]></total_amount>");
        reqXmlStr.append("<total_num><![CDATA["+wechatPayConfig.getTotal_num() +"]]></total_num>");
        reqXmlStr.append("<wishing><![CDATA["+wechatPayConfig.getWishing()+"]]></wishing>");
        reqXmlStr.append("<client_ip><![CDATA["+wechatPayConfig.getClient_ip()+"]]></client_ip>");
        reqXmlStr.append("<act_name><![CDATA["+wechatPayConfig.getAct_name()+"]]></act_name>");
        reqXmlStr.append("<remark><![CDATA["+wechatPayConfig.getRemark()+"]]></remark>");
        reqXmlStr.append("<nonce_str><![CDATA["+nonce_str+"]]></nonce_str>");
        if (wechatPayConfig.getRisk_info() != null && !wechatPayConfig.getRisk_info().equals("")) {
            reqXmlStr.append("<risk_info>" + wechatPayConfig.getRisk_info() + "</risk_info>");
        }
        reqXmlStr.append("</xml>");
        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<String, Object> paramMap = new HashMap<String, Object>();
        //随机字符串
        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;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瞬间的醒悟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值