Skip to content

Commit 6ab618c

Browse files
committed
1.微信支付修复部分jdk无法加载证书问题
2.微信下载对账单报无法类型转换问题 3.微信退款非必填参数忽略
1 parent e4ca2a0 commit 6ab618c

File tree

11 files changed

+76
-60
lines changed

11 files changed

+76
-60
lines changed

pay-java-ali/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<dependency>
2323
<groupId>org.bouncycastle</groupId>
2424
<artifactId>bcprov-jdk15on</artifactId>
25-
<version>${bcprov-jdk15on.version}</version>
2625
</dependency>
2726
</dependencies>
2827

pay-java-ali/src/main/java/com/egzosn/pay/ali/utils/AntCertificationUtil.java

+15-17
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
29-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
3030

3131
import com.egzosn.pay.common.bean.result.PayException;
3232
import com.egzosn.pay.common.exception.PayErrorException;
@@ -38,17 +38,19 @@
3838
* 证书文件可信校验
3939
*
4040
* @author junying.wjy
41-
* @version $Id: AntCertificationUtil.java, v 0.1 2019-07-29 下午04:46 junying.wjy Exp $
42-
*
4341
* @author egan update 2020/10/12
44-
*
42+
* @version $Id: AntCertificationUtil.java, v 0.1 2019-07-29 下午04:46 junying.wjy Exp $
4543
*/
4644
public class AntCertificationUtil {
4745
private static final Logger LOGGER = LoggerFactory.getLogger(AntCertificationUtil.class);
46+
4847
static {
49-
Security.removeProvider("SunEC");
50-
Security.addProvider(new BouncyCastleProvider());
48+
if (null == Security.getProvider("BC")) {
49+
Security.removeProvider("SunEC");
50+
Security.addProvider(new BouncyCastleProvider());
51+
}
5152
}
53+
5254
/**
5355
* 验证证书是否可信
5456
*
@@ -141,7 +143,7 @@ private static boolean verifyCertChain(X509Certificate[] certs, X509Certificate[
141143
//验证证书链
142144
for (int i = 1; i < certs.length; i++) {
143145
X509Certificate cert = certs[i];
144-
if (!checkValidity(cert)){
146+
if (!checkValidity(cert)) {
145147
return false;
146148
}
147149
verifySignature(prev.getPublicKey(), cert);
@@ -155,7 +157,7 @@ private static boolean verifyCertChain(X509Certificate[] certs, X509Certificate[
155157
/**
156158
* 验证证书链是否是信任证书库中证书签发的
157159
*
158-
* @param cert 目标验证证书
160+
* @param cert 目标验证证书
159161
* @return 验证结果
160162
*/
161163
private static boolean checkValidity(X509Certificate cert) {
@@ -172,13 +174,11 @@ private static boolean checkValidity(X509Certificate cert) {
172174
}
173175

174176

175-
176-
private static void verifySignature(PublicKey publicKey, X509Certificate cert){
177+
private static void verifySignature(PublicKey publicKey, X509Certificate cert) {
177178
try {
178179
cert.verify(publicKey);
179-
}
180-
catch (GeneralSecurityException e) {
181-
throw new PayErrorException(new PayException("证书校验失败", e.getMessage()));
180+
} catch (GeneralSecurityException e) {
181+
throw new PayErrorException(new PayException("证书校验失败", e.getMessage()));
182182
}
183183
}
184184

@@ -281,7 +281,7 @@ private static void addressingDown(final Map<Principal, X509Certificate> issuerM
281281
addressingDown(issuerMap, certChain, subject);
282282
}
283283

284-
private static X509Certificate[] readPemCertChain(String cert){
284+
private static X509Certificate[] readPemCertChain(String cert) {
285285
ByteArrayInputStream inputStream = new ByteArrayInputStream(cert.getBytes());
286286
CertificateFactory factory = null;
287287
try {
@@ -305,7 +305,7 @@ public static String getRootCertSN(String rootCertContent) {
305305
String rootCertSN = null;
306306
try {
307307
X509Certificate[] x509Certificates = readPemCertChain(rootCertContent);
308-
if (null == x509Certificates){
308+
if (null == x509Certificates) {
309309
return null;
310310
}
311311
MessageDigest md = MessageDigest.getInstance("MD5");
@@ -383,8 +383,6 @@ public static String getCertPublicKey(String certContent) {
383383
}
384384

385385

386-
387-
388386
public static String readFromInputStream(InputStream cert) {
389387
try {
390388
return new String(IOUtils.toByteArray(cert), StandardCharsets.UTF_8);

pay-java-common/src/main/java/com/egzosn/pay/common/http/ClientHttpRequest.java

+32-29
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,38 @@ public T handleResponse(HttpResponse response) throws ClientProtocolException, I
317317
* @throws IOException 响应类型文本转换时抛出异常
318318
*/
319319
private T toBean(HttpEntity entity, String[] contentType) throws IOException {
320+
321+
322+
//是否为 输入流
323+
if (InputStream.class.isAssignableFrom(responseType)) {
324+
ByteArrayOutputStream os = new ByteArrayOutputStream();
325+
entity.writeTo(os);
326+
return (T) new ByteArrayInputStream(os.toByteArray());
327+
}
328+
//是否为 字节数数组
329+
if (byte[].class.isAssignableFrom(responseType)) {
330+
ByteArrayOutputStream os = new ByteArrayOutputStream();
331+
entity.writeTo(os);
332+
return (T) os.toByteArray();
333+
}
334+
//输出流
335+
if (OutputStream.class.isAssignableFrom(responseType)) {
336+
try {
337+
OutputStream t;
338+
if (responseType == OutputStream.class){
339+
t= new ByteArrayOutputStream();
340+
}else {
341+
t = (OutputStream) responseType.newInstance();
342+
}
343+
entity.writeTo( t);
344+
return (T) t;
345+
} catch (InstantiationException e) {
346+
throw new PayErrorException(new PayException("InstantiationException", e.getMessage()));
347+
} catch (IllegalAccessException e) {
348+
throw new PayErrorException(new PayException("IllegalAccessException", e.getMessage()));
349+
}
350+
}
351+
320352
//判断内容类型是否为文本类型
321353
if (isText(contentType[0])) {
322354
/* String charset = "UTF-8";
@@ -359,35 +391,6 @@ private T toBean(HttpEntity entity, String[] contentType) throws IOException {
359391
throw new PayErrorException(new PayException("failure", "类型转化异常,contentType:" + entity.getContentType().getValue(), result));
360392
}
361393

362-
//是否为 输入流
363-
if (InputStream.class.isAssignableFrom(responseType)) {
364-
ByteArrayOutputStream os = new ByteArrayOutputStream();
365-
entity.writeTo(os);
366-
return (T) new ByteArrayInputStream(os.toByteArray());
367-
}
368-
//是否为 字节数数组
369-
if (byte[].class.isAssignableFrom(responseType)) {
370-
ByteArrayOutputStream os = new ByteArrayOutputStream();
371-
entity.writeTo(os);
372-
return (T) os.toByteArray();
373-
}
374-
//输出流
375-
if (OutputStream.class.isAssignableFrom(responseType)) {
376-
try {
377-
OutputStream t;
378-
if (responseType == OutputStream.class){
379-
t= new ByteArrayOutputStream();
380-
}else {
381-
t = (OutputStream) responseType.newInstance();
382-
}
383-
entity.writeTo( t);
384-
return (T) t;
385-
} catch (InstantiationException e) {
386-
throw new PayErrorException(new PayException("InstantiationException", e.getMessage()));
387-
} catch (IllegalAccessException e) {
388-
throw new PayErrorException(new PayException("IllegalAccessException", e.getMessage()));
389-
}
390-
}
391394
throw new PayErrorException(new PayException("failure", "类型转化异常,contentType:" + entity.getContentType().getValue()));
392395
}
393396

pay-java-demo/src/main/java/com/egzosn/pay/demo/controller/WxV3CombinePayController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class WxV3CombinePayController {
4646

4747
private WxCombinePayService service = null;
4848

49-
@PostConstruct //没有证书的情况下注释掉,避免启动报错
49+
// @PostConstruct //没有证书的情况下注释掉,避免启动报错
5050
public void init() {
5151
WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
5252
wxPayConfigStorage.setAppId("wxc7b993ff15a9f26c");

pay-java-demo/src/main/java/com/egzosn/pay/demo/controller/WxV3PayController.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import com.egzosn.pay.wx.v3.api.WxPayConfigStorage;
2828
import com.egzosn.pay.wx.v3.api.WxPayService;
2929
import com.egzosn.pay.wx.v3.bean.WxTransactionType;
30+
import com.egzosn.pay.wx.v3.bean.order.H5Info;
31+
import com.egzosn.pay.wx.v3.bean.order.SceneInfo;
32+
import com.egzosn.pay.wx.v3.utils.WxConst;
3033

3134
/**
3235
* 微信V3发起支付入口
@@ -75,12 +78,11 @@ public void init() {
7578
@RequestMapping(value = "toPay.html", produces = "text/html;charset=UTF-8")
7679
public String toPay(HttpServletRequest request, BigDecimal price) {
7780
PayOrder order = new PayOrder("订单title", "摘要", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), WxTransactionType.H5);
78-
order.setSpbillCreateIp(request.getHeader("X-Real-IP"));
7981
StringBuffer requestURL = request.getRequestURL();
80-
//设置网页地址
81-
order.setWapUrl(requestURL.substring(0, requestURL.indexOf("/") > 0 ? requestURL.indexOf("/") : requestURL.length()));
82-
//设置网页名称
83-
order.setWapName("在线充值");
82+
SceneInfo sceneInfo = new SceneInfo();
83+
sceneInfo.setPayerClientIp(request.getHeader("X-Real-IP"));
84+
sceneInfo.setH5Info(new H5Info("在线充值", requestURL.substring(0, requestURL.indexOf("/") > 0 ? requestURL.indexOf("/") : requestURL.length())));
85+
order.addAttr(WxConst.SCENE_INFO, sceneInfo);
8486

8587
// Map orderInfo = service.orderInfo(order);
8688
// return service.buildRequest(orderInfo, MethodType.POST);

pay-java-demo/src/main/java/com/egzosn/pay/demo/controller/WxV3ProfitSharingController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class WxV3ProfitSharingController {
4444

4545
private WxProfitSharingService service = null;
4646

47-
@PostConstruct //没有证书的情况下注释掉,避免启动报错
47+
// @PostConstruct //没有证书的情况下注释掉,避免启动报错
4848
public void init() {
4949
WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
5050
wxPayConfigStorage.setAppId("wxc7b993ff15a9f26c");

pay-java-wx/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<dependency>
2424
<groupId>org.bouncycastle</groupId>
2525
<artifactId>bcprov-jdk15on</artifactId>
26-
<version>${bcprov-jdk15on.version}</version>
26+
2727
</dependency>
2828

2929

pay-java-wx/src/main/java/com/egzosn/pay/wx/v3/api/DefaultWxPayAssistService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public DefaultWxPayAssistService(WxPayService wxPayService) {
7070
* @return 响应内容体
7171
*/
7272
public JSONObject doExecute(Map<String, Object> parameters, TransactionType transactionType) {
73-
String requestBody = JSON.toJSONString(parameters, SerializerFeature.WriteMapNullValue);
73+
// String requestBody = JSON.toJSONString(parameters, SerializerFeature.WriteMapNullValue);
74+
String requestBody = JSON.toJSONString(parameters);
7475
return doExecute(requestBody, transactionType);
7576
}
7677

pay-java-wx/src/main/java/com/egzosn/pay/wx/v3/api/WxPayService.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ else if (WxTransactionType.APP == order.getTransactionType()) {
302302
params.put("prepayid", prepayId);
303303
params.put("package", "Sign=WXPay");
304304
}
305-
String signText = StringUtils.joining("\n", appId, timeStamp, prepayId);
305+
String signText = StringUtils.joining("\n", appId, timeStamp, randomStr, prepayId);
306306
String paySign = createSign(signText, payConfigStorage.getInputCharset());
307307
params.put(WxTransactionType.JSAPI.equals(order.getTransactionType()) ? "paySign" : "sign", paySign);
308308
return params;
@@ -525,6 +525,8 @@ public RefundResult refund(RefundOrder refundOrder) {
525525
}
526526

527527

528+
529+
528530
/**
529531
* 查询退款
530532
*

pay-java-wx/src/main/java/com/egzosn/pay/wx/v3/utils/AntCertificationUtil.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.security.NoSuchProviderException;
1111
import java.security.PrivateKey;
1212
import java.security.PublicKey;
13+
import java.security.Security;
1314
import java.security.cert.Certificate;
1415
import java.security.cert.CertificateException;
1516
import java.security.cert.CertificateFactory;
@@ -21,6 +22,8 @@
2122
import javax.crypto.spec.GCMParameterSpec;
2223
import javax.crypto.spec.SecretKeySpec;
2324

25+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
26+
2427
import com.egzosn.pay.common.exception.PayErrorException;
2528
import com.egzosn.pay.common.util.sign.encrypt.Base64;
2629
import com.egzosn.pay.wx.bean.WxPayError;
@@ -49,6 +52,10 @@ private AntCertificationUtil() {
4952

5053
static {
5154
try {
55+
if (null == Security.getProvider("BC")) {
56+
Security.removeProvider("SunEC");
57+
Security.addProvider(new BouncyCastleProvider());
58+
}
5259
PKCS12_KEY_STORE = KeyStore.getInstance("PKCS12");
5360
}
5461
catch (KeyStoreException e) {
@@ -133,7 +140,7 @@ public static CertEnvironment initCertification(InputStream keyCertStream, Strin
133140
* @param cipherText 需要解密的文本
134141
* @param secretKey 密钥
135142
* @param characterEncoding 编码类型
136-
* @return 解密后的信息
143+
* @return 解密后的信息
137144
*/
138145
public static String decryptToString(String associatedData, String nonce, String cipherText, String secretKey, String characterEncoding) {
139146

@@ -156,7 +163,7 @@ public static String decryptToString(String associatedData, String nonce, String
156163
*
157164
* @param message the message
158165
* @param certificate the certificate
159-
* @return 加密后的内容
166+
* @return 加密后的内容
160167
*/
161168
public static String encryptToString(String message, Certificate certificate) {
162169
try {

pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@
106106
<artifactId>fastjson</artifactId>
107107
<version>1.2.73</version>
108108
</dependency>
109-
109+
<dependency>
110+
<groupId>org.bouncycastle</groupId>
111+
<artifactId>bcprov-jdk15on</artifactId>
112+
<version>${bcprov-jdk15on.version}</version>
113+
</dependency>
110114

111115
<!-- log4j -->
112116
<dependency>

0 commit comments

Comments
 (0)