public static String downloadReceipt(PrivateKey privateKey, String platformCertSerialNo, JeePayDTO payData,
String downloadUrl, String expectedHash, String settleNo) throws Exception {
HttpUriRequest httpRequest = WechatConfigUtil.buildRequestForDownload("GET", downloadUrl, null, privateKey, payData.getMchSerialNo(), payData.getMchId(), platformCertSerialNo);
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(httpRequest)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
throw new ServiceException("下载电子回单失败, 响应: " + responseBody, HttpStatus.BUSINESS);
}
byte[] fileBytes;
try (InputStream inputStream = response.getEntity().getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
fileBytes = baos.toByteArray();
}
String actualHash = computeHash(fileBytes);
log.info("期望hash:{}", expectedHash);
log.info("实际hash:{}", actualHash);
boolean isValid = expectedHash.equalsIgnoreCase(actualHash);
if (!isValid) {
throw new ServiceException("文件hash校验失败,文件可能不完整或被篡改!", HttpStatus.BUSINESS);
}
return "";
}
}
private static String computeHash(byte[] data) {
try {
MessageDigest md = MessageDigest.getInstance("SM3");
byte[] hash = md.digest(data);
StringBuilder hex = new StringBuilder();
for (byte b : hash) {
hex.append(String.format("%02X", b));
}
return hex.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SM3算法不可用", e);
}
}
https://pay.weixin.qq.com/doc/v3/merchant/4014931831你好 用这个计算对比下试试
哪个电子回单接口?