Skip to content

Commit 5594c88

Browse files
committed
Fix the bug that fails to query order due to sign error.
1 parent 89a260d commit 5594c88

File tree

2 files changed

+275
-233
lines changed

2 files changed

+275
-233
lines changed

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -892,44 +892,44 @@ public Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) {
892892

893893
@Override
894894
public WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) {
895-
String nonce_str = System.currentTimeMillis() + "";
895+
String nonce_str = System.currentTimeMillis() + "";
896896

897-
SortedMap<String, String> packageParams = new TreeMap<String, String>();
898-
packageParams.put("appid", wxMpConfigStorage.getAppId());
899-
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
897+
SortedMap<String, String> packageParams = new TreeMap<String, String>();
898+
packageParams.put("appid", wxMpConfigStorage.getAppId());
899+
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
900+
if (transactionId != null && !"".equals(transactionId.trim()))
900901
packageParams.put("transaction_id", transactionId);
902+
else if (outTradeNo != null && !"".equals(outTradeNo.trim()))
901903
packageParams.put("out_trade_no", outTradeNo);
902-
packageParams.put("nonce_str", nonce_str);
903-
904-
String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
905-
String xml = "<xml>" +
906-
"<appid>" + wxMpConfigStorage.getAppId() + "</appid>" +
907-
"<mch_id>" + wxMpConfigStorage.getPartnerId() + "</mch_id>" +
908-
"<transaction_id>" + transactionId + "</transaction_id>" +
909-
"<out_trade_no>" + outTradeNo + "</out_trade_no>" +
910-
"<nonce_str>" + nonce_str + "</nonce_str>" +
911-
"<sign>" + sign + "</sign>" +
912-
"</xml>";
913-
914-
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery");
915-
if (httpProxy != null) {
916-
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
917-
httpPost.setConfig(config);
918-
}
904+
else
905+
throw new IllegalArgumentException("Either 'transactionId' or 'outTradeNo' must be given.");
906+
packageParams.put("nonce_str", nonce_str);
907+
packageParams.put("sign", WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()));
919908

920-
StringEntity entity = new StringEntity(xml, Consts.UTF_8);
921-
httpPost.setEntity(entity);
922-
try {
923-
CloseableHttpResponse response = httpClient.execute(httpPost);
924-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
925-
XStream xstream = XStreamInitializer.getInstance();
926-
xstream.alias("xml", WxMpPayResult.class);
927-
WxMpPayResult wxMpPayResult = (WxMpPayResult) xstream.fromXML(responseContent);
928-
return wxMpPayResult;
929-
} catch (IOException e) {
930-
e.printStackTrace();
931-
}
932-
return new WxMpPayResult();
909+
StringBuilder request = new StringBuilder("<xml>");
910+
for (Entry<String, String> para : packageParams.entrySet()) {
911+
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
912+
}
913+
request.append("</xml>");
914+
915+
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery");
916+
if (httpProxy != null) {
917+
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
918+
httpPost.setConfig(config);
919+
}
920+
921+
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
922+
httpPost.setEntity(entity);
923+
try {
924+
CloseableHttpResponse response = httpClient.execute(httpPost);
925+
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
926+
XStream xstream = XStreamInitializer.getInstance();
927+
xstream.alias("xml", WxMpPayResult.class);
928+
WxMpPayResult wxMpPayResult = (WxMpPayResult) xstream.fromXML(responseContent);
929+
return wxMpPayResult;
930+
} catch (IOException e) {
931+
throw new RuntimeException("Failed to query order due to IO exception.", e);
932+
}
933933
}
934934

935935
@Override

0 commit comments

Comments
 (0)