package com.kmall.manager.manager.wechat;

import com.kmall.manager.manager.pay.wxpay.CommonWxPayPropertiesBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;

import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;

@SuppressWarnings("deprecation")
public class WechatConfig {

    private static SSLConnectionSocketFactory sslcsf;

    public static SSLConnectionSocketFactory getSslcsf() {
        if (null == sslcsf) {
            setSsslcsf();
        }
        return sslcsf;
    }

    private static void setSsslcsf() {
        try {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            Thread.currentThread().getContextClassLoader();
            InputStream instream = new FileInputStream(CommonWxPayPropertiesBuilder.instance().getCertName());
//            InputStream instream = new FileInputStream(CommonWxGlobalPayPropertiesBuilder.instance().getCertName());
            try {
                keyStore.load(instream, CommonWxPayPropertiesBuilder.instance().getMchId().toCharArray());
//                keyStore.load(instream, CommonWxGlobalPayPropertiesBuilder.instance().getMchId().toCharArray());
            } finally {
                instream.close();
            }
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, CommonWxPayPropertiesBuilder.instance().getMchId().toCharArray()).build();
//            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, CommonWxGlobalPayPropertiesBuilder.instance().getMchId().toCharArray()).build();
            sslcsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
