Java微信支付-申请退款API

接上一篇Java微信支付-统一下单API,本篇在上篇文章的基础上讲述微信支付-申请退款API
下文中所需配置、类都在以请查看以上链接内容。

话不多说,直接上码

/**
 * 微信支付接口
 * 
 * @create: 2019-10-10 15:40
 * @author: Sun
 */
public interface WxPayService {

    /**
     * 申请退款
     *
     * @param uid        用户id
     * @param orderNo    商户订单号
     * @param refundNo   商户退款单号
     * @param totalFee   订单金额
     * @param refundFee  退款金额
     * @param refundDesc 退款描述
     * @return return_code为SUCCESS的时候,将响应结果,存放到map中返回,否则throw WxPayException
     */
    Map<String, String> refund(String uid, String orderNo, String refundNo, String totalFee,
                               String refundFee, String refundDesc) throws WxPayException;
}

/**
 * @author: Sun
 * @create: 2019-10-10 17:54
 * @version: v1.0
 */
@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {

	@Autowired
    private MyWxPayConfig myWxPayConfig;

	/**
     * 退款 异步通知地址
     */
    @Value("${wxconfig.refund.notify.url}")
    private String refundAsyncNotifyUrl;

	
	@Override
    public Map<String, String> refund(String uid, String orderNo, String refundNo,
                                      String totalFee, String refundFee, String refundDesc) throws WxPayException {
        log.info("[refund] [入参] [uid:{}] [orderNo:{}] [refundNo:{}] [totalFee:{}] [refundFee:{}] [refundDesc:{}] ", uid, orderNo, refundNo, totalFee, refundFee, refundDesc);

        WXPay wxPay = new WXPay(myWxPayConfig);
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("out_trade_no", orderNo);
        paramMap.put("out_refund_no", refundNo);
        paramMap.put("total_fee", totalFee);
        paramMap.put("refund_fee", refundFee);
        paramMap.put("refund_desc", refundDesc);
        paramMap.put("notify_url", refundAsyncNotifyUrl);

        Map<String, String> resultMap = null;
        try {
            resultMap = wxPay.refund(paramMap);
            log.info("[refund] [退款API响应参数] [uid:{}] [out_trade_no:{}] [resultMap:{}]", uid, orderNo, resultMap);
        } catch (Exception e) {
            log.error("[refund] [uid:{}] [out_trade_no:{}]  [调用微信SDK过程中出现异常:{}]", uid, orderNo, ExceptionUtils.getStackTrace(e));
            throw new WxPayException(4506, "调用退款API异常,请重新尝试!");
        }

        return resultMap;
    }
}

你可能感兴趣的:(#,微信支付)