Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #242 from videome/pay-result-bug
Browse files Browse the repository at this point in the history
Fix the bug that fails to query order due to sign error.
  • Loading branch information
chanjarster committed Dec 25, 2015
2 parents 1e67c3f + 5594c88 commit 097fc09
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -916,44 +916,44 @@ public Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) {

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

SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", wxMpConfigStorage.getAppId());
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", wxMpConfigStorage.getAppId());
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
if (transactionId != null && !"".equals(transactionId.trim()))
packageParams.put("transaction_id", transactionId);
else if (outTradeNo != null && !"".equals(outTradeNo.trim()))
packageParams.put("out_trade_no", outTradeNo);
packageParams.put("nonce_str", nonce_str);

String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
String xml = "<xml>" +
"<appid>" + wxMpConfigStorage.getAppId() + "</appid>" +
"<mch_id>" + wxMpConfigStorage.getPartnerId() + "</mch_id>" +
"<transaction_id>" + transactionId + "</transaction_id>" +
"<out_trade_no>" + outTradeNo + "</out_trade_no>" +
"<nonce_str>" + nonce_str + "</nonce_str>" +
"<sign>" + sign + "</sign>" +
"</xml>";

HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery");
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
else
throw new IllegalArgumentException("Either 'transactionId' or 'outTradeNo' must be given.");
packageParams.put("nonce_str", nonce_str);
packageParams.put("sign", WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()));

StringEntity entity = new StringEntity(xml, Consts.UTF_8);
httpPost.setEntity(entity);
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", WxMpPayResult.class);
WxMpPayResult wxMpPayResult = (WxMpPayResult) xstream.fromXML(responseContent);
return wxMpPayResult;
} catch (IOException e) {
e.printStackTrace();
}
return new WxMpPayResult();
StringBuilder request = new StringBuilder("<xml>");
for (Entry<String, String> para : packageParams.entrySet()) {
request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
}
request.append("</xml>");

HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery");
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}

StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
httpPost.setEntity(entity);
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", WxMpPayResult.class);
WxMpPayResult wxMpPayResult = (WxMpPayResult) xstream.fromXML(responseContent);
return wxMpPayResult;
} catch (IOException e) {
throw new RuntimeException("Failed to query order due to IO exception.", e);
}
}

@Override
Expand Down
Loading

0 comments on commit 097fc09

Please sign in to comment.