package com.tzy.framework.pay.config;

import com.alipay.api.AlipayConfig;
import com.tzy.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * 全民付配置文件
 */
@Data
@Component
@ConfigurationProperties(prefix = "pay.qm.app")
@NoArgsConstructor
@AllArgsConstructor
public class QmPayConfig extends AlipayConfig {

    public QmPayConfig(String appid, String appkey, String privateKey, String instMid){
        this.appid = appid;
        this.appkey = appkey;
        this.privateKey = privateKey;
        this.instMid = instMid;
    }
    private String name = "qm";
    //appid
    private String appid;
    //appkey
    private String appkey;
    //商户号
    private String mid;
    //终端号
    private String tid;
    //子商户号
    private String sub_mid;
    //子终端号
    private String sub_tid;
    //来源编号
    private String msgSrcId;
    //机构商户号
    private String instMid;
    //私钥
    private String privateKey;

    private String privateKeyPath;
    //支付回调地址
    private String payNotifyUrl;
    //退款回调地址
    private String refundNotifyUrl;
    //订单展示页面
    private String showUrl;
    //支付标记
    private String payFlag;
    //退款标记
    private String refundFlag;
    //微信子商户appid
    private String subAppId;
    //企业用户号
    private String businessUserNo;
    //企业用户号
//    private boolean limitCreditCard = false;


    public String getPrivateKey(){
        String privateKey = this.privateKey;
        if(StringUtils.isNotEmpty(privateKey)){
            return 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;
    }

}
