Java实现微信退款报错unexpected end of file from server

问题分析

微信扫码支付,申请退款接口报错:

1
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证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :