package com.fastbee.common.utils.pay;

import java.io.*;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class wxPayConfig {

    /**
     * 获取平台证书公钥
     * @param platformCertificatePath
     * @return
     */
    public static String getPublicKeyByCertificat(String platformCertificateContent)
    {
        try {
            // 读取私钥文件内容
            //String platformCertificateContent = readFile(platformCertificatePath);
            // 去除私钥文件中的头尾信息
            platformCertificateContent = platformCertificateContent.replace("-----BEGIN CERTIFICATE-----", "")
                    .replace("-----END CERTIFICATE-----", "")
                    .replaceAll("\\s+", "");
            /*byte[] certBytes = Base64.getDecoder().decode(platformCertificateContent);
            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(certBytes));
            String publicKey = certificate.getPublicKey().toString();*/
            return platformCertificateContent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取微信支付公钥
    public static String getPublicKey(String publicKeyContent) {
        try {
            // 读取私钥文件内容
            //String publicKeyContent = readFile(publicKeyPath);
            // 去除私钥文件中的头尾信息
            publicKeyContent = publicKeyContent.replace("-----BEGIN PUBLIC KEY-----", "")
                    .replace("-----END PUBLIC KEY-----", "")
                    .replaceAll("\\s+", "");
            return publicKeyContent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取商户私钥
    public static PrivateKey getPrivateKey(String privateKeyContent) {
        try {
            // 读取私钥文件内容
            //String privateKeyContent = readFile(privateKeyPath);
            // 去除私钥文件中的头尾信息
            privateKeyContent = privateKeyContent.replace("-----BEGIN PRIVATE KEY-----", "")
                    .replace("-----END PRIVATE KEY-----", "")
                    .replaceAll("\\s+", "");
            // 使用Base64解码
            byte[] keyBytes = Base64.getDecoder().decode(privateKeyContent);
            // 通过PKCS#8EncodedKeySpec指定者来构造PrivateKey对象
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            // 获取RSA KeyFactory对象
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            // 从PKCS#8EncodedKeySpec指定者中构造出PrivateKey对象
            return keyFactory.generatePrivate(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 读取文件内容为字符串
    private static String readFile(String filePath) {
        StringBuilder fileContent = new StringBuilder("");
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                fileContent.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileContent.toString();
    }
    // 这里可以添加其他与微信支付相关的配置方法
}