支付功能
首先需要自己在微信平台申请账户,将openid与key放入配置文件中进行读取,在公众平台配置支付回调地址,还需要下载好加密的证书,对信息进行加密
编写代码按照微信提供的文档进行拼接自己的参数,进行加密处理后请求微信提供的地址,因为我写的是小程序,在登陆时候就保存用户的openid了,直接获取即可。同时配置的回调地址需要外网能够访问。
@RequestMapping(value = "do", method = RequestMethod.POST)
public R payVip(@RequestBody WxVo vo) throws Exception {
User user = userService.getById(vo.getUserid());
String userOpenid = user.getOpenid();
if (StringUtils.isEmpty(userOpenid)){
StringBuffer baseAccessTokenUrl = new StringBuffer()
.append("https://api.weixin.qq.com/sns/jscode2session")
.append("?appid=%s")
.append("&secret=%s")
.append("&js_code=%s")
.append("&grant_type=authorization_code");
String accessTokenUrl = String.format(baseAccessTokenUrl.toString(),
ConstantPropertiesUtil.WX_OPEN_APP_ID,
ConstantPropertiesUtil.WX_OPEN_APP_SECRET,
vo.getCode());
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(accessTokenUrl).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String body = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(body);
String session_key = jsonObject.getString("session_key");
String openid = jsonObject.getString("openid");
userOpenid=openid;
}else {
return R.error().message("微信授权失败");
}
}
//判断自己的业务逻辑
//开始支付,创建数据体
JSONObject order = new JSONObject();
order.put("appid", ConstantPropertiesUtil.WX_OPEN_APP_ID);
order.put("mchid",ConstantPropertiesUtil.PARTNER);
order.put("description","支付");
String str_no = new DateTime().toString("yyyyMMddHHmmssSSS");
order.put("out_trade_no",str_no);
System.out.println(str_no);
order.put("notify_url","https://www.dsxssd.com/api/dsxs/wx/payNotice");
JSONObject amount = new JSONObject();
amount.put("total",(long)(companyClass.getMoney()*100));
amount.put("currency","CNY");
order.put(&#