Skip to content

Commit e30138f

Browse files
committed
#53 实现微信支付查询订单的接口,并重构规范化微信支付相关接口类的命名
1 parent dff627b commit e30138f

20 files changed

+964
-955
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@
1313
*/
1414
public interface WxMpPayService {
1515

16+
/**
17+
* <pre>
18+
* 查询订单(详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2)
19+
* 该接口提供所有微信支付订单的查询,商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。
20+
* 需要调用查询接口的情况:
21+
◆ 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知;
22+
◆ 调用支付接口后,返回系统错误或未知交易状态情况;
23+
◆ 调用被扫支付API,返回USERPAYING的状态;
24+
◆ 调用关单或撤销接口API之前,需确认支付状态;
25+
* 接口地址:https://api.mch.weixin.qq.com/pay/orderquery
26+
* </pre>
27+
* @param transactionId 微信支付分配的商户号
28+
* @param outTradeNo 商户系统内部的订单号,当没提供transaction_id时需要传这个。
29+
* @throws WxErrorException
30+
*/
31+
WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException;
32+
1633
/**
1734
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
1835
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
@@ -21,31 +38,21 @@ public interface WxMpPayService {
2138
* @param request 请求对象
2239
*
2340
*/
24-
WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
25-
throws WxErrorException;
41+
WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxErrorException;
2642

2743
/**
2844
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数
2945
* 详见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN
3046
* @param request 请求对象
3147
*/
32-
Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxErrorException;
33-
34-
/**
35-
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。
36-
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
37-
* @throws WxErrorException
38-
*
39-
*/
40-
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo)
41-
throws WxErrorException;
48+
Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException;
4249

4350
/**
4451
* 读取支付结果通知
4552
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
4653
*
4754
*/
48-
WxMpPayCallback getJSSDKCallbackData(String xmlData);
55+
WxPayJsSDKCallback getJSSDKCallbackData(String xmlData);
4956

5057
/**
5158
* <pre>
@@ -57,7 +64,7 @@ WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo)
5764
* @param keyFile 证书文件对象
5865
* @return 退款操作结果
5966
*/
60-
WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile) throws WxErrorException;
67+
WxPayRefundResult refund(WxPayRefundRequest request, File keyFile) throws WxErrorException;
6168

6269
/**
6370
* <pre>
@@ -78,7 +85,7 @@ WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo)
7885
* @param request 请求对象
7986
* @param keyFile 证书文件对象
8087
*/
81-
WxRedpackResult sendRedpack(WxSendRedpackRequest request, File keyFile) throws WxErrorException;
88+
WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File keyFile) throws WxErrorException;
8289

8390
/**
8491
* <pre>

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

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -46,64 +46,26 @@ public WxMpPayServiceImpl(WxMpService wxMpService) {
4646
}
4747

4848
@Override
49-
public WxMpPayResult getJSSDKPayResult(String transactionId,
50-
String outTradeNo) throws WxErrorException {
51-
String nonce_str = System.currentTimeMillis() + "";
52-
53-
SortedMap<String, String> packageParams = new TreeMap<>();
54-
packageParams.put("appid",
55-
this.wxMpService.getWxMpConfigStorage().getAppId());
56-
packageParams.put("mch_id",
57-
this.wxMpService.getWxMpConfigStorage().getPartnerId());
58-
59-
if (transactionId != null && !"".equals(transactionId.trim())) {
60-
packageParams.put("transaction_id", transactionId);
61-
} else if (outTradeNo != null && !"".equals(outTradeNo.trim())) {
62-
packageParams.put("out_trade_no", outTradeNo);
63-
} else {
64-
throw new IllegalArgumentException(
65-
"Either 'transactionId' or 'outTradeNo' must be given.");
66-
}
67-
68-
packageParams.put("nonce_str", nonce_str);
69-
packageParams.put("sign", this.createSign(packageParams,
70-
this.wxMpService.getWxMpConfigStorage().getPartnerKey()));
71-
72-
StringBuilder request = new StringBuilder("<xml>");
73-
for (Map.Entry<String, String> para : packageParams.entrySet()) {
74-
request.append(String.format("<%s>%s</%s>", para.getKey(),
75-
para.getValue(), para.getKey()));
76-
}
77-
request.append("</xml>");
78-
79-
String url = PAY_BASE_URL + "/pay/orderquery";
80-
String responseContent = this.wxMpService.post(url, request.toString());
81-
XStream xstream = XStreamInitializer.getInstance();
82-
xstream.alias("xml", WxMpPayResult.class);
83-
return (WxMpPayResult) xstream.fromXML(responseContent);
84-
}
85-
86-
@Override
87-
public WxMpPayCallback getJSSDKCallbackData(String xmlData) {
49+
public WxPayJsSDKCallback getJSSDKCallbackData(String xmlData) {
8850
try {
8951
XStream xstream = XStreamInitializer.getInstance();
90-
xstream.alias("xml", WxMpPayCallback.class);
91-
return (WxMpPayCallback) xstream.fromXML(xmlData);
52+
xstream.alias("xml", WxPayJsSDKCallback.class);
53+
return (WxPayJsSDKCallback) xstream.fromXML(xmlData);
9254
} catch (Exception e) {
9355
e.printStackTrace();
9456
}
9557

96-
return new WxMpPayCallback();
58+
return new WxPayJsSDKCallback();
9759
}
9860

9961
@Override
100-
public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
62+
public WxPayRefundResult refund(WxPayRefundRequest request, File keyFile)
10163
throws WxErrorException {
10264
checkParameters(request);
10365

10466
XStream xstream = XStreamInitializer.getInstance();
105-
xstream.processAnnotations(WxMpPayRefundResult.class);
106-
xstream.processAnnotations(WxMpPayRefundRequest.class);
67+
xstream.processAnnotations(WxPayRefundResult.class);
68+
xstream.processAnnotations(WxPayRefundRequest.class);
10769

10870
request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId());
10971
String partnerId = this.wxMpService.getWxMpConfigStorage().getPartnerId();
@@ -115,7 +77,7 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
11577

11678
String url = PAY_BASE_URL + "/secapi/pay/refund";
11779
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), partnerId);
118-
WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream.fromXML(responseContent);
80+
WxPayRefundResult wxMpPayRefundResult = (WxPayRefundResult) xstream.fromXML(responseContent);
11981

12082
if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode())
12183
|| !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) {
@@ -132,7 +94,7 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
13294
return wxMpPayRefundResult;
13395
}
13496

135-
private void checkParameters(WxMpPayRefundRequest request) throws WxErrorException {
97+
private void checkParameters(WxPayRefundRequest request) throws WxErrorException {
13698
BeanUtils.checkRequiredFields(request);
13799

138100
if (StringUtils.isNotBlank(request.getRefundAccount())) {
@@ -154,11 +116,11 @@ public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm,
154116
}
155117

156118
@Override
157-
public WxRedpackResult sendRedpack(WxSendRedpackRequest request, File keyFile)
119+
public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File keyFile)
158120
throws WxErrorException {
159121
XStream xstream = XStreamInitializer.getInstance();
160-
xstream.processAnnotations(WxSendRedpackRequest.class);
161-
xstream.processAnnotations(WxRedpackResult.class);
122+
xstream.processAnnotations(WxPaySendRedpackRequest.class);
123+
xstream.processAnnotations(WxPaySendRedpackResult.class);
162124

163125
request.setWxAppid(this.wxMpService.getWxMpConfigStorage().getAppId());
164126
String mchId = this.wxMpService.getWxMpConfigStorage().getPartnerId();
@@ -176,7 +138,7 @@ public WxRedpackResult sendRedpack(WxSendRedpackRequest request, File keyFile)
176138
}
177139

178140
String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), mchId);
179-
WxRedpackResult redpackResult = (WxRedpackResult) xstream
141+
WxPaySendRedpackResult redpackResult = (WxPaySendRedpackResult) xstream
180142
.fromXML(responseContent);
181143
if ("FAIL".equals(redpackResult.getResultCode())) {
182144
throw new WxErrorException(WxError.newBuilder()
@@ -212,13 +174,49 @@ private String createSign(Map<String, String> packageParams, String signKey) {
212174
}
213175

214176
@Override
215-
public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
177+
public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException {
178+
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) ||
179+
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) {
180+
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一");
181+
}
182+
183+
XStream xstream = XStreamInitializer.getInstance();
184+
xstream.processAnnotations(WxPayOrderQueryRequest.class);
185+
xstream.processAnnotations(WxPayOrderQueryResult.class);
186+
187+
WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
188+
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
189+
request.setTransactionId(StringUtils.trimToNull(transactionId));
190+
request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId());
191+
request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId());
192+
request.setNonceStr(System.currentTimeMillis() + "");
193+
194+
String sign = this.createSign(BeanUtils.xmlBean2Map(request),
195+
this.wxMpService.getWxMpConfigStorage().getPartnerKey());
196+
request.setSign(sign);
197+
198+
String url = PAY_BASE_URL + "/pay/orderquery";
199+
200+
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
201+
WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
202+
result.composeCoupons(responseContent);
203+
if ("FAIL".equals(result.getResultCode())) {
204+
throw new WxErrorException(WxError.newBuilder()
205+
.setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
206+
.build());
207+
}
208+
209+
return result;
210+
}
211+
212+
@Override
213+
public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
216214
throws WxErrorException {
217215
checkParameters(request);
218216

219217
XStream xstream = XStreamInitializer.getInstance();
220-
xstream.processAnnotations(WxUnifiedOrderRequest.class);
221-
xstream.processAnnotations(WxUnifiedOrderResult.class);
218+
xstream.processAnnotations(WxPayUnifiedOrderRequest.class);
219+
xstream.processAnnotations(WxPayUnifiedOrderResult.class);
222220

223221
request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId());
224222
request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId());
@@ -231,7 +229,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
231229
String url = PAY_BASE_URL + "/pay/unifiedorder";
232230

233231
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
234-
WxUnifiedOrderResult result = (WxUnifiedOrderResult) xstream
232+
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
235233
.fromXML(responseContent);
236234
if ("FAIL".equals(result.getResultCode())) {
237235
throw new WxErrorException(WxError.newBuilder()
@@ -242,7 +240,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
242240
return result;
243241
}
244242

245-
private void checkParameters(WxUnifiedOrderRequest request) throws WxErrorException {
243+
private void checkParameters(WxPayUnifiedOrderRequest request) throws WxErrorException {
246244
BeanUtils.checkRequiredFields(request);
247245

248246
if (! ArrayUtils.contains(TRADE_TYPES, request.getTradeType())) {
@@ -259,8 +257,8 @@ private void checkParameters(WxUnifiedOrderRequest request) throws WxErrorExcept
259257
}
260258

261259
@Override
262-
public Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxErrorException {
263-
WxUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
260+
public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException {
261+
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
264262

265263
if (!"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getReturnCode())
266264
|| !"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getResultCode())) {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/WxEntPayQueryRequest.java

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,7 @@
2020
* @author binarywang (https://github.com/binarywang)
2121
*/
2222
@XStreamAlias("xml")
23-
public class WxEntPayQueryRequest {
24-
/**
25-
* <pre>
26-
* Appid
27-
* appid
28-
* 是
29-
* wxe062425f740d30d8
30-
* String(32)
31-
* 商户号的appid
32-
* </pre>
33-
*/
34-
@XStreamAlias("appid")
35-
private String appid;
36-
23+
public class WxEntPayQueryRequest extends WxPayBaseRequest {
3724
/**
3825
* <pre>
3926
* 商户号
@@ -47,32 +34,6 @@ public class WxEntPayQueryRequest {
4734
@XStreamAlias("mchid")
4835
private String mchId;
4936

50-
/**
51-
* <pre>
52-
* 随机字符串
53-
* nonce_str
54-
* 是
55-
* 5K8264ILTKCH16CQ2502SI8ZNMTM67VS
56-
* String(32)
57-
* 随机字符串,不长于32位
58-
* </pre>
59-
*/
60-
@XStreamAlias("nonce_str")
61-
private String nonceStr;
62-
63-
/**
64-
* <pre>
65-
* 签名
66-
* sign
67-
* 是
68-
* C380BEC2BFD727A4B6845133519F3AD6
69-
* String(32)
70-
*签名,详见签名算法
71-
* </pre>
72-
*/
73-
@XStreamAlias("sign")
74-
private String sign;
75-
7637
/**
7738
* <pre>
7839
* 商户订单号

0 commit comments

Comments
 (0)