public static void sendRedPack(String mch_billno,String openId,String send_name,String total_fee,String total_num,String wishing,String act_name,String remark,String ip) throws Exception{
String non=PayCommonUtil.CreateNoncestr();
SortedMap<Object, Object> p = new TreeMap<Object, Object>();
p.put("nonce_str", non);
p.put("mch_billno", mch_billno);
p.put("mch_id", ConfigUtil.MCH_ID);
p.put("wxappid", ConfigUtil.APPID);
p.put("re_openid", openId);
p.put("total_amount", total_fee);
p.put("total_num", "1");
p.put("client_ip", "127.0.0.1");
p.put("act_name",act_name);
p.put("send_name", send_name);
p.put("wishing", wishing);
p.put("remark",remark);
String sign = PayCommonUtil.createSign("UTF-8", p);
System.out.println(sign);
p.put("sign", sign);
String reuqestXml = PayCommonUtil.getRequestXml(p);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(ConfigUtil.CERT_PATH));
try {
keyStore.load(instream, ConfigUtil.MCH_ID.toCharArray());
} finally {
instream.close();
}
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,
ConfigUtil.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();
try {
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");// 退款接口
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
System.out.println("executing request" + httpPost.getRequestLine());
//请求的xml需转码为iso8859-1编码,否则易出现签名错误或红包上的文字显示有误
StringEntity reqEntity = new StringEntity(new String(reuqestXml.getBytes(), "ISO8859-1"));
// 设置类型
httpPost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength());
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(entity.getContent(), "UTF-8"));
String text;
while ((text = bufferedReader.readLine()) != null) {
System.out.println(text);
}
}
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
StringEntity reqEntity = new StringEntity(new String(reuqestXml.getBytes(), "ISO8859-1"));
public static void main(String args[]){
try {
sendRedPack("12828839012016101420","接收者的openid","xxx","100","1","恭喜发财,年年有余","新年红包","新年红包还不快抢","127.0.0.1");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
TTP/1.1 200 OK Response content length: 567 <xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[发放成功]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <err_code><![CDATA[SUCCESS]]></err_code> <err_code_des><![CDATA[发放成功]]></err_code_des> <mch_billno><![CDATA[12828839012016101421]]></mch_billno> <mch_id><![CDATA[1282883901]]></mch_id> <wxappid><![CDATA[xxxxx]]></wxappid> <re_openid><![CDATA[xxxx]]></re_openid> <total_amount>100</total_amount> <send_listid><![CDATA[1000041701201610143000090813093]]></send_listid> </xml>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有