1、首先可以通过服务端来获取openid,openid可以作为自己平台微信用户身份的唯一标识。
/**
* @Description: 获取openId
* @param: code 小程序授权后获得的code
* @Author: zhangpeng32
* @Date: 2018/3/11 17:39
* @Version: 1.0.0
*/
@RequestMapping(value = "/getOpenId", method = RequestMethod.POST)
public BaseResp<Map<String, String>> getOpenId(String code, HttpServletRequest request) throws Exception{
if (StringUtils.isEmpty(code)) {
return new BaseResp<Map<String, String>>(ResultStatus.error_weixin_user_code_empty);
}
//获取openId
String param = "?grant_type=" + PaymentConfig.GRANT_TYPE + "&appid=" + PaymentConfig.APPID + "&secret=" + PaymentConfig.API_KEY + "&js_code=" + code;
System.out.println(PaymentConfig.GET_OPEN_ID_URL + param);
//创建请求对象
String httpRet = PayUtils.httpRequest(PaymentConfig.GET_OPEN_ID_URL, "GET", param);
System.out.println(httpRet);
Map<String, String> result = new HashMap<String, String>();
JSONObject jsonObject = JSONObject.parseObject(httpRet);
if (jsonObject != null) {
Integer errcode = jsonObject.getInteger("errcode");
if (errcode != null) {
//返回异常信息
return new BaseResp<Map<String, String>>(errcode, jsonObject.getString("errmsg"), null);
}
result.put("openId", jsonObject.getString("openid"));
result.put("sessionKey", jsonObject.getString("session_key"));
}
return new BaseResp<Map<String, String>>(ResultStatus.SUCCESS, result);
}
2、调用支付统一下单API来获取prepay_id,并将小程序调起支付数据需要签名的字段appId,timeStamp,nonceStr,package再次签名。
特别要注意,调用官方的SDK默认的加密方式为HMACSHA256,SKD里面wxpay的初始化源码如下:
public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception