package com.ichangzuo.config;

import com.ichangzuo.config.property.AliPayProperties;
import com.ichangzuo.config.property.WxPayProperties;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(WxPayProperties.class)
public class WxpayConfig {
    private static WxPayProperties wxPayProperties;

    public WxpayConfig(WxPayProperties properties) {
        wxPayProperties = properties;
    }

    public static final int WX_APP = 0;
    public static final int WX_WEB = 1;
    /** App号 */
    public static String appAppId = "wx8f83a7f750d63431";
    /** 商户号 */
    public static String appMerchantId = "1266239101";
    /** 商户API私钥路径 */
    /** 商户证书序列号 */
    public static String appMerchantSerialNumber = "18650196CADB6AB05753AFF90B5C60EBFDDB9858";

    /** App号 */
    public static String webAppId = "wx0243718acff3873f";//
    /** 商户号 */
    public static String webMerchantId = "1250765101";//
    /** 商户API私钥路径 */
    /** 商户证书序列号 */
    public static String webMerchantSerialNumber = "6D24B656FFCD5DFADAC7CDFFDFEC60FDA07B8654";//

    /** 商户APIV3密钥 */
    public static String apiV3Key = "aS1yC4sS3xM8hI3oF1qD2uZ7kH8mO3uB";//
    //回调地址
    //Wxpayconfig
    private static Config wxpayAppConfig = null;
    private static Config wxpayWebConfig = null;

    public static Config getWxpayConfig(int type) {
        Config config = type == WX_APP ? wxpayAppConfig : wxpayWebConfig;
        synchronized (WxpayConfig.class) {

            if (null == config) {
                if(type==WX_APP) {
                    config = new RSAAutoCertificateConfig.Builder()
                            .merchantId(appMerchantId)
                            .privateKeyFromPath(wxPayProperties.getAppPrivateKeyPath() + "apiclient_key.pem")
                            .merchantSerialNumber(appMerchantSerialNumber)
                            .apiV3Key(apiV3Key)
                            .build();
                }
                else if(type==WX_WEB){
                    config = new RSAAutoCertificateConfig.Builder()
                            .merchantId(webMerchantId)
                            .privateKeyFromPath(wxPayProperties.getWebPrivateKeyPath() + "apiclient_key.pem")
                            .merchantSerialNumber(webMerchantSerialNumber)
                            .apiV3Key(apiV3Key)
                            .build();
                }
            }
        }
        return config;
    }

    public static String notifyUrl(int type){
        if( type==WX_APP )
            return wxPayProperties.getAppNotifyUrl();
        else if( type==WX_WEB )
            return wxPayProperties.getWebNotifyUrl();

        return null;
    }
}
