后台代码:
public Map<String,String> wxPayMoney(HttpServletRequest request, String payinfoid, String paymoney,String userip,String openId) throws Exception {
System.out.println("微信支付后台开始");
WxpayParam wxpayParam = new WxpayParam();
wxpayParam.setTotalFee(paymoney);
WxPayConfig ourWxPayConfig = new WxPayConfig();
System.out.println("支付时openId:======="+openId);
//获取请求ip地址
String out_trade_no =getTradeNo();
System.out.println("out_trade_no:"+out_trade_no);
String ip = request.getHeader("x-forwarded-for");
String notifyUrl ="https://www.12333333.com.cn"; //我这里的回调地址是随便写的,到时候需要换成处理业务的回调接口
WXPay wxPay = new WXPay(ourWxPayConfig);
//根据微信支付api来设置
TreeMap<String,String> paramMap = new TreeMap();
TreeMap<String,String> dataMap = new TreeMap();
paramMap.put("appid",ourWxPayConfig.getAppID());
paramMap.put("body",wxpayParam.getBody());
paramMap.put("fee_type","CNY");
paramMap.put("mch_id",ourWxPayConfig.getMchID());
paramMap.put("nonce_str",WXPayUtil.generateNonceStr());
paramMap.put("notify_url",notifyUrl);
paramMap.put("openid",openId);
paramMap.put("out_trade_no",out_trade_no); //交易号
paramMap.put("spbill_create_ip",userip);
paramMap.put("total_fee",wxpayParam.getTotalFee());
paramMap.put("trade_type","JSAPI");
dataMap.put("appid",ourWxPayConfig.getAppID());
dataMap.put("nonceStr",WXPayUtil.generateNonceStr());
String sign = WXPayUtil.generateSignature(paramMap, ourWxPayConfig.getKey()); //第一次签名
paramMap.put("sign",sign);
String xml = WXPayUtil.mapToXml(paramMap);//将所有参数(map)转xml格式
System.out.println("发送的xml:"+xml);
String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//String xmlStr = HttpRequest.sendPost(unifiedorder_url, xml);//发送post请求"统一下单接口"返回预支付id:prepay_id
String xmlStr = HttpUtil.sendPostXml(unifiedorder_url, xml, null);
System.out.println("xmlStr:======="+xmlStr);
String prepay_id = "";//预支付id
if (xmlStr.indexOf("SUCCESS") != -1) {
Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
prepay_id = (String) map.get("prepay_id");
dataMap.put("package","prepay_id="+prepay_id);
}
dataMap.put("signType",WXPayConstants.SignType.MD5.toString());
dataMap.put("timeStamp",String.valueOf(System.currentTimeMillis()/1000));
String secSign = WXPayUtil.generateSignature(paramMap, ourWxPayConfig.getKey()); //第二次签名
/** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
Map<String, String> respData = wxPay.unifiedOrder(paramMap);
respData.put("out_trade_no",payinfoid); //交易号
if (respData.get("return_code").equals("SUCCESS")){
System.out.println("微信统一下单");
//返回给APP端的参数,APP端再调起支付接口
respData.put("appId",ourWxPayConfig.getAppID());
respData.put("mchid",ourWxPayConfig.getMchID());
respData.put("paySign",secSign);
respData.put("signType", "MD5");
respData.put("total_fee",paymoney);
respData.put("timeStamp",dataMap.get("timeStamp"));
respData.put("nonceStr", dataMap.get("nonceStr"));
respData.put("package", "prepay_id=" + prepay_id);
respData.put("prepay_id",prepay_id);
System.out.println("respData:"+respData);
return respData;
}
System.out.println("微信支付后台结束");
throw new Exception(respData.get("return_msg"));
}
前台代码:
function pay_weixin(){
$.ajax({
type: "get",
url: http+'pay/wxPayMoney',
data: {payinfoid : payinfoid,paymoney:paymoney,userip:userip,openId:openid},
dataType: "json",
success: function(payConfig) {
var datamap={
"appId": payConfig.appid,
"nonceStr":payConfig.nonceStr,
"package": payConfig.package,
"paySign": payConfig.paySign,
"signType": "MD5",
"timeStamp": payConfig.timeStamp
};
alert("paySign"+datamap.paySign);
WeixinJSBridge.invoke(
'getBrandWCPayRequest', datamap,
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ){
// 使用以上方式判断前端返回,微信团队郑重提示:
//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
}
});
}
})
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();
}
}
dataMap.put("timeStamp",String.valueOf(System.currentTimeMillis()/1000));
把这个值打印出来,然后对比文档,看下