当前位置:   article > 正文

weixin-java-pay 微信退款

weixin-java-pay

使用第三方类库支持微信退款

jdk1.8
maven3.8.1
weixin-java-pay4.2.0
spring-cloud Hoxton.SR8
spring-cloud-alibaba 2.2.3.RELEASE


前言

本文仅做记录,遇到其他接口使用问题参考[weixin-java](https://gitee.com/binary/weixin-java-pay-demo)官方文档

第一次对接微信支付相关接口,遇到很多问题,记录一下


一、获取证书

从微信商户支付平台下载安全证书放在本地目录,不需要安装可以直接使用
支付证书,调用一些安全级别高的接口时需要验证

二、配置支付yml文件

微服务项目需要在配置中心里配置一份

application.yam中配置wx.pay

代码如下(示例):

wx:
  pay:
    mp-app-id: wx1111111111
    app-app-id: wx1111111111
    mch-id: 111111111
    mch-key: 234lkjlhkh34hjhjkl45hk45hk34k
    key-path: D:/apiclient_cert.p12 # 你自己的p12证书的位置,可以指定绝对路径,也可以指定类路径(以classpath:开头)
    notify-url: https://xx.com/etonkids.wonder.box.teacher.user/wonderBoxClassTeacher/refundCallback # 退款回调地址,必须是公网可访问的https请求地址,不能携带参数
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

三、编写配置类

@Data
@Component
@ConfigurationProperties(prefix = "wx.pay")
public class WechatPayConfig {

    private static Logger logger = LoggerFactory.getLogger(WechatPayConfig.class);

    /**
     * app appId
     */
    private String appAppId;

    /**
     * 公众号 appId
     */
    private String mpAppId;

    /**
     * 商户号
     */
    private String mchId;
    /**
     * 商户密钥
     */
    private String mchKey;
    /**
     * 证书
     */
    private String keyPath;

    /**
     * 支付成功的回调地址
     */
    private String notifyUrlRefund;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    @Bean
    @ConditionalOnMissingBean
    public WxPayConfig wxPayConfig() {
        logger.info("======================开始初始化微信支付参数======================");
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(maAppId);
        payConfig.setMchId(mchId);
        payConfig.setMchKey(mchKey);
        payConfig.setKeyPath(keyPath);
        payConfig.setNotifyUrl(notifyUrlRefund);
        logger.info(this.toString());
        logger.info("======================初始化微信支付参数完成======================");
        return payConfig;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig wxPayConfig) {
        logger.info("======================初始化微信支付接口服务开始======================");
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        logger.info("======================初始化微信支付接口服务完成======================");
        return wxPayService;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

四、Service层注入WxPayService调用退款

@Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized Result refund(RefundDto applicationRecord) throws Exception {
    	// 你的业务代码
    	// 调用申请退款接口
        WxPayConfig config = wxPayService.getConfig();
        WxPayRefundRequest wxPayRefundRequest = WxPayRefundRequest.newBuilder()
                .outRefundNo(outRefundNo)
                .outTradeNo(String.valueOf(id))
                .refundFee(payAmount)
                .totalFee(amount)
                .notifyUrl(config.getNotifyUrl()).build();

        log.info(">>>>>wxPayRefundRequest={}", wxPayRefundRequest.toString());
        wxPayService.refundV2(wxPayRefundRequest);

        return Result.OK();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized String refundCallback(String xmlData) throws Exception {
        // 回调函数中处理你的业务即可
        return WxPayNotifyResponse.success("SUCCESS");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

需要注意

1.微信支付分V2和V3版本,我用的是V2,不能使用JSONObject.toJSONString(config)打印日志,否则会走initApiV3HttpClient方法,提示缺少参数
2.发起退款传入的退款单号要不同,不能使用相同的退款单号
3.退款回调的地址必须是公网可访问的https请求

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/874173
推荐阅读
相关标签
  

闽ICP备14008679号