package com.fzm.mall.server.front.repo.wxpay;



import com.fzm.mall.server.front.repo.wxpay.wxpaysdk.IWXPayDomain;
import com.fzm.mall.server.front.repo.wxpay.wxpaysdk.WXPayConfig;
import com.fzm.mall.server.front.repo.wxpay.wxpaysdk.WXPayConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

@Configuration
public class MyWxpayConfig extends WXPayConfig {

    private byte[] certData;

    @Value("${wxpay.appid}")
    private String appId;
    @Value("${wxpay.mch-id}")
    private String mchId;
    @Value("${wxpay.mch-key}")
//    @Value("${wxpay.mch-sand-key}")
    private String mchKey;
    @Value("${wxpay.return-url}")
    private String returnUrl;
    @Value("${wxpay.pay-notify-url}")
    private String payNotifyUrl;
    @Value("${wxpay.refund-notify-url}")
    private String refundNotifyUrl;


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

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

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

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

    public String getReturnUrl(){
        return returnUrl;
    }

    public String getPayNotifyUrl(){
        return payNotifyUrl;
    }

    public String getRefundNotifyUrl(){
        return refundNotifyUrl;
    }

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

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

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

    @Override
    public IWXPayDomain getWXPayDomain() { // 这个方法需要这样实现, 否则无法正常初始化WXPay
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }

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

}
