feat: 重命名 ICertificateStorer 接口为 ICertificateManager

This commit is contained in:
Fu Diwei 2021-07-27 00:00:14 +08:00
parent 771e21708d
commit 2062d1d3d9
9 changed files with 25 additions and 23 deletions

View File

@ -35,13 +35,13 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
if (callbackSignature == null) throw new ArgumentNullException(nameof(callbackSignature)); if (callbackSignature == null) throw new ArgumentNullException(nameof(callbackSignature));
if (callbackSerialNumber == null) throw new ArgumentNullException(nameof(callbackSerialNumber)); if (callbackSerialNumber == null) throw new ArgumentNullException(nameof(callbackSerialNumber));
if (client.WechatCertificateStorer == null) if (client.WechatCertificateManager == null)
{ {
throw new Exceptions.WechatTenpayResponseVerificationException($"You must set an instance of `{nameof(Settings.ICertificateStorer)}` at first."); throw new Exceptions.WechatTenpayResponseVerificationException($"You must set an instance of `{nameof(Settings.ICertificateManager)}` at first.");
} }
else else
{ {
string? certificate = client.WechatCertificateStorer.Get(callbackSerialNumber); string? certificate = client.WechatCertificateManager.GetCertificate(callbackSerialNumber);
if (certificate == null) if (certificate == null)
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get certificate by the serial number, may not be stored."); throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get certificate by the serial number, may not be stored.");

View File

@ -101,6 +101,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_8.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_8.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_8.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_8.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_19.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_19.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter8_1_12.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_8.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_8.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_2_8.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_2_8.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_3_8.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_3_8.shtml </para>
@ -108,6 +109,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_5_8.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_5_8.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter5_1_19.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter5_1_19.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_9_3.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_9_3.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter8_1_12.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transfer/chapter4_3.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transfer/chapter4_3.shtml </para>
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transfer_partner/chapter4_3.shtml </para> /// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transfer_partner/chapter4_3.shtml </para>
/// </summary> /// </summary>

View File

@ -23,16 +23,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
if (client == null) throw new ArgumentNullException(nameof(client)); if (client == null) throw new ArgumentNullException(nameof(client));
if (response == null) throw new ArgumentNullException(nameof(response)); if (response == null) throw new ArgumentNullException(nameof(response));
if (client.WechatCertificateStorer == null) if (client.WechatCertificateManager == null)
{ {
throw new Exceptions.WechatTenpayResponseVerificationException($"You must set an instance of `{nameof(Settings.ICertificateStorer)}` at first."); throw new Exceptions.WechatTenpayResponseVerificationException($"You must set an instance of `{nameof(Settings.ICertificateManager)}` at first.");
} }
else else
{ {
if (response.WechatpayCertSerialNumber == null) if (response.WechatpayCertSerialNumber == null)
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot read the serial number in headers of this response."); throw new Exceptions.WechatTenpayResponseVerificationException("Cannot read the serial number in headers of this response.");
string? certificate = client.WechatCertificateStorer.Get(response.WechatpayCertSerialNumber); string? certificate = client.WechatCertificateManager.GetCertificate(response.WechatpayCertSerialNumber);
if (certificate == null) if (certificate == null)
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get certificate by the serial number, may not be stored."); throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get certificate by the serial number, may not be stored.");

View File

@ -7,22 +7,22 @@ using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
{ {
/// <summary> /// <summary>
/// 微信商户平台证书存储器接口。 /// 微信商户平台证书管理器接口。
/// </summary> /// </summary>
public interface ICertificateStorer public interface ICertificateManager
{ {
/// <summary> /// <summary>
/// 根据证书序列号获取证书cer 格式)。 /// 根据证书序列号获取证书cer 格式)。
/// </summary> /// </summary>
/// <param name="serialNumber"></param> /// <param name="serialNumber"></param>
/// <returns></returns> /// <returns></returns>
string? Get(string serialNumber); string? GetCertificate(string serialNumber);
/// <summary> /// <summary>
/// 设置证书序列号与证书cer 格式)的映射关系。 /// 设置证书序列号与证书cer 格式)的映射关系。
/// </summary> /// </summary>
/// <param name="serialNumber"></param> /// <param name="serialNumber"></param>
/// <param name="certificate"></param> /// <param name="certificate"></param>
void Set(string serialNumber, string certificate); void SetCertificate(string serialNumber, string certificate);
} }
} }

View File

@ -8,25 +8,25 @@ using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
{ {
/// <summary> /// <summary>
/// 一个基于内存实现的 <see cref="ICertificateStorer"/>。 /// 一个基于内存实现的 <see cref="ICertificateManager"/>。
/// </summary> /// </summary>
public class InMemoryCertificateStorer : ICertificateStorer public class InMemoryCertificateManager : ICertificateManager
{ {
public IDictionary<string, string> _dict; public IDictionary<string, string> _dict;
public InMemoryCertificateStorer() public InMemoryCertificateManager()
{ {
_dict = new ConcurrentDictionary<string, string>(); _dict = new ConcurrentDictionary<string, string>();
} }
string? ICertificateStorer.Get(string serialNumber) string? ICertificateManager.GetCertificate(string serialNumber)
{ {
if (serialNumber == null) throw new ArgumentNullException(nameof(serialNumber)); if (serialNumber == null) throw new ArgumentNullException(nameof(serialNumber));
return _dict[serialNumber]; return _dict[serialNumber];
} }
void ICertificateStorer.Set(string serialNumber, string certificate) void ICertificateManager.SetCertificate(string serialNumber, string certificate)
{ {
if (serialNumber == null) throw new ArgumentNullException(nameof(serialNumber)); if (serialNumber == null) throw new ArgumentNullException(nameof(serialNumber));
if (certificate == null) throw new ArgumentNullException(nameof(certificate)); if (certificate == null) throw new ArgumentNullException(nameof(certificate));

View File

@ -42,7 +42,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
/// <summary> /// <summary>
/// 获取当前客户端使用的微信商户平台证书存储器。 /// 获取当前客户端使用的微信商户平台证书存储器。
/// </summary> /// </summary>
internal Settings.ICertificateStorer? WechatCertificateStorer { get; } internal Settings.ICertificateManager? WechatCertificateManager { get; }
/// <summary> /// <summary>
/// 获取当前客户端使用的 JSON 序列化器。 /// 获取当前客户端使用的 JSON 序列化器。
@ -64,7 +64,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
WechatMerchantCertSerialNumber = options.MerchantCertSerialNumber; WechatMerchantCertSerialNumber = options.MerchantCertSerialNumber;
WechatMerchantCertPrivateKey = options.MerchantCertPrivateKey; WechatMerchantCertPrivateKey = options.MerchantCertPrivateKey;
WechatMerchantV3Secret = options.MerchantV3Secret; WechatMerchantV3Secret = options.MerchantV3Secret;
WechatCertificateStorer = options.CertificateStorer; WechatCertificateManager = options.CertificateManager;
FlurlClient.BaseUrl = options.Endpoints ?? WechatTenpayEndpoints.DEFAULT; FlurlClient.BaseUrl = options.Endpoints ?? WechatTenpayEndpoints.DEFAULT;
FlurlClient.Headers.Remove("Accept"); FlurlClient.Headers.Remove("Accept");

View File

@ -60,8 +60,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
/// <summary> /// <summary>
/// 获取或设置微信商户平台证书存储器。 /// 获取或设置微信商户平台证书存储器。
/// <para>默认值:<see cref="Settings.InMemoryCertificateStorer"/></para> /// <para>默认值:<see cref="Settings.InMemoryCertificateManager"/></para>
/// </summary> /// </summary>
public Settings.ICertificateStorer? CertificateStorer { get; set; } = new Settings.InMemoryCertificateStorer(); public Settings.ICertificateManager? CertificateManager { get; set; } = new Settings.InMemoryCertificateManager();
} }
} }

View File

@ -6,18 +6,18 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
{ {
static TestClients() static TestClients()
{ {
GlobalCertificateStorer = new Settings.InMemoryCertificateStorer(); GlobalCertificateManager = new Settings.InMemoryCertificateManager();
Instance = new WechatTenpayClient(new WechatTenpayClientOptions() Instance = new WechatTenpayClient(new WechatTenpayClientOptions()
{ {
MerchantId = TestConfigs.WechatMerchantId, MerchantId = TestConfigs.WechatMerchantId,
MerchantV3Secret = TestConfigs.WechatMerchantSecret, MerchantV3Secret = TestConfigs.WechatMerchantSecret,
MerchantCertSerialNumber = TestConfigs.WechatMerchantCertSerialNumber, MerchantCertSerialNumber = TestConfigs.WechatMerchantCertSerialNumber,
MerchantCertPrivateKey = TestConfigs.WechatMerchantCertPrivateKey, MerchantCertPrivateKey = TestConfigs.WechatMerchantCertPrivateKey,
CertificateStorer = GlobalCertificateStorer CertificateManager = GlobalCertificateManager
}); });
} }
public static readonly Settings.ICertificateStorer GlobalCertificateStorer; public static readonly Settings.ICertificateManager GlobalCertificateManager;
public static readonly WechatTenpayClient Instance; public static readonly WechatTenpayClient Instance;
} }

View File

@ -24,7 +24,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.UnitTests
TestClients.Instance.DecryptResponseEncryptedData(ref response); TestClients.Instance.DecryptResponseEncryptedData(ref response);
foreach (var certificateModel in response.CertificateList) foreach (var certificateModel in response.CertificateList)
{ {
TestClients.GlobalCertificateStorer.Set(certificateModel.SerialNumber, certificateModel.EncryptCertificate.CipherText); TestClients.GlobalCertificateManager.SetCertificate(certificateModel.SerialNumber, certificateModel.EncryptCertificate.CipherText);
} }
Assert.True(TestClients.Instance.VerifyResponseSignature(response)); Assert.True(TestClients.Instance.VerifyResponseSignature(response));