package com.ruoyi.newwxpay.config;

import com.wechat.pay.java.core.RSAPublicKeyConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class NewWxPayConfig {

    @Resource
    private NewWxPayProperties newWxPayProperties;

    @Bean(name = "rsaPublicKeyConfig")
    @ConditionalOnBean(NewWxPayProperties.class)
    public RSAPublicKeyConfig rsaPublicKeyConfig() {
        RSAPublicKeyConfig config = new RSAPublicKeyConfig.Builder()
            .merchantId(newWxPayProperties.getMchId())
            .privateKeyFromPath(newWxPayProperties.getPrivateKeyPath())
            .publicKeyFromPath(newWxPayProperties.getPublicKeyPath())
            .publicKeyId(newWxPayProperties.getPublicKeyId())
            .merchantSerialNumber(newWxPayProperties.getCertSerialNo())
            .apiV3Key(newWxPayProperties.getApiv3())
            .build();
        return config;
    }
}
