一、设置支付目录
请确保实际支付时的请求目录与后台配置的目录一致(现在已经支持配置根目录,配置后有一定的生效时间,一般5分钟内生效),否则将无法成功唤起微信支付。
在微信商户平台(pay.weixin.qq.com)设置您的JSAPI支付支付目录,设置路径:商户平台–>产品中心–>开发配置,如图7.7所示。JSAPI支付在请求支付的时候会校验请求来源是否有在商户平台做了配置,所以必须确保支付目录已经正确的被配置,否则将验证失败,请求支付不成功。
二、设置授权域名
开发JSAPI支付时,在统一下单接口中要求必传用户openid,而获取openid则需要您在公众平台设置获取openid的域名,只有被设置过的域名才是一个有效的获取openid的域名,否则将获取失败。具体界面如图7.8所示:
三.将支付页面的连接编码后替换连接中的redirect-url参数,获取code
参考链接(请在微信客户端中打开此链接体验)
Scope为snsapi_base ,此连接为静默获取用户授权
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=http%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
四.通过code获取openid,有openid后就可以调用统一下单接口了,此时secret不能忽略,在微信公众平台获取
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
五.调用统一下单接口,AppID等敏感信息我封装到实体类中了
/**
* 微信内浏览器支付
*
* @return
*/
@PostMapping("/createJSAPI")
@ApiOperation("微信内支付,orderId订单ID,openId用户唯一识别ID")
public DzResult createJSAPI(String orderId, String openId) throws Exception {
if (StringUtils.isBlank(orderId) || StringUtils.isBlank(openId)) {
System.out.println("微信内支付空订单号-------------------->");
return new DzResult().error(OrderCode.ERROR_PARAMS, "参数错误");
}
// 获取当前用户
Claims claims = (Claims) request.getAttribute("authInfo");
if (claims == null) {
return new DzResult().error(OrderCode.NOT_LEGAL, "非法操作");
}
// 调用业务层查询订单信息
CyOrder order = weixinPayService.findOrderByOrderId(orderId);
if (order == null) {
return new DzResult().error(OrderCode.ORDER_NOT_EX