From df656663c79476afdffca9366dd85c39f6411b7b Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Sat, 4 Dec 2021 16:11:49 +0800 Subject: [PATCH] demo(tenpayv3): clean code --- ...ontroller.cs => TenpayNotifyController.cs} | 13 ++++----- ...Controller.cs => TenpayOrderController.cs} | 22 +++++++-------- ...ontroller.cs => TenpayRefundController.cs} | 22 +++++++-------- .../{WxpayOptions.cs => TenpayOptions.cs} | 8 +++--- ...CertificateRefreshingBackgroundService.cs} | 28 +++++++++---------- .../WechatTenpayHttpClientFactory.cs | 4 +-- .../Startup.cs | 4 +-- .../appsettings.json | 9 ++++-- 8 files changed, 54 insertions(+), 56 deletions(-) rename samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/{WxpayNotifyController.cs => TenpayNotifyController.cs} (87%) rename samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/{WxpayOrderController.cs => TenpayOrderController.cs} (78%) rename samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/{WxpayRefundController.cs => TenpayRefundController.cs} (78%) rename samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/{WxpayOptions.cs => TenpayOptions.cs} (75%) rename samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/{WxpayCertificateRefreshingBackgroundService.cs => TenpayCertificateRefreshingBackgroundService.cs} (71%) diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayNotifyController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayNotifyController.cs similarity index 87% rename from samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayNotifyController.cs rename to samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayNotifyController.cs index 57d00395..dfbd98a8 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayNotifyController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayNotifyController.cs @@ -8,18 +8,17 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers { [ApiController] [Route("notify")] - public class WxpayNotifyController : ControllerBase + public class TenpayNotifyController : ControllerBase { private readonly ILogger _logger; + private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory; - private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _wechatTenpayHttpClientFactory; - - public WxpayNotifyController( + public TenpayNotifyController( ILoggerFactory loggerFactory, - Services.HttpClients.IWechatTenpayHttpClientFactory wechatTenpayHttpClientFactory) + Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory) { _logger = loggerFactory.CreateLogger(GetType()); - _wechatTenpayHttpClientFactory = wechatTenpayHttpClientFactory; + _tenpayHttpClientFactory = tenpayHttpClientFactory; } @@ -39,7 +38,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers string content = await reader.ReadToEndAsync(); _logger.LogInformation("接收到微信支付推送的数据:{0}", content); - var client = _wechatTenpayHttpClientFactory.Create(merchantId); + var client = _tenpayHttpClientFactory.Create(merchantId); bool valid = client.VerifyEventSignature( callbackTimestamp: timestamp, callbackNonce: nonce, diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayOrderController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayOrderController.cs similarity index 78% rename from samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayOrderController.cs rename to samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayOrderController.cs index edfe9720..860e02fa 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayOrderController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayOrderController.cs @@ -14,22 +14,20 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers { [ApiController] [Route("order")] - public class WxpayOrderController : ControllerBase + public class TenpayOrderController : ControllerBase { private readonly ILogger _logger; + private readonly Options.TenpayOptions _tenpayOptions; + private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory; - private readonly Options.WxpayOptions _wxpayOptions; - - private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _wechatTenpayHttpClientFactory; - - public WxpayOrderController( + public TenpayOrderController( ILoggerFactory loggerFactory, - IOptions wxpayOptions, - Services.HttpClients.IWechatTenpayHttpClientFactory wechatTenpayHttpClientFactory) + IOptions tenpayOptions, + Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory) { _logger = loggerFactory.CreateLogger(GetType()); - _wxpayOptions = wxpayOptions.Value; - _wechatTenpayHttpClientFactory = wechatTenpayHttpClientFactory; + _tenpayOptions = tenpayOptions.Value; + _tenpayHttpClientFactory = tenpayHttpClientFactory; } [HttpPost] @@ -39,13 +37,13 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers // JSAPI 下单 // 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml - var client = _wechatTenpayHttpClientFactory.Create(requestModel.MerchantId); + var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId); var request = new CreatePayTransactionJsapiRequest() { OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"), AppId = requestModel.AppId, Description = "演示订单", - NotifyUrl = _wxpayOptions.CallbackUrl, + NotifyUrl = _tenpayOptions.NotifyUrl, Amount = new CreatePayTransactionJsapiRequest.Types.Amount() { Total = requestModel.Amount }, Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { OpenId = requestModel.OpenId } }; diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayRefundController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayRefundController.cs similarity index 78% rename from samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayRefundController.cs rename to samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayRefundController.cs index 9ded710c..be0cd26b 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/WxpayRefundController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Controllers/TenpayRefundController.cs @@ -14,22 +14,20 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers { [ApiController] [Route("refund")] - public class WxpayRefundController : ControllerBase + public class TenpayRefundController : ControllerBase { private readonly ILogger _logger; + private readonly Options.TenpayOptions _tenpayOptions; + private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory; - private readonly Options.WxpayOptions _wxpayOptions; - - private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _wechatTenpayHttpClientFactory; - - public WxpayRefundController( + public TenpayRefundController( ILoggerFactory loggerFactory, - IOptions wxpayOptions, - Services.HttpClients.IWechatTenpayHttpClientFactory wechatTenpayHttpClientFactory) + IOptions tenpayOptions, + Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory) { _logger = loggerFactory.CreateLogger(GetType()); - _wxpayOptions = wxpayOptions.Value; - _wechatTenpayHttpClientFactory = wechatTenpayHttpClientFactory; + _tenpayOptions = tenpayOptions.Value; + _tenpayHttpClientFactory = tenpayHttpClientFactory; } [HttpPost] @@ -39,7 +37,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers // 申请退款 // 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml - var client = _wechatTenpayHttpClientFactory.Create(requestModel.MerchantId); + var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId); var request = new CreateRefundDomesticRefundRequest() { TransactionId = requestModel.TransactionId, @@ -50,7 +48,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers Refund = requestModel.RefundAmount }, Reason = "示例退款", - NotifyUrl = _wxpayOptions.CallbackUrl + NotifyUrl = _tenpayOptions.NotifyUrl }; var response = await client.ExecuteCreateRefundDomesticRefundAsync(request, cancellationToken: HttpContext.RequestAborted); if (!response.IsSuccessful()) diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/TenpayOptions.cs similarity index 75% rename from samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs rename to samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/TenpayOptions.cs index 8213161e..940849a9 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/WxpayOptions.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Options/TenpayOptions.cs @@ -6,16 +6,16 @@ using Microsoft.Extensions.Options; namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Options { - public partial class WxpayOptions : IOptions + public partial class TenpayOptions : IOptions { - WxpayOptions IOptions.Value => this; + TenpayOptions IOptions.Value => this; public WechatMerchant[] Merchants { get; set; } = Array.Empty(); - public string CallbackUrl { get; set; } = string.Empty; + public string NotifyUrl { get; set; } = string.Empty; } - partial class WxpayOptions + partial class TenpayOptions { public class WechatMerchant { diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/WxpayCertificateRefreshingBackgroundService.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/TenpayCertificateRefreshingBackgroundService.cs similarity index 71% rename from samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/WxpayCertificateRefreshingBackgroundService.cs rename to samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/TenpayCertificateRefreshingBackgroundService.cs index b2e275a4..80529d8b 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/WxpayCertificateRefreshingBackgroundService.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/BackgroundServices/TenpayCertificateRefreshingBackgroundService.cs @@ -10,30 +10,30 @@ using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings; namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundServices { - class WxpayCertificateRefreshingBackgroundService : BackgroundService + class TenpayCertificateRefreshingBackgroundService : BackgroundService { private readonly ILogger _logger; - private readonly Options.WxpayOptions _wxpayOptions; - private readonly CertificateManager _certificateManager; - private readonly HttpClients.IWechatTenpayHttpClientFactory _wechatTenpayHttpClientFactory; + private readonly Options.TenpayOptions _tenpayOptions; + private readonly CertificateManager _tenpayCertificateManager; + private readonly HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory; - public WxpayCertificateRefreshingBackgroundService( + public TenpayCertificateRefreshingBackgroundService( ILoggerFactory loggerFactory, - IOptions wxpayOptions, - CertificateManager certificateManager, - HttpClients.IWechatTenpayHttpClientFactory wechatTenpayHttpClientFactory) + IOptions tenpayOptions, + CertificateManager tenpayCertificateManager, + HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory) { _logger = loggerFactory.CreateLogger(GetType()); - _wxpayOptions = wxpayOptions.Value; - _certificateManager = certificateManager; - _wechatTenpayHttpClientFactory = wechatTenpayHttpClientFactory; + _tenpayOptions = tenpayOptions.Value; + _tenpayCertificateManager = tenpayCertificateManager; + _tenpayHttpClientFactory = tenpayHttpClientFactory; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { - var wxpayMerchant = _wxpayOptions.Merchants.FirstOrDefault(); + var wxpayMerchant = _tenpayOptions.Merchants.FirstOrDefault(); if (wxpayMerchant == null) { _logger.LogWarning("未找到微信商户配置项。"); @@ -42,7 +42,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundSe try { - var client = _wechatTenpayHttpClientFactory.Create(wxpayMerchant.MerchantId); + var client = _tenpayHttpClientFactory.Create(wxpayMerchant.MerchantId); var request = new QueryCertificatesRequest(); var response = await client.ExecuteQueryCertificatesAsync(request, cancellationToken: stoppingToken); if (response.IsSuccessful()) @@ -52,7 +52,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundSe foreach (var certificateModel in response.CertificateList) { - _certificateManager.AddEntry(new CertificateEntry(certificateModel)); + _tenpayCertificateManager.AddEntry(new CertificateEntry(certificateModel)); } _logger.LogInformation("刷新微信商户平台证书成功。"); diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs index 0bc0be54..b7b366bc 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Services/HttpClients/Implements/WechatTenpayHttpClientFactory.cs @@ -12,12 +12,12 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients. partial class WechatTenpayHttpClientFactory : IWechatTenpayHttpClientFactory { private readonly System.Net.Http.IHttpClientFactory _httpClientFactory; - private readonly Options.WxpayOptions _wechatOptions; + private readonly Options.TenpayOptions _wechatOptions; private readonly CertificateManager _certificateManager; public WechatTenpayHttpClientFactory( System.Net.Http.IHttpClientFactory httpClientFactory, - IOptions wechatOptions, + IOptions wechatOptions, CertificateManager certificateManager) { _httpClientFactory = httpClientFactory; diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs index 020b89fc..789e278f 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/Startup.cs @@ -27,7 +27,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5 // ע services.AddOptions(); - services.Configure(Configuration.GetSection(nameof(Options.WxpayOptions))); + services.Configure(Configuration.GetSection(nameof(Options.TenpayOptions))); // ע빤 HTTP ͻ services.AddHttpClient(); @@ -35,7 +35,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5 services.AddSingleton(); // ע̨ - services.AddHostedService(); + services.AddHostedService(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json index f4d7cdd3..320d2d15 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5/appsettings.json @@ -9,12 +9,15 @@ "AllowedHosts": "*", - "WxpayOptions": { + "TenpayOptions": { "Merchants": [ { - "MerchantId": "д̻" + "MerchantId": "д̻", + "SecretV3": "д V3 API Կ", + "CertSerialNumber": "д̻֤к", + "CertPrivateKey": "д̻֤ļ" } ], - "CallbackEntry": "https://localhost:5001" + "NotifyUrl": "https://localhost:5001" } }