package com.izouma.weixin.wxpay;


import com.izouma.awesomeadmin.util.PropertiesFileLoader;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class MyConfig extends WXPayConfig implements IWXPayDomain {

    private byte[] certData;

    public MyConfig() throws Exception {
        //String certPath = "/path/to/apiclient_cert.p12";
        String certPath = PropertiesFileLoader.getProperties("weixin_cert_path");
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getAppID() {
        return PropertiesFileLoader.getProperties("weixinappid");
    }

    public String getMchID() {
        return PropertiesFileLoader.getProperties("weixin_mch_id");
    }

    public String getKey() {
        return PropertiesFileLoader.getProperties("weixin_mch_secret");
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        return this;
    }

    @Override
    public void report(String domain, long elapsedTimeMillis, Exception ex) {

    }

    @Override
    public DomainInfo getDomain(WXPayConfig config) {
        return new DomainInfo(WXPayConstants.DOMAIN_API, true);
    }
}

