params) {
+ url = Arrays.stream(params.entrySet().toArray(new Map.Entry[0]))
+ .map(entry -> entry.getKey() + "=" + URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8))
+ .collect(Collectors.joining("&", url + "?", ""));
+ return req(configDto, url, "GET", "");
+ }
+
+ private static String req(WechatPayConfigDto configDto, String url, String method, String body) {
long timestamp = getTimestamp();
String nonce = getNonceStr();
-
String signature = encryptReqParam(configDto, method, url, body, timestamp, nonce);
-
String authorization = String.format("WECHATPAY2-SHA256-RSA2048 mchid=\"%s\",nonce_str=\"%s\",signature=\"%s\",timestamp=\"%d\",serial_no=\"%s\"",
- configDto.getMerchantId(), nonce, signature, timestamp, "4DE9BAC2EA584C3F274F694C9753CA814C4E9BF4");
+ configDto.getMerchantId(), nonce, signature, timestamp, configDto.getSerialNumber());
- log.info("authorization = {}", authorization);
-
- HttpRequest request = HttpUtil.createPost(configDto.getDomain() + url)
- .header("Authorization", authorization)
- .header("Content-Type", "application/json")
- .header("Wechatpay-Serial", configDto.getPublicKeyId())
-// .header("Wechatpay-Serial", "4DE9BAC2EA584C3F274F694C9753CA814C4E9BF4")
- .body(body);
+ HttpRequest request = switch (method) {
+ case "POST" -> HttpUtil.createPost(configDto.getDomain() + url)
+ .header("Authorization", authorization)
+ .header("Content-Type", "application/json")
+ .header("Wechatpay-Serial", configDto.getPublicKeyId())
+ .body(body);
+ case "GET" -> HttpUtil.createGet(configDto.getDomain() + url)
+ .header("Authorization", authorization)
+ .header("Content-Type", "application/json")
+ .header("Wechatpay-Serial", configDto.getPublicKeyId());
+ default -> throw new CzgException("不支持的请求方法");
+ };
String s = request.execute().body();
- log.info("s = {}", s);
-
- return "";
+ log.info("微信支付请求:url = {}, method: {}, body: {}, resp: {}", url, method, body, s);
+ return s;
}
/**
* 加密请求参数
- * @param configDto 配置
- * @param method 请求方法
- * @param url 请求地址
- * @param body 请求报文主体
- * @return 加密后的报文
*
+ * @param configDto 配置
+ * @param method 请求方法
+ * @param url 请求地址
+ * @param body 请求报文主体
+ * @return 加密后的报文
+ *
* 签名方法
* HTTP请求方法\n
* URL\n
@@ -62,12 +81,12 @@ public class WechatReqUtils {
public static String encryptReqParam(WechatPayConfigDto configDto, String method, String url, String body, long timestamp, String nonce) {
String encryptStr = String.format("%s\n%s\n%d\n%s\n%s\n",
method, url, timestamp, nonce, body);
- System.out.println("encryptStr = \n" + encryptStr);
+ log.info("encryptStr = {}", encryptStr);
Config config = WechatConfig.getRsaConfig(configDto);
Signer signer = config.createSigner();
String signature = signer.sign(encryptStr).getSign();
- System.out.println("signature = " + signature);
+ log.info("签名 signature:{}", signature);
return signature;
}
@@ -82,6 +101,6 @@ public class WechatReqUtils {
* 获取时间戳
*/
private static long getTimestamp() {
- return System.currentTimeMillis()/1000;
+ return System.currentTimeMillis() / 1000;
}
}