Skip to content

Commit 097fc09

Browse files
committed
Merge pull request #242 from videome/pay-result-bug
Fix the bug that fails to query order due to sign error.
2 parents 1e67c3f + 5594c88 commit 097fc09

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
@@ -916,44 +916,44 @@ public Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) {
916916

917917
@Override
918918
public WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) {
919-
String nonce_str = System.currentTimeMillis() + "";
919+
String nonce_str = System.currentTimeMillis() + "";
920920

921-
SortedMap<String, String> packageParams = new TreeMap<String, String>();
922-
packageParams.put("appid", wxMpConfigStorage.getAppId());
923-
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
921+
SortedMap<String, String> packageParams = new TreeMap<String, String>();
922+
packageParams.put("appid", wxMpConfigStorage.getAppId());
923+
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
924+
if (transactionId != null && !"".equals(transactionId.trim()))
924925
packageParams.put("transaction_id", transactionId);
926+
else if (outTradeNo != null && !"".equals(outTradeNo.trim()))
925927
packageParams.put("out_trade_no", outTradeNo);
926-
packageParams.put("nonce_str", nonce_str);
927-
928-
String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
929-
String xml = "<xml>" +
930-
"<appid>" + wxMpConfigStorage.getAppId() + "</appid>" +
931-
"<mch_id>" + wxMpConfigStorage.getPartnerId() + "</mch_id>" +
932-
"<transaction_id>" + transactionId + "</transaction_id>" +
933-
"<out_trade_no>" + outTradeNo + "</out_trade_no>" +
934-
"<nonce_str>" + nonce_str + "</nonce_str>" +
935-
"<sign>" + sign + "</sign>" +
936-
"</xml>";
937-
938-
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery");
939-
if (httpProxy != null) {
940-
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
941-
httpPost.setConfig(config);
942-
}
928+
else
929+
throw new IllegalArgumentException("Either 'transactionId' or 'outTradeNo' must be given.");
930+
packageParams.put("nonce_str", nonce_str);
931+
packageParams.put("sign", WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()));
943932

944-
StringEntity entity = new StringEntity(xml, Consts.UTF_8);
945-
httpPost.setEntity(entity);
946-
try {
947-
CloseableHttpResponse response = httpClient.execute(httpPost);
948-
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
949-
XStream xstream = XStreamInitializer.getInstance();
950-
xstream.alias("xml", WxMpPayResult.class);
951-
WxMpPayResult wxMpPayResult = (WxMpPayResult) xstream.fromXML(responseContent);
952-
return wxMpPayResult;
953-
} catch (IOException e) {
954-
e.printStackTrace();
955-
}
956-
return new WxMpPayResult();
933+
StringBuilder request = new StringBuilder("<xml>");
934+
for (Entry<String, String> para : packageParams.entrySet()) {
935+
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
936+
}
937+
request.append("</xml>");
938+
939+
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery");
940+
if (httpProxy != null) {
941+
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
942+
httpPost.setConfig(config);
943+
}
944+
945+
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
946+
httpPost.setEntity(entity);
947+
try {
948+
CloseableHttpResponse response = httpClient.execute(httpPost);
949+
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
950+
XStream xstream = XStreamInitializer.getInstance();
951+
xstream.alias("xml", WxMpPayResult.class);
952+
WxMpPayResult wxMpPayResult = (WxMpPayResult) xstream.fromXML(responseContent);
953+
return wxMpPayResult;
954+
} catch (IOException e) {
955+
throw new RuntimeException("Failed to query order due to IO exception.", e);
956+
}
957957
}
958958

959959
@Override

0 commit comments

Comments
 (0)