package com.tzy.framework.pay.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * 宝付支付配置
 */
@Data
@Component
@ConfigurationProperties(prefix = "pay.bf.app")
public class BfPayConfig {

    private String name = "bf";

    private String format;
    //#商户在慧收钱的商户号，由慧收钱生成并下发
    private String appid;

    private String signType;

    private String version;
    //#门店编号
    private String subMerchantNo;
    //后端通知地址-支付成功
    private String payNotifyUrl;
    //后端通知地址-退款
    private String refundNotifyUrl;
    //前台通知地址
    private String pageUrl;
    //
    private String privateKeyPath;
    //私钥密码
    private String privatePwd;

    private String publicKeyPath;

    private String key;

    public String getPrivateKey(){
        String privateKey = "";
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(privateKeyPath);
        if(null == inputStream ) {
            try {
                inputStream = new FileInputStream(new File(privateKeyPath));
                StringBuilder sb = new StringBuilder();
                String line;

                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                privateKey = sb.toString();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return privateKey;
    }

    public String getPublicKey(){
        String publicKey = "";
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(publicKeyPath);
        if(null == inputStream ) {
            try {
                inputStream = new FileInputStream(new File(publicKeyPath));
                StringBuilder sb = new StringBuilder();
                String line;

                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                publicKey = sb.toString();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return publicKey;
    }

}
