/**
* 微信退款
*
* @param orderNo
* @param amount
*/
public static Ret wechatRefund(String orderNo, BigDecimal amount) {
Prop prop = PropKit.use("wechat.txt");
// 封裝請求參數(shù)
Map<String, String> data = new HashMap<>();
data.put("appid", prop.get("appid"));
data.put("mch_id", prop.get("mchid"));
data.put("nonce_str", StrKit.getRandomUUID());
data.put("out_trade_no", orderNo);
data.put("out_refund_no", orderNo);
String totalFee = String.valueOf(amount.multiply(new BigDecimal("100")).intValue());
data.put("total_fee", totalFee);
data.put("refund_fee", totalFee);
data.put("sign", StringKit.genMd5Sign(data, prop.get("key")));
// 發(fā)送退款請求
String result = null;
try {
// 實例化密碼庫并設置證書格式
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// 將證書文件轉為文件輸入流
FileInputStream inputStream = new FileInputStream(new File(prop.get("cert_path")));
// 加載證書文件流和密碼(默認為商戶id)到密鑰庫
keyStore.load(inputStream, prop.get("mchid").toCharArray());
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, prop.get("mchid").toCharArray())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
// 構建ssl套接字的證書內容和密碼
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
// 創(chuàng)建post請求
HttpPost httpPost = new HttpPost(prop.get("refund_url"));
// 填充數(shù)據(jù)實體
httpPost.setEntity(new StringEntity(StringKit.mapToXml(data), Const.CHARSET));
// 發(fā)送退款請求
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, Const.CHARSET);
} catch (Exception e) {
e.printStackTrace();
return Ret.fail("msg", ErrorMsg.REQUEST_FAIL);
}
if (StrKit.isBlank(result))
return Ret.fail("msg", ErrorMsg.REQUEST_FAIL);
Map<String, String> response = StringKit.xmlToMap(result);
// 請求成功需返回狀態(tài)和返回結果均為成功
if (response == null || !response.get("return_code").equals(UserPaymentRecord.WechatResult.CODE)
|| !response.get("result_code").equals(UserPaymentRecord.WechatResult.CODE)) {
LogKit.error("請求微信退款失斣矸贰:" + response.get("return_msg"));
return Ret.fail("msg", ErrorMsg.REQUEST_FAIL);
}
return Ret.ok();
}
/**
* 支付寶退款
*
* @param orderNo
* @param amount
*/
public static Ret alipayRefund(String orderNo, BigDecimal amount) {
Prop prop = PropKit.use("alipay.txt");
// 獲取請求參數(shù)
String serverUrl = prop.get("open_api_domain");
String appid = prop.get("appid");
String privateKey = prop.get("private_key");
String format = "JSON";
String charset = "UTF-8";
String alipayPublicKey = prop.get("alipay_public_key");
String signType = prop.get("sign_type");
// 實例化支付寶客戶端
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appid, privateKey, format, charset,
alipayPublicKey, signType);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
// 填充業(yè)務內容
Map<String, String> bizContent = new HashMap<>();
bizContent.put("out_trade_no", orderNo);
bizContent.put("refund_amount", amount.toString());
request.setBizContent(JsonKit.toJson(bizContent));
try {
// 發(fā)送退款請求
if (alipayClient.execute(request).isSuccess())
return Ret.ok();
} catch (AlipayApiException e) {
e.printStackTrace();
return Ret.fail("msg", ErrorMsg.REQUEST_FAIL);
}
return Ret.fail("msg", ErrorMsg.OPERATION_FAIL);
}
注意:微信退款需要登錄微信后臺下載雙向證書