使用微信支付接口退款
准备工作
微信支付开发文档:https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/create.html
退款与查单的请求头类似,但是查单是GET请求,所以在构造签名的时候相对简单些,但是退款请求中有请求参数,在构造签名时,需要将请求体添加到请求头参数中。
开始开发
1、构造请求参数
查看微信支付开发文档,请求参数中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);
2.构造请求头签名(具体方法类可以查看博主上篇文章)
⚠️:与查单不同的是,退款借口是post请求并且携带参数,在构建请求头签名时,getToken()中的第三个参数是请求体的json类型文章来源:https://www.toymoban.com/news/detail-803178.html
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);
3.完整接口代码
@PostMapping("/refund_order")
public Map<String, Object> refund_order(@RequestBody OrderDao orderDao) throws IOException, SignatureException, NoSuchAlgorithmException, InvalidKeyException {
Map<String, Object> 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;
}
4.apifox测试结果
文章来源地址https://www.toymoban.com/news/detail-803178.html
到了这里,关于【微信支付】springboot-java接入微信支付-JSAPI支付/查单/退款/发送红包(三)---退款的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!