处理退款带证书请求

This commit is contained in:
2025-08-26 00:57:06 +08:00
parent af51ba522e
commit 2c9f5b8a5a
11 changed files with 164 additions and 185 deletions

View File

@ -1,11 +1,27 @@
package com.sczx.pay.sdk;
import com.sczx.pay.utils.WXPayUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Map;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import javax.net.ssl.SSLContext;
/**
* 微信支付接口
*/
@ -180,7 +196,7 @@ public class WXPay {
String xml = WXPayUtil.mapToXml(reqData);
logger.info("微信支付请求URL: {}, 请求数据: {}", url, xml);
String response = httpRequest(url, xml, connectTimeoutMs, readTimeoutMs);
String response = httpsRequestWithCert(url, xml, connectTimeoutMs, readTimeoutMs);
logger.info("微信支付响应数据: {}", response);
return response;
@ -230,6 +246,59 @@ public class WXPay {
}
}
/**
* 带证书的HTTPS请求
*/
private String httpsRequestWithCert(String url, String requestData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
// 加载证书
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(this.config.getCertPath()));
try {
keyStore.load(instream, this.config.getMchID().toCharArray());
} finally {
instream.close();
}
// 创建SSL上下文
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, this.config.getMchID().toCharArray())
.build();
// 创建SSL连接工厂
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"},
null,
(hostname, session) -> true);
// 创建HTTP客户端
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
// 创建POST请求
HttpPost httpost = new HttpPost(url);
httpost.addHeader("Content-Type", "application/xml;charset=UTF-8");
httpost.setEntity(new StringEntity(requestData, "UTF-8"));
// 执行请求
CloseableHttpResponse response = httpclient.execute(httpost);
try {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(entity);
return result;
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
/**
* 判断支付结果
*/
@ -241,4 +310,6 @@ public class WXPay {
return false;
}
}
}

View File

@ -21,6 +21,8 @@ public abstract class WXPayConfig {
* 获取 API 密钥
*/
public abstract String getKey();
private String certPath;
/**
* 获取证书内容
@ -40,4 +42,13 @@ public abstract class WXPayConfig {
public int getHttpReadTimeoutMs() {
return 8*1000;
}
public String getCertPath() {
return certPath;
}
public void setCertPath(String certPath) {
this.certPath = certPath;
}
}