微信退款
开发需求遇到需要给用户退款,个人在开发中碰到很多小坑,个人记录以免再碰到
[微信退款文档](https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_4)
@Data
public class Refund{
String orderId; 微信支付商户返回id
String id; 自己后台生成的id
BigDecimal fee; 退款金额
}
public class RefundUtil{
@Value("${wechat.certificate.refund}")
private String WECHAT_REFUND_VERIFY_FILE_PATH;
@Value("${wechat.certificate.appid}")
private String APPID;
@Value("${wechat.certificate.mchid}")
private String MCHID;
@Value("${wechat.certificate.key}")
private String KEY;
private final static String WECHAT_REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
public ResponseEntity refundOperation(Refund refund) throws Exception {
Map<String, String> params= new HashMap<>();
//生成32位字符串
String nonce_str = codeUtil.UUID().toUpperCase();
params.put("appid", APPID);
params.put("mch_id", MCHID);
params.put("nonce_str", nonce_str);
params.put("out_trade_no", refund.getOrderId());// 微信返回的交易订单号
params.put("out_refund_no", refund.getId());
params.put("op_user_id", MCHID);
params.put("total_fee", fee); // 金额(分)
params.put("refund_fee", fee); // 金额(分)
String sign = WXPayUtil.generateSignature(params, KEY);
params.put("sign", sign);
String requestXML = WXPayUtil.mapToXml(params);
String xmlResult = getWeixinVerify(WECHAT_REFUND_URL, requestXML, WECHAT_REFUND_VERIFY_FILE_PATH);
Map<String, String> mapResult = WXPayUtil.xmlToMap(xmlResult);
}
}