Skip to content

Commit

Permalink
[WeChatPay.V3] 支持微信支付公钥
Browse files Browse the repository at this point in the history
  • Loading branch information
roc916 committed Nov 4, 2024
1 parent 7ae77bb commit 2e4976e
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 175 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<GenerateDocumentationFile Condition="'$(Configuration)' == 'Release'">true</GenerateDocumentationFile>
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
<SystemTextJsonPackageVersion>8.0.3</SystemTextJsonPackageVersion>
<MicrosoftExtensionsHttpPackageVersion>8.0.0</MicrosoftExtensionsHttpPackageVersion>
<SystemTextJsonPackageVersion>8.0.5</SystemTextJsonPackageVersion>
<MicrosoftExtensionsHttpPackageVersion>8.0.1</MicrosoftExtensionsHttpPackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.204",
"version": "8.0.403",
"rollForward": "latestFeature"
}
}
11 changes: 7 additions & 4 deletions samples/WebApplicationSample/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
// 可为证书文件路径 / 证书文件的base64字符串
"Certificate": "",

// 商户API私钥
// 当配置了P12格式证书时,已包含私钥,不必再单独配置API私钥。
// PEM格式证书,需要单独配置。
"APIPrivateKey": "",
// 微信支付公钥Id
// 使用V3接口微信支付公钥时必填
"WeChatPayPublicKeyId": "",

// 微信支付公钥
// 使用V3接口微信支付公钥时必填
"WeChatPayPublicKey": "",

// RSA公钥
// 目前仅调用"企业付款到银行卡API [V2]"时使用,执行本示例中的"获取RSA加密公钥API [V2]"即可获取。
Expand Down
11 changes: 7 additions & 4 deletions samples/WebApplicationSample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
// 可为证书文件路径 / 证书文件的base64字符串
"Certificate": "",

// 商户API私钥
// 当配置了P12格式证书时,已包含私钥,不必再单独配置API私钥。
// PEM格式证书,需要单独配置。
"APIPrivateKey": "",
// 微信支付公钥Id
// 使用V3接口微信支付公钥时必填
"WeChatPayPublicKeyId": "",

// 微信支付公钥
// 使用V3接口微信支付公钥时必填
"WeChatPayPublicKey": "",

// RSA公钥
// 目前仅调用"企业付款到银行卡API [V2]"时使用,执行本示例中的"获取RSA加密公钥API [V2]"即可获取。
Expand Down
30 changes: 0 additions & 30 deletions src/Essensoft.Paylink.Security/OaepSHA1WithRSA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,5 @@ public static string Decrypt(string data, string privateKey)
return Encoding.UTF8.GetString(rsa.Decrypt(Convert.FromBase64String(data), RSAEncryptionPadding.OaepSHA1));
}
}

public static string Encrypt(RSA rsa, string data)
{
if (rsa == null)
{
throw new ArgumentNullException(nameof(rsa));
}

if (string.IsNullOrEmpty(data))
{
throw new ArgumentNullException(nameof(data));
}

return Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes(data), RSAEncryptionPadding.OaepSHA1));
}

public static string Decrypt(RSA rsa, string data)
{
if (rsa == null)
{
throw new ArgumentNullException(nameof(rsa));
}

if (string.IsNullOrEmpty(data))
{
throw new ArgumentNullException(nameof(data));
}

return Encoding.UTF8.GetString(rsa.Decrypt(Convert.FromBase64String(data), RSAEncryptionPadding.OaepSHA1));
}
}
}
35 changes: 0 additions & 35 deletions src/Essensoft.Paylink.Security/SHA256WithRSA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,5 @@ public static bool Verify(string data, string sign, string publicKey)
return rsa.VerifyData(Encoding.UTF8.GetBytes(data), Convert.FromBase64String(sign), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}

public static string Sign(RSA rsa, string data)
{
if (rsa == null)
{
throw new ArgumentNullException(nameof(rsa));
}

if (string.IsNullOrEmpty(data))
{
throw new ArgumentNullException(nameof(data));
}

return Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(data), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
}

public static bool Verify(RSA rsa, string data, string sign)
{
if (rsa == null)
{
throw new ArgumentNullException(nameof(rsa));
}

if (string.IsNullOrEmpty(data))
{
throw new ArgumentNullException(nameof(data));
}

if (string.IsNullOrEmpty(sign))
{
throw new ArgumentNullException(nameof(sign));
}

return rsa.VerifyData(Encoding.UTF8.GetBytes(data), Convert.FromBase64String(sign), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using Essensoft.Paylink.WeChatPay.V2.Response;

namespace Essensoft.Paylink.WeChatPay.V2.Request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using Essensoft.Paylink.WeChatPay.V2.Response;

namespace Essensoft.Paylink.WeChatPay.V2.Request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml.Serialization;

namespace Essensoft.Paylink.WeChatPay.V2.Response
{
Expand Down
4 changes: 1 addition & 3 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/AccountCertInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
Expand Down
4 changes: 1 addition & 3 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/AppInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
Expand Down
5 changes: 1 addition & 4 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/BizStoreInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
Expand Down
4 changes: 1 addition & 3 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/MiniProgramInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
Expand Down
4 changes: 1 addition & 3 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/MpInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
Expand Down
2 changes: 0 additions & 2 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/SaleInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
Expand Down
4 changes: 1 addition & 3 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/WeWorkInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
Expand Down
5 changes: 1 addition & 4 deletions src/Essensoft.Paylink.WeChatPay/V3/Domain/WebInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;

namespace Essensoft.Paylink.WeChatPay.V3.Domain
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static string BuildToken(string url, string method, string body, WeChatP
var timestamp = WeChatPayUtility.GetTimeStamp();
var nonce = WeChatPayUtility.GenerateNonceStr();
var message = BuildMessage(method, uri, timestamp, nonce, body);
var signature = SHA256WithRSA.Sign(options.RSAPrivateKey, message);
var signature = SHA256WithRSA.Sign(message, options.RSAPrivateKey);

return $"mchid=\"{options.MchId}\",nonce_str=\"{nonce}\",timestamp=\"{timestamp}\",serial_no=\"{options.CertificateSerialNo}\",signature=\"{signature}\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void PrimaryHandler(WeChatPayDictionary sortedTxtParams, WeChatPayOptions
sortedTxtParams.Add(WeChatPayConsts.timestamp, WeChatPayUtility.GetTimeStamp());

var signatureSourceData = BuildSignatureSourceData(sortedTxtParams);
sortedTxtParams.Add(WeChatPayConsts.sign, SHA256WithRSA.Sign(options.RSAPrivateKey, signatureSourceData));
sortedTxtParams.Add(WeChatPayConsts.sign, SHA256WithRSA.Sign(signatureSourceData, options.RSAPrivateKey));
}

private static string BuildSignatureSourceData(WeChatPayDictionary sortedTxtParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void PrimaryHandler(WeChatPayDictionary sortedTxtParams, WeChatPayOptions
sortedTxtParams.Add(WeChatPayConsts.signType, WeChatPayConsts.RSA);

var signatureSourceData = BuildSignatureSourceData(sortedTxtParams);
sortedTxtParams.Add(WeChatPayConsts.paySign, SHA256WithRSA.Sign(options.RSAPrivateKey, signatureSourceData));
sortedTxtParams.Add(WeChatPayConsts.paySign, SHA256WithRSA.Sign(signatureSourceData, options.RSAPrivateKey));
}

private static string BuildSignatureSourceData(WeChatPayDictionary sortedTxtParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void PrimaryHandler(WeChatPayDictionary sortedTxtParams, WeChatPayOptions
sortedTxtParams.Add(WeChatPayConsts.signType, WeChatPayConsts.RSA);

var signatureSourceData = BuildSignatureSourceData(sortedTxtParams);
sortedTxtParams.Add(WeChatPayConsts.paySign, SHA256WithRSA.Sign(options.RSAPrivateKey, signatureSourceData));
sortedTxtParams.Add(WeChatPayConsts.paySign, SHA256WithRSA.Sign(signatureSourceData, options.RSAPrivateKey));
}

private static string BuildSignatureSourceData(WeChatPayDictionary sortedTxtParams)
Expand Down
Loading

0 comments on commit 2e4976e

Please sign in to comment.