微信支付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_EXISTSthrow new ServiceException(e.getMessage());}}