using System;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
{
///
/// 为 提供回调通知事件敏感数据解密的扩展方法。
///
public static class WechatTenpayClientEventDecryptionExtensions
{
///
/// 反序列化得到 对象。
///
///
///
///
public static WechatTenpayEvent DeserializeEvent(this WechatTenpayClient client, string callbackJson)
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (string.IsNullOrEmpty(callbackJson)) throw new ArgumentNullException(callbackJson);
return client.JsonSerializer.Deserialize(callbackJson);
}
///
/// 返回序列化并解密事件数据中被加密的通知数据。
///
///
///
///
///
public static T DecryptEventResource(this WechatTenpayClient client, WechatTenpayEvent callback)
where T : WechatTenpayEvent.Types.IDecryptedResource, new()
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (callback == null) throw new ArgumentNullException(nameof(callback));
return DecryptEventResource(client, callback.Resource);
}
///
/// 返回序列化并解密事件数据中被加密的通知数据。
///
///
///
///
///
public static T DecryptEventResource(this WechatTenpayClient client, WechatTenpayEvent.Types.Resource resource)
where T : WechatTenpayEvent.Types.IDecryptedResource, new()
{
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
{
plainJson = Utilities.AESUtility.DecryptWithGCM(
key: client.Credentials.MerchantV3Secret,
iv: resource.Nonce,
aad: resource.AssociatedData,
cipherText: resource.CipherText
);
}
catch (Exception ex)
{
throw new Exceptions.WechatTenpayEventDecryptionException("Decrypt event resource failed. Please see the `InnerException` for more details.", ex);
}
}
else
{
throw new Exceptions.WechatTenpayEventDecryptionException("Unsupported encrypt algorithm of the resource.");
}
return client.JsonSerializer.Deserialize(plainJson);
}
}
}