微信支付V3
微信支付V3 开发文档:
https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/index.shtml
1. 查看文档
使用微信提供的SDK,在文档中进入SDK
2. 开发
1. 添加jar包
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.10</version>
</dependency>
2. 代码逻辑
public R payAuth(PayDto payDto) throws Exception {
BdTerminalUserAuth bdTerminalUserAuth = bdTerminalUserAuthMapper.selectById(payDto.getAuthId());
//审核状态:1 待通过,3 不通过
if (bdTerminalUserAuth.getStatus() == 1 || bdTerminalUserAuth.getStatus() == 3) {
return R.fail("审核未通过");
}
if (bdTerminalUserAuth.getPayStatus() == 2 || bdTerminalUserAuth.getStatus() == 4) {
return R.fail("已支付,请勿重复支付");
}
//============ 设置微信下单参数 ==============
WxOrderDto wxOrderDto = new WxOrderDto();
Amount amount = new Amount();
//CNY:人民币
amount.setCurrency("CNY");
//将价格化为分
int i = (int) (payDto.getPrice() * 100);
amount.setTotal(i);
wxOrderDto.setAmount(amount);
//商品描述
wxOrderDto.setDescription(payDto.getDescription());
//设置订单编号
wxOrderDto.setOut_trade_no(bdTerminalUserAuth.getOrderNo());
//============ 设置微信下单参数 ==============
//支付,生成二维码
String qrCode = wxPayService.createNativeOrder(wxOrderDto);
//判断二维码是否包含”weixin"
if (qrCode.contains("weixin")) {
//设置支付状态为待支付
bdTerminalUserAuth.setPayType(Constants.PAY_TOPAY);
bdTerminalUserAuthMapper.updateById(bdTerminalUserAuth);
}
return R.ok(qrCode, "二维码生成成功");
}
3. 下单,返回二维码
上面代码中用到了 wxPayService.createNativeOrder(wxOrderDto);
方法,
private NativePayService getService() {
// 使用自动更新平台证书的RSA配置
// 一个商户号只能初始化一个配置,否则会因为重复的下载任务报错
// 使用自动更新平台证书的RSA配置
// 一个商户号只能初始化一个配置,否则会因为重复的下载任务报错
Config config =
new RSAAutoCertificateConfig.Builder()
.merchantId(wechatConfig.getMchId())
.privateKeyFromPath("E:\\workspace\\ctbigdata\\ctbigdata-modules\\ctbigdata-system\\src\\main\\resources\\wechatpay-cert\\apiclient_key.pem")
.merchantSerialNumber(wechatConfig.getMcHserialNo())
.apiV3Key(wechatConfig.getAPIv3key())
.build();
return new NativePayService.Builder().config(config).build();
}
/**
* 下单生成二维码
*
* @param wxOrderDto
* @return
*/
public String createNativeOrder(WxOrderDto wxOrderDto) {
// request.setXxx(val)设置所需参数,具体参数可见Request定义
PrepayRequest request = new PrepayRequest();
request.setAmount(wxOrderDto.getAmount());
request.setAppid(wechatConfig.getAppId());
request.setMchid(wechatConfig.getMchId());
request.setDescription(wxOrderDto.getDescription());
//设置回调地址
request.setNotifyUrl("http://www.xxx.cn/wechatPay/paycallback");
request.setOutTradeNo(wxOrderDto.getOut_trade_no());
request.setAttach("authPay");
// 调用下单方法,得到应答
PrepayResponse response = null;
try {
response = this.getService().prepay(request);
} catch (Exception e) {
//异常处理
e.printStackTrace();
throw new ServiceException(e.getMessage());
}
// 使用微信扫描 code_url 对应的二维码,即可体验Native支付
return response.getCodeUrl();
}
官网接口文档:
4. 查询订单
这个是通过TransactionId交易Id进行查询订单状态
也可以通过OutTradeNo我们代码逻辑中的订单号进行查询
/**
* 查询订单状态
*
* @return
*/
public Transaction selectOrderStatus(String outTradeNo) {
try {
QueryOrderByOutTradeNoRequest queryRequest = new QueryOrderByOutTradeNoRequest();
queryRequest.setMchid(wechatConfig.getMchId());
queryRequest.setOutTradeNo(outTradeNo);
//返回结果
return this.getService().queryOrderByOutTradeNo(queryRequest);
} catch (ServiceException e) {
// API返回失败, 例如ORDER_NOT_EXISTS
throw new ServiceException(e.getMessage());
}
}
取消订单
/**
* 关闭订单
*/
public void closeOrder(String outTradeNo) {
CloseOrderRequest closeRequest = new CloseOrderRequest();
closeRequest.setMchid(wechatConfig.getMchId());
closeRequest.setOutTradeNo(outTradeNo);
// 方法没有返回值,意味着成功时API返回204 No Content
this.getService().closeOrder(closeRequest);
}
分账
参考资料:
payment-spring-boot-starter实现微信支付,封装了很多微信支付的方法:如分账等
https://felord.gitee.io/payment-spring-boot/#/
微信支付分账api
https://pay.weixin.qq.com/wiki/doc/api/allocation_sl.php?chapter=25_1&index=1
微信支付接口文档
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_1.shtml