All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.target123.develop.pay.PayAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.2.4
Show newest version
package com.gitee.target123.develop.pay;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.gitee.target123.develop.pay.wx.WxPay;
import com.gitee.target123.develop.pay.wx.WxPayMap;
import com.gitee.target123.develop.pay.ali.AliPay;
import com.gitee.target123.develop.pay.ali.AliPayMap;
import com.gitee.target123.develop.pay.ali.AliPayProperties;
import com.gitee.target123.develop.pay.wx.WxPayProperties;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableConfigurationProperties(PayProperties.class)
public class PayAutoConfiguration {

    @Bean(name = "wxPayMap")
    @ConditionalOnMissingBean(WxPayMap.class)
    @ConditionalOnProperty(name = "pay.map.wx-enabled",havingValue = "true")
    public WxPayMap wxPayMap(PayProperties payProperties) {
        WxPayMap wxPayMap = new WxPayMap();
        Map map = new HashMap<>();
        Map wx = payProperties.getWx();
        wx.forEach((s, properties) -> {
            WxPay wxPay = new WxPay();
            wxPay.setWxPayProperties(properties);

            WxPayConfig wxPayConfig = new WxPayConfig();
            wxPayConfig.setAppId(properties.getAppId());
            wxPayConfig.setMchId(properties.getMchId());
            wxPayConfig.setMchKey(properties.getMchKey());
            wxPayConfig.setKeyPath(properties.getKeyPath());
            WxPayService wxPayService = new WxPayServiceImpl();
            wxPayService.setConfig(wxPayConfig);

            wxPay.setWxPayService(wxPayService);
            map.put(s,wxPay);
        });
        wxPayMap.setMap(map);
        return wxPayMap;
    }

    @Bean(name = "aliPayMap")
    @ConditionalOnMissingBean(AliPayMap.class)
    @ConditionalOnProperty(name = "pay.map.ali-enabled",havingValue = "true")
    public AliPayMap aliPayMap(PayProperties payProperties){
        AliPayMap aliPayMap = new AliPayMap();
        Map map = new HashMap<>();
        Map ali = payProperties.getAli();
        ali.forEach((s, properties) -> {
            AliPay aliPay = new AliPay();
            aliPay.setAliPayProperties(properties);

            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", properties.getAppId(), properties.getPrivateKey(), "json", "utf-8", properties.getPublicKey(), "RSA2");
            aliPay.setAlipayClient(alipayClient);
            map.put(s,aliPay);
        });
        aliPayMap.setMap(map);
        return aliPayMap;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy