Java实现微信V3版本退款
之前的文章写过java实现V3版本的微信支付,现续上篇支付文章之后,使用相同的配置文件实现微信退款
核心步骤代码
public void mpOrderRefund(MpOrderDTO mpOrder) throws Exception {
String orderNo = mpOrder.getOrderNo();
BigDecimal totalPrice = mpOrder.getTotalPrice();
totalPrice = totalPrice.multiply(new BigDecimal(100));
String orderRefundNo = generateNonceStr(PayTypeConstant.MP_ORDER.getType());
Map<String, Object> data = new HashMap<String, Object>();
data.put("out_trade_no", orderNo);
data.put("out_refund_no", orderRefundNo);
data.put("notify_url", "这个是退款回调接收地址,自己定义填写");
Map<String, Object> amount = new HashMap<String, Object>();
amount.put("refund", totalPrice.intValue());
amount.put("total", totalPrice.intValue());
amount.put("currency", "CNY");
data.put("amount", amount);
String refundUrl = WxApiType.DOMESTIC_REFUNDS.toString();
String privateKeyPath = "apiclient_key.pem这个文件的位置";
IJPayHttpResponse response = WxPayApi.v3(
RequestMethod.POST,
WxDomain.CHINA.toString(),
refundUrl,
"1111111",
getSerialNumber(),
null,
privateKeyPath,
JSONUtil.toJsonStr(data)
);
log.info("统一退款响应 {}", response);
String body = response.getBody();
try {
if (StringUtil.isNotBlank(body) && body.contains("NOT_ENOUGH") && body.contains("基本账户余额不足")) {
mpOrder.setRefundStatus(6);
MpOrderMapper.updateById(mpOrder);
}
} catch (Exception e) {
log.info("退款调整失败 {}", e.getMessage());
}
}
private String getSerialNumber() throws IOException {
String certPath = "apiclient_cert.pem这个文件的位置";
log.info("path:{}", certPath);
X509Certificate certificate = PayKit.getCertificate(FileUtil.getInputStream(certPath));
String serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
log.info("获取证书序列号:{},", serialNo);
return serialNo;
}