Skip to content

Commit 02679bb

Browse files
committed
微信支付相关接口优化,支持自定义参数信息,不在默认使用配置信息
1 parent dc821ff commit 02679bb

File tree

4 files changed

+112
-292
lines changed

4 files changed

+112
-292
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java

Lines changed: 96 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
4444

4545
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com";
4646
private static final String[] TRADE_TYPES = new String[]{"JSAPI", "NATIVE", "APP"};
47-
private static final String[] REFUND_ACCOUNT = new String[]{"REFUND_SOURCE_RECHARGE_FUNDS",
48-
"REFUND_SOURCE_UNSETTLED_FUNDS"};
47+
private static final String[] REFUND_ACCOUNT = new String[]{"REFUND_SOURCE_RECHARGE_FUNDS","REFUND_SOURCE_UNSETTLED_FUNDS"};
4948
private final Logger log = LoggerFactory.getLogger(this.getClass());
5049
private WxMpService wxMpService;
5150

@@ -58,23 +57,20 @@ private WxMpConfigStorage getConfig() {
5857
}
5958

6059
@Override
61-
public WxPayRefundResult refund(WxPayRefundRequest request, File keyFile)
62-
throws WxErrorException {
63-
checkParameters(request);
64-
60+
public WxPayRefundResult refund(WxPayRefundRequest request, File keyFile) throws WxErrorException {
6561
XStream xstream = XStreamInitializer.getInstance();
6662
xstream.processAnnotations(WxPayRefundRequest.class);
6763
xstream.processAnnotations(WxPayRefundResult.class);
68-
69-
request.setAppid(getConfig().getAppId());
70-
String partnerId = getConfig().getPartnerId();
71-
request.setMchId(partnerId);
72-
request.setNonceStr(System.currentTimeMillis() + "");
73-
request.setOpUserId(partnerId);
64+
65+
initRequest(request);
66+
if(StringUtils.isBlank(request.getOpUserId())){
67+
request.setOpUserId(getConfig().getPartnerId());
68+
}
69+
checkParameters(request);
7470
request.setSign(this.createSign(request));
7571

7672
String url = PAY_BASE_URL + "/secapi/pay/refund";
77-
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), partnerId);
73+
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
7874
WxPayRefundResult result = (WxPayRefundResult) xstream.fromXML(responseContent);
7975
this.checkResult(result);
8076
return result;
@@ -92,18 +88,14 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
9288
xstream.processAnnotations(WxPayRefundQueryResult.class);
9389

9490
WxPayRefundQueryRequest request = new WxPayRefundQueryRequest();
91+
initRequest(request);
9592
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
9693
request.setTransactionId(StringUtils.trimToNull(transactionId));
9794
request.setOutRefundNo(StringUtils.trimToNull(outRefundNo));
9895
request.setRefundId(StringUtils.trimToNull(refundId));
99-
100-
request.setAppid(getConfig().getAppId());
101-
request.setMchId(getConfig().getPartnerId());
102-
request.setNonceStr(System.currentTimeMillis() + "");
10396
request.setSign(this.createSign(request));
10497

10598
String url = PAY_BASE_URL + "/pay/refundquery";
106-
10799
String responseContent = this.executeRequest(url, xstream.toXML(request));
108100
WxPayRefundQueryResult result = (WxPayRefundQueryResult) xstream.fromXML(responseContent);
109101
result.composeRefundRecords(responseContent);
@@ -127,7 +119,7 @@ private void checkResult(WxPayBaseResult result) throws WxErrorException {
127119
+ result.getReturnMsg() + ", 结果代码: " + result.getResultCode() + ", 错误代码: "
128120
+ result.getErrCode() + ", 错误详情: " + result.getErrCodeDes())
129121
.build();
130-
log.debug("结果校验失败,参数:{},详细:{}", result.toMap(), error);
122+
log.error("结果业务代码异常,参数:{},详细:{}", result.toMap(), error);
131123
throw new WxErrorException(error);
132124
}
133125
}
@@ -174,19 +166,15 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File
174166
xstream.processAnnotations(WxPaySendRedpackRequest.class);
175167
xstream.processAnnotations(WxPaySendRedpackResult.class);
176168

177-
request.setWxAppid(getConfig().getAppId());
178-
String mchId = getConfig().getPartnerId();
179-
request.setMchId(mchId);
180-
request.setNonceStr(System.currentTimeMillis() + "");
169+
initRequest(request);
181170
request.setSign(this.createSign(request));
182171

183172
String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack";
184173
if (request.getAmtType() != null) {
185174
//裂变红包
186175
url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack";
187176
}
188-
189-
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), mchId);
177+
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
190178
WxPaySendRedpackResult result = (WxPaySendRedpackResult) xstream.fromXML(responseContent);
191179
this.checkResult(result);
192180
return result;
@@ -201,75 +189,16 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) thro
201189
WxPayRedpackQueryRequest request = new WxPayRedpackQueryRequest();
202190
request.setMchBillNo(mchBillNo);
203191
request.setBillType("MCHT");
204-
205-
request.setAppid(getConfig().getAppId());
206-
String mchId = getConfig().getPartnerId();
207-
request.setMchId(mchId);
208-
request.setNonceStr(System.currentTimeMillis() + "");
209-
210-
String sign = this.createSign(request);
211-
request.setSign(sign);
192+
initRequest(request);
193+
request.setSign(this.createSign(request));
212194

213195
String url = PAY_BASE_URL + "/mmpaymkttransfers/gethbinfo";
214-
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), mchId);
196+
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
215197
WxPayRedpackQueryResult result = (WxPayRedpackQueryResult) xstream.fromXML(responseContent);
216198
this.checkResult(result);
217199
return result;
218200
}
219201

220-
@Override
221-
public String createSign(Object xmlBean) {
222-
return createSign(BeanUtils.xmlBean2Map(xmlBean), getConfig().getPartnerKey());
223-
}
224-
225-
@Override
226-
public String createSign(Object xmlBean, String signKey) {
227-
return createSign(BeanUtils.xmlBean2Map(xmlBean), signKey);
228-
}
229-
230-
@Override
231-
public String createSign(Map<String, String> params) {
232-
return createSign(params, getConfig().getPartnerKey());
233-
}
234-
235-
@Override
236-
public String createSign(Map<String, String> params, String signKey) {
237-
SortedMap<String, String> sortedMap = new TreeMap<>(params);
238-
239-
StringBuilder toSign = new StringBuilder();
240-
for (String key : sortedMap.keySet()) {
241-
String value = params.get(key);
242-
if (StringUtils.isNotEmpty(value) && !"sign".equals(key) && !"key".equals(key)) {
243-
toSign.append(key + "=" + value + "&");
244-
}
245-
}
246-
247-
toSign.append("key=" + signKey);
248-
return DigestUtils.md5Hex(toSign.toString()).toUpperCase();
249-
}
250-
251-
@Override
252-
public boolean checkSign(Object xmlBean) {
253-
return checkSign(BeanUtils.xmlBean2Map(xmlBean), getConfig().getPartnerKey());
254-
}
255-
256-
@Override
257-
public boolean checkSign(Object xmlBean, String signKey) {
258-
return checkSign(BeanUtils.xmlBean2Map(xmlBean), signKey);
259-
}
260-
261-
@Override
262-
public boolean checkSign(Map<String, String> params) {
263-
return checkSign(params, getConfig().getPartnerKey());
264-
}
265-
266-
@Override
267-
public boolean checkSign(Map<String, String> params, String signKey) {
268-
String sign = this.createSign(params, signKey);
269-
return sign.equals(params.get("sign"));
270-
}
271-
272-
273202
@Override
274203
public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException {
275204
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) ||
@@ -284,13 +213,10 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
284213
WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
285214
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
286215
request.setTransactionId(StringUtils.trimToNull(transactionId));
287-
request.setAppid(getConfig().getAppId());
288-
request.setMchId(getConfig().getPartnerId());
289-
request.setNonceStr(System.currentTimeMillis() + "");
216+
initRequest(request);
290217
request.setSign(this.createSign(request));
291218

292219
String url = PAY_BASE_URL + "/pay/orderquery";
293-
294220
String responseContent = this.executeRequest(url, xstream.toXML(request));
295221
WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
296222
result.composeCoupons(responseContent);
@@ -310,13 +236,10 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
310236

311237
WxPayOrderCloseRequest request = new WxPayOrderCloseRequest();
312238
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
313-
request.setAppid(getConfig().getAppId());
314-
request.setMchId(getConfig().getPartnerId());
315-
request.setNonceStr(System.currentTimeMillis() + "");
239+
initRequest(request);
316240
request.setSign(this.createSign(request));
317241

318242
String url = PAY_BASE_URL + "/pay/closeorder";
319-
320243
String responseContent = this.executeRequest(url, xstream.toXML(request));
321244
WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent);
322245
this.checkResult(result);
@@ -331,16 +254,17 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
331254
XStream xstream = XStreamInitializer.getInstance();
332255
xstream.processAnnotations(WxPayUnifiedOrderRequest.class);
333256
xstream.processAnnotations(WxPayUnifiedOrderResult.class);
334-
335-
request.setAppid(getConfig().getAppId());
336-
request.setMchId(getConfig().getPartnerId());
337-
request.setNotifyURL(getConfig().getNotifyURL());
338-
request.setTradeType(getConfig().getTradeType());
339-
request.setNonceStr(System.currentTimeMillis() + "");
340-
257+
258+
initRequest(request);
259+
if(StringUtils.isBlank(request.getNotifyURL())){
260+
request.setNotifyURL(getConfig().getNotifyURL());
261+
}
262+
if(StringUtils.isBlank(request.getTradeType())){
263+
request.setTradeType(getConfig().getTradeType());
264+
}
341265
checkParameters(request);//校验参数
342-
343266
request.setSign(this.createSign(request));
267+
344268
String url = PAY_BASE_URL + "/pay/unifiedorder";
345269
String xmlParam = xstream.toXML(request);
346270
log.debug("微信统一下单接口,URL:{},参数:{}", url, xmlParam);
@@ -351,6 +275,18 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
351275
this.checkResult(result);
352276
return result;
353277
}
278+
279+
private void initRequest(WxPayBaseRequest request){
280+
if(StringUtils.isBlank(request.getAppid())){
281+
request.setAppid(getConfig().getAppId());
282+
}
283+
if(StringUtils.isBlank(request.getMchId())){
284+
request.setMchId(getConfig().getPartnerId());
285+
}
286+
if(StringUtils.isBlank(request.getNonceStr())){
287+
request.setNonceStr(String.valueOf(System.currentTimeMillis()));
288+
}
289+
}
354290

355291
private void checkParameters(WxPayUnifiedOrderRequest request) throws WxErrorException {
356292
BeanUtils.checkRequiredFields(request);
@@ -381,7 +317,7 @@ public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws W
381317
payInfo.put("appId", getConfig().getAppId());
382318
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
383319
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
384-
payInfo.put("nonceStr", System.currentTimeMillis() + "");
320+
payInfo.put("nonceStr", String.valueOf(System.currentTimeMillis()));
385321
payInfo.put("package", "prepay_id=" + prepayId);
386322
payInfo.put("signType", "MD5");
387323
if ("NATIVE".equals(request.getTradeType())) {
@@ -393,15 +329,12 @@ public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws W
393329

394330
@Override
395331
public WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErrorException {
396-
BeanUtils.checkRequiredFields(request);
397-
398332
XStream xstream = XStreamInitializer.getInstance();
399333
xstream.processAnnotations(WxEntPayRequest.class);
400334
xstream.processAnnotations(WxEntPayResult.class);
401-
402-
request.setMchAppid(getConfig().getAppId());
403-
request.setMchId(getConfig().getPartnerId());
404-
request.setNonceStr(System.currentTimeMillis() + "");
335+
336+
initRequest(request);
337+
BeanUtils.checkRequiredFields(request);
405338
request.setSign(this.createSign(request));
406339

407340
String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers";
@@ -419,13 +352,10 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
419352
xstream.processAnnotations(WxEntPayQueryResult.class);
420353

421354
WxEntPayQueryRequest request = new WxEntPayQueryRequest();
422-
request.setAppid(getConfig().getAppId());
423-
request.setMchId(getConfig().getPartnerId());
424-
request.setNonceStr(System.currentTimeMillis() + "");
355+
initRequest(request);
425356
request.setSign(this.createSign(request));
426357

427358
String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo";
428-
429359
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId());
430360
WxEntPayQueryResult result = (WxEntPayQueryResult) xstream.fromXML(responseContent);
431361
this.checkResult(result);
@@ -483,4 +413,56 @@ private String executeRequestWithKeyFile(String url, File keyFile, String reques
483413
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e);
484414
}
485415
}
416+
417+
@Override
418+
public String createSign(Object xmlBean) {
419+
return createSign(BeanUtils.xmlBean2Map(xmlBean), getConfig().getPartnerKey());
420+
}
421+
422+
@Override
423+
public String createSign(Object xmlBean, String signKey) {
424+
return createSign(BeanUtils.xmlBean2Map(xmlBean), signKey);
425+
}
426+
427+
@Override
428+
public String createSign(Map<String, String> params) {
429+
return createSign(params, getConfig().getPartnerKey());
430+
}
431+
432+
@Override
433+
public String createSign(Map<String, String> params, String signKey) {
434+
SortedMap<String, String> sortedMap = new TreeMap<>(params);
435+
436+
StringBuilder toSign = new StringBuilder();
437+
for (String key : sortedMap.keySet()) {
438+
String value = params.get(key);
439+
if (StringUtils.isNotEmpty(value) && !"sign".equals(key) && !"key".equals(key)) {
440+
toSign.append(key + "=" + value + "&");
441+
}
442+
}
443+
444+
toSign.append("key=" + signKey);
445+
return DigestUtils.md5Hex(toSign.toString()).toUpperCase();
446+
}
447+
448+
@Override
449+
public boolean checkSign(Object xmlBean) {
450+
return checkSign(BeanUtils.xmlBean2Map(xmlBean), getConfig().getPartnerKey());
451+
}
452+
453+
@Override
454+
public boolean checkSign(Object xmlBean, String signKey) {
455+
return checkSign(BeanUtils.xmlBean2Map(xmlBean), signKey);
456+
}
457+
458+
@Override
459+
public boolean checkSign(Map<String, String> params) {
460+
return checkSign(params, getConfig().getPartnerKey());
461+
}
462+
463+
@Override
464+
public boolean checkSign(Map<String, String> params, String signKey) {
465+
String sign = this.createSign(params, signKey);
466+
return sign.equals(params.get("sign"));
467+
}
486468
}

0 commit comments

Comments
 (0)