微信现金红包

微信现金红包的发放实则是借助于公众号的形式进行发放,可不进行关注公众号,发放模式:

发放的规则:

详情可见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3

直接发放红包的申请参数:

上述都是必填字段,非必填字段没有展示可取自行查看:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3

需要注意的是,微信现金红包中需要证书才可以发放,证书需要在商户网站api安全菜单栏中下载:

代码如下:

public static Map<String, String> sendRedPack(String applyId, String openId, String totalAmount, String wishing, String actName, String remark){
        
        Map<String, String> resMap = new HashMap<String, String>();
        resMap.put("status", "false");
        
        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 随机数
        paramMap.put("nonce_str", WxUtil.getNonceStr());
        // 商户订单号
        paramMap.put("mch_billno", applyId);
        //商户号
        paramMap.put("mch_id", MCH_ID);
        // 公众账号appid
        paramMap.put("wxappid", WX_APP_ID);
        //商户名称
        paramMap.put("send_name", SEND_NAME);
        //用户openid
        paramMap.put("re_openid", openId);
        //付款金额,单位分
        paramMap.put("total_amount", totalAmount);
        // 红包发放总人数(目前固定)
        paramMap.put("total_num", "1");
        // 红包祝福语
        paramMap.put("wishing", wishing);
        // 调用接口的机器Ip地址
        paramMap.put("client_ip", "127.0.0.1");
        // 活动名称
        paramMap.put("act_name", actName);
        // 备注
        remark = StringUtils.isNotBlank(remark)?remark:"备注";
        paramMap.put("remark", remark);

        String sign = WxUtil.sortMap(paramMap);
        sign = sign + "&key="+SEND_RED_PACK_KEY;
        sign = WxUtil.MD5Encode(sign, "UTF-8").toUpperCase();
        HttpPost httpPost = new HttpPost(SEND_RED_PACK_URL);
        StringEntity entityParams;
        
        CloseableHttpResponse response = null;
        try {
            KeyStore keyStore  = KeyStore.getInstance("PKCS12");
            FileInputStream instream = new FileInputStream(new File(LOCAL_CERT_URL));
            try {
                //证书密码,默认证书密码等于商户号
                keyStore.load(instream, MCH_ID.toCharArray());
            } finally {
                instream.close();
            }
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, MCH_ID.toCharArray()).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            
            entityParams = new StringEntity(WxUtil.parseXML(paramMap, sign), "UTF-8");
            httpPost.setEntity(entityParams);
            response = httpclient.execute(httpPost);
            log.info("调用发红包返回信息:" + response);
            if (response != null && response.getEntity() != null) {
                Map<String, String> resultMap = WxUtil.toMap(EntityUtils.toByteArray(response.getEntity()), "utf-8");
                System.out.println(WxUtil.toXml(resultMap));
                String return_code = resultMap.get("return_code");
                String return_msg = resultMap.get("return_msg");
                resMap.put("returnMsg", return_msg);
                if("SUCCESS".equals(return_code)){
                    String result_code = resultMap.get("result_code");
                    if("SUCCESS".equals(result_code)){
                        resMap.put("status", "true");
                    }
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resMap;
    }

有问题咨询QQ:1147726728

猜你喜欢

转载自www.cnblogs.com/JackRen-Developer/p/8990300.html
今日推荐