1. 前端js(ajax)走后端接口 这个就不多说了
2. 调用微信统一下单接口
Map<String, String> result = wechatAppPayPlugin.getParameterMap((), new BigDecimal("0.01"), ().toString(), "充值支付", request);
()为自定义生成的订单编号
new BigDecimal("0.01") 为当前支付的总价格
().toString() 为当前用户的openid
private static final String WECHAT_PAY_URL = "/pay/unifiedorder"; //统一下单接口
public Map<String, String> getParameterMap(String sn, BigDecimal amount, String openId, String description,
HttpServletRequest request) {
Date now = new Date();
WechatOrderReturn order = orderSubmit(sn, amount, now, openId, getByteStr(description, 250, "..."));
String timestamp = (()).substring(0, 10);
Map<String, String> map = new HashMap<String, String>();
("appId", ());
("nonceStr", order.getNonce_str());// 预支付交易会话标识
("package", "prepay_signType", "MD5");
("timeStamp", timestamp);
Setting setting = ();
String wechatPayKey = (); //微信支付秘钥
String sign = WechatPayUtil.getSignByMap(map, wechatPayKey);
("sign", sign);
return map;
}
// 收集统一下单的参数
public WechatOrderReturn orderSubmit(String sn, BigDecimal amount, Date now, String openId, String description) {
Setting setting = ();
String wechatPayKey = ();
String appid = (); //公众号的appid
String mchId = (); //商户号id
// 构造微信统一下单API
WechatOrder wechatOrder = new WechatOrder();
(appid);
wechatOrder.setMch_id(mchId);
String nonce_str = ().toString().substring(0, 32);// 随机字符串,不长于32位
int total_fee = (new BigDecimal(100)).intValue();// 总额,以分为单位
String spbill_create_ip = "";// 终端ip
wechatOrder.setNonce_str(nonce_str);// 随机字符串 不长于32位
// 商品描述125
if (description == null) {
("xxxxx");
} else if (() > 124) {
((0, 124));
} else {
(description);
}
wechatOrder.setOut_trade_no(sn);// 商户订单号(必须唯一)
wechatOrder.setTotal_fee(total_fee);// 总额 以分为单位
wechatOrder.setSpbill_create_ip(spbill_create_ip);// 终端ip
wechatOrder.setNotify_url(getNtifyUrl(sn));// 接收微信支付异步通知回调地址
wechatOrder.setTrade_type("JSAPI");
(openId);
String sign = WechatPayUtil.getSign(wechatOrder, wechatPayKey);// 生成签名
(sign);
("【微信下单参数】" + new Gson().toJson(wechatOrder));
String xml = (wechatOrder);
xml = ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
("【微信下单参数】" + xml);
String result = WechatPayUtil.httpsRequest(WECHAT_PAY_URL, "POST", xml);
("【微信下单结果】" + result);
WechatOrderReturn wechatOrderReturn = (result, );
(sign);
(sign);
return wechatOrderReturn;
}
//生成签名工具类
public static String getSign(Object bean, String wechatPayKey) {
List<String> list = new ArrayList<String>();
Class<? extends Object> c = ();
Field[] fields = ();
for (int i = 0; i < ; i++) {
Field field = fields[i];
(true); // 设置些属性是可以访问的
Object val = null;
try {
val = (bean);
} catch (IllegalArgumentException e) {
();
} catch (IllegalAccessException e) {
();
}// 得到此属性的值
String name = ();// 得到此属性的名字
if (val != null && "sign".equals(name) == false) {
(name + "=" + ());
}
}
Object[] os = ();
(os);
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < ; i++) {
if (i > 0) {
("&");
}
(os[i]);
}
("&key=");
(wechatPayKey);
String content = ();
("【微信签名字符串】" + content);
String sign = null;
try {
sign = DigestUtils.md5DigestAsHex(("utf-8")).toString();
} catch (UnsupportedEncodingException e) {
();
}
return ();
}
// 发送请求工具类
private static String httpsRequest(String requestUrl, String requestMethod, String outputStr, Integer count) {
String result = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = ("SSL", "SunJSSE");
(null, tm, new ());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = ();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) ();
(ssf);
(true);
(true);
(false);
// 设置请求方式(GET/POST)
(requestMethod);
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = ();
// 注意编码格式
(("UTF-8"));
();
}
// 从输入流读取返回内容
InputStream inputStream = ();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = ()) != null) {
(str);
}
// 释放资源
();
();
();
inputStream = null;
();
result = ();
} catch (ConnectException ce) {
//判断类型为超时重新调用
if(count < 3 && "Connection timed out: connect".equals(())){
//超时调用3次
count++;
return httpsRequest(requestUrl, requestMethod, outputStr, count);
}else{
();
}
} catch (Exception e) {
();
}
(result);
return result;
}
//回调函数
private String getNtifyUrl(String sn) {
Setting setting = ();
StringBuffer sb = new StringBuffer();
(());
("/api/order/notify/wechat/");
(sn);
(".jhtml");
return ();
}
/**
*
* @param str
* 被截取字符串
* @param length
* 截取长度
* @param more
* 更多代替字符
* @return
*/
protected String getByteStr(String str, Integer length, String more) {
StringBuilder sb = new StringBuilder();
int currentLength = 0;
char[] _moreChar = ();
length = length - _moreChar.length;
for (char c : ()) {
try {
currentLength += (c).getBytes("UTF8").length;
} catch (UnsupportedEncodingException e) {
();
}
if (currentLength <= length) {
(c);
} else {
str = () + more;
break;
}
}
return str;
}