Skip to content

Commit 89beb1d

Browse files
committed
微信修复V3转账查询接口,补充分账功能证书序列参数,V2沙箱地址变更
1 parent c51499a commit 89beb1d

7 files changed

Lines changed: 111 additions & 41 deletions

File tree

pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxConst.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface WxConst {
1616
/**
1717
* 沙箱
1818
*/
19-
String SANDBOXNEW = "sandboxnew/";
19+
String SANDBOXNEW = "xdc/apiv2sandbox/";
2020

2121
String SUCCESS = "SUCCESS";
2222
String FAIL = "FAIL";

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public class DefaultWxPayAssistService implements WxPayAssistService {
4343

4444
private HttpRequestTemplate requestTemplate;
4545

46-
private WxPayService wxPayService;
46+
private WxPayServiceInf wxPayService;
4747

4848

49-
public DefaultWxPayAssistService(WxPayService wxPayService) {
49+
public DefaultWxPayAssistService(WxPayServiceInf wxPayService) {
5050
this.wxPayService = wxPayService;
5151
payConfigStorage = wxPayService.getPayConfigStorage();
5252
requestTemplate = wxPayService.getHttpRequestTemplate();
@@ -111,7 +111,7 @@ public JSONObject doExecute(String body, TransactionType transactionType, Object
111111
return responseBody;
112112
}
113113
for (Header header : headers) {
114-
if ("Wechatpay-Serial".equals(header.getName())) {
114+
if (WxConst.WECHATPAY_SERIAL.equals(header.getName())) {
115115
// 更新平台证书的序列号,需要每次都更新,因为这个可能会改变
116116
payConfigStorage.getCertEnvironment().setPlatformSerialNumber(header.getValue());
117117
break;
@@ -138,7 +138,7 @@ public JSONObject doExecute(Map<String, Object> parameters, PayOrder order) {
138138
* 构建请求实体
139139
* 这里也做签名处理
140140
*
141-
* @param url url
141+
* @param url url
142142
* @param body 请求内容体
143143
* @param method 请求方法
144144
* @return 请求实体
@@ -158,7 +158,9 @@ public HttpEntity buildHttpEntity(String url, String body, String method) {
158158
entity.addHeader(new BasicHeader("Authorization", WxConst.SCHEMA.concat(token)));
159159
entity.addHeader(new BasicHeader("User-Agent", "Pay-Java-Parent"));
160160
entity.addHeader(new BasicHeader("Accept", APPLICATION_JSON.getMimeType()));
161-
return entity;
161+
162+
163+
return wxPayService.hookHttpEntity(entity);
162164
}
163165

164166

@@ -210,5 +212,4 @@ public Certificate getCertificate(String serialNo) {
210212
}
211213

212214

213-
214215
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
* date 2021/10/6
7676
* </pre>
7777
*/
78-
public class WxPayService extends BasePayService<WxPayConfigStorage> implements TransferService {
78+
public class WxPayService extends BasePayService<WxPayConfigStorage> implements TransferService, WxPayServiceInf {
7979

8080

8181
/**
@@ -123,12 +123,13 @@ public WxPayService(WxPayConfigStorage payConfigStorage, HttpConfigStorage confi
123123
*
124124
* @return 辅助api
125125
*/
126+
@Override
126127
public WxPayAssistService getAssistService() {
127128
if (null == assistService) {
128129
assistService = new DefaultWxPayAssistService(this);
129-
//在这预先进行初始化
130-
assistService.refreshCertificate();
131130
}
131+
//在这预先进行初始化
132+
assistService.refreshCertificate();
132133
return assistService;
133134
}
134135

@@ -144,7 +145,7 @@ protected void initAfter() {
144145
// new Thread(() -> {
145146
payConfigStorage.loadCertEnvironment();
146147
wxParameterStructure = new WxParameterStructure(payConfigStorage);
147-
getAssistService();
148+
setApiServerUrl(WxConst.URI);
148149
// }).start();
149150

150151
}
@@ -155,11 +156,14 @@ protected void initAfter() {
155156
* @param apiServerUrl api服务器地址
156157
* @return 自身
157158
*/
159+
@Override
158160
public WxPayService setApiServerUrl(String apiServerUrl) {
159161
this.apiServerUrl = apiServerUrl;
162+
getAssistService();
160163
return this;
161164
}
162165

166+
@Override
163167
public String getApiServerUrl() {
164168
return apiServerUrl;
165169
}
@@ -218,14 +222,9 @@ public boolean verify(NoticeParams noticeParams) {
218222

219223
Certificate certificate = getAssistService().getCertificate(serial);
220224

221-
Map<String, Object> attr = noticeParams.getAttr();
222-
223-
if (Util.isEmpty(attr)) {
224-
throw new PayErrorException(new WxPayError(FAILURE, "请勿置空NoticeParams.attr中的数据"));
225-
}
226225

227226
//这里为微信回调时的请求内容体,原值数据
228-
String body = (String) attr.get(WxConst.RESP_BODY);
227+
String body = noticeParams.getBodyStr();
229228
//签名信息
230229
String signText = StringUtils.joining("\n", timestamp, nonce, body);
231230

@@ -362,6 +361,7 @@ public NoticeParams getNoticeParams(NoticeRequest request) {
362361
String body = IOUtils.toString(is);
363362
noticeParams = JSON.parseObject(body, WxNoticeParams.class);
364363
noticeParams.setAttr(new MapGen<String, Object>(WxConst.RESP_BODY, body).getAttr());
364+
noticeParams.setBodyStr(body);
365365
Resource resource = noticeParams.getResource();
366366
String associatedData = resource.getAssociatedData();
367367
String nonce = resource.getNonce();
@@ -703,14 +703,15 @@ else if (StringUtils.isNotEmpty(assistOrder.getOutTradeNo())) {
703703
}
704704
}
705705

706-
if (transactionType == com.egzosn.pay.wx.v3.bean.WxTransferType.QUERY_BATCH_BY_BATCH_ID || transactionType == com.egzosn.pay.wx.v3.bean.WxTransferType.QUERY_BATCH_BY_OUT_BATCH_NO) {
706+
if (transactionType == WxTransferType.QUERY_BATCH_BY_BATCH_ID || transactionType == WxTransferType.QUERY_BATCH_BY_OUT_BATCH_NO) {
707707
OrderParaStructure.loadParameters(parameters, WxConst.NEED_QUERY_DETAIL, assistOrder);
708708
OrderParaStructure.loadParameters(parameters, WxConst.OFFSET, assistOrder);
709709
OrderParaStructure.loadParameters(parameters, WxConst.LIMIT, assistOrder);
710710
OrderParaStructure.loadParameters(parameters, WxConst.DETAIL_STATUS, assistOrder);
711711
}
712712

713-
String requestBody = JSON.toJSONString(parameters);
713+
714+
String requestBody = UriVariables.getMapToParameters(parameters);
714715
return getAssistService().doExecute(requestBody, assistOrder.getTransactionType(), uriVariables.toArray());
715716
}
716717

@@ -728,7 +729,7 @@ else if (StringUtils.isNotEmpty(assistOrder.getOutTradeNo())) {
728729
*/
729730
@Override
730731
public Map<String, Object> transferQuery(String outNo, String wxTransferType) {
731-
throw new PayErrorException(new WxPayError("", "V3不支持转账查询"));
732+
throw new PayErrorException(new WxPayError("", "V3不支持此转账查询:替代方法transferQuery(AssistOrder assistOrder)"));
732733
}
733734

734735

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.egzosn.pay.wx.v3.api;
2+
3+
import com.egzosn.pay.common.api.PayService;
4+
import com.egzosn.pay.common.bean.NoticeParams;
5+
import com.egzosn.pay.common.bean.TransactionType;
6+
import com.egzosn.pay.common.http.HttpStringEntity;
7+
8+
/**
9+
* @author Egan
10+
* @email egan@egzosn.com
11+
* @date 2023/9/11
12+
*/
13+
public interface WxPayServiceInf extends PayService<WxPayConfigStorage> {
14+
/**
15+
* 辅助api
16+
*
17+
* @return 辅助api
18+
*/
19+
WxPayAssistService getAssistService();
20+
21+
/**
22+
* 设置api服务器地址
23+
*
24+
* @param apiServerUrl api服务器地址
25+
* @return 自身
26+
*/
27+
WxPayService setApiServerUrl(String apiServerUrl);
28+
29+
String getApiServerUrl();
30+
31+
/**
32+
* 根据交易类型获取url
33+
*
34+
* @param transactionType 交易类型
35+
* @return 请求url
36+
*/
37+
@Override
38+
String getReqUrl(TransactionType transactionType);
39+
40+
/**
41+
* 验签,使用微信平台证书.
42+
*
43+
* @param noticeParams 通知参数
44+
* @return the boolean
45+
*/
46+
@Override
47+
boolean verify(NoticeParams noticeParams);
48+
49+
/**
50+
* 签名
51+
*
52+
* @param content 需要签名的内容 不包含key
53+
* @param characterEncoding 字符编码
54+
* @return 签名结果
55+
*/
56+
@Override
57+
String createSign(String content, String characterEncoding);
58+
59+
/**
60+
* http 实体 钩子
61+
* @param entity 实体
62+
* @return 返回处理后的实体
63+
*/
64+
default HttpStringEntity hookHttpEntity(HttpStringEntity entity){
65+
return entity;
66+
}
67+
}

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
package com.egzosn.pay.wx.v3.api;
22

33
import java.io.InputStream;
4-
import java.security.PrivateKey;
54
import java.util.Date;
65
import java.util.Map;
76

7+
import org.apache.http.message.BasicHeader;
8+
89
import com.alibaba.fastjson.JSONObject;
910
import com.egzosn.pay.common.bean.AssistOrder;
1011
import com.egzosn.pay.common.bean.BillType;
1112
import com.egzosn.pay.common.bean.MethodType;
1213
import com.egzosn.pay.common.bean.NoticeParams;
13-
import com.egzosn.pay.common.bean.NoticeRequest;
1414
import com.egzosn.pay.common.bean.OrderParaStructure;
1515
import com.egzosn.pay.common.bean.PayMessage;
1616
import com.egzosn.pay.common.bean.PayOrder;
17-
import com.egzosn.pay.common.bean.PayOutMessage;
1817
import com.egzosn.pay.common.bean.RefundOrder;
1918
import com.egzosn.pay.common.bean.RefundResult;
2019
import com.egzosn.pay.common.bean.TransferOrder;
2120
import com.egzosn.pay.common.bean.result.PayException;
2221
import com.egzosn.pay.common.exception.PayErrorException;
2322
import com.egzosn.pay.common.http.HttpConfigStorage;
23+
import com.egzosn.pay.common.http.HttpStringEntity;
2424
import com.egzosn.pay.common.http.UriVariables;
2525
import com.egzosn.pay.common.util.DateUtils;
2626
import com.egzosn.pay.common.util.MapGen;
27-
import com.egzosn.pay.common.util.sign.encrypt.RSA2;
2827
import com.egzosn.pay.wx.bean.WxPayError;
2928
import com.egzosn.pay.wx.bean.WxTransferType;
3029
import com.egzosn.pay.wx.v3.bean.WxProfitSharingTransactionType;
@@ -70,8 +69,8 @@ public WxProfitSharingService(WxPayConfigStorage payConfigStorage, HttpConfigSto
7069
@Override
7170
protected void initAfter() {
7271
// new Thread(() -> {
73-
payConfigStorage.loadCertEnvironment();
74-
getAssistService();
72+
payConfigStorage.loadCertEnvironment();
73+
setApiServerUrl(WxConst.URI);
7574
// }).start();
7675

7776
}
@@ -121,6 +120,17 @@ public JSONObject unifiedOrder(PayOrder order) {
121120
return getAssistService().doExecute(parameters, order);
122121
}
123122

123+
/**
124+
* http 实体 钩子
125+
*
126+
* @param entity 实体
127+
* @return 返回处理后的实体
128+
*/
129+
@Override
130+
public HttpStringEntity hookHttpEntity(HttpStringEntity entity) {
131+
entity.addHeader(new BasicHeader(WxConst.WECHATPAY_SERIAL, payConfigStorage.getCertEnvironment().getPlatformSerialNumber()));
132+
return entity;
133+
}
124134

125135
/**
126136
* 返回创建的订单信息
@@ -148,19 +158,6 @@ public Map<String, Object> orderInfo(PayOrder order) {
148158
}
149159

150160

151-
/**
152-
* 签名
153-
*
154-
* @param content 需要签名的内容 不包含key
155-
* @param characterEncoding 字符编码
156-
* @return 签名结果
157-
*/
158-
@Override
159-
public String createSign(String content, String characterEncoding) {
160-
PrivateKey privateKey = payConfigStorage.getCertEnvironment().getPrivateKey();
161-
return RSA2.sign(content, privateKey, characterEncoding);
162-
}
163-
164161
/**
165162
* 将请求参数或者请求流转化为 Map
166163
*

pay-java-wx/src/main/java/com/egzosn/pay/wx/v3/bean/CertEnvironment.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.security.PrivateKey;
44
import java.security.PublicKey;
55

6+
import com.egzosn.pay.common.util.str.StringUtils;
7+
68
/**
79
* 证书模式运行时环境
810
*
@@ -32,8 +34,6 @@ public class CertEnvironment {
3234
private String platformSerialNumber;
3335

3436

35-
36-
3737
public CertEnvironment() {
3838
}
3939

@@ -68,6 +68,9 @@ public void setSerialNumber(String serialNumber) {
6868
}
6969

7070
public String getPlatformSerialNumber() {
71+
if (StringUtils.isEmpty(platformSerialNumber)) {
72+
setPlatformSerialNumber(serialNumber);
73+
}
7174
return platformSerialNumber;
7275
}
7376

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ private WxConst() {
8383
public static final String OFFSET = "offset";
8484
public static final String LIMIT = "limit";
8585
public static final String DETAIL_STATUS = "detail_status";
86+
public static final String WECHATPAY_SERIAL = "Wechatpay-Serial";
8687

8788

8889
}

0 commit comments

Comments
 (0)