微信公众号 JSAPI方式支付
*前端验证地址*
@RequestMapping(value = "/pay", method = RequestMethod.GET)
public @ResponseBody String pay(HttpSession httpSession, HttpServletRequest request, String totalFee, String openId, String outTradeNo,Integer id) throws Exception {
String paramContent = "";
String sign = "";
String nonceStr = create_nonce_str();
TreeMap<String, String> param = new TreeMap<>();
param.put("appid",WXPayConfig.APPID);
param.put("mch_id",WXPayConfig.MCH_ID);
param.put("device_info","WEB");
param.put("nonce_str",nonceStr);
param.put("body","RFYX");
param.put("out_trade_no",outTradeNo);
param.put("total_fee",totalFee);
param.put("trade_type","JSAPI");
param.put("notify_url",WXPayConfig.NOTIFY_URL);
param.put("openid",openId);
param.put("sign_type", "MD5");
String spbill_create_ip = CheckUtil.getIpAddr(request);
if (StringUtils.isEmpty(spbill_create_ip)) {
param.put("spbill_create_ip","192.168.0.1");
} else {
param.put("spbill_create_ip",spbill_create_ip);
}
String text = CheckUtil.generateSignature(param);
sign = MD5.rijisign(text,WXPayConfig.API_KEY,"utf-8").toUpperCase();
param.put("sign",sign);
paramContent = CheckUtil.getXMLFromMap(param);
String orderInfo = HttpsUtil.httpsRequest(WXPayConfig.UNIFIED_ORDER_URL,"POST", paramContent);
String prepay_id;
try {
Map<String, String> orderInfoMap = CheckUtil.xmlToMap(orderInfo);
if (orderInfoMap.get("return_code").equals("SUCCESS") && orderInfoMap.get("result_code").equals("SUCCESS")) {
prepay_id = orderInfoMap.get("prepay_id");
String timestamp = CheckUtil.create_timestamp();
TreeMap<String, String> pageParam = new TreeMap<>();
pageParam.put("appId",WXPayConfig.APPID);
pageParam.put("nonceStr",nonceStr);
pageParam.put("package","prepay_id="+prepay_id);
pageParam.put("timeStamp",timestamp);
pageParam.put("signType","MD5");
String text1 = CheckUtil.generateSignature(pageParam);
String pageSign = MD5.rijisign(text1,WXPayConfig.NOTIFY_URL,"utf-8").toUpperCase();
pageParam.put("paySign",pageSign);
pageParam.put("prepay_id",prepay_id);
int resule = memberrechargeService.insert(memberrecharge);
return JSON.toJSONString(pageParam);
}
} catch (Exception e) {
return null;
}
return null;
}
前端
$.ajax({
url: "*验证地址*?totalFee={param.totalFee}&openId=${param.openid}&id=${param.id}&outTradeNo=" + guid2(),
type: "get",
dataType: "json",
success: function (result) {
console.log(result)
wx.chooseWXPay({
timestamp: result.timeStamp,
nonceStr: result.nonceStr,
package: "prepay_id=" + result.prepay_id,
signType: 'MD5',
paySign: result.paySign,
success: function (res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
window.location.href = "支付成功跳转地址"
}
}
});
},
error: function (data) {
alert("错误!");
alert(data);
}
});