美文网首页
JAVA 微信 API v3 wechatpay-apache-

JAVA 微信 API v3 wechatpay-apache-

作者: RookieSnail | 来源:发表于2022-11-24 18:38 被阅读0次

    【wechatpay-apache-httpclient】官网:
    https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
    这个点一定要清晰(即:不需要自己去是生成签名)

    image.png

    以JSAPI为例

    【JSAPI支付】官网地址:
    https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_6.shtml

    项目目录:

    image.png

    工具类【WechatPayUtil】代码 :

      /**
       *  APP_ID(公众号/小程序等)
       */
      private static final String APP_ID = "xxxx";
      /**
       * 商户号 (微信商户平台可以获取)
       */
      private static final String MERCHATN_ID = "xxx";
      /**
       * API_V3  (微信商户平台配置)
       */
      private static final String API_V3 = "xxxx";
      /**
       * 私钥 (微信商户平台配置)
       */
      private static final String PRIVATE_KEY_PATH = "ice/channel_config/wechat/apiclient_key.pem";
      /**
       * 证书路径 (自行设置)
       */
      private static final String CERT_PATH = "ice/channel_config/wechat/apiclient_cert.pem";
      /**
       * 证书序列号(微信商户平台配置) 
       */
      private static final String CERT_ID = "xxxxxx";
    
    /**
       * 初始化  clientBuilder和证书管理器
       *
       * @return com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder
       * @author xxxx
       * @date 2022-11-25
       */
      public static WechatPayHttpClientBuilder initClientBuilder() throws IOException, GeneralSecurityException, HttpCodeException, NotFoundException {
        // 读取证书及私钥
        Resource resourcePrivateKey = new FileSystemResource(new File(PRIVATE_KEY_PATH));
        PrivateKey privateKey = PemUtil.loadPrivateKey(resourcePrivateKey.getInputStream());
        Resource resourceCert = new FileSystemResource(new File(CERT_PATH));
        X509Certificate cert = PemUtil.loadCertificate(resourceCert.getInputStream());
        // 微信证书管理器 
        CertificatesManager certificatesManager = CertificatesManager.getInstance();
        certificatesManager.putMerchant(
          MERCHATN_ID, 
          new WechatPay2Credentials(
            MERCHATN_ID, 
            new PrivateKeySigner(CERT_ID, privateKey)
          ), 
          API_V3.getBytes(StandardCharsets.UTF_8)
        );
        Verifier verifier = certificatesManager.getVerifier(MERCHATN_ID);
        // 初始化
        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(MERCHATN_ID, CERT_ID, privateKey)
                .withWechatPay(Collections.singletonList(cert))
                .withValidator(new WechatPay2Validator(verifier));
        return builder;
      }
    

    以对账文件下载为例进行测试

    对账文件下载,根据官网说明:


    image.png

    JSAPI对账文件下载API文档官网:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_8.shtml
    请求头【Accept: application/json】必须要设置,不设置会报错;【Content-Type: application/json】测试发现可以不设置

    image.png

    下面这个地方看起来像是要进行签名后再发起请求,但是实际上使用【wechatpay-apache-httpclient】的时候并不需要(工具类自己会签名),别被误导了


    image.png

    【测试代码】

    public static void main(String[] args) throws Exception {
    
        // 交易账单申请 https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=2022-11-23
        CloseableHttpClient applyClient = WechatPayUtil.initClientBuilder().build();
        // 初始化请求
        HttpGet applyHttpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=2022-11-23");
        // 请求头设置 官方说明:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay2_0.shtml
        applyHttpGet.addHeader("Accept", "application/json");
        applyHttpGet.addHeader("Content-type", "application/json; charset=utf-8");
        // 发起请求
        CloseableHttpResponse applyResponse = applyClient.execute(applyHttpGet);
        // 结果转换为Map
        String applyResponseEntityStr = EntityUtils.toString(applyResponse.getEntity());
        System.out.println(">>>>>>>>>【对账文件申请】响应结果:" + applyResponseEntityStr);
        Map applyResponseMap = new ObjectMapper().readValue(applyResponseEntityStr, Map.class);
        applyClient.close();
        applyResponse.close();
    
        // 交易账单下载
        String downloadUrl = (String) applyResponseMap.get("download_url");
       CloseableHttpClient downloadClient = WechatPayUtil.initClientBuilder().withValidator(response -> true).build();
        HttpGet downloadHttpGet = new HttpGet(downloadUrl);
        // 请求头设置 官方说明:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay2_0.shtml
        downloadHttpGet.addHeader("Accept", "application/json");
        downloadHttpGet.addHeader("Content-type", "application/json; charset=utf-8");
        // 发起请求
        CloseableHttpResponse downloadResponse = downloadClient.execute(downloadHttpGet);
        // 结果转换为String
        String downloadResponseEntityStr = EntityUtils.toString(downloadResponse.getEntity());
        System.out.println(">>>>>>>>>【交易账单下载-string】响应结果:");
        System.out.println(downloadResponseEntityStr);
        // 结果转换为List<String>
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(EntityUtils.toByteArray(downloadResponse.getEntity()))));
        List<String> resultList = bufferedReader.lines().collect(Collectors.toList());
        System.out.println(MessageFormat.format(">>>>>>>>>【交易账单下载-List<String>】响应结果:总计【{0}】条",resultList.size()));
        resultList.forEach(System.out::println);
        downloadClient.close();
        downloadResponse.close();
      }
    

    【测试结果】

    【对账交易文件申请】返回结果:

    【对账文件申请】响应结果:{"download_url":"https://api.mch.weixin.qq.com/v3/billdownload/file?token=om9r_WnzFUSMg24K4PYrtC7_byEZrIlmsMOJ8y7u_JPJP0D1F2AC_U6rtLAIsjlm","hash_type":"SHA1","hash_value":"f9ecef7bc7429e89b83e5ec4742b2699c74b5a11"}
    

    【对账交易文件下载】返回结果:(返回数据中,敏感数据已被替换为对应中文)

    >>>>>>>>>【交易账单下载-string】响应结果:
    交易时间,公众账号ID,商户号,特约商户号,设备号,微信订单号,商户订单号,用户标识,交易类型,交易状态,付款银行,货币种类,应结订单金额,代金券金额,微信退款单号,商户退款单号,退款金额,充值券退款金额,退款类型,退款状态,商品名称,商户数据包,手续费,费率,订单金额,申请退款金额,费率备注
    `2022-11-23 15:21:15,`公众账号ID,`商户号,`0,`,`4200001642202211233962981879,`1595316481882824704,`用户标识,`JSAPI,`SUCCESS,`OTHERS,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`null用户的水费订单收费记录,`,`0.00000,`0.54%,`0.01,`0.00,`
    `2022-11-23 15:11:51,`公众账号ID,`商户号,`0,`,`4200001661202211230109762244,`1595314109068582912,`用户标识,`JSAPI,`SUCCESS,`OTHERS,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`null用户的水费订单收费记录,`,`0.00000,`0.54%,`0.01,`0.00,`
    `2022-11-23 19:24:25,`公众账号ID,`商户号,`0,`,`4200001666202211235431097357,`1595377702036283392,`用户标识,`JSAPI,`SUCCESS,`OTHERS,`CNY,`0.03,`0.00,`0,`0,`0.00,`0.00,`,`,`h001用户的水费订单收费记录,`,`0.00000,`0.54%,`0.03,`0.00,`
    总交易单数,应结订单总金额,退款总金额,充值券退款总金额,手续费总金额,订单总金额,申请退款总金额
    `3,`0.05,`0.00,`0.00,`0.00000,`0.05,`0.00
    
    >>>>>>>>>【交易账单下载-List<String>】响应结果:总计【6】条
    交易时间,公众账号ID,商户号,特约商户号,设备号,微信订单号,商户订单号,用户标识,交易类型,交易状态,付款银行,货币种类,应结订单金额,代金券金额,微信退款单号,商户退款单号,退款金额,充值券退款金额,退款类型,退款状态,商品名称,商户数据包,手续费,费率,订单金额,申请退款金额,费率备注
    `2022-11-23 15:21:15,`公众账号ID,`商户号,`0,`,`4200001642202211233962981879,`1595316481882824704,`用户标识,`JSAPI,`SUCCESS,`OTHERS,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`null用户的水费订单收费记录,`,`0.00000,`0.54%,`0.01,`0.00,`
    `2022-11-23 15:11:51,`公众账号ID,`商户号,`0,`,`4200001661202211230109762244,`1595314109068582912,`用户标识,`JSAPI,`SUCCESS,`OTHERS,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`null用户的水费订单收费记录,`,`0.00000,`0.54%,`0.01,`0.00,`
    `2022-11-23 19:24:25,`公众账号ID,`商户号,`0,`,`4200001666202211235431097357,`1595377702036283392,`用户标识,`JSAPI,`SUCCESS,`OTHERS,`CNY,`0.03,`0.00,`0,`0,`0.00,`0.00,`,`,`h001用户的水费订单收费记录,`,`0.00000,`0.54%,`0.03,`0.00,`
    总交易单数,应结订单总金额,退款总金额,充值券退款总金额,手续费总金额,订单总金额,申请退款总金额
    `3,`0.05,`0.00,`0.00,`0.00000,`0.05,`0.00
    

    相关文章

      网友评论

          本文标题:JAVA 微信 API v3 wechatpay-apache-

          本文链接:https://www.haomeiwen.com/subject/tudxfdtx.html