package com.hzs.third.pay.jdpay.config;

import com.hzs.third.pay.config.JdPayBankProperties;
import com.hzs.third.pay.config.JdPayWechatAlipayProperties;
import com.hzs.third.pay.jdpay.sdk.JdPay;
import com.hzs.third.pay.jdpay.sdk.JdPayNewConfig;
import com.hzs.third.pay.jdpay.sdk.JdPayDefaultNewConfig;
import com.hzs.third.pay.jdpay.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*************************************************
 *
 * 京东支付sdk初始化
 *
 *************************************************/
@Configuration
public class JdPayAutoConfiguration {
    @Autowired
    private JdPayBankProperties jdPayBankProperties;
    @Autowired
    private JdPayWechatAlipayProperties jdPayWechatAlipayProperties;



    @Bean(name = "jdPayBank")
    public JdPay initJdBankPay() {
        // 加载商户私钥证书
//        byte[] privateCert = FileUtil.readFile(priCert);
        byte[] privateCert = FileUtil.readFile(jdPayBankProperties.getPriCert());
        // 加载商户公钥证书
//        byte[] publicCert = FileUtil.readFile(pubCert);
        byte[] publicCert = FileUtil.readFile(jdPayBankProperties.getPubCert());
        // 检查商户证书
        checkCert(privateCert, publicCert);
        // 初始化京东支付配置对象
//        JdPayNewConfig myConfig = new JdPayDefaultNewConfig(merchantNo, signKey, privateCert, priCertPwd, publicCert, apiDomain);
        JdPayNewConfig myConfig = new JdPayDefaultNewConfig(jdPayBankProperties.getMerchantNo()
                , jdPayBankProperties.getSignKey()
                , privateCert
                , jdPayBankProperties.getPriCertPwd()
                , publicCert
                , jdPayBankProperties.getApiDomain());
        return new JdPay(myConfig);
    }@Bean(name = "jdPayWechatAli")
    public JdPay initJdWechatAliPay() {
        // 加载商户私钥证书
//        byte[] privateCert = FileUtil.readFile(priCert);
        byte[] privateCert = FileUtil.readFile(jdPayWechatAlipayProperties.getPriCert());
        // 加载商户公钥证书
//        byte[] publicCert = FileUtil.readFile(pubCert);
        byte[] publicCert = FileUtil.readFile(jdPayWechatAlipayProperties.getPubCert());
        // 检查商户证书
        checkCert(privateCert, publicCert);
        // 初始化京东支付配置对象
//        JdPayNewConfig myConfig = new JdPayDefaultNewConfig(merchantNo, signKey, privateCert, priCertPwd, publicCert, apiDomain);
        JdPayNewConfig myConfig = new JdPayDefaultNewConfig(jdPayWechatAlipayProperties.getMerchantNo()
                , jdPayWechatAlipayProperties.getSignKey()
                , privateCert
                , jdPayWechatAlipayProperties.getPriCertPwd()
                , publicCert
                , jdPayWechatAlipayProperties.getApiDomain());
        return new JdPay(myConfig);
    }

    private void checkCert(byte[] privateCert, byte[] publicCert) {
        if (privateCert == null) {
            throw new RuntimeException("读取京东支付商户私钥证书为空");
        }
        if (publicCert == null) {
            throw new RuntimeException("读取京东支付商户公钥证书为空");
        }
    }

}

