"sign".equalsIgnoreCase(k)) {
sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
} else {
sb.append("<" + k + ">" + v + "</" + k + ">");
}
}
sb.append("</xml>");
return sb.toString();
}
/**
* 创建map
*
* @param billNo
* @param openid
* @param userId
* @param amount
* @return
*/
public static SortedMap<String, String> createMap(String openid, String userId, int amount) {
SortedMap<String, String> params = new TreeMap<String, String>();
params.put("wxappid", WXAPPID);
params.put("nonce_str", createNonceStr());
params.put("mch_billno", createBillNo(userId));
params.put("mch_id", MCH_ID);
params.put("nick_name", NICK_NAME);
params.put("send_name", SEND_NAME);
params.put("re_openid", openid);
params.put("total_amount", amount + "");
params.put("min_value", amount + "");
params.put("max_value", amount + "");
params.put("total_num", TOTAL_NUM + "");
params.put("wishing", WISHING);
params.put("client_ip", CLIENT_IP);
params.put("act_name", ACT_NAME);
params.put("remark", REMARK);
return params;
}
/**
* 生成随机字符串
*
* @return
*/
public static String createNonceStr() {
return UUID.randomUUID().toString().toUpperCase().replace("-", "");
}
/**
* 生成商户订单号
*
* @param mch_id
* 商户号
* @param userId
* 该用户的userID
* @return
*/
public static String createBillNo(String userId) {
// 组成: mch_id+yyyymmdd+10位一天内不能重复的数字
// 10位一天内不能重复的数字实现方法如下:
// 因为每个用户绑定了userId,他们的userId不同,加上随机生成的(10-length(userId))可保证这10位数字不一样
Date dt = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyymmdd");
String nowTime = df.format(dt);
int length = 10 - userId.length();
return MCH_ID + nowTime + userId + getRandomNum(length);
}
/**
* 生成特定位数的随机数字
*
* @param length
* @return
*/
private static String getRandomNum(int length) {
String val = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
val += String.valueOf(random.nextInt(10));
}
return val;
}
/**
* post提交到微信服务器
*
* @param requestXML
* @param instream 传入的在微信支付的PKCS12证书的位置
* @return
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
* @throws KeyManagementException
* @throws UnrecoverableKeyException
* @throws KeyStoreException
*/
public static String post(String requestXML, InputStream instream) throws Exception {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
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();
String result = "";
try {
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
StringEntity reqEntity = new StringEntity(requestXML, "utf-8"); // 如果此处编码不对,可能导致客户端签名跟微信的签名不一致
reqEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(entity.getContent(), "UTF-8"));
String text;
while ((text = bufferedReader.readLine()) != null) {
result += text;
}
}
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
return result;
}
}
调用:
@Test
public void testHongBao() throws Exception {
SortedMap<String, String> sortedMap = HongBaoUtil.createMap(openId, userId, money);
HongBaoUtil.sign(sortedMap);
String postXML = HongBaoUtil.getRequestXml(sortedMap);
FileInputStream instream = new FileInputStream(new File("证书文件地址"));
HongBaoUtil.post(postXML, instream);
}好咯 ...别被微信支付后台的那么多的配置和参数侠盗 ,微信红包开发就是这么简单 ~
【相关推荐】
1. 分享小程序开发调用接口的实例教程
2. 微信小程序支付接口的实例详解
3. 微信开发之微信支付
以上就是小程序之红包接口开发实例代码的详细内容,更多请关注php中文网其它相关文章!
小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用。
关键词:小程序之红包接口开发案例代码