package com.miaxis.common.config;

import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.core.io.ClassPathResource;

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

public class WxPayConfigImpl implements WXPayConfig {
   // public static String url = "你设置的回调接口";
   // private static WxPayConfigImpl wxPayConfig;
    private byte[] certData = null;
    private static WxPayConfigImpl wxPayConfig;

    private static String v3url = "https://api.mch.weixin.qq.com/v3/pay/transactions/app";
    private static String v3urlRefund = "https://api.mch.weixin.qq.com/v3/refund/domestic/refunds"; //退款
    private static String merchantId = "1611324484"; //商户id
    private static String serialNumber = "53EBF541F383371B121D7B57B25305FC0BB04F42";
    private static String v3key = "7hM14893GvG3JK05575jk1l6P4tF042B";
    private static String notifyUrl = "http://218.85.55.253:8888/open-api/wx/notify/wxpay";
    private static String notifyUrlRefund = "http://218.85.55.253:8888/open-api/wx/notify/refund"; //退款回调
    private static String appId = "wx606fa102dbc4b8ab";



    public WxPayConfigImpl() {
        try {
            //这个证书的位置不是瞎鸡儿填的，你要在这个路径真的有一个证书
            // InputStream is = new FileInputStream("F:\\twzd\\twzd\\twzd-admin\\src\\main\\resources\\wechatpay\\apiclient_cert.p12");
            InputStream is = new FileInputStream(new ClassPathResource("wechatpay/apiclient_cert.p12").getFile());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] bs = new byte[1024];
            int cnt = -1;
            while ((cnt = is.read(bs)) != -1) {
                baos.write(bs, 0, cnt);
            }
            is.close();
            certData = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static WxPayConfigImpl getInstance() {
        if (wxPayConfig == null) {
            synchronized (WxPayConfigImpl.class) {
                wxPayConfig = new WxPayConfigImpl();
            }
        }
        return wxPayConfig;
    }

    @Override
    public String getAppID() {
        return appId;
    }

    @Override
    public String getMchID() {
        return merchantId;
    }

    @Override
    public String getKey() {
        return v3key;
    }




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

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }


    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getNotifyUrl(){
        return notifyUrl;
    }
}
