mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-18 17:48:12 +08:00
chore(tenpayv3): 新增基于 .NET Framework 4.7 的示例项目
This commit is contained in:
@@ -4,10 +4,10 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("notify")]
|
||||
[Route("api/notify")]
|
||||
public class TenpayNotifyController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
@@ -31,9 +31,6 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
[FromHeader(Name = "Wechatpay-Signature")] string signature,
|
||||
[FromHeader(Name = "Wechatpay-Serial")] string serialNumber)
|
||||
{
|
||||
// 接收服务器推送
|
||||
// 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_2.shtml
|
||||
|
||||
using var reader = new StreamReader(Request.Body, Encoding.UTF8);
|
||||
string content = await reader.ReadToEndAsync();
|
||||
_logger.LogInformation("接收到微信支付推送的数据:{0}", content);
|
||||
@@ -48,8 +45,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
);
|
||||
if (!valid)
|
||||
{
|
||||
// 注意:需提前注入 CertificateManager、并添加平台证书,才可以使用扩展方法执行验签操作
|
||||
// 有关 CertificateManager 的用法请参阅《开发文档 / 高级技巧 / 如何验证回调通知事件签名?》
|
||||
// NOTICE:
|
||||
// 需提前注入 CertificateManager、并添加平台证书,才可以使用扩展方法执行验签操作。
|
||||
// 有关 CertificateManager 的用法请参阅《开发文档 / 高级技巧 / 如何验证回调通知事件签名?》。
|
||||
// 后续如何解密并反序列化,请参阅《开发文档 / 高级技巧 / 如何解密回调通知事件中的敏感数据?》。
|
||||
|
||||
return new JsonResult(new { code = "FAIL", message = "验签失败" });
|
||||
}
|
||||
|
||||
|
@@ -1,19 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
[ApiController]
|
||||
[Route("order")]
|
||||
[Route("api/order")]
|
||||
public class TenpayOrderController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
@@ -34,9 +31,6 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
[Route("jsapi")]
|
||||
public async Task<IActionResult> CreateOrderByJsapi([FromBody] Models.CreateOrderByJsapiRequest requestModel)
|
||||
{
|
||||
// JSAPI 下单
|
||||
// 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
|
||||
|
||||
var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId);
|
||||
var request = new CreatePayTransactionJsapiRequest()
|
||||
{
|
||||
|
@@ -1,19 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
[ApiController]
|
||||
[Route("refund")]
|
||||
[Route("api/refund")]
|
||||
public class TenpayRefundController : ControllerBase
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
@@ -34,9 +31,6 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Controllers
|
||||
[Route("")]
|
||||
public async Task<IActionResult> CreateRefund([FromBody] Models.CreateRefundRequest requestModel)
|
||||
{
|
||||
// 申请退款
|
||||
// 文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml
|
||||
|
||||
var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId);
|
||||
var request = new CreateRefundDomesticRefundRequest()
|
||||
{
|
||||
|
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Models
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Models
|
||||
{
|
||||
public class CreateOrderByJsapiRequest
|
||||
{
|
||||
@@ -13,7 +8,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Models
|
||||
|
||||
public string OpenId { get; set; } = default!;
|
||||
|
||||
// NOTICE: 单机演示时金额来源于客户端请求,生产项目请替换成服务端计算生成
|
||||
// NOTICE:
|
||||
// 单机演示时金额来源于客户端请求,生产项目请改为服务端计算生成,切勿依赖客户端提供的金额结果。
|
||||
public int Amount { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Models
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Models
|
||||
{
|
||||
public class CreateRefundRequest
|
||||
{
|
||||
@@ -11,7 +6,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Models
|
||||
|
||||
public string TransactionId { get; set; } = default!;
|
||||
|
||||
// NOTICE: 单机演示时金额来源于客户端请求,生产项目请替换成服务端获取生成
|
||||
// NOTICE:
|
||||
// 单机演示时金额来源于客户端请求,生产项目请改为服务端计算生成,切勿依赖客户端提供的金额结果。
|
||||
public int OrderAmount { get; set; }
|
||||
|
||||
public int RefundAmount { get; set; }
|
||||
|
@@ -4,28 +4,31 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Options
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Options
|
||||
{
|
||||
public partial class TenpayOptions : IOptions<TenpayOptions>
|
||||
{
|
||||
TenpayOptions IOptions<TenpayOptions>.Value => this;
|
||||
|
||||
public WechatMerchant[] Merchants { get; set; } = Array.Empty<WechatMerchant>();
|
||||
public Types.WechatMerchant[] Merchants { get; set; } = Array.Empty<Types.WechatMerchant>();
|
||||
|
||||
public string NotifyUrl { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
partial class TenpayOptions
|
||||
{
|
||||
public class WechatMerchant
|
||||
{
|
||||
public string MerchantId { get; set; } = string.Empty;
|
||||
public static class Types
|
||||
{
|
||||
public class WechatMerchant
|
||||
{
|
||||
public string MerchantId { get; set; } = string.Empty;
|
||||
|
||||
public string SecretV3 { get; set; } = string.Empty;
|
||||
public string SecretV3 { get; set; } = string.Empty;
|
||||
|
||||
public string CertSerialNumber { get; set; } = string.Empty;
|
||||
public string CertSerialNumber { get; set; } = string.Empty;
|
||||
|
||||
public string CertPrivateKey { get; set; } = string.Empty;
|
||||
public string CertPrivateKey { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,9 @@
|
||||
using System.Text;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
@@ -11,6 +12,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
|
||||
.ConfigureWebHostDefaults(builder =>
|
||||
{
|
||||
builder.UseStartup<Startup>();
|
||||
|
@@ -1,11 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<NullableReferenceTypes>true</NullableReferenceTypes>
|
||||
<RootNamespace>SKIT.FlurlHttpClient.Wechat.Api.Sample</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="6.3.0" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\SKIT.FlurlHttpClient.Wechat.TenpayV3\SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj" />
|
||||
</ItemGroup>
|
||||
|
@@ -5,11 +5,12 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundServices
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.BackgroundServices
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
class TenpayCertificateRefreshingBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
@@ -30,42 +31,39 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundSe
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var wxpayMerchant = _tenpayOptions.Merchants.FirstOrDefault();
|
||||
if (wxpayMerchant == null)
|
||||
foreach (var tenpayMerchantOptions in _tenpayOptions.Merchants)
|
||||
{
|
||||
_logger.LogWarning("未找到微信商户配置项。");
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var client = _tenpayHttpClientFactory.Create(wxpayMerchant.MerchantId);
|
||||
var request = new QueryCertificatesRequest();
|
||||
var response = await client.ExecuteQueryCertificatesAsync(request, cancellationToken: stoppingToken);
|
||||
if (response.IsSuccessful())
|
||||
try
|
||||
{
|
||||
// 注意:如果启用了 AutoDecryptResponseSensitiveProperty,则无需再手动执行下面被注释的解密方法
|
||||
// response = client.DecryptResponseSensitiveProperty(response);
|
||||
|
||||
foreach (var certificateModel in response.CertificateList)
|
||||
var client = _tenpayHttpClientFactory.Create(tenpayMerchantOptions.MerchantId);
|
||||
var request = new QueryCertificatesRequest();
|
||||
var response = await client.ExecuteQueryCertificatesAsync(request, cancellationToken: stoppingToken);
|
||||
if (response.IsSuccessful())
|
||||
{
|
||||
client.CertificateManager.AddEntry(new CertificateEntry(certificateModel));
|
||||
}
|
||||
// NOTICE:
|
||||
// 如果启用了 `AutoDecryptResponseSensitiveProperty` 配置项,则无需再手动执行下面被注释的解密方法:
|
||||
// response = client.DecryptResponseSensitiveProperty(response);
|
||||
|
||||
_logger.LogInformation("刷新微信商户平台证书成功。");
|
||||
foreach (var certificateModel in response.CertificateList)
|
||||
{
|
||||
client.CertificateManager.AddEntry(new CertificateEntry(certificateModel));
|
||||
}
|
||||
|
||||
_logger.LogInformation("刷新微信商户平台证书成功。");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"刷新微信商户平台证书失败(状态码:{0},错误代码:{1},错误描述:{2})。",
|
||||
response.RawStatus, response.ErrorCode, response.ErrorMessage
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"刷新微信商户平台证书失败(状态码:{0},错误代码:{1},错误描述:{2})。",
|
||||
response.RawStatus, response.ErrorCode, response.ErrorMessage
|
||||
);
|
||||
_logger.LogError(ex, "刷新微信商户平台证书遇到异常。");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "刷新微信商户平台证书遇到异常。");
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromDays(1)); // 每隔 1 天轮询刷新
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients
|
||||
{
|
||||
public interface IWechatTenpayHttpClientFactory
|
||||
{
|
||||
|
@@ -1,6 +1,6 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients.Implements
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Implements
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
@@ -15,7 +15,9 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients.
|
||||
|
||||
public CertificateManager Create(string merchantId)
|
||||
{
|
||||
// 注意:这里的工厂方法是为了演示多租户而存在的;如果你的项目只存在唯一一个租户,那么直接注入 `CertificateManager` 就可以
|
||||
// NOTICE:
|
||||
// 这里的工厂方法是为了演示多租户而存在的,可根据商户号生成不同的证书管理器。
|
||||
// 如果你的项目只存在唯一一个租户,那么直接注入 `CertificateManager` 即可。
|
||||
|
||||
return _dict.GetOrAdd(merchantId, new InMemoryCertificateManager());
|
||||
}
|
||||
|
@@ -5,10 +5,8 @@ using Flurl.Http;
|
||||
using Flurl.Http.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients.Implements
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Implements
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
||||
|
||||
partial class WechatTenpayHttpClientFactory : IWechatTenpayHttpClientFactory
|
||||
{
|
||||
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
|
||||
@@ -29,7 +27,9 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients.
|
||||
|
||||
public WechatTenpayClient Create(string merchantId)
|
||||
{
|
||||
// 注意:这里的工厂方法是为了演示多租户而存在的;如果你的项目只存在唯一一个租户,那么直接注入 `WechatTenpayClient` 就可以
|
||||
// NOTICE:
|
||||
// 这里的工厂方法是为了演示多租户而存在的,可根据商户号生成不同的 API 客户端。
|
||||
// 如果你的项目只存在唯一一个租户,那么直接注入 `WechatTenpayClient` 即可。
|
||||
|
||||
var tenpayMerchantOptions = _tenpayOptions.Merchants?.FirstOrDefault(e => string.Equals(merchantId, e.MerchantId));
|
||||
if (tenpayMerchantOptions == null)
|
||||
|
@@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
@@ -18,7 +18,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5
|
||||
{
|
||||
services.AddControllers();
|
||||
|
||||
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD> `appsettings.json` <20>ļ<EFBFBD><C4BC><EFBFBD>
|
||||
services.AddOptions();
|
||||
services.Configure<Options.TenpayOptions>(Configuration.GetSection(nameof(Options.TenpayOptions)));
|
||||
|
||||
|
@@ -13,7 +13,7 @@
|
||||
"Merchants": [
|
||||
{
|
||||
"MerchantId": "<22><>д<EFBFBD>̻<EFBFBD><CCBB><EFBFBD>",
|
||||
"SecretV3": "<22><>д V3 API <20><>Կ",
|
||||
"SecretV3": "<22><>д<EFBFBD>̻<EFBFBD> V3 API <20><>Կ",
|
||||
"CertSerialNumber": "<22><>д<EFBFBD>̻<EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>",
|
||||
"CertPrivateKey": "<22><>д<EFBFBD>̻<EFBFBD>֤<EFBFBD><D6A4><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>"
|
||||
}
|
||||
|
Reference in New Issue
Block a user