公司用到支付宝及微信退款业务,无特殊要求,仅需完成订单的退款.
1.微信退款
private Boolean weixinRefund(String orderCode, BigDecimal actualAmount) {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
httpclient = initCert();
// 构造http请求
HttpPost httpPost = new HttpPost(cthirdPartyService.findValueByConfig("weixinRefundUrl"));
SortedMap<String, String> xmlList = refundArg(orderCode, actualAmount);
// 把参数组合成xml形式
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
xmlList.keySet().stream().forEach(entry -> {
if (StringUtils.isNotBlank(xmlList.get(entry))) {
sb.append("<" + entry + ">" + xmlList.get(entry) + "</" + entry + ">");
}
});
sb.append("</xml>");
StringEntity stringEntity = new StringEntity(sb.toString());
httpPost.setEntity(stringEntity);
// 发送请求
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (Objects.nonNull(entity)) {
BufferedReader reader1 = new BufferedReader(new InputStreamReader(entity.getContent()));
SAXBuilder jdomBuilder = new SAXBuilder();
Document document = jdomBuilder.build(reader1);
Element rootElement = document.getRootElement();
// 结果码
String returnCode = rootElement.getChild("return_code").getText();
if (Objects.equals(returnCode, "SUCCESS")) {
log.info("微信退款成功。");
return true;
} else {
log.error("微信退款失败!");
return false;
}
}
return false;
}
private SortedMap<String, String> refundArg(String orderCode, BigDecimal actualAmount) {
SortedMap<String, String> xmlList = Maps.newTreeMap();
try {
// 随机生成32位随机数
String code = GenerateCode.randomCode(32);
xmlList.put("nonce_str", code);
BigDecimal multiply = actualAmount.setScale(2, BigDecimal.ROUND_DOWN).multiply(BigDecimal.valueOf(100));
int amount = multiply.intValue();
xmlList.put("appid", cthirdPartyService.findValueByConfig("weixinAppId"));
xmlList.put("refund_fee_type", "CNY");
xmlList.put("out_refund_no", code);
xmlList.put("total_fee", String.valueOf(amount));
xmlList.put("refund_fee", String.valueOf(amount));
xmlList.put("op_user_id", cthirdPartyService.findValueByConfig("weixinMchId"));
xmlList.put("mch_id", cthirdPartyService.findValueByConfig("weixinMchId"));
xmlList.put("out_trade_no", orderCode);
String sign = PayCommonUtil.createSign(cthirdPartyService.findValueByConfig("weixinPublicKey"), xmlList);
xmlList.put("sign", sign);
return xmlList;
} catch (Exception e) {
log.error("e", "genProductArgs fail, 异常: " + e.getMessage());
return null;
}
}
具体参数意义参见微信官方.
2.支付宝退款
private Boolean aLiRefund(String orderCode, BigDecimal actualAmount) {
try {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
cthirdPartyService.findValueByConfig("alipayAppId"),
cthirdPartyService.findValueByConfig("alipayPrivateKey"), "json", "UTF-8",
cthirdPartyService.findValueByConfig("aliPublicKey"));
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
JSONObject jsonObject = new JSONObject();
jsonObject.put("out_trade_no", orderCode);
jsonObject.put("refund_amount", actualAmount);
request.setBizContent(jsonObject.toString());
log.debug("request参数==>" + jsonObject.toString());
AlipayTradeRefundResponse response = alipayClient.execute(request);
String refundStatu = response.getCode();
if (Objects.equals(refundStatu, "10000")) {
log.info("支付宝退款成功。");
return true;
} else {
log.error("支付宝退款失败!!");
return false;
}
} catch (AlipayApiException e) {
e.printStackTrace();
return false;
}
}
相较于微信,支付宝操作更为简便.具体参数意义,参见支付宝官方.
仅作为个人工作中的一点记录,无任何其他用途.