package com.tzy.framework.pay.util;

import com.tzy.framework.pay.dto.AlipayData;

import java.io.*;

/**
 * 支付宝的支付配置
 */
public class AlipayConfigUtil {

    public static AlipayData initConfig(String appid){
        String privateKey = null;
        String publicKey = null;
        switch (appid){
            case "2021002181669560":
                appid = "2021002181669560";
//                privateKey = "/home/tzy/alipay/hs/privatekey.txt";
                privateKey = "/app/configs/privatekey.txt";
                publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgCIKpTwnnPm2D1IsMlTvuCq3WBAZKm8IaUqR/IxqQAzwEVuW0+lf6Ririm4PLNcU5eZXK7w+oHqi23b7rL5htqz1K0/q/oBHEeAliCNX+zMr+6sTBN33i5yX/buvVmBPGJJg791lFdnnnELGqZf+nQrJBlOAW/aCVTga7hcf8CH/ZRDboExzf9zK5N6qcxKexsgXZRZso+d47DcAj3sF1lAcYQvTAOG+aJRcwBFFOrb3QYZI1WLOZdjq3kdhXUuVrR8HhQ68BRIvfJGX7Hwy4Ffmb9hD/ljkT1S6+jzslMp3cMbU4IlJPF0o9xxCz4KWiW0pWhwQTolNVqW3GyHG2wIDAQAB";
                break;
            default:
                break;
        }
        return new AlipayData(appid,getPrivateKey(privateKey),publicKey);
    }

    private static String getPrivateKey(String privateKeyPath){
        String privateKey = "";
        InputStream inputStream = AlipayConfigUtil.class.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;
    }
}
