[ 最近的机器内存又爆满了,出了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于随意化,这些不好的习惯或对程序语言的不了解是应该好好打压打压了
话不多说直接上餐
主类
package com.yuwei.redpack.main;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import com.yuwei.redpack.sign.Signature;
import com.yuwei.redpack.util.Configure;
import com.yuwei.redpack.util.HttpsRequest;
import com.yuwei.redpack.util.RequestUtil;
public class CopyOfMain {
public static void main(String[] args) throws Exception{
initSDKConfiguration("","","","","");
RedpackBean bean = new RedpackBean();
bean.setNonce_str(RequestUtil.getRandomStringByLength(32));
bean.setMch_billno("10位商户号"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+new Random().nextInt(10));
bean.setMch_id("商户号");
bean.setWxappid("微信appid");
bean.setSend_name("商户名称");
bean.setRe_openid("接收用户openid");
bean.setTotal_amount(111);//金额分
bean.setTotal_num(1);//发送人数
bean.setWishing("红包祝福语");
bean.setClient_ip("活动名称");
bean.setAct_name("java");
bean.setRemark("备注");
bean.setSign(Signature.getSign(bean));
HttpsRequest https = new HttpsRequest();
String rets = https.sendPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", bean);
System.out.println(rets);
}
/**
*
* @param key API密钥
* @param mchID 商户id
* @param wxappid 公众号id
* @param certLocalPath 证书路径
* @param certPassword 证书密码
*/
private static void initSDKConfiguration(String key, String mchID, String wxappid, String certLocalPath, String certPassword){
Configure.setKey(key);
Configure.setMchID(mchID);
Configure.setWxappid(wxappid);
Configure.setCertLocalPath(certLocalPath);
Configure.setCertPassword(certPassword);
}
}
请求类
[1、接口文档https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.phpchapter=13_52、证书下载及证书安装首先,商户调用微信红包接口时,服务器会进行证书验证,所以要在
package com.yuwei.redpack.main;
public class RedpackBean {
private String nonce_str;
private String sign;
private String mch_billno;
private String mch_id;
private String wxappid;
private String send_name;
private String re_openid;
private int total_amount;
private int total_num;
private String wishing;
private String client_ip;
private String act_name;
private String remark;
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonceStr) {
nonce_str = nonceStr;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getMch_billno() {
return mch_billno;
}
public void setMch_billno(String mchBillno) {
mch_billno = mchBillno;
}
public String getMch_id() {
return mch_id;
}
public void setMch_id(String mchId) {
mch_id = mchId;
}
public String getWxappid() {
return wxappid;
}
public void setWxappid(String wxappid) {
this.wxappid = wxappid;
}
public String getSend_name() {
return send_name;
}
public void setSend_name(String sendName) {
send_name = sendName;
}
public String getRe_openid() {
return re_openid;
}
public void setRe_openid(String reOpenid) {
re_openid = reOpenid;
}
public int getTotal_amount() {
return total_amount;
}
public void setTotal_amount(int totalAmount) {
total_amount = totalAmount;
}
public int getTotal_num() {
return total_num;
}
public void setTotal_num(int totalNum) {
total_num = totalNum;
}
public String getWishing() {
return wishing;
}
public void setWishing(String wishing) {
this.wishing = wishing;
}
public String getClient_ip() {
return client_ip;
}
public void setClient_ip(String clientIp) {
client_ip = clientIp;
}
public String getAct_name() {
return act_name;
}
public void setAct_name(String actName) {
act_name = actName;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
知道各位对sign比较头痛,我上方法
public static String getSign(Object o) throws IllegalAccessException {
ArrayList list = new ArrayList();
Class cls = o.getClass();
Field[] fields = cls.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
if (f.get(o) != null && f.get(o) != "") {
list.add(f.getName() + "=" + f.get(o) + "&");
}
}
int size = list.size();
String [] arrayToSort = list.toArray(new String[size]);
Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < size; i ++) {
sb.append(arrayToSort[i]);
}
String result = sb.toString();
result += "key=" + Configure.getKey();
result = MD5.MD5Encode(result).toUpperCase();
return result;
}用java反射的,适合所有class
祝各位成功!
[目录()[-]功能介绍微信红包发送规则商户侧调用红包接口流程用户交互流程接口详细说明一开发现金红包SDK二构造现金红包并发送本文介绍的是基于Java语言开发微信现金红包的