使用微信支付接口退款
準(zhǔn)備工作
微信支付開(kāi)發(fā)文檔:https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/create.html退款與查單的請(qǐng)求頭類似,但是查單是GET請(qǐng)求,所以在構(gòu)造簽名的時(shí)候相對(duì)簡(jiǎn)單些,但是退款請(qǐng)求中有請(qǐng)求參數(shù),在構(gòu)造簽名時(shí),需要將請(qǐng)求體添加到請(qǐng)求頭參數(shù)中。
開(kāi)始開(kāi)發(fā)
1、構(gòu)造請(qǐng)求參數(shù)
查看微信支付開(kāi)發(fā)文檔,請(qǐng)求參數(shù)中refund_id/out_refund_no,transaction_id/out_trade_no這兩個(gè)參數(shù),一個(gè)是微信支付系統(tǒng)中的退款號(hào)以及訂單號(hào),一個(gè)是自己的系統(tǒng)中的退款號(hào)以及訂單號(hào),這里我們使用后者;其次必填的參數(shù)還有refund、total、currency、amount
//構(gòu)造請(qǐng)求參數(shù)
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.構(gòu)造請(qǐng)求頭簽名(具體方法類可以查看博主上篇文章)
??:與查單不同的是,退款借口是post請(qǐng)求并且攜帶參數(shù),在構(gòu)建請(qǐng)求頭簽名時(shí),getToken()中的第三個(gè)參數(shù)是請(qǐng)求體的json類型文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-803178.html
String schema = "WECHATPAY2-SHA256-RSA2048 ";
HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
// 設(shè)置請(qǐng)求鏈接
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
//設(shè)置請(qǐng)求頭信息
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);
// 獲取響應(yīng)狀態(tài)碼
int statusCode = response.getStatusLine().getStatusCode();
// 獲取響應(yīng)內(nèi)容
String responseBody = EntityUtils.toString(response.getEntity());
// 關(guān)閉響應(yīng)對(duì)象
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<>();
//構(gòu)造請(qǐng)求參數(shù)
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類型的單位為元的價(jià)格,所以需要轉(zhuǎn)換成整形單位為分
amount.put("total", (int) (Double.parseDouble(orderDao.getPrice()) * 100));
amount.put("currency", "CNY");
data.put("amount", amount);
String schema = "WECHATPAY2-SHA256-RSA2048 "; //注意有一個(gè)空格
HttpUrl httpurl = HttpUrl.parse("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
// 設(shè)置請(qǐng)求鏈接
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
//設(shè)置請(qǐng)求頭信息
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))); //設(shè)置請(qǐng)求參數(shù)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(httpPost);
// 獲取響應(yīng)狀態(tài)碼
int statusCode = response.getStatusLine().getStatusCode();
// 獲取響應(yīng)內(nèi)容
String responseBody = EntityUtils.toString(response.getEntity());
// 關(guān)閉響應(yīng)對(duì)象
response.close();
map.put("code", statusCode);
map.put("data", responseBody);
return map;
}
4.apifox測(cè)試結(jié)果
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-803178.html
到了這里,關(guān)于【微信支付】springboot-java接入微信支付-JSAPI支付/查單/退款/發(fā)送紅包(三)---退款的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!