#简介
现金红包,是微信支付商户平台提供的营销工具之一,上线以来深受广大商户与用户的喜爱。商户可以通过本平台向微信支付用户发放现金红包。用户领取红包后,资金到达用户微信支付零钱账户,和零钱包的其他资金有一样的使用出口;若用户未领取,资金将会在24小时后退回商户的微信支付账户中。
微信支付api文档
PS:此功能需要用户申请微信商家支付,下载支付证书。
package com.lh.wx.service;
import okhttp3.*;
import okhttp3.internal.platform.Platform;
import org.apache.http.ssl.SSLContexts;
import com.lh.utils.common.DateUtils;
import com.lh.wx.model.SendRedPack;
import com.lh.wx.utils.Tool;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.Date;
import java.util.TreeMap;
public class WeChatSendRedPack {
public static void main(String[] args) throws Exception {
String nonce_str = System.currentTimeMillis() + "";
// 设置订单号
String orderid ="1325860101"
+DateUtils.format(new Date(), "yyyyMMddHHmmss");
//具体参数查看具体实体类,实体类中的的参数参考微信的红包发放接口,这里你直接用map,进行设置参数也可以。。。
SendRedPack sendRedPack = new SendRedPack(
nonce_str,
orderid,
"商户号",
"公众号appid",
"蓝海",
"用户Openid",
100,
1,
"恭喜发财",
"127.0.0.1",
"微信推广",
"蓝海",
"PRODUCT_5"
);
String content=sendRedPackage(sendRedPack);
System.out.println(content);
}
@SuppressWarnings("unchecked")
public static String sendRedPackage(SendRedPack sendRedPack)throws Exception{
//将实体类转换为url形式
String urlParamsByMap = Tool.getUrlParamsByMap(Tool.toMap(sendRedPack));
//拼接我们再前期准备好的API密钥,前期准备第5条
urlParamsByMap += "&key=支付key";
//进行签名,需要说明的是,如果内容包含中文的话,要使用utf-8进行md5签名,不然会签名错误
String sign = Tool.parseStrToMd5L32(urlParamsByMap).toUpperCase();
sendRedPack.setSign(sign);
//微信要求按照参数名ASCII字典序排序,这里巧用treeMap进行字典排序
@SuppressWarnings("rawtypes")
TreeMap treeMap = new TreeMap(Tool.toMap(sendRedPack));
//然后转换成xml格式
String soapRequestData = Tool.getSoapRequestData(treeMap);
//发起请求前准备
RequestBody body = RequestBody.create(MediaType.parse("text/xml;charset=UTF-8"), soapRequestData);
Request request = new Request.Builder()
.url("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack")
.post(body)
.build();
//为http请求设置证书
SSLSocketFactory socketFactory = getSSL().getSocketFactory();
X509TrustManager x509TrustManager = Platform.get().trustManager(socketFactory);
OkHttpClient okHttpClient = new OkHttpClient.Builder().sslSocketFactory(socketFactory, x509TrustManager).build();
//得到输出内容
Response response = okHttpClient.newCall(request).execute();
String content = response.body().string();
return content;
}
public static SSLContext getSSL() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
//证书位置自己定义
FileInputStream instream = new FileInputStream(new File("D:/soft/apiclient_cert.p12"));
try {
keyStore.load(instream, "商户号".toCharArray());
} finally {
instream.close();
}
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, "商户号".toCharArray())
.build();
return sslcontext;
}
}