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