微信小程序支付V3版本
项目需要微信小程序支付,好多资料里都是旧版本,没有APIv3新版本,经过各种碰壁,准备把支付以及支付回调部分开发过程以及参数的细节记录下来 。
– 2021-02-07
微信支付文档参考地址:https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_2.shtml
第一步:导入maven
wechatpay-apache-httpclient来源于文档中git中
huTool工具包 用于解析支付回调信息
<!-- 微信小程序支付 -->
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.1</version>
</dependency>
<!-- huTool 工具包 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
第二步:Controller层
createOrder创建订单 GetMapping
total 为金额 分做单位
description 为商品描述
appletOpenId 我的项目中需要做token验证,取openid
参数和返回值根据自己项目需要修改
notify 订单支付后回调 PostMapping
@GetMapping("/createOrder")
public AjaxResult createOrder(HttpServletRequest request,Long total,String description) throws Exception
{
//获取用户ID
SysUser sysUser = tokenService.getLoginUser(request).getUser();
String appletOpenId = sysUser.getAppletOpenId();
System.out.println("appletOpenId:" + appletOpenId);
startPage();
Map<String, Object> resultMap = wecharpayService.createOrder(total,description,appletOpenId);
AjaxResult ajax = AjaxResult.success();
ajax.put("data", resultMap);
return ajax;
}
@PostMapping("/notify")
public String notify(HttpServletRequest request,HttpServletResponse response) throws Exception {
String result = wecharpayService.notify(request,response);
System.out.println("===========wxpay notify message:" + result);
return result;
}
第三步:Service层
创建订单:
即开发文档中统一下单:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_1.shtml
在创建订单之前,需要以下开发参数
加载商户私钥(privateKey:私钥字符串)
加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3秘钥)
这四个参数需要去自己申请
参考文档地址https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_1.shtml
*初始化httpClient有个小坑,该对象可以通过 HttpClient 创建,但文档