微信支付 API V3 JSAPI支付 JAVA下载账单

下载账单

写这个主要是太气人了,开发文档未写具体的代码示例。网上各种搜索了一天都是V2接口的示例V3的标题党,感觉被欺骗了,太气人了(V2接口有个参数APPID,具体业务使用了多个APPID所以不合适),下面内容转载请标明出处。不足之处望各大佬指点。

测试类

    @Test
    public void testBill() throws Exception {
        ApplyBill applyBill = wxPayService.applyForTransactionBill("2021-09-04", GoodsConstant.ALL);
        wxPayService.downloadBill(applyBill.getDownloadUrl())
    }

申请交易账单

    /**
     * @Description: 申请交易账单
     * @param billDate 账单日期 格式YYYY-MM-DD
     * @param billType 账单类型	枚举值:ALL:返回当日所有订单信息(不含充值退款订单)SUCCESS:返回当日成功支付的订单(不含充值退款订单)REFUND:返回当日退款订单(不含充值退款订单)
     */
    public ApplyBill applyForTransactionBill(String billDate, String billType) throws Exception{

        HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date="+billDate+"&bill_type=" + billType);
        CloseableHttpClient httpClient = wxHttpClientService.createHttpClient();
        httpGet.setHeader("Accept", "application/json");
        //完成签名并执行请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) { //处理成功
                String responseData = EntityUtils.toString(response.getEntity());
                return JSON.parseObject(responseData, ApplyBill.class);
            } else if (statusCode == 204) { //处理成功,无返回Body
                System.out.println("success");
            } else if (statusCode == 400){
                System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
            } else {
                throw new IOException("request failed");
            }
            return null;
        } finally {
            httpClient.close();
            response.close();
        }
    }

下载账单

    public String downloadBill(String downloadUrl) throws Exception{

        String timestamp = System.currentTimeMillis() + "";
        String nonceStr = UUID.randomUUID().toString().replace("-", "");
        String billSign = this.createBillSign(nonceStr, timestamp, downloadUrl);
        String url = downloadUrl + "&mchid=" +WXPayConstant.mchId + "&nonce_str=" + nonceStr +"&signature=" + billSign + "&timestamp=" + timestamp + "&serial_no=" + WXPayConstant.mchSerialNo;
        HttpGet httpGet = new HttpGet(url);

        httpGet.setHeader("Accept", "application/json");
        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(
                new ByteArrayInputStream(WXPayConstant.privateKey.getBytes("utf-8")));
        X509Certificate wechatpayCertificate = PemUtil.loadCertificate(
                new ByteArrayInputStream(WXPayConstant.certificate.getBytes("utf-8")));

        ArrayList<X509Certificate> listCertificates = new ArrayList<>();
        listCertificates.add(wechatpayCertificate);
        CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
                .withMerchant(WXPayConstant.mchId, WXPayConstant.mchSerialNo, merchantPrivateKey)
                .withWechatpay(listCertificates)
                .withValidator(response -> true)
                .build();

        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) { //处理成功
                int i = response.hashCode();

                String responseData = EntityUtils.toString(response.getEntity());
                System.out.println(responseData);
                return responseData;
            } else if (statusCode == 204) { //处理成功,无返回Body
                System.out.println("success");
            } else if (statusCode == 400){
                System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
            } else {
                System.out.println("failed,resp code = " + statusCode+ ",return body = " + EntityUtils.toString(response.getEntity()));
                throw new IOException("request failed");
            }
            return null;
        } finally {
            httpClient.close();
            response.close();
        }
    }

下载账单-生成签名

    /**
     * @Description: 下载账单->生成签名
     */
    public String createBillSign(String nonceStr, String timestamp, String download) throws Exception{

        String token = download.split("token=")[1];
        String plain_text =   WXPayConstant.mchId +"\n" + nonceStr +"\n" + WXPayConstant.mchSerialNo + "\n" + timestamp + "\n" + token;
        PrivateKey privateKey = PemUtil.loadPrivateKey(
                new ByteArrayInputStream(WXPayConstant.privateKey.getBytes("utf-8")));
        Signature sign = Signature.getInstance("SHA256withRSA");
        sign.initSign(privateKey);
        sign.update(plain_text.getBytes("utf-8"));

        return Base64.getEncoder().encodeToString(sign.sign());

    }
  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值