本讲涉及到小程序微信支付、以及java服务器两部分。
商户系统和微信支付系统主要交互:
第一步: 获取客户的openid
小程序端
var that = this;
wx.login({
success: function(res) {
that.getOpenId(res.code);
}
});
//获取openid
getOpenId: function(code){
var that = this;
wx.request({
url: 'https://www.see-source.com/weixinpay/GetOpenId',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {'code':code},
success: function(res) {
var openId = res.data.openid;
that.xiadan(openId);
}
})
}
java后端
String code = request.getParameter("code");
HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?appid="+Configure.getAppID()+"&secret="+Configure.getSecret()+"&js_code="+code+"&grant_type=authorization_code");
//设置请求器的配置
HttpClient httpClient = HttpClients.createDefault();
HttpResponse res = httpClient.execute(httpGet);
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
response.getWriter().append(result);
第二步:调用微信的统一下单接口,返回预订单prepay_id
小程序端
var that = this;
wx.request({
url: 'https://www.see-source.com/weixinpay/xiadan',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {'openid':openId},
success: function(res) {
var prepay_id = res.data.prepay_id;
console.log("统一下单返回 prepay_id:"+prepay_id);
that.sign(prepay_id);
}
})
java后端
String openid = request.getParameter("openid");
OrderInfo order = new OrderInfo();
order.setAppid(Configure.getAppID());
order.setMch_id(Configure.getMch_id());
order.setNonce_str(RandomStringGenerator.getRandomStringByLength(32));
order.setBody("dfdfdf");
order.setOut_trade_no(RandomStringGenerator.getRandomStringByLength(32));
order.setTotal_fee(10);
order.setSpbill_create_ip("123.57.218.54");
order.setNotify_url("https://www.see-source.com/weixinpay/PayResult");
order.setTrade_type("JSAPI");
order.setOpenid(openid);
order.setSign_type("MD5");
//生成签名
String sign = Signature.getSign(order);
order.setSign(sign);
String result = HttpRequest.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", order);
System.out.println(result);
L.info("---------下单返回:"+result);
XStream xStream = new XStream();
xStream.alias("xml", OrderReturnInfo.class);
OrderReturnInfo returnInfo = (OrderReturnInfo)xStream.fromXML(result);
JSONObject json = new JSONObject();
json.put("prepay_id", returnInfo.getPrepay_id());
response.getWriter().append(json.toJSONString());
notify_url 是支付完成后就收微信的通知的,告诉你用户是否付款了
注意:Total_fee单位是分,必须是整数,不能是小数
Trade_type字段对于小程序来说固定写成JSAPI
第三步:再次签名
小程序端
var that = this;
wx.request({
url: 'https://www.see-source.com/weixinpay/sign',
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
'repay_id':prepay_id
},
success: function(res) {
that.requestPayment(res.data);
}
})
java后端
String repay_id = request.getParameter("repay_id");
SignInfo signInfo = new SignInfo();
signInfo.setAppId(Configure.getAppID());
long time = System.currentTimeMillis()/1000;
signInfo.setTimeStamp(String.valueOf(time));
signInfo.setNonceStr(RandomStringGenerator.getRandomStringByLength(32));
signInfo.setRepay_id("prepay_id="+repay_id);
signInfo.setSignType("MD5");
//生成签名
String sign = Signature.getSign(signInfo);
JSONObject json = new JSONObject();
json.put("timeStamp", signInfo.getTimeStamp());
json.put("nonceStr", signInfo.getNonceStr());
json.put("package", signInfo.getRepay_id());
json.put("signType", signInfo.getSignType());
json.put("paySign", sign);
L.info("-------再签名:"+json.toJSONString());
response.getWriter().append(json.toJSONString());
第四步:小程序调起支付
wx.requestPayment({
'timeStamp': obj.timeStamp,
'nonceStr': obj.nonceStr,
'package': obj.package,
'signType': obj.signType,
'paySign': obj.paySign,
'success':function(res){
},
'fail':function(res){
}
})
接着输入支付密码即可。