使用微信支付接口退款
准备工作
微信支付开发文档: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 =newHashMap();
data.put("out_trade_no", orderDao.getOrder_no());
data.put("out_refund_no", orderDao.getRefund_order_no());Map amount =newHashMap();
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 =newHttpPost("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(newStringEntity(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")publicMap<String,Object>refund_order(@RequestBodyOrderDao orderDao)throwsIOException,SignatureException,NoSuchAlgorithmException,InvalidKeyException{Map<String,Object> map =newHashMap<>();//构造请求参数Map data =newHashMap();
data.put("out_trade_no", orderDao.getOrder_no());
data.put("out_refund_no", orderDao.getRefund_order_no());Map amount =newHashMap();
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 =newHttpPost("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(newStringEntity(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 所有, 如有侵权,请联系我们删除。