package com.ruoyi.common.auth.ali;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.ruoyi.common.exception.ServiceException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author wjh
 * 2024/11/25
 */
@Component
@Data
public class AliConfig {

    // 应用ID
    private String appId;

    // 应用私钥
    private String privateKey;

    private String aesPrivateKey;

    private String notifyUrl;

    private String transferNotifyUrl;

    private String appCertPath;

    private String alipayCertPath;

    private String alipayRootCertPath;

    // 证书模式
    @Bean
    public AlipayClient alipayClient() {
        try {
            AlipayConfig alipayConfig = new AlipayConfig();
            //设置网关地址
            alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
            //设置应用APPID
            alipayConfig.setAppId(this.getAppId());
            //设置应用私钥
            alipayConfig.setPrivateKey(this.getPrivateKey());
            //设置应用公钥证书路径
            alipayConfig.setAppCertPath(this.getAppCertPath());
            //设置支付宝公钥证书路径
            alipayConfig.setAlipayPublicCertPath(this.getAlipayCertPath());
            //设置支付宝根证书路径
            alipayConfig.setRootCertPath(this.alipayRootCertPath);
            //设置请求格式，固定值json
            alipayConfig.setFormat("json");
            //设置字符集
            alipayConfig.setCharset("UTF-8");
            //设置签名类型
            alipayConfig.setSignType("RSA2");

            return new DefaultAlipayClient(alipayConfig);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
