mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-15 23:13:32 +08:00
chore(tenpayv3): 新增多租户证书平台管理器的示例代码
This commit is contained in:
parent
b9d2cdfffe
commit
560ef39609
@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
|
@ -14,18 +14,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundSe
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly Options.TenpayOptions _tenpayOptions;
|
||||
private readonly CertificateManager _tenpayCertificateManager;
|
||||
private readonly HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
|
||||
|
||||
public TenpayCertificateRefreshingBackgroundService(
|
||||
ILoggerFactory loggerFactory,
|
||||
IOptions<Options.TenpayOptions> tenpayOptions,
|
||||
CertificateManager tenpayCertificateManager,
|
||||
HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger(GetType());
|
||||
_tenpayOptions = tenpayOptions.Value;
|
||||
_tenpayCertificateManager = tenpayCertificateManager;
|
||||
_tenpayHttpClientFactory = tenpayHttpClientFactory;
|
||||
}
|
||||
|
||||
@ -52,7 +49,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundSe
|
||||
|
||||
foreach (var certificateModel in response.CertificateList)
|
||||
{
|
||||
_tenpayCertificateManager.AddEntry(new CertificateEntry(certificateModel));
|
||||
client.CertificateManager.AddEntry(new CertificateEntry(certificateModel));
|
||||
}
|
||||
|
||||
_logger.LogInformation("刷新微信商户平台证书成功。");
|
||||
|
@ -0,0 +1,9 @@
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
public interface IWechatTenpayCertificateManagerFactory
|
||||
{
|
||||
CertificateManager Create(string merchantId);
|
||||
}
|
||||
}
|
@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients
|
||||
{
|
||||
public interface IWechatTenpayHttpClientFactory
|
||||
{
|
||||
|
@ -0,0 +1,21 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients.Implements
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
partial class WechatTenpayCertificateManagerFactory : IWechatTenpayCertificateManagerFactory
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, CertificateManager> _dict;
|
||||
|
||||
public WechatTenpayCertificateManagerFactory()
|
||||
{
|
||||
_dict = new ConcurrentDictionary<string, CertificateManager>();
|
||||
}
|
||||
|
||||
public CertificateManager Create(string merchantId)
|
||||
{
|
||||
return _dict.GetOrAdd(merchantId, new InMemoryCertificateManager());
|
||||
}
|
||||
}
|
||||
}
|
@ -12,34 +12,34 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients.
|
||||
partial class WechatTenpayHttpClientFactory : IWechatTenpayHttpClientFactory
|
||||
{
|
||||
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
|
||||
private readonly Options.TenpayOptions _wechatOptions;
|
||||
private readonly CertificateManager _certificateManager;
|
||||
private readonly Options.TenpayOptions _tenpayOptions;
|
||||
private readonly IWechatTenpayCertificateManagerFactory _tenpayCertificateManagerFactory;
|
||||
|
||||
public WechatTenpayHttpClientFactory(
|
||||
System.Net.Http.IHttpClientFactory httpClientFactory,
|
||||
IOptions<Options.TenpayOptions> wechatOptions,
|
||||
CertificateManager certificateManager)
|
||||
IOptions<Options.TenpayOptions> tenpayOptions,
|
||||
IWechatTenpayCertificateManagerFactory tenpayCertificateManagerFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_wechatOptions = wechatOptions.Value;
|
||||
_certificateManager = certificateManager;
|
||||
_tenpayOptions = tenpayOptions.Value;
|
||||
_tenpayCertificateManagerFactory = tenpayCertificateManagerFactory;
|
||||
|
||||
FlurlHttp.GlobalSettings.FlurlClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory);
|
||||
}
|
||||
|
||||
public WechatTenpayClient Create(string merchantId)
|
||||
{
|
||||
var wechatMerchant = _wechatOptions.Merchants?.FirstOrDefault(e => string.Equals(merchantId, e.MerchantId));
|
||||
if (wechatMerchant == null)
|
||||
var merchantOptions = _tenpayOptions.Merchants?.FirstOrDefault(e => string.Equals(merchantId, e.MerchantId));
|
||||
if (merchantOptions == null)
|
||||
throw new Exception("未在配置项中找到该 MerchantId 对应的微信商户号。");
|
||||
|
||||
return new WechatTenpayClient(new WechatTenpayClientOptions()
|
||||
{
|
||||
MerchantId = wechatMerchant.MerchantId,
|
||||
MerchantV3Secret = wechatMerchant.SecretV3,
|
||||
MerchantCertSerialNumber = wechatMerchant.CertSerialNumber,
|
||||
MerchantCertPrivateKey = wechatMerchant.CertPrivateKey,
|
||||
CertificateManager = _certificateManager,
|
||||
MerchantId = merchantOptions.MerchantId,
|
||||
MerchantV3Secret = merchantOptions.SecretV3,
|
||||
MerchantCertSerialNumber = merchantOptions.CertSerialNumber,
|
||||
MerchantCertPrivateKey = merchantOptions.CertPrivateKey,
|
||||
CertificateManager = _tenpayCertificateManagerFactory.Create(merchantOptions.MerchantId),
|
||||
AutoEncryptRequestSensitiveProperty = true,
|
||||
AutoDecryptResponseSensitiveProperty = true
|
||||
});
|
||||
|
@ -1,17 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
public class Startup
|
||||
{
|
||||
public IConfiguration Configuration { get; }
|
||||
@ -31,7 +24,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5
|
||||
|
||||
// 注入工厂 HTTP 客户端
|
||||
services.AddHttpClient();
|
||||
services.AddSingleton<CertificateManager, InMemoryCertificateManager>();
|
||||
services.AddSingleton<Services.HttpClients.IWechatTenpayCertificateManagerFactory, Services.HttpClients.Implements.WechatTenpayCertificateManagerFactory>();
|
||||
services.AddSingleton<Services.HttpClients.IWechatTenpayHttpClientFactory, Services.HttpClients.Implements.WechatTenpayHttpClientFactory>();
|
||||
|
||||
// 注入后台任务
|
||||
|
Loading…
Reference in New Issue
Block a user