package com.yiqi.common.utils.pay;

import com.github.wxpay.sdk.WXPayConfig;
import com.yiqi.common.config.WxPayConfigs;

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

/**
 * @author xiaohei
 * @date 2024/1/28
 */
public class WxPayV2Config implements WXPayConfig {

    public static final String PAY_SUCCESS = "SUCCESS";
    public static final String PAY_USERPAYING = "USERPAYING";

    /** 加载证书  这里证书需要到微信商户平台进行下载*/
    private byte[] certData;

    public WxPayV2Config() throws Exception {
        //证书只是撤销订单时会使用，在这里的demo中没有用到
        String certPath = WxPayConfigs.getPrivateKeyPath();
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    /**
     * 设置自己的appId ,商户号,密钥
     * @return
     */
    @Override
    public String getAppID() {
        return "wxb74fad4c5a538738";
    }

    @Override
    public String getMchID() {
        return "1557749741";
    }

    @Override
    public String getKey() {
        return "c378426dba2f0fb797012f99119c57d6";
    }

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

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

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

    /**
     *这里的方法，实现必须如下
     */
    IWXPayDomain getWXPayDomain() {
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }
            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new IWXPayDomain.DomainInfo("api.mch.weixin.qq.com", true);
            }
        };
        return iwxPayDomain;
    }
}
