package com.izouma.nineth.config;

import com.huifu.adapay.Adapay;
import com.huifu.adapay.model.MerConfig;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
@AllArgsConstructor
@EnableConfigurationProperties({AdapayProperties.class})
public class AdapayConfig {
    private final AdapayProperties adapayProperties;

    @PostConstruct
    public void init() {
        Adapay.debug = adapayProperties.isDebug();
        Adapay.prodMode = adapayProperties.isProd();

        MerConfig merConfig = new MerConfig();
        merConfig.setApiKey(adapayProperties.getApiKey());
        merConfig.setApiMockKey(adapayProperties.getMockKey());
        merConfig.setRSAPrivateKey(adapayProperties.getPrivKey());
        merConfig.setRSAPublicKey(adapayProperties.getPublicKey());
        Adapay.publicKey = adapayProperties.getAppPublicKey();
        try {
            Adapay.initWithMerConfig(merConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
