使用微信支付接口退款
准备工作
微信支付开发文档:https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/create.html
开始开发
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类型
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测试结果
版权声明:本文为博主作者:看海O.o原创文章,版权归属原作者,如果侵权,请联系我们删除!
原文链接:https://blog.csdn.net/ssdadasd15623/article/details/135381761