java调用微信企业发红包,微信付款到零钱

1 篇文章 0 订阅

一、微信付款接口可参考:付款到零钱:

https://blog.csdn.net/daotiao0199/article/details/85319451
二、企业微信发红包## 标题main方法,调用发红包接口

public static void main (String[] args) throws IOException{
		String  url="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
		String wx_appid="wx1be1xxxxxxx";
		String wx_mchid="1555xxxxx";
		String wx_mchkey="Thxxxxxxxxxxxxxxxx";
		String wxCertPath="E:\\cert\\apiclient_cert.p12";
		String logNo="xxxx2019091019933ddd";

		SortedMap<String,Object> map =new TreeMap<String,Object>();
		map.put("nonce_str", "ASDXCV");//可以写死
		map.put("mch_billno", logNo);
		map.put("mch_id", wx_mchid);
		map.put("wxappid", wx_appid);
		map.put("send_name", "测试红包");
		map.put("re_openid", "oKvRh5ljxxxxxx");
		map.put("total_amount", 30);
		map.put("total_num", "1");
		map.put("wishing", "祝福红包");
		map.put("client_ip", "127.0.0.1");
		map.put("act_name", "祝福红包二");
		map.put("remark", "祝福红包描述");
		map.put("scene_id", "PRODUCT_1");
		String sign=createSignBalance("utf-8", map, wx_mchkey);
		map.put("sign", sign);
		String rest=WXHttpClient.postData(url, XmlParse.mapToXml(map, "xml"), wx_mchid, wxCertPath);
		System.out.println("http请求结果:"+rest);
	}
/** 
	 * 支付签名算法sign 
	 * @param characterEncoding    编码集   UTF-8
	 * @param parameters           传入的有序map
	 * @param key                  传入的秘钥
	 * @return 
	 */  
	@SuppressWarnings({ "rawtypes" })  
	public static String createSignBalance(String characterEncoding,SortedMap<String,Object> parameters,String pkey){  
		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 + "&");  
			}  
		}  
		sb.append("key=" + pkey);  
		String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
		return sign;  
	}  

MD5Util类

package com.sanxia.common.utils;

import java.security.MessageDigest;

public class MD5Util {
	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;  
    }  
  
    /**
     * Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
     * @param src
     * @return
     */
	public static String bytesToHexString(byte[] src){   
	    StringBuilder stringBuilder = new StringBuilder("");   
	    if (src == null || src.length <= 0) {   
	        return null;   
	    }   
	    for (int i = 0; i < src.length; i++) {   
	        int v = src[i] & 0xFF;   
	        String hv = Integer.toHexString(v);   
	        if (hv.length() < 2) {   
	            stringBuilder.append(0);   
	        }   
	        stringBuilder.append(hv);   
	    }   
	    return stringBuilder.toString();   
	} 
    
    
    
    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",  
        "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };  
}

WXHttpClient类

@SuppressWarnings("deprecation")
public class WXHttpClient {
 
    private static int socketTimeout = 10000;// 连接超时时间,默认10秒
    private static int connectTimeout = 30000;// 传输超时时间,默认30秒
    private static RequestConfig requestConfig;// 请求器的配置
    private static CloseableHttpClient httpClient;// HTTP请求器
 
    /**
     * 通过Https往API post xml数据
     *
     * @param url API地址
     * @param xmlObj 要提交的XML数据对象
    * @param mchId 商户ID
    * @param certPath 证书位置
     * @return
     */
    public static String postData(String url, String xmlObj, String mchId, String certPath) {
        // 加载证书
        try {
            initCert(mchId, certPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = null;
        HttpPost httpPost = new HttpPost(url);
        // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
        StringEntity postEntity = new StringEntity(xmlObj, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.setEntity(postEntity);
        // 根据默认超时限制初始化requestConfig
        requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
        // 设置请求器的配置
        httpPost.setConfig(requestConfig);
        try {
            HttpResponse response = null;
            try {
                response = httpClient.execute(httpPost);
            } catch (IOException e) {
                e.printStackTrace();
            }
            HttpEntity entity = response.getEntity();
            try {
                result = EntityUtils.toString(entity, "UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            httpPost.abort();
        }
        return result;
    }
 
    /**
     * 加载证书
     *
     * @param mchId 商户ID
     * @param certPath 证书位置
     * @throws Exception
     */
    private static void initCert(String mchId, String certPath) throws Exception {
        // 证书密码,默认为商户ID
        String key = mchId;
        // 证书的路径
        String path = certPath;
        // 指定读取证书格式为PKCS12
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        // 读取本机存放的PKCS12证书文件
        FileInputStream instream = new FileInputStream(new File(path));
        try {
            // 指定PKCS12的密码(商户ID)
            keyStore.load(instream, key.toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build();
        SSLConnectionSocketFactory sslsf =
                new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null,
                        SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wangyue23com

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

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

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

打赏作者

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

抵扣说明:

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

余额充值