上篇文章我们已经结束了微信支付准备工作以及JSAPI调起支付

文章地址:https://blog.csdn.net/ssdadasd15623/article/details/134684556

接下来实现,付款后的查单操作

查询订单分为微信订单号查询以及商户订单号查询,这里使用商户订单号,也就是自己的系统的订单号

查看微信支付文档-商户订单号查询订单

https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/query-by-out-trade-no.html

在请求接口时,注意⚠️:请求参数内的Authorization参数需要提前生成
https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-generation.html
在这里微信支付给出了明确的文档教我们如何构造请求签名

构造请求签名串

  1. 第一步是请求方式,这里查询订单是“GET”
  2. 第二步是请求的绝对URL,这里请求地址是“
    https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}”,微信给出示例代码如何获取绝对URL,例如订单号为202401010001,商户号为12345678
HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/202401010001" />); //使用okhttp3 SDK
  1. 第三步是当前时间戳,这里使用currentTimeMillis()方法生成时间戳
long timestamp = System.currentTimeMillis() / 1000; //生成时间戳
  1. 第四步是请求随机字符串,这里微信支付的工具类给出了生成随机字符串的方法
String nonceStr = WXPayUtil.generateNonceStr(); //生成随机字符串
  1. 最后是请求报文主体,这里是GET方法,没有请求体,所以就是一个空的字符串
  2. 最终组合在一起的字符串为
GET\n//请求方式/v3/pay/transactions/out-trade-no/202401010001?mchid=12345678\n //请求地址绝对路径1554208460\n //时间戳593BEC0C930BF1AFEB40B4A08C8FB242\n //随机字符串\n

⚠️每行内容后需添加换行符‘\n‘

完整代码:

Controller.java文件

......@Autowiredprivate WxPayConfig wxPayConfig; //存放微信基本配置数据@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 "; //注意这个,有一个空格HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + order_no + "?mchid=" + wxPayConfig.getMchId()); //构造参与签名的URLHttpGet 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请求时body为空httpGet.setHeader("Accept", "application/json;charset=utf-8");CloseableHttpClient httpClient = HttpClientBuilder.create().build();CloseableHttpResponse response = httpClient.execute(httpGet);// 获取响应状态码int statusCode = response.getStatusLine().getStatusCode();// 获取响应内容String responseBody = EntityUtils.toString(response.getEntity());// 关闭响应对象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是微信支付自带的sdklong timestamp = System.currentTimeMillis() / 1000; //生成时间戳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是微信支付中申请的证书序列号+ "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测试结果: