收藏
回答

java 上传电子发票文件一直提示签名错误?无法排查求助各位大佬

求助各位大佬是否有对应的代码片段参考!!!!!!!!!!

https://api.mch.weixin.qq.com/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file 接口地址

返回的参数{"code":"SIGN_ERROR","detail":{"detail":{"issue":"sign not match"},"field":"signature","location":"authorization","sign_information":{"method":"POST","sign_message_length":119,"truncated_sign_message":"POST\n/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file\n1733365654080\n4df7b7ae369d4afe91710bfb62dca2cb\n\n","url":"/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file"}},"message":"错误的签名,验签失败"}

String pdfUrl = "http://121.33.250.162:18001/saas-industry/ebill.html?t=%C3%BD_%C3%BD%C3%90%C3%A5%C4%9C%C3%9C%C3%B9%C3%83%C3%85%C4%ACtx%C3%9A";
Path path = CommonUtils.downloadFileFromUrl(pdfUrl);
String filePath = path.toString();

String digest = CommonUtils.sm3(filePath);
JSONObject meta = new JSONObject();
meta.put("file_type", "PDF");
meta.put("digest", digest);
meta.put("digest_algorithm", "SM3");
String jsonString = meta.toJSONString();
HttpUrl parse = HttpUrl.parse(MiniProgramConstant.MINI_PROGRAM_UPLOAD_INVOICE_FILE_URL);
String signatureString = WxSignatureUtil.getSignatureString("POST", parse, jsonString);
log.info("signature[{}]", signatureString);

MultipartBody multipartBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("meta", "meta", RequestBody.create(jsonString,MediaType.parse("application/json")))
        .addFormDataPart("file", "file", RequestBody.create(new File(filePath), MediaType.parse("pdf/plain")))
        .build();

Request build = new Request.Builder()
        .addHeader("Authorization", signatureString)
        .addHeader("Accept", "application/json")
        .url(MiniProgramConstant.MINI_PROGRAM_UPLOAD_INVOICE_FILE_URL)
        .post(multipartBody)
        .build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(build).execute();
ResponseBody responseBody = response.body();


签名代码

public static String getSignatureString(String method, HttpUrl url, String body) throws Exception {
    String nonceStr = WxPayKit.generateStr();
    long timestamp = System.currentTimeMillis();
    String message = buildMessage(method, url, timestamp, nonceStr, body);
    String signature = sign(message.getBytes("utf-8"));
    return "WECHATPAY2-SHA256-RSA2048 mchid=\"" + mchid + "\","
            + "nonce_str=\"" + nonceStr + "\","
            + "timestamp=\"" + timestamp + "\","
            + "serial_no=\"" + serial + "\","
            + "signature=\"" + signature + "\"";
}

public static String sign(byte[] message) throws Exception {
    Signature sign = Signature.getInstance("SHA256withRSA");
    sign.initSign(getPrivateKeyFromUrl("pem.url"));
    sign.update(message);
    return Base64.getEncoder().encodeToString(sign.sign());
}

public static String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) {
    String canonicalUrl = url.encodedPath();
    if (url.encodedQuery() != null) {
        canonicalUrl += "?" + url.encodedQuery();
    }
    return method + "\n"
            + canonicalUrl + "\n"
            + timestamp + "\n"
            + nonceStr + "\n"
            + body + "\n";
}

回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容