Java实现微信退款报错unexpected end of file from server
问题分析
微信扫码支付,申请退款接口报错:
unexpected end of file from server
问题在于申请退款是需要需要双向证书,详见申请退款文档说明:
境内支付:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_5
境外支付:https://pay.weixin.qq.com/wiki/doc/api/external/native_sl.php?chapter=9_4
支付不需要证书,退款需要双向证书
Java使用apiclient_cert.p12
即可
证书下载入口:账户设置(Account Setting) -> API安全(API Security)-> API证书(API certificate)
证书默认密码:与商户号一致
Spring Boot RestTemplate 加载 p12证书
import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
@Slf4j
@Configuration
public class RestTemplateConfig {
@Autowired
private WxpayConfig wxpayConfig;
@Bean
//@LoadBalanced //增加 load balance 特性
public RestTemplate restTemplate() {
ClientHttpRequestFactory factory = httpComponentsClientHttpRequestFactory();
return new RestTemplate(factory);
}
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(15000); //单位为ms
factory.setConnectTimeout(15000); //单位为ms
return factory;
}
public ClientHttpRequestFactory httpComponentsClientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = null;
String password = wxpayConfig.getMchId();
try {
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(new File("/home/appblog/apiclient_cert.p12")), password.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password.toCharArray());
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(kmf.getKeyManagers(), null, null);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ssl, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setReadTimeout(15000); //单位为ms
factory.setConnectTimeout(15000); //单位为ms
return factory;
} catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
log.error("RestTemplateConfig.httpComponentsClientHttpRequestFactory error: ", e);
}
return null;
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/25/java-implementation-of-wechat-refund-error-unexpected-end-of-file-from-server/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论