代码如下

    1. @PostConstruct
    2. public void init() {
    3. log.info("微信支付初始化 appId={}", appId);
    4. WxPayService wxPayService = new WxPayServiceImpl();
    5. WxPayConfig payConfig = new WxPayConfig();
    6. payConfig.setAppId(appId);
    7. payConfig.setMchId(mchId);
    8. payConfig.setMchKey(mchKey);
    9. // 证书配置
    10. payConfig.setKeyPath("classpath:cert/apiclient_cert.p12");
    11. wxPayService.setConfig(payConfig);
    12. payService = wxPayService;
    13. log.info("微信支付初始化完毕 ");
    14. }

    退款代码如下

    1. WxPayRefundRequest request = new WxPayRefundRequest();
    2. Integer totalFee = price.multiply(new BigDecimal("100")).intValue();
    3. Integer refundFee = totalFee;
    4. // 订单金额
    5. request.setTotalFee(totalFee);
    6. // 退款金额
    7. request.setRefundFee(refundFee);
    8. // 商户订单号
    9. request.setOutTradeNo(orderNo);
    10. // 商户退款单号
    11. request.setOutRefundNo(refundNo);
    12. // 回调地址
    13. request.setNotifyUrl(WxConstants.notifyUrl);
    14. WxPayService wxPayService = WxPayAutoConfiguration.getWxPayService();
    15. try {
    16. return wxPayService.refund(request);
    17. } catch (WxPayException e) {
    18. throw new BusinessException(e.getMessage());
    19. }