上篇文章我們已經(jīng)結(jié)束了微信支付準(zhǔn)備工作以及JSAPI調(diào)起支付
文章地址:https://blog.csdn.net/ssdadasd15623/article/details/134684556
接下來實(shí)現(xiàn),付款后的查單操作
查詢訂單分為微信訂單號(hào)查詢以及商戶訂單號(hào)查詢,這里使用商戶訂單號(hào),也就是自己的系統(tǒng)的訂單號(hào)
查看微信支付文檔-商戶訂單號(hào)查詢訂單
https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/query-by-out-trade-no.html
在請(qǐng)求接口時(shí),注意??:請(qǐng)求參數(shù)內(nèi)的Authorization參數(shù)需要提前生成
https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-generation.html
在這里微信支付給出了明確的文檔教我們?nèi)绾螛?gòu)造請(qǐng)求簽名
構(gòu)造請(qǐng)求簽名串
- 第一步是請(qǐng)求方式,這里查詢訂單是“GET”
- 第二步是請(qǐng)求的絕對(duì)URL,這里請(qǐng)求地址是“
https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}”,微信給出示例代碼如何獲取絕對(duì)URL,例如訂單號(hào)為202401010001,商戶號(hào)為12345678
HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/202401010001?mchid=12345678"); //使用okhttp3 SDK
- 第三步是當(dāng)前時(shí)間戳,這里使用currentTimeMillis()方法生成時(shí)間戳
long timestamp = System.currentTimeMillis() / 1000; //生成時(shí)間戳
- 第四步是請(qǐng)求隨機(jī)字符串,這里微信支付的工具類給出了生成隨機(jī)字符串的方法
String nonceStr = WXPayUtil.generateNonceStr(); //生成隨機(jī)字符串
- 最后是請(qǐng)求報(bào)文主體,這里是GET方法,沒有請(qǐng)求體,所以就是一個(gè)空的字符串
- 最終組合在一起的字符串為
GET\n //請(qǐng)求方式
/v3/pay/transactions/out-trade-no/202401010001?mchid=12345678\n //請(qǐng)求地址絕對(duì)路徑
1554208460\n //時(shí)間戳
593BEC0C930BF1AFEB40B4A08C8FB242\n //隨機(jī)字符串
\n
??每行內(nèi)容后需添加換行符‘\n‘
完整代碼:
Controller.java文件文章來源:http://www.zghlxwxcb.cn/news/detail-777969.html
......
@Autowired
private WxPayConfig wxPayConfig; //存放微信基本配置數(shù)據(jù)
@GetMapping("/get_order_status")
public Map<String, Object> get_authorization(@RequestParam String order_no) throws IOException, SignatureException, NoSuchAlgorithmException, InvalidKeyException {
Map<String, Object> map = new HashMap<>();
String schema = "WECHATPAY2-SHA256-RSA2048 "; //注意這個(gè),有一個(gè)空格
HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + order_no + "?mchid=" + wxPayConfig.getMchId()); //構(gòu)造參與簽名的URL
HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + order_no + "?mchid=" + wxPayConfig.getMchId());
httpGet.setHeader("Authorization", schema + getToken("GET", httpurl, "")); //微信支付文檔說明,GET請(qǐng)求時(shí)body為空
httpGet.setHeader("Accept", "application/json;charset=utf-8");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(httpGet);
// 獲取響應(yīng)狀態(tài)碼
int statusCode = response.getStatusLine().getStatusCode();
// 獲取響應(yīng)內(nèi)容
String responseBody = EntityUtils.toString(response.getEntity());
// 關(guān)閉響應(yīng)對(duì)象
response.close();
map.put("code", statusCode);
map.put("data", responseBody);
return map;
}
String getToken(String method, HttpUrl url, String body) throws UnsupportedEncodingException, SignatureException, NoSuchAlgorithmException, InvalidKeyException {
String nonceStr = WXPayUtil.generateNonceStr(); //WXPayUtil是微信支付自帶的sdk
long timestamp = System.currentTimeMillis() / 1000; //生成時(shí)間戳
String message = buildMessage(method, url, timestamp, nonceStr, body);
String signature = sign(message.getBytes("utf-8"));
return "mchid=\"" + wxPayConfig.getMchId() + "\","
+ "nonce_str=\"" + nonceStr + "\","
+ "timestamp=\"" + timestamp + "\","
+ "serial_no=\"" + wxPayConfig.getMchSerialNo() + "\"," //mchserialNo是微信支付中申請(qǐng)的證書序列號(hào)
+ "signature=\"" + signature + "\"";
}
String sign(byte[] message) throws InvalidKeyException, SignatureException, NoSuchAlgorithmException {
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(wxPayConfig.getPrivateKey());
sign.update(message);
return Base64.getEncoder().encodeToString(sign.sign());
}
String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) {
String canonicalUrl = url.encodedPath();
if (url.encodedQuery() != null) {
canonicalUrl += "?" + url.encodedQuery();
}
return method + "\n"
+ canonicalUrl + "\n"
+ timestamp + "\n"
+ nonceStr + "\n"
+ body + "\n";
}
......
使用apifox測試結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-777969.html
到了這里,關(guān)于【微信支付】springboot-java接入微信支付-JSAPI支付/查單/退款/發(fā)送紅包(二)---查單的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!