using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
{
public static class WechatTenpayClientParameterExtensions
{
///
/// 生成客户端 JSAPI / 小程序调起支付所需的参数字典。
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_8.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_9.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_5_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter5_1_3.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter5_1_9.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_8.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_9.shtml
///
///
///
///
///
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(
privateKey: client.Credentials.MerchantCertificatePrivateKey,
message: $"{appId}\n{timestamp}\n{nonce}\n{package}\n"
);
return new ReadOnlyDictionary(new Dictionary()
{
{ "appId", appId },
{ "timeStamp", timestamp },
{ "nonceStr", nonce },
{ "package", package },
{ "signType", Constants.SignTypes.RSA },
{ "paySign", sign }
});
}
///
/// 生成 APP 调起支付所需的参数字典。
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_6.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_2_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter5_1_6.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_6.shtml
///
///
///
///
///
public static IDictionary GenerateParametersForAppPayRequest(this WechatTenpayClient client, string appId, string prepayId)
{
return GenerateParametersForAppPayRequest(client, merchantId: client.Credentials.MerchantId, appId: appId, prepayId: prepayId);
}
///
/// 生成 APP 调起支付所需的参数字典。
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter5_1_6.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_2_4.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter5_1_6.shtml
/// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_3_6.shtml
///
///
///
///
///
///
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(
privateKey: client.Credentials.MerchantCertificatePrivateKey,
message: $"{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 }
});
}
}
}