package com.ghy.common.adapay;

import com.huifu.adapay.Adapay;
import com.huifu.adapay.model.MerConfig;
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 org.springframework.util.Assert;

/**
 * Adapay 自动装载
 *
 * @author HH 2022/3/25
 */
@Configuration
@EnableConfigurationProperties(AdapayProperties.class)
public class AdapayAutoConfiguration {

    @Bean
    public AdapayService adapayService(AdapayProperties adapayProperties) throws Exception {

        Assert.hasText(adapayProperties.getAppId(), "Adapay.appId is blank!");
        Assert.hasText(adapayProperties.getApiKey(), "Adapay.apiKey is blank!");
        Assert.hasText(adapayProperties.getMockApiKey(), "Adapay.mockApiKey is blank!");
        Assert.hasText(adapayProperties.getRsaPrivateKey(), "Adapay.rsaPrivateKey is blank!");

        MerConfig merConfig = new MerConfig();
        merConfig.setApiKey(adapayProperties.getApiKey());
        merConfig.setApiMockKey(adapayProperties.getMockApiKey());
        merConfig.setRSAPrivateKey(adapayProperties.getRsaPrivateKey());
        Adapay.initWithMerConfig(merConfig);

        AdapayService adapayService = new AdapayService();
        adapayService.setAdapayProperties(adapayProperties);
        return adapayService;
    }
}