diff --git a/src/PaySharp.Unionpay/Merchant.cs b/src/PaySharp.Unionpay/Merchant.cs index 021ffc3..03ae031 100644 --- a/src/PaySharp.Unionpay/Merchant.cs +++ b/src/PaySharp.Unionpay/Merchant.cs @@ -21,11 +21,6 @@ namespace PaySharp.Unionpay /// public string CertId { get; internal set; } - /// - /// 加密证书号 - /// - public string EncryptCertId { get; internal set; } - /// /// 证书私钥 /// diff --git a/src/PaySharp.Unionpay/PaySharp.Unionpay.csproj b/src/PaySharp.Unionpay/PaySharp.Unionpay.csproj index da36d6c..0c26122 100644 --- a/src/PaySharp.Unionpay/PaySharp.Unionpay.csproj +++ b/src/PaySharp.Unionpay/PaySharp.Unionpay.csproj @@ -28,7 +28,6 @@ - diff --git a/src/PaySharp.Unionpay/UnionpayGateway.cs b/src/PaySharp.Unionpay/UnionpayGateway.cs index 021c681..915342d 100644 --- a/src/PaySharp.Unionpay/UnionpayGateway.cs +++ b/src/PaySharp.Unionpay/UnionpayGateway.cs @@ -30,10 +30,9 @@ namespace PaySharp.Unionpay : base(merchant) { _merchant = merchant; - //TODO:测试同时读取是否有问题 + _merchant.CertId = Util.GetCertId(merchant.CertPath, merchant.CertPwd); _merchant.CertKey = Util.GetCertKey(merchant.CertPath, merchant.CertPwd); - //_merchant.EncryptCertId = Util.GetEncryptCertId(); } #if NETSTANDARD2_0 diff --git a/src/PaySharp.Unionpay/Util.cs b/src/PaySharp.Unionpay/Util.cs index 233a539..e961422 100644 --- a/src/PaySharp.Unionpay/Util.cs +++ b/src/PaySharp.Unionpay/Util.cs @@ -18,6 +18,13 @@ namespace PaySharp.Unionpay { internal static class Util { + #region 字段 + + private static Pkcs12Store _pkcs12Store; + private static string _aliase; + + #endregion + #region 证书 private static X509Certificate ACPENCCER = null; @@ -34,21 +41,20 @@ namespace PaySharp.Unionpay /// 证书路径 /// 证书密码 /// - private static (Pkcs12Store Store, string Aliase) GetPkcs12Store(string path, string pwd) + private static void GetPkcs12Store(string path, string pwd) { - using (FileStream fileStream = new FileStream(path, FileMode.Open)) + using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { - Pkcs12Store store = new Pkcs12Store(fileStream, pwd.ToCharArray()); - foreach (string n in store.Aliases) + var pkcs12Store = new Pkcs12Store(fileStream, pwd.ToCharArray()); + foreach (string item in pkcs12Store.Aliases) { - if (store.IsKeyEntry(n)) + if (pkcs12Store.IsKeyEntry(item)) { - return (store, n); + _pkcs12Store = pkcs12Store; + _aliase = item; } } } - - return (null, null); } /// @@ -59,9 +65,13 @@ namespace PaySharp.Unionpay /// public static string GetCertId(string path, string pwd) { - var (Store, Aliase) = GetPkcs12Store(path, pwd); - return Store - .GetCertificateChain(Aliase)[0] + if (string.IsNullOrEmpty(_aliase)) + { + GetPkcs12Store(path, pwd); + } + + return _pkcs12Store + .GetCertificateChain(_aliase)[0] .Certificate .SerialNumber .ToString(); @@ -81,9 +91,13 @@ namespace PaySharp.Unionpay /// public static AsymmetricKeyParameter GetCertKey(string path, string pwd) { - var (Store, Aliase) = GetPkcs12Store(path, pwd); - return Store - .GetKey(Aliase) + if (string.IsNullOrEmpty(_aliase)) + { + GetPkcs12Store(path, pwd); + } + + return _pkcs12Store + .GetKey(_aliase) .Key; }