xml version="1.0" encoding="UTF-8" standalone="yes"?> <xml> <appid>wxb1427ebebexxxxxxappid> <body>XXX费用body> <device_info>WEBdevice_info> <mch_id>132186xxxxmch_id> <nonce_str>6AED000AF86A084F9CB0264161E29DD3nonce_str> <notify_url>https://一个域名/api/wechatPay/jsapiPayNotifynotify_url> <openid>oo8WUt0taCqjt552htW1vw-xxxxxopenid> <out_trade_no>1out_trade_no> <sign>各种排序+key生成的那个signsign> <total_fee>1total_fee> <trade_type>JSAPItrade_type> xml>
代码如下
String requestXML = PayCommonUtil.getRequestXml(packageParams);
/**
* @author
* @date 2016-4-22
* @Description:将请求参数转换为xml格式的string
* @param parameters
* 请求参数
* @return
*/
public static String getRequestXml(SortedMap parameters) {
StringBuffer sb = new StringBuffer();
sb.append("");
Set es = parameters.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {
sb.append("<" + k + ">" + "" + k + ">");
} else {
sb.append("<" + k + ">" + v + "" + k + ">");
}
}
sb.append(" ");
return sb.toString();
}
我们需要的,就是这货
prepay_id
拿到prepay_id后 进行下一步 生成paySign 签名 代码如下
SortedMap packagePara = new TreeMap();
//自1970年1月1日 0点0分0秒以来的秒数。注意:部分系统取到的值为毫秒级,需要转换成秒(10位数字)。
long timeSt = (LLPayUtil.getPhoneCurrentDateTimeStr());
String timeStr = Long.toString(timeSt);
packagePara.put("appId", appid); //公众号名称,由商户传入
packagePara.put("timeStamp",timeStr); //时间戳,自1970年以来的秒数
packagePara.put("nonceStr", nonce_str); // 随机串
packagePara.put("package", "prepay_id="+urlCode); //prepay_id
packagePara.put("signType", "MD5"); //微信签名方式
String paySign = PayCommonUtil.createSign("UTF-8", packagePara, key); // 微信签名
/**
* @author
* @date 2016-4-22
* @Description:paySign 签名
* @param characterEncoding
* 编码格式
* @param parameters
* 请求参数
* @return
*/
public static String createSign(String characterEncoding, SortedMap packageParams, String API_KEY) {
StringBuffer sb = new StringBuffer();
Set es = packageParams.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + API_KEY);
String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
return sign;
}
拼接好参数之后 传回给前台
if(null != paySign){
model.put("res", true);
model.put("appId", appid);
model.put("timeStamp", timeStr);
model.put("nonceStr", nonce_str);
model.put("packag","prepay_id="+urlCode);
model.put("signType", "MD5");
model.put("paySign", paySign);
return model;
}
注意:记得添加授权目录
前台JSP代码
//微信充值
function wechat(){
var phone = '${userInfo.phone}';
var money = $("#money").val();
if(money == "" || money == 0){
layer.msg("充值的金额不能为空");
return false;
}
var moneyCheck = /^[0-9]+(.[0-9]{1,2})?$/;
if(!moneyCheck.test(money)){
layer.msg("请保留两位小数!");
return;
}
$.ajax({
url: '<%=WEBPATH%>/fund/phoenWechatpay',
type: "post",
async: false,
data: {"money" : money},
success: function(data){
if(data.res){
callPay(data.appId,data.timeStamp,data.nonceStr,
data.packag,data.signType,data.paySign);
}else{
alert("充值失败!");
}
}
})
}
//调用微信内置浏览器的微信支付
function callPay(appId,timeStamp,nonceStr,
packag,signType,paySign) {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
} else {
onBridgeReady(appId,timeStamp,nonceStr,
packag,signType,paySign);
}
}
//提交微信充值
function onBridgeReady(appId,timeStamp,nonceStr,
packag,signType,paySign) {
// alert("appId:"+appId+",timeStamp:"+timeStamp+",nonceStr:"+nonceStr+",packag:"+packag+",signType:"+signType+",paySign:"+paySign);
WeixinJSBridge.invoke('getBrandWCPayRequest', {
"appId" : appId, //公众号名称,由商户传入
"timeStamp" : timeStamp, //时间戳,自1970年以来的秒数
"nonceStr" : nonceStr, //随机串
"package" : packag, //"prepay_id=u802345jgfjsdfgsdg888",
"signType" : signType, //微信签名方式:
"paySign" : paySign //微信签名
}, function(res) { // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
if (res.err_msg == "get_brand_wcpay_request:ok") {
//充值成功后返回页面
window.location.replace("<%=WEBPATH %>/fund/phone_return_url");
}
if (res.err_msg == "get_brand_wcpay_request:cancel") {
layer.msg("交易取消");
return false;
}
if (res.err_msg == "get_brand_wcpay_request:fail") {
layer.msg("支付失败");
return false;
}
});
}
效果图如下