Skip to content

Commit 1921976

Browse files
committed
支付-统一处理回调验签
1 parent 8418f79 commit 1921976

16 files changed

Lines changed: 65 additions & 180 deletions

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/ecommerce/SignatureHeader.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/EcommerceService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.binarywang.wxpay.bean.ecommerce.*;
44
import com.github.binarywang.wxpay.bean.ecommerce.enums.FundBillTypeEnum;
55
import com.github.binarywang.wxpay.bean.ecommerce.enums.SpAccountTypeEnum;
6+
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
67
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
78
import com.github.binarywang.wxpay.exception.WxPayException;
89

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/MarketingFavorService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.binarywang.wxpay.service;
22

3-
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
3+
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
44
import com.github.binarywang.wxpay.bean.marketing.*;
55
import com.github.binarywang.wxpay.exception.WxPayException;
66

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/PartnerPayScoreService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.binarywang.wxpay.service;
22

3-
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
3+
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
44
import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData;
55
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreRequest;
66
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreResult;

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/PartnerPayScoreSignPlanService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.binarywang.wxpay.service;
22

3-
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
3+
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
44
import com.github.binarywang.wxpay.bean.payscore.PartnerUserSignPlanEntity;
55
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreSignPlanRequest;
66
import com.github.binarywang.wxpay.bean.payscore.WxPartnerPayScoreSignPlanResult;

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/PayScoreService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.binarywang.wxpay.service;
22

3-
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
3+
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
44
import com.github.binarywang.wxpay.bean.payscore.PayScoreNotifyData;
55
import com.github.binarywang.wxpay.bean.payscore.UserAuthorizationStatusNotifyResult;
66
import com.github.binarywang.wxpay.bean.payscore.WxPayScoreRequest;

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.github.binarywang.wxpay.config.WxPayConfig;
1212
import com.github.binarywang.wxpay.constant.WxPayConstants;
1313
import com.github.binarywang.wxpay.exception.WxPayException;
14+
import com.github.binarywang.wxpay.exception.WxSignTestException;
1415
import org.apache.http.client.methods.HttpPost;
1516
import org.apache.http.client.methods.HttpRequestBase;
1617

@@ -1069,6 +1070,16 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
10691070
*/
10701071
WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String signType) throws WxPayException;
10711072

1073+
/**
1074+
* 校验通知签名
1075+
*
1076+
* @param header 通知头信息
1077+
* @param data 通知数据
1078+
* @return true:校验通过 false:校验不通过
1079+
* @throws WxSignTestException 微信支付签名探测流量异常
1080+
*/
1081+
boolean verifyNotifySign(SignatureHeader header, String data) throws WxSignTestException;
1082+
10721083
/**
10731084
* 解析支付结果v3通知. 直连商户模式
10741085
* 详见https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
package com.github.binarywang.wxpay.service.impl;
22

3-
import static com.github.binarywang.wxpay.constant.WxPayConstants.QUERY_COMMENT_DATE_FORMAT;
4-
import static com.github.binarywang.wxpay.constant.WxPayConstants.TarType;
5-
import com.github.binarywang.wxpay.bean.coupon.*;
6-
import com.github.binarywang.wxpay.bean.notify.*;
7-
import com.github.binarywang.wxpay.bean.request.*;
8-
import com.github.binarywang.wxpay.bean.result.*;
9-
import com.github.binarywang.wxpay.service.*;
10-
import java.util.*;
11-
import com.github.binarywang.wxpay.bean.result.enums.GlobalTradeTypeEnum;
12-
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
133
import com.github.binarywang.utils.qrcode.QrcodeUtils;
144
import com.github.binarywang.wxpay.bean.WxPayApiData;
5+
import com.github.binarywang.wxpay.bean.coupon.*;
6+
import com.github.binarywang.wxpay.bean.notify.*;
157
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
168
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
179
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
1810
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
11+
import com.github.binarywang.wxpay.bean.request.*;
12+
import com.github.binarywang.wxpay.bean.result.*;
13+
import com.github.binarywang.wxpay.bean.result.enums.GlobalTradeTypeEnum;
14+
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
1915
import com.github.binarywang.wxpay.bean.transfer.TransferBillsNotifyResult;
2016
import com.github.binarywang.wxpay.config.WxPayConfig;
2117
import com.github.binarywang.wxpay.config.WxPayConfigHolder;
2218
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
2319
import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
2420
import com.github.binarywang.wxpay.exception.WxPayException;
2521
import com.github.binarywang.wxpay.exception.WxSignTestException;
22+
import com.github.binarywang.wxpay.service.*;
2623
import com.github.binarywang.wxpay.util.SignUtils;
2724
import com.github.binarywang.wxpay.util.XmlConfig;
2825
import com.github.binarywang.wxpay.util.ZipUtils;
@@ -32,6 +29,14 @@
3229
import com.google.common.collect.Maps;
3330
import com.google.gson.Gson;
3431
import com.google.gson.GsonBuilder;
32+
import lombok.Getter;
33+
import lombok.Setter;
34+
import lombok.extern.slf4j.Slf4j;
35+
import me.chanjar.weixin.common.error.WxRuntimeException;
36+
import org.apache.commons.lang3.StringUtils;
37+
import org.apache.commons.lang3.reflect.ConstructorUtils;
38+
import org.apache.http.entity.ContentType;
39+
3540
import java.io.File;
3641
import java.io.IOException;
3742
import java.io.InputStream;
@@ -40,15 +45,12 @@
4045
import java.nio.file.Path;
4146
import java.nio.file.Paths;
4247
import java.security.GeneralSecurityException;
48+
import java.util.*;
4349
import java.util.concurrent.ConcurrentHashMap;
4450
import java.util.zip.ZipException;
45-
import lombok.Getter;
46-
import lombok.Setter;
47-
import lombok.extern.slf4j.Slf4j;
48-
import me.chanjar.weixin.common.error.WxRuntimeException;
49-
import org.apache.commons.lang3.StringUtils;
50-
import org.apache.commons.lang3.reflect.ConstructorUtils;
51-
import org.apache.http.entity.ContentType;
51+
52+
import static com.github.binarywang.wxpay.constant.WxPayConstants.QUERY_COMMENT_DATE_FORMAT;
53+
import static com.github.binarywang.wxpay.constant.WxPayConstants.TarType;
5254

5355
/**
5456
* <pre>
@@ -526,7 +528,8 @@ public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String sign
526528
* @param data 通知数据
527529
* @return true:校验通过 false:校验不通过
528530
*/
529-
private boolean verifyNotifySign(SignatureHeader header, String data) throws WxSignTestException {
531+
@Override
532+
public boolean verifyNotifySign(SignatureHeader header, String data) throws WxSignTestException {
530533
String wxPaySign = header.getSignature();
531534
if (wxPaySign.startsWith("WECHATPAY/SIGNTEST/")) {
532535
throw new WxSignTestException("微信支付签名探测流量");
@@ -1360,8 +1363,8 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
13601363
@Override
13611364
public WxPayCodepayResult codepay(WxPayCodepayRequest request) throws WxPayException {
13621365
// 判断是否为服务商模式:如果设置了sp_appid或sp_mchid或sub_mchid中的任何一个,则认为是服务商模式
1363-
boolean isPartnerMode = StringUtils.isNotBlank(request.getSpAppid())
1364-
|| StringUtils.isNotBlank(request.getSpMchid())
1366+
boolean isPartnerMode = StringUtils.isNotBlank(request.getSpAppid())
1367+
|| StringUtils.isNotBlank(request.getSpMchid())
13651368
|| StringUtils.isNotBlank(request.getSubMchid());
13661369

13671370
if (isPartnerMode) {

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BusinessCircleServiceImpl.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.github.binarywang.wxpay.bean.businesscircle.PaidResult;
55
import com.github.binarywang.wxpay.bean.businesscircle.PointsNotifyRequest;
66
import com.github.binarywang.wxpay.bean.businesscircle.RefundResult;
7-
import com.github.binarywang.wxpay.bean.ecommerce.SignatureHeader;
7+
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
88
import com.github.binarywang.wxpay.exception.WxPayException;
99
import com.github.binarywang.wxpay.service.BusinessCircleService;
1010
import com.github.binarywang.wxpay.service.WxPayService;
@@ -38,22 +38,9 @@ public void notifyPoints(PointsNotifyRequest request) throws WxPayException {
3838
this.payService.postV3WithWechatpaySerial(url, GSON.toJson(request));
3939
}
4040

41-
/**
42-
* 校验通知签名
43-
*
44-
* @param header 通知头信息
45-
* @param data 通知数据
46-
* @return true:校验通过 false:校验不通过
47-
*/
48-
private boolean verifyNotifySign(SignatureHeader header, String data) {
49-
String beforeSign = String.format("%s\n%s\n%s\n", header.getTimeStamp(), header.getNonce(), data);
50-
return payService.getConfig().getVerifier().verify(header.getSerialNo(),
51-
beforeSign.getBytes(StandardCharsets.UTF_8), header.getSigned());
52-
}
53-
54-
@Override
41+
@Override
5542
public BusinessCircleNotifyData parseNotifyData(String data, SignatureHeader header) throws WxPayException {
56-
if (Objects.nonNull(header) && !this.verifyNotifySign(header, data)) {
43+
if (Objects.nonNull(header) && !this.payService.verifyNotifySign(header, data)) {
5744
throw new WxPayException("非法请求,头部信息验证失败");
5845
}
5946
return GSON.fromJson(data, BusinessCircleNotifyData.class);

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/EcommerceServiceImpl.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.binarywang.wxpay.bean.ecommerce.*;
44
import com.github.binarywang.wxpay.bean.ecommerce.enums.FundBillTypeEnum;
55
import com.github.binarywang.wxpay.bean.ecommerce.enums.SpAccountTypeEnum;
6+
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
67
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
78
import com.github.binarywang.wxpay.exception.WxPayException;
89
import com.github.binarywang.wxpay.service.EcommerceService;
@@ -28,7 +29,6 @@
2829
import java.lang.reflect.InvocationTargetException;
2930
import java.lang.reflect.Method;
3031
import java.net.URI;
31-
import java.nio.charset.StandardCharsets;
3232
import java.security.GeneralSecurityException;
3333
import java.text.DateFormat;
3434
import java.util.*;
@@ -83,7 +83,7 @@ public <T> T combineTransactions(TradeTypeEnum tradeType, CombineTransactionsReq
8383

8484
@Override
8585
public CombineTransactionsNotifyResult parseCombineNotifyResult(String notifyData, SignatureHeader header) throws WxPayException {
86-
if (Objects.nonNull(header) && !this.verifyNotifySign(header, notifyData)) {
86+
if (Objects.nonNull(header) && !payService.verifyNotifySign(header, notifyData)) {
8787
throw new WxPayException("非法请求,头部信息验证失败");
8888
}
8989
NotifyResponse response = GSON.fromJson(notifyData, NotifyResponse.class);
@@ -129,7 +129,7 @@ public <T> T partnerTransactions(TradeTypeEnum tradeType, PartnerTransactionsReq
129129

130130
@Override
131131
public PartnerTransactionsNotifyResult parsePartnerNotifyResult(String notifyData, SignatureHeader header) throws WxPayException {
132-
if (Objects.nonNull(header) && !this.verifyNotifySign(header, notifyData)) {
132+
if (Objects.nonNull(header) && !payService.verifyNotifySign(header, notifyData)) {
133133
throw new WxPayException("非法请求,头部信息验证失败");
134134
}
135135
NotifyResponse response = GSON.fromJson(notifyData, NotifyResponse.class);
@@ -318,7 +318,7 @@ public RefundQueryResult queryRefundByOutRefundNo(String subMchid, String outRef
318318

319319
@Override
320320
public RefundNotifyResult parseRefundNotifyResult(String notifyData, SignatureHeader header) throws WxPayException {
321-
if (Objects.nonNull(header) && !this.verifyNotifySign(header, notifyData)) {
321+
if (Objects.nonNull(header) && !payService.verifyNotifySign(header, notifyData)) {
322322
throw new WxPayException("非法请求,头部信息验证失败");
323323
}
324324
NotifyResponse response = GSON.fromJson(notifyData, NotifyResponse.class);
@@ -339,7 +339,7 @@ public RefundNotifyResult parseRefundNotifyResult(String notifyData, SignatureHe
339339

340340
@Override
341341
public WithdrawNotifyResult parseWithdrawNotifyResult(String notifyData, SignatureHeader header) throws WxPayException {
342-
if (Objects.nonNull(header) && !this.verifyNotifySign(header, notifyData)) {
342+
if (Objects.nonNull(header) && !payService.verifyNotifySign(header, notifyData)) {
343343
throw new WxPayException("非法请求,头部信息验证失败");
344344
}
345345
NotifyResponse response = GSON.fromJson(notifyData, NotifyResponse.class);
@@ -491,22 +491,6 @@ public AccountCancelApplicationsMediaResult uploadMediaAccountCancelApplication(
491491
}
492492
}
493493

494-
/**
495-
* 校验通知签名
496-
*
497-
* @param header 通知头信息
498-
* @param data 通知数据
499-
* @return true:校验通过 false:校验不通过
500-
*/
501-
private boolean verifyNotifySign(SignatureHeader header, String data) {
502-
String beforeSign = String.format("%s\n%s\n%s\n",
503-
header.getTimeStamp(),
504-
header.getNonce(),
505-
data);
506-
return payService.getConfig().getVerifier().verify(header.getSerialNo(),
507-
beforeSign.getBytes(StandardCharsets.UTF_8), header.getSigned());
508-
}
509-
510494
/**
511495
* 对象拼接到url
512496
*

0 commit comments

Comments
 (0)