微信小程序支付(需申请小程序https://mp.weixin.qq.com/wxopen/waregister?action=step1获取appid和开通微信支付),主要步骤如下
1.流程
小程序获取openid(下单时openid非必填)-->调用后台下单接口 -->获取到prepay_id等参数-->调起微信支付-->用户输入密码支付-->微信前台通知 后台回调通知支付结果
2.后台统一下单接口
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1&index=1
请求(主要参数)
小程序ID appid 是 String(32) wxd678efh567hg6787 微信分配的小程序ID
商户号 mch_id 是 String(32) 1230000109 微信支付分配的商户号
返回(主要参数)
预支付交易会话标识 prepay_id 是 String(64) wx201410272009395522657a690389285100
随机字符串 nonce_str 是 String(32) 5K8264ILTKCH16CQ2502SI8ZNMTM67VS
签名 key为商户平台设置的密钥key
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
3.小程序支付(使用下单返回值)
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=5
wx.requestPayment(
{
"timeStamp":"时间戳",
"nonceStr": "32位随机数",
"package": "prepay_id=wx2017033010242291fcfe0db70013231072",
"signType": "MD5",
"paySign": "签名",
"success":function(res){},
"fail":function(res){},
"complete":function(res){}
})
paySign = MD5(appId=wxd678efh567hg6787&nonceStr=5K8264ILTKCH16CQ2502SI8ZNMTM67VS&package=prepay_id=wx2017033010242291fcfe0db70013231072&signType=MD5&timeStamp=1490840662&key=qazwsxedcrfvtgbyhnujmikolp111111) = 22D9B4E54AB1950F51E0649E8810ACD6
key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置
4.小程序获取用户openId
根据用户登录后code获取
wx.login({
success (res) {
if (res.code) {
//发起网络请求
console.log('登录成功!' + res.code)
wx.request({
url: 'https://example.com/onLogin',
data: {
code: res.code
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
https://developers.weixin.qq.com/community/develop/article/doc/000c80906b4210625f3bde3775bc13
@RequestMapping("/testopenid")
public String getUserInfo(@RequestParam(name = "code") String code) throws Exception {
System.out.println("code" + code);
String url = "https://api.weixin.qq.com/sns/jscode2session";
url += "?appid=xxxxxxxxxxxxx";//自己的appid
url += "&secret=xxxxxxxxxxxxxxxxxxx";//自己的appSecret
url += "&js_code=" + code;
url += "&grant_type=authorization_code";
url += "&connect_redirect=1";
String res = null;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// DefaultHttpClient();
HttpGet httpget = new HttpGet(url); //GET方式
CloseableHttpResponse response = null;
// 配置信息
RequestConfig requestConfig = RequestConfig.custom() // 设置连接超时时间(单位毫秒)
.setConnectTimeout(5000) // 设置请求超时时间(单位毫秒)
.setConnectionRequestTimeout(5000) // socket读写超时时间(单位毫秒)
.setSocketTimeout(5000) // 设置是否允许重定向(默认为true)
.setRedirectsEnabled(false).build(); // 将上面的配置信息 运用到这个Get请求里
httpget.setConfig(requestConfig); // 由客户端执行(发送)Get请求
response = httpClient.execute(httpget); // 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
res = EntityUtils.toString(responseEntity);
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + res);
}
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
JSONObject jo = JSON.parseObject(res);
String openid = jo.getString("openid");
System.out.println("openid" + openid);
return openid;
}