From 4b255c3cb30955542645b51e741f84688d59abb5 Mon Sep 17 00:00:00 2001 From: zhengfangyuan Date: Wed, 3 Aug 2022 20:08:40 +0800 Subject: [PATCH] fix v3 bug --- .../wxpay/v3/auth/WxPayCredentials.java | 106 +++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java index 80eea8f6..f4bf181f 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java @@ -1,16 +1,27 @@ package com.github.binarywang.wxpay.v3.auth; +import com.github.binarywang.wxpay.exception.WxPayException; import com.github.binarywang.wxpay.v3.Credentials; import com.github.binarywang.wxpay.v3.WechatPayUploadHttpPost; +import com.github.binarywang.wxpay.v3.util.PemUtils; + +import jodd.util.ResourcesUtil; import lombok.extern.slf4j.Slf4j; + +import org.apache.commons.lang3.RegExUtils; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.util.EntityUtils; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URI; +import java.net.URL; import java.nio.charset.StandardCharsets; +import java.security.PrivateKey; import java.security.SecureRandom; @Slf4j @@ -80,7 +91,7 @@ public class WxPayCredentials implements Credentials { if (request.getOriginal() instanceof WechatPayUploadHttpPost) { body = ((WechatPayUploadHttpPost) request.getOriginal()).getMeta(); } else if (request instanceof HttpEntityEnclosingRequest) { - body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity()); + body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(),"UTF-8"); } return request.getRequestLine().getMethod() + "\n" @@ -89,5 +100,96 @@ public class WxPayCredentials implements Credentials { + nonce + "\n" + body + "\n"; } - + + + + public static void main(String[] args) { + try { + String nonceStr = "1659525492505"; + long timestamp = 1659525493; + String message = buildMessagettt(nonceStr, timestamp); + InputStream keyInputStream = loadConfigInputStream("C:\\ttttttt\\1036\\apiclient_key.pem"); + PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); + Signer signer = new PrivateKeySigner("562723E501CBEACA6B6835666168EBEB777360A4", merchantPrivateKey); + Signer.SignatureResult signature = signer.sign(message.getBytes(StandardCharsets.UTF_8)); + String mchid = "1623869862"; + + String token = "mchid=\"" + mchid + "\"," + + "nonce_str=\"" + nonceStr + "\"," + + "timestamp=\"" + timestamp + "\"," + + "serial_no=\"" + signature.certificateSerialNumber + "\"," + + "signature=\"" + signature.sign + "\""; + + System.out.println(token); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (WxPayException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + private static String buildMessagettt(String nonce, long timestamp) + throws IOException { + String body = "{\"appid\":\"wx649b3be73c1afe47\",\"batch_name\":\"百丽提现\",\"batch_remark\":\"提现编号:714465978943332352\",\"out_batch_no\":\"714465978943332352\",\"total_amount\":100,\"total_num\":1,\"transfer_detail_list\":[{\"openid\":\"opvcM5IEkl0U5pNiHcN49Ens40jU\",\"out_detail_no\":\"8c2145ceb1bf427ea147a88b42af9e9e\",\"transfer_amount\":100,\"transfer_remark\":\"百丽提现\"}]}"; + + return "POST" + "\n" + + "/v3/transfer/batches" + "\n" + + timestamp + "\n" + + nonce + "\n" + + body + "\n"; + } + + private static final String PROBLEM_MSG = "证书文件【%s】有问题,请核实!"; + private static final String NOT_FOUND_MSG = "证书文件【%s】不存在,请核实!"; + /** + * 从配置路径 加载配置 信息(支持 classpath、本地路径、网络url) + * @param configPath 配置路径 + * @return + * @throws WxPayException + */ + private static InputStream loadConfigInputStream(String configPath) throws WxPayException { + InputStream inputStream; + final String prefix = "classpath:"; + String fileHasProblemMsg = String.format(PROBLEM_MSG, configPath); + String fileNotFoundMsg = String.format(NOT_FOUND_MSG, configPath); + if (configPath.startsWith(prefix)) { + String path = RegExUtils.removeFirst(configPath, prefix); + if (!path.startsWith("/")) { + path = "/" + path; + } + try { + inputStream = ResourcesUtil.getResourceAsStream(path); + if (inputStream == null) { + throw new WxPayException(fileNotFoundMsg); + } + } catch (Exception e) { + throw new WxPayException(fileNotFoundMsg, e); + } + } else if (configPath.startsWith("http://") || configPath.startsWith("https://")) { + try { + inputStream = new URL(configPath).openStream(); + if (inputStream == null) { + throw new WxPayException(fileNotFoundMsg); + } + } catch (IOException e) { + throw new WxPayException(fileNotFoundMsg, e); + } + } else { + try { + File file = new File(configPath); + if (!file.exists()) { + throw new WxPayException(fileNotFoundMsg); + } + + inputStream = new FileInputStream(file); + } catch (IOException e) { + throw new WxPayException(fileHasProblemMsg, e); + } + } + return inputStream; + } } +