2021-05-10 15:30:00 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2021-07-20 01:20:31 +08:00
|
|
|
|
/// 为 <see cref="WechatTenpayClient"/> 提供回调通知事件敏感数据解密的扩展方法。
|
2021-05-10 15:30:00 +08:00
|
|
|
|
/// </summary>
|
2021-07-20 01:20:31 +08:00
|
|
|
|
public static class WechatTenpayClientEventDecryptionExtensions
|
2021-05-10 15:30:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2021-05-28 19:08:11 +08:00
|
|
|
|
/// <para>反序列化得到 <see cref="WechatTenpayEvent"/> 对象。</para>
|
2021-05-10 15:30:00 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="callbackJson"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-05-28 19:08:11 +08:00
|
|
|
|
public static WechatTenpayEvent DeserializeEvent(this WechatTenpayClient client, string callbackJson)
|
2021-05-10 15:30:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
|
if (string.IsNullOrEmpty(callbackJson)) throw new ArgumentNullException(callbackJson);
|
|
|
|
|
|
|
2021-07-27 00:50:03 +08:00
|
|
|
|
return client.JsonSerializer.Deserialize<WechatTenpayEvent>(callbackJson);
|
2021-05-10 15:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回序列化并解密事件数据中被加密的通知数据。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="callback"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-05-28 19:08:11 +08:00
|
|
|
|
public static T DecryptEventResource<T>(this WechatTenpayClient client, WechatTenpayEvent callback)
|
|
|
|
|
|
where T : WechatTenpayEvent.Types.IDecryptedResource, new()
|
2021-05-10 15:30:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
|
if (callback == null) throw new ArgumentNullException(nameof(callback));
|
|
|
|
|
|
|
2021-05-28 19:08:11 +08:00
|
|
|
|
return DecryptEventResource<T>(client, callback.Resource);
|
2021-05-10 15:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回序列化并解密事件数据中被加密的通知数据。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="resource"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-05-28 19:08:11 +08:00
|
|
|
|
public static T DecryptEventResource<T>(this WechatTenpayClient client, WechatTenpayEvent.Types.Resource resource)
|
|
|
|
|
|
where T : WechatTenpayEvent.Types.IDecryptedResource, new()
|
2021-05-10 15:30:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
|
if (resource == null) throw new ArgumentNullException(nameof(resource));
|
|
|
|
|
|
|
|
|
|
|
|
string plainJson;
|
|
|
|
|
|
|
|
|
|
|
|
if (Constants.EncryptionAlgorithms.AEAD_AES_256_GCM.Equals(resource.Algorithm))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-06-07 00:28:00 +08:00
|
|
|
|
plainJson = Utilities.AESUtility.DecryptWithGCM(
|
2021-07-27 00:50:03 +08:00
|
|
|
|
key: client.Credentials.MerchantV3Secret,
|
2021-06-07 00:28:00 +08:00
|
|
|
|
iv: resource.Nonce,
|
|
|
|
|
|
aad: resource.AssociatedData,
|
2021-05-10 15:30:00 +08:00
|
|
|
|
cipherText: resource.CipherText
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-07-27 00:01:54 +08:00
|
|
|
|
throw new Exceptions.WechatTenpayEventDecryptionException("Decrypt event resource failed. Please see the `InnerException` for more details.", ex);
|
2021-05-10 15:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-11-25 18:05:22 +08:00
|
|
|
|
throw new Exceptions.WechatTenpayEventDecryptionException("Unsupported encrypt algorithm of the resource.");
|
2021-05-10 15:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-27 00:50:03 +08:00
|
|
|
|
return client.JsonSerializer.Deserialize<T>(plainJson);
|
2021-05-10 15:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|