前端js
uni.request({
url: that.urlPath + 'workfolder/toWeChatRequestPay',
data: {
body: that.copyrightOpusOrderinfo.sku_name,
out_trade_no: that.copyrightOpusOrderinfo.order_sn,
total_fee: parseInt(that.copyrightOpusOrderinfo.real_price*100),
openid: uni.getStorageSync('self_user').openid,
attach: 'WeChat'
},
method: 'GET',
header: {
'content-type': 'application/json'
},
success(res){
// console.log(res.data)
// console.log('prepay_id=' + res.data.prepay_id)
//前去支付
uni.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonce_str,
package: 'prepay_id=' + res.data.prepay_id,
signType: 'MD5',
paySign: res.data.signKeyPay,
success(res){
// console.log(res.data)
//支付成功,回调修改状态
//that.updateCopyrightOpusOrder()
},
fail(error){
console.log(error.errMsg)
if (res.errMsg === 'requestPayment: fail cancel') {
uni.showToast({
title: '取消支付',
icon: 'none'
})
} else {
uni.showToast({
title: '请重新支付',
icon: 'none'
})
}
}
})
}
})
@RequestMapping(value = "/toWeChatRequestPay" , method = { RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public Map<String, String> toWeChatRequestPay(String body, String out_trade_no, String openid, String total_fee, String attach) {
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
String spbill_create_ip = addr.getHostAddress();
Map<String, String> auMap = new HashMap<>();
auMap.put("appid", DataConfig.APPID);
auMap.put("attach", attach);
auMap.put("body", body);
auMap.put("detail", "线上版权登记付款");
auMap.put("mch_id", DataConfig.MCHID);//商户号
auMap.put("nonce_str", WXPayUtil.generateNonceStr().toString());//随机数
auMap.put("notify_url", "https://www.1ipr.top/yysipr_beta/workfolder");
auMap.put("openid", openid);
auMap.put("out_trade_no", out_trade_no);//商户订单号 Long.toString(System.currentTimeMillis())
auMap.put("spbill_create_ip", spbill_create_ip);//订单生成的机器 IP
auMap.put("total_fee", total_fee);//总金额
auMap.put("trade_type", "JSAPI");
try {
String xmlParam = WXPayUtil.generateSignedXml(auMap, DataConfig.MCHID_KEY);
// System.out.println(xmlParam);
HttpClientM client= new HttpClientM("https://api.mch.weixin.qq.com/pay/unifiedorder");
client.setHttps(true);
client.setXmlParam(xmlParam);
client.post();
String result = client.getContent();
// System.out.println(result);
Document doc = DocumentHelper.parseText(result);//获取xml文件
Element rootel = doc.getRootElement();//获取根目录
// String prepay_id = rootel.elementText("prepay_id");
auMap.put("prepay_id", rootel.elementText("prepay_id"));
auMap.put("timeStamp", String.valueOf(Instant.now().getEpochSecond()));
String signPay = "appId=" + auMap.get("appid") + "&nonceStr=" + auMap.get("nonce_str")
+ "&package=prepay_id=" + rootel.elementText("prepay_id") + "&signType=" + "MD5" + "&timeStamp=" + String.valueOf(Instant.now().getEpochSecond())
+ "&key=" + DataConfig.MCHID_KEY;
auMap.put("signKeyPay", new BigInteger(1, MessageDigest.getInstance("MD5").digest(signPay.getBytes())).toString(16).toUpperCase());
// System.out.println(prepay_id);
return auMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
// sql = null;
}
}
WXPayUtil等微信sdk
微信支付java-sdk-v3.0.9