下载账单
写这个主要是太气人了,开发文档未写具体的代码示例。网上各种搜索了一天都是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 + "×tamp=" + 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());
}