using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Constants;
public static class WechatTenpayClientParameterExtensions
{
///
/// 生成客户端 JSAPI / 小程序调起支付所需的参数字典。
///
/// REF:
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static IDictionary GenerateParametersForJsapiPayRequest(this WechatTenpayClient client, string appId, string prepayId)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (appId is null) throw new ArgumentNullException(nameof(appId));
if (prepayId is null) throw new ArgumentNullException(nameof(prepayId));
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
string nonce = Guid.NewGuid().ToString("N");
string package = $"prepay_id={prepayId}";
string sign = Utilities.RSAUtility.SignWithSHA256(
privateKeyPem: client.Credentials.MerchantCertificatePrivateKey,
messageData: $"{appId}\n{timestamp}\n{nonce}\n{package}\n"
)!;
return new ReadOnlyDictionary(new Dictionary()
{
{ "appId", appId },
{ "timeStamp", timestamp },
{ "nonceStr", nonce },
{ "package", package },
{ "signType", SignTypes.RSA },
{ "paySign", sign }
});
}
///
/// 生成 APP 调起支付所需的参数字典。
///
/// REF:
///
///
///
///
///
///
///
///
///
///
///
///
///
public static IDictionary GenerateParametersForAppPayRequest(this WechatTenpayClient client, string appId, string prepayId)
{
return GenerateParametersForAppPayRequest(client, merchantId: client.Credentials.MerchantId, appId: appId, prepayId: prepayId);
}
///
/// 生成 APP 调起支付所需的参数字典。
///
/// REF:
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public static IDictionary GenerateParametersForAppPayRequest(this WechatTenpayClient client, string merchantId, string appId, string prepayId)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (merchantId is null) throw new ArgumentNullException(nameof(merchantId));
if (appId is null) throw new ArgumentNullException(nameof(appId));
if (prepayId is null) throw new ArgumentNullException(nameof(prepayId));
string timestamp = DateTimeOffset.Now.ToLocalTime().ToUnixTimeSeconds().ToString();
string nonce = Guid.NewGuid().ToString("N");
string sign = Utilities.RSAUtility.SignWithSHA256(
privateKeyPem: client.Credentials.MerchantCertificatePrivateKey,
messageData: $"{appId}\n{timestamp}\n{nonce}\n{prepayId}\n"
)!;
return new ReadOnlyDictionary(new Dictionary()
{
{ "appid", appId },
{ "partnerid", merchantId },
{ "prepayid", prepayId },
{ "package", "Sign=WXPay" },
{ "noncestr", nonce },
{ "timestamp", timestamp },
{ "sign", sign }
});
}
}
}