Skip to content

Commit

Permalink
feat: Added a new JS API signature method. (V3)
Browse files Browse the repository at this point in the history
  • Loading branch information
real-zony committed Jan 7, 2024
1 parent 36a799c commit 210eeb9
Showing 1 changed file with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common.Extensions;
using EasyAbp.Abp.WeChat.Common.Infrastructure;
using EasyAbp.Abp.WeChat.Common.Infrastructure.Signature;
using EasyAbp.Abp.WeChat.Pay.Options;
using EasyAbp.Abp.WeChat.Pay.RequestHandling.Dtos;
using EasyAbp.Abp.WeChat.Pay.Security;
using Volo.Abp.DependencyInjection;

namespace EasyAbp.Abp.WeChat.Pay.RequestHandling;

public class WeChatPayClientRequestHandlingService : IWeChatPayClientRequestHandlingService, ITransientDependency
{
private readonly ICertificatesManager _certificatesManager;
private readonly IAbpWeChatPayOptionsProvider _optionsProvider;
private readonly ISignatureGenerator _signatureGenerator;

public WeChatPayClientRequestHandlingService(
IAbpWeChatPayOptionsProvider optionsProvider,
ISignatureGenerator signatureGenerator)
public WeChatPayClientRequestHandlingService(ICertificatesManager certificatesManager,
IAbpWeChatPayOptionsProvider optionsProvider)
{
_certificatesManager = certificatesManager;
_optionsProvider = optionsProvider;
_signatureGenerator = signatureGenerator;
}

public virtual async Task<GetJsSdkWeChatPayParametersResult> GetJsSdkWeChatPayParametersAsync(
Expand All @@ -32,20 +30,18 @@ public virtual async Task<GetJsSdkWeChatPayParametersResult> GetJsSdkWeChatPayPa

var options = await _optionsProvider.GetAsync(input.MchId);

const string signType = "RSA";
var nonceStr = RandomStringHelper.GetRandomString();
var timeStamp = DateTimeHelper.GetNowTimeStamp();
var package = $"prepay_id={input.PrepayId}";

const string signType = "MD5";

var @params = new WeChatParameters();
@params.AddParameter("appId", input.AppId);
@params.AddParameter("nonceStr", nonceStr);
@params.AddParameter("timeStamp", timeStamp);
@params.AddParameter("package", package);
@params.AddParameter("signType", signType);

var paySign = _signatureGenerator.Generate(@params, MD5.Create(), options.ApiV3Key);
var waitSignString = new StringBuilder();
waitSignString.Append(input.AppId).Append('\n')
.Append(timeStamp).Append('\n')
.Append(nonceStr).Append('\n')
.Append("prepay_id=").Append(input.PrepayId).Append('\n');
var certificate = await _certificatesManager.GetCertificateAsync(options.MchId);
var paySign = _certificatesManager.GetSignature(waitSignString.ToString(), certificate);

return new GetJsSdkWeChatPayParametersResult(nonceStr, timeStamp, package, signType, paySign);
}
Expand Down

0 comments on commit 210eeb9

Please sign in to comment.