From 96f1f262e7335b532a4dfed78088a8c8931ee0e7 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Fri, 3 Dec 2021 16:53:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(tenpayv3):=20=E4=BC=98=E5=8C=96=E5=8F=8D?= =?UTF-8?q?=E5=B0=84=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...TenpayClientRequestEncryptionExtensions.cs | 11 +++-- ...enpayClientResponseDecryptionExtensions.cs | 11 +++-- ...KIT.FlurlHttpClient.Wechat.TenpayV3.csproj | 5 +- .../Utilities/Internal/ReflectionUtility.cs | 46 ++++++++++++------- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientRequestEncryptionExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientRequestEncryptionExtensions.cs index b2d43dfa..f233a266 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientRequestEncryptionExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientRequestEncryptionExtensions.cs @@ -27,11 +27,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3 if (requireEncrypt) { // 遍历并加密被标记为敏感数据的字段 - Utilities.ReflectionUtility.ReplacePropertyStringValue(ref request, (obj, prop, value) => + Utilities.ReflectionUtility.ReplacePropertyStringValue(ref request, (target, currentProp, oldValue) => { - var attr = prop.GetCustomAttribute(); + var attr = currentProp.GetCustomAttribute(); if (attr == null) - return value; + return (false, oldValue); if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm)) { @@ -65,10 +65,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3 request.WechatpayCertSerialNumber = cert.SerialNumber; } - return Utilities.RSAUtility.EncryptWithECBByCertificate( + string newValue = Utilities.RSAUtility.EncryptWithECBByCertificate( certificate: certificate, - plainText: value + plainText: oldValue ); + return (true, newValue); } else { diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientResponseDecryptionExtensions.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientResponseDecryptionExtensions.cs index 202280b8..c681835a 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientResponseDecryptionExtensions.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientResponseDecryptionExtensions.cs @@ -59,18 +59,19 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3 if (requireDecrypt) { // 遍历并解密被标记为敏感数据的字段 - Utilities.ReflectionUtility.ReplacePropertyStringValue(ref response, (obj, prop, value) => + Utilities.ReflectionUtility.ReplacePropertyStringValue(ref response, (target, currentProp, oldValue) => { - var attr = prop.GetCustomAttribute(); + var attr = currentProp.GetCustomAttribute(); if (attr == null) - return value; + return (false, oldValue); if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm)) { - return Utilities.RSAUtility.DecryptWithECB( + string newValue = Utilities.RSAUtility.DecryptWithECB( privateKey: client.Credentials.MerchantCertPrivateKey, - cipherText: value + cipherText: oldValue ); + return (true, newValue); } else { diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj index c1034b04..2ce65b3e 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj @@ -1,7 +1,7 @@ - net461; netstandard2.0; net5.0; net6.0 + net461; net47; netstandard2.0; net5.0; net6.0 8.0 enable true @@ -23,10 +23,11 @@ - + + diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/Internal/ReflectionUtility.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/Internal/ReflectionUtility.cs index 28b03599..c35c24a8 100644 --- a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/Internal/ReflectionUtility.cs +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Utilities/Internal/ReflectionUtility.cs @@ -6,14 +6,14 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities { internal static class ReflectionUtility { - public delegate string ReplacePropertyStringValueReplacement(object obj, PropertyInfo prop, string value); + public delegate (bool Modified, string NewValue) ReplacePropertyStringValueReplacement(object target, PropertyInfo currentProp, string oldValue); public static void ReplacePropertyStringValue(ref T obj, ReplacePropertyStringValueReplacement replacement) { - InnerReplacePropertyStringValue(ref obj, replacement, null); + InnerReplacePropertyStringValue(ref obj, replacement); } - private static void InnerReplacePropertyStringValue(ref T obj, ReplacePropertyStringValueReplacement replacement, PropertyInfo? currentProp) + private static void InnerReplacePropertyStringValue(ref T obj, ReplacePropertyStringValueReplacement replacement) { if (obj == null) throw new ArgumentNullException(nameof(obj)); if (replacement == null) throw new ArgumentNullException(nameof(replacement)); @@ -37,8 +37,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities if (propType == typeof(string)) { string oldValue = (string)childProp.GetValue(obj, null)!; - string newValue = replacement(obj, childProp, oldValue); - childProp.SetValue(obj, newValue); + var result = replacement(obj, childProp, oldValue); + if (result.Modified) + { + childProp.SetValue(obj, result.NewValue); + } } else if (propType.IsClass) { @@ -46,7 +49,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities if (value is null) continue; - InnerReplacePropertyStringValue(ref value, replacement, childProp); + InnerReplacePropertyStringValue(ref value, replacement); childProp.SetValue(obj, value); } else @@ -83,13 +86,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities if (!currentProp.CanWrite) continue; - string oldValue = (string)element!; - string newValue = replacement(obj!, currentProp, oldValue); - array.SetValue(newValue, i); + var oldValue = (string)element!; + var resHandler = replacement(obj!, currentProp, oldValue); + if (resHandler.Modified) + { + array.SetValue(resHandler.NewValue, i); + } } else if (elementType.IsClass) { - InnerReplacePropertyStringValue(ref element, replacement, currentProp); + InnerReplacePropertyStringValue(ref element, replacement); array.SetValue(element, i); } else @@ -118,13 +124,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities if (!currentProp.CanWrite) continue; - string oldValue = (string)element!; - string newValue = replacement(obj, currentProp, oldValue); - list[i] = newValue; + var oldValue = (string)element!; + var resHandler = replacement(obj, currentProp, oldValue); + if (resHandler.Modified) + { + list[i] = resHandler.NewValue; + } } else if (elementType.IsClass) { - InnerReplacePropertyStringValue(ref element, replacement, currentProp); + InnerReplacePropertyStringValue(ref element, replacement); list[i] = element; } else @@ -154,12 +163,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities continue; string oldValue = (string)entryValue!; - string newValue = replacement(obj, currentProp, oldValue); - dict[entry.Key] = newValue; + var resHandler = replacement(obj, currentProp, oldValue); + if (resHandler.Modified) + { + dict[entry.Key] = resHandler.NewValue; + } } else if (entryValueType.IsClass) { - InnerReplacePropertyStringValue(ref entryValue, replacement, currentProp); + InnerReplacePropertyStringValue(ref entryValue, replacement); dict[entry.Key] = entryValue; } else