feat(tenpayv3): 构造器模式

This commit is contained in:
Fu Diwei
2024-02-06 14:25:41 +08:00
committed by RHQYZ
parent fbb9d3c6af
commit 1dcd2a9158
6 changed files with 105 additions and 5 deletions

View File

@@ -35,7 +35,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
string content = await reader.ReadToEndAsync();
_logger.LogInformation("接收到微信支付推送的数据:{0}", content);
using var client = _wechatTenpayClientFactory.Create(merchantId);
var client = _wechatTenpayClientFactory.Create(merchantId);
bool valid = client.VerifyEventSignature(
webhookTimestamp: timestamp,
webhookNonce: nonce,

View File

@@ -31,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
[Route("jsapi")]
public async Task<IActionResult> CreateOrderByJsapi([FromBody] Models.CreateOrderByJsapiRequest requestModel)
{
using var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId);
var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId);
var request = new CreatePayTransactionJsapiRequest()
{
OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"),

View File

@@ -31,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
[Route("")]
public async Task<IActionResult> CreateRefund([FromBody] Models.CreateRefundRequest requestModel)
{
using var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId);
var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId);
var request = new CreateRefundDomesticRefundRequest()
{
TransactionId = requestModel.TransactionId,

View File

@@ -1,18 +1,22 @@
using System;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Implements
{
internal partial class WechatTenpayClientFactory : IWechatTenpayClientFactory
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly Options.TenpayOptions _tenpayOptions;
private readonly IWechatTenpayCertificateManagerFactory _tenpayCertificateManagerFactory;
public WechatTenpayClientFactory(
IHttpClientFactory httpClientFactory,
IOptions<Options.TenpayOptions> tenpayOptions,
IWechatTenpayCertificateManagerFactory tenpayCertificateManagerFactory)
{
_httpClientFactory = httpClientFactory;
_tenpayOptions = tenpayOptions.Value;
_tenpayCertificateManagerFactory = tenpayCertificateManagerFactory;
}
@@ -37,7 +41,9 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Imple
AutoEncryptRequestSensitiveProperty = false,
AutoDecryptResponseSensitiveProperty = false
};
var wechatTenpayClient = new WechatTenpayClient(wechatTenpayClientOptions);
var wechatTenpayClient = WechatTenpayClientBuilder.Create(wechatTenpayClientOptions)
.UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false)
.Build();
return wechatTenpayClient;
}
}

View File

@@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.BackgroundJobs
try
{
const string ALGORITHM_TYPE = "RSA";
var client = _wechatTenpayClientFactory.Create(tenpayMerchantOptions.MerchantId);
using var client = _wechatTenpayClientFactory.Create(tenpayMerchantOptions.MerchantId);
var request = new QueryCertificatesRequest() { AlgorithmType = ALGORITHM_TYPE };
var response = await client.ExecuteQueryCertificatesAsync(request);
if (response.IsSuccessful())