微信收付通版本对接

 对接收付通支付接口,使用微信支付pom版本,

<dependency>
     <groupId>com.github.wechatpay-apiv3</groupId>
     <artifactId>wechatpay-apache-httpclient</artifactId>
     <version>0.2.1</version>
</dependency>

   支付的时候使用的是WechatPay2来初始化的httpClient,支付正常

 @Before
    public void setup() throws IOException {
        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
                new ByteArrayInputStream(privateKey.getBytes("utf-8")));

        //使用自动更新的签名验证器,不需要传入证书
        verifier = new AutoUpdateCertificatesVerifier(
                new WechatPay2Credentials(mchId, new PrivateKeySigner(mchSerialNo, merchantPrivateKey)),
                apiV3Key.getBytes("utf-8"));

        httpClient = WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, merchantPrivateKey)
                .withValidator(new WechatPay2Validator(verifier))
                .build();
    }

  支付接口V2和V3都可以,版本没特殊限制。但是联调账单下载接口,必须要用v3的支付签名client ; version 版本必须要用4.0.0以上,否则调用会报错:应答签名验证失败

为了适用账单下载,使用可以初始化V3的版本:

<dependency>
   <groupId>com.github.binarywang</groupId>
   <artifactId>weixin-java-pay</artifactId>
   <version>4.0.2.B</version>
</dependency>

 具体代码如下:

 @Test
    public void downloadBill() throws WxPayException, IOException {
        String url = "https://api.mch.weixin.qq.com/v3/billdownload/file?token=6gt-tBq6jYj-Q9ACTbUA_u6oGIpWAK-bdCGXzNisOHCS7KMZdjqZ3Tb_tr1fXVu1";

        InputStream inputStream = downloadV3(URI.create(url));

        FileOutputStream fos = new FileOutputStream("d:h3.xls");
        byte[] b = new byte[1024];
        int length;
        while ((length = inputStream.read(b)) > 0) {
            fos.write(b, 0, length);
        }
        inputStream.close();
        fos.close();
        System.out.println("执行完成" + inputStream);
    }


public InputStream downloadV3(URI url) throws WxPayException {
        CloseableHttpClient httpClient = initApiV3HttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Accept", ContentType.WILDCARD.getMimeType());
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            //v3已经改为通过状态码判断200 204 成功
            int statusCode = response.getStatusLine().getStatusCode();
            if (HttpStatus.SC_OK == statusCode || HttpStatus.SC_NO_CONTENT == statusCode) {
                System.out.println("\n【请求地址】:{}\n" + url);
                return response.getEntity().getContent();
            } else {
                //有错误提示信息返回
                String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                JsonObject jsonObject = GsonParser.parse(responseString);
                System.out.println("异常数据:" + jsonObject.toString());
            }
        } catch (Exception e) {
            System.out.println("\n【请求地址:" + url + "\n【异常信息】:" + e.getMessage());
            throw (e instanceof WxPayException) ? (WxPayException) e : new WxPayException(e.getMessage(), e);
        } finally {
            httpGet.releaseConnection();
        }
        return null;
    }


public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
        try {
            PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes("utf-8")));
            X509Certificate certificate = PemUtils.loadCertificate(new ByteArrayInputStream(certKey.getBytes("utf-8")));
            if (StringUtils.isBlank(mchSerialNo)) {
                this.mchSerialNo = certificate.getSerialNumber().toString(16).toUpperCase();
            }
            com.github.binarywang.wxpay.v3.auth.AutoUpdateCertificatesVerifier verifier = new com.github.binarywang.wxpay.v3.auth.AutoUpdateCertificatesVerifier(
                    new WxPayCredentials(mchId, new com.github.binarywang.wxpay.v3.auth.PrivateKeySigner(mchSerialNo, merchantPrivateKey)),
                    apiV3Key.getBytes(StandardCharsets.UTF_8), 60);

            CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
                    .withMerchant(mchId, mchSerialNo, merchantPrivateKey)
                    .withWechatpay(Collections.singletonList(certificate)) //用不用下载都正常
                    .withValidator(new WxPayValidator(verifier))
                    .build();

            return httpClient;
        } catch (Exception e) {
            throw new WxPayException("v3请求构造异常!", e);
        }
    }

   如图下载成功:

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值