微信支付开发文档:https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/create.html
退款与查单的请求头类似,但是查单是GET请求,所以在构造签名的时候相对简单些,但是退款请求中有请求参数,在构造签名时,需要将请求体添加到请求头参数中。
查看微信支付开发文档,请求参数中refund_id/out_refund_no,transaction_id/out_trade_no这两个参数,一个是微信支付系统中的退款号以及订单号,一个是自己的系统中的退款号以及订单号,这里我们使用后者;其次必填的参数还有refund、total、currency、amount
//构造请求参数 Map data = new HashMap(); data.put("out_trade_no", orderDao.getOrder_no()); data.put("out_refund_no", orderDao.getRefund_order_no()); Map amount = new HashMap(); amount.put("refund", (int) (Double.parseDouble(orderDao.getPrice()) * 100)); amount.put("total", (int) (Double.parseDouble(orderDao.getPrice()) * 100)); amount.put("currency", "CNY"); data.put("amount", amount);
⚠️:与查单不同的是,退款借口是post请求并且携带参数,在构建请求头签名时,getToken()中的第三个参数是请求体的json类型
String schema = "WECHATPAY2-SHA256-RSA2048 "; HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds"); // 设置请求链接 HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds"); //设置请求头信息 httpPost.setHeader("Authorization", schema + getToken("POST", httpurl, JSONObject.toJSONString(data))); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(JSONObject.toJSONString(data))); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpResponse response = httpClient.execute(httpPost); // 获取响应状态码 int statusCode = response.getStatusLine().getStatusCode(); // 获取响应内容 String responseBody = EntityUtils.toString(response.getEntity()); // 关闭响应对象 response.close(); map.put("code", statusCode); map.put("data", responseBody);
@PostMapping("/refund_order") public Maprefund_order(@RequestBody OrderDao orderDao) throws IOException, SignatureException, NoSuchAlgorithmException, InvalidKeyException { Map map = new HashMap<>(); //构造请求参数 Map data = new HashMap(); data.put("out_trade_no", orderDao.getOrder_no()); data.put("out_refund_no", orderDao.getRefund_order_no()); Map amount = new HashMap(); amount.put("refund", (int) (Double.parseDouble(orderDao.getPrice()) * 100)); // 我存的是string类型的单位为元的价格,所以需要转换成整形单位为分 amount.put("total", (int) (Double.parseDouble(orderDao.getPrice()) * 100)); amount.put("currency", "CNY"); data.put("amount", amount); String schema = "WECHATPAY2-SHA256-RSA2048 "; //注意有一个空格 HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds"); // 设置请求链接 HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds"); //设置请求头信息 httpPost.setHeader("Authorization", schema + getToken("POST", httpurl, JSONObject.toJSONString(data))); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(JSONObject.toJSONString(data))); //设置请求参数 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpResponse response = httpClient.execute(httpPost); // 获取响应状态码 int statusCode = response.getStatusLine().getStatusCode(); // 获取响应内容 String responseBody = EntityUtils.toString(response.getEntity()); // 关闭响应对象 response.close(); map.put("code", statusCode); map.put("data", responseBody); return map; }
上一篇:深入理解Spring IOC