Java实现微信支付功能

第二步创建配置类

@Configuration
public class WxConfig {
    @Autowired
    private WxPayConfigs properties;
    @Bean
    public WxMaConfig wxMaConfig() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppId());
        config.setSecret(properties.getAppSecret());
        return config;
    }


    @Bean
    public WxMaService wxMaService(WxMaConfig maConfig) {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(maConfig);
        return service;
    }


    @Bean
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
        wxMaConfig.setAppid(this.properties.getAppId());
        wxMaConfig.setSecret(this.properties.getAppSecret());
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaConfig);
        return wxMaService;
    }


    @Bean
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppId());
        payConfig.setMchId(properties.getMchId());
        payConfig.setMchKey(properties.getMchKey());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setKeyPath(properties.getKeyPath());
        payConfig.setSignType("MD5");
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}

第三步,创建实体类

@Data
@Configuration
@ConfigurationProperties("wx")
public class WxPayConfigs {


    private String appId;


    private String appSecret;


    private String mchId;


    private String mchKey;


    private String notifyUrl;


    private String keyPath;

    private String refundUrl;


}

分两步,支付接口和支付成功和失败的回调接口

支付接口是提供给前端调用的,回调接口是我们自己回调的

支付接口实现层

只要代码块在try catch,其余根据自己的业务需求修改

ps:金额要把元转化成分

回调接口

同样的,后半部分根据自己的业务需求修改

第五步申请退款

有错误,希望大佬们指出,不要喷我,谢谢!

经验分享 程序员 微信小程序 职场和发展