chore(tenpayv3): 新增基于 .NET Framework 4.7 的示例项目

This commit is contained in:
Fu Diwei 2021-12-28 14:05:39 +08:00
parent b2ce287136
commit 2dcfcab262
58 changed files with 1114 additions and 204 deletions

View File

@ -42,6 +42,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6", "samples\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6.csproj", "{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47", "samples\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47\SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47.csproj", "{7667F0D3-B41D-43C2-B69D-A68FE230EBF7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -100,6 +102,10 @@ Global
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705}.Release|Any CPU.Build.0 = Release|Any CPU
{7667F0D3-B41D-43C2-B69D-A68FE230EBF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7667F0D3-B41D-43C2-B69D-A68FE230EBF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7667F0D3-B41D-43C2-B69D-A68FE230EBF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7667F0D3-B41D-43C2-B69D-A68FE230EBF7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -119,6 +125,7 @@ Global
{9689135B-44BA-4D55-8663-7C669BAFE066} = {C95AF531-CF44-44AA-AC90-F4DF9F941674}
{D1B321C9-3004-4645-A78D-A85C152062FA} = {35C901ED-C234-4A91-9561-AD89B3BB788D}
{65E51735-73CE-4E9B-AA65-4BF5E4C8A705} = {35C901ED-C234-4A91-9561-AD89B3BB788D}
{7667F0D3-B41D-43C2-B69D-A68FE230EBF7} = {35C901ED-C234-4A91-9561-AD89B3BB788D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F08ED64E-2517-4B51-A4BE-D33D56CC7B39}

View File

@ -2,11 +2,13 @@
---
示例项目位于 _samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net6_。
示例项目基于 .NET 6.0 实现,位于 _samples/SKIT.FlurlHttpClient.Wechat.Api.Sample_Net6_。
示例项目基于 .NET 6.0 实现,依赖以下第三方库:
示例项目依赖以下第三方库:
- [`DistributedLock`](https://github.com/madelson/DistributedLock):分布式锁。
- [`Autofac`](https://github.com/autofac/Autofac):依赖注入容器;
- [`DistributedLock`](https://github.com/madelson/DistributedLock):分布式锁;
- [`NMemory`](https://github.com/zzzprojects/nmemory):内存数据库。
@ -22,19 +24,7 @@
---
### 关于 AccessToken 的刷新机制:
示例项目遵循微信官方建议的“使用中控服务器统一获取和刷新”原则,实现了 AccessToken 的主动刷新机制,开发者不需要、也不应该在业务代码中手动执行刷新操作。
所谓“主动刷新机制”,即由系统在后台周期性地执行刷新操作,在需要使用 AccessToken 时直接读取已有的记录即可,无需关心 AccessToken 是否过期。
与之相对应的是“被动刷新机制”,即在每次读取时先判断已有的记录是否过期,如果未过期则直接返回,如果过期则执行一次刷新操作后再返回。
开发者可根据业务需要自行实现被动刷新机制,也可以二者相结合。
---
### 【重要】使用须知:
### 【重要】使用前须知:
示例项目仅作为业务上的参考,不代表可直接用于生产。
@ -61,3 +51,15 @@
#### 4. 安全性:
示例项目中不包含授权认证等相关的业务逻辑,开发者可根据业务需要自行实现。
---
### 【附录】关于 AccessToken 的刷新机制:
示例项目遵循微信官方建议的“使用中控服务器统一获取和刷新”原则,实现了 AccessToken 的主动刷新机制,开发者不需要、也不应该在业务代码中手动执行刷新操作。
所谓“主动刷新机制”,即由系统在后台周期性地执行刷新操作,在需要使用 AccessToken 时直接读取已有的记录即可,无需关心 AccessToken 是否过期。
与之相对应的是“被动刷新机制”,即在每次读取时先判断已有的记录是否过期,如果未过期则直接返回,如果过期则执行一次刷新操作后再返回。
开发者可根据业务需要自行实现被动刷新机制,也可以二者相结合。

View File

@ -16,7 +16,7 @@
---
### 解密流程
对于回调通知事件的敏感信息,微信商户平台使用了商户公钥基于 RSA 算法加密。

View File

@ -2,15 +2,19 @@
---
示例项目位于 _samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6_。
示例项目基于 .NET Framework 4.7 和 .NET 6.0 实现,分别位于 _samples/SKIT.FlurlHttpClient.Wechat.Sample_NetFramework47_ 和 _samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6_。
示例项目基于 .NET 6.0 实现。
示例项目依赖以下第三方库:
- [`Autofac`](https://github.com/autofac/Autofac):依赖注入容器。
- [`Hangfire`](https://github.com/HangfireIO/Hangfire):后台作业调度框架。
示例项目实现了以下功能:
- 多租户商户号;
- 微信商户平台证书自动更新
- 微信商户平台证书自动更新
- 验证并接收微信服务器推送数据;
@ -18,7 +22,7 @@
---
### 【重要】使用须知:
### 【重要】使用须知:
示例项目仅作为业务上的参考,不代表可直接用于生产。

View File

@ -1,32 +1,25 @@
using System;
using System.IO;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.Api.Events;
[ApiController]
[Route("notify")]
[Route("api/notify")]
public class WechatNotifyController : ControllerBase
{
private readonly ILogger _logger;
private readonly Options.WechatOptions _wechatOptions;
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
public WechatNotifyController(
ILoggerFactory loggerFactory,
IOptions<Options.WechatOptions> wechatOptions,
Services.HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory)
{
_logger = loggerFactory.CreateLogger(GetType());
_wechatOptions = wechatOptions.Value;
_wechatApiHttpClientFactory = wechatApiHttpClientFactory;
}

View File

@ -1,25 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
[ApiController]
[Route("user")]
[Route("api/user")]
public class WechatUserController : ControllerBase
{
private readonly ILogger _logger;
private readonly Services.Repositories.IWechatAccessTokenEntityRepository _wechatAccessTokenEntityRepository;
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
public WechatUserController(
@ -38,9 +32,6 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
[FromQuery(Name = "app_id")] string appId,
[FromQuery(Name = "open_id")] string openId)
{
// 获取用户基本信息
// 文档https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId
var entity = _wechatAccessTokenEntityRepository.FirstOrDefault(e => e.AppId == appId);
var client = _wechatApiHttpClientFactory.Create(appId);
var request = new CgibinUserInfoRequest() { AccessToken = entity?.AccessToken, OpenId = openId };

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Models
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Models
{
public class WechatAccessTokenEntity
{

View File

@ -1,16 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Options
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Options
{
public partial class WechatOptions : IOptions<WechatOptions>
{
WechatOptions IOptions<WechatOptions>.Value => this;
public WechatAccount[] Accounts { get; set; } = Array.Empty<WechatAccount>();
public Types.WechatAccount[] Accounts { get; set; } = Array.Empty<Types.WechatAccount>();
public string CallbackState { get; set; } = string.Empty;
@ -19,13 +16,16 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Options
partial class WechatOptions
{
public class WechatAccount
{
public string? GhId { get; set; }
public static class Types
{
public class WechatAccount
{
public string? GhId { get; set; }
public string AppId { get; set; } = string.Empty;
public string AppId { get; set; } = string.Empty;
public string AppSecret { get; set; } = string.Empty;
public string AppSecret { get; set; } = string.Empty;
}
}
}
}

View File

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample
{
public class Program
{
@ -15,6 +12,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();

View File

@ -4,11 +4,14 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>
<RootNamespace>SKIT.FlurlHttpClient.Wechat.Api.Sample</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DistributedLock.Core" Version="1.0.2" />
<PackageReference Include="DistributedLock.FileSystem" Version="1.0.0" />
<PackageReference Include="Autofac" Version="6.3.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
<PackageReference Include="DistributedLock.Core" Version="1.0.4" />
<PackageReference Include="DistributedLock.FileSystem" Version="1.0.1" />
<PackageReference Include="NMemory" Version="3.1.3" />
</ItemGroup>

View File

@ -6,11 +6,12 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.BackgroundServices
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.BackgroundServices
{
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
class WechatAccessTokenRefreshingBackgroundService : BackgroundService
{
private readonly ILogger _logger;

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Medallion.Threading;
using Medallion.Threading;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.DistributedLock
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.DistributedLock
{
public interface IDistributedLockFactory
{

View File

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Medallion.Threading;
using Medallion.Threading.FileSystem;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.DistributedLock.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.DistributedLock.Implements
{
class DistributedLockFactory : IDistributedLockFactory
{
@ -14,7 +11,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.DistributedLock.I
public IDistributedLock Create(string lockName)
{
// NOTICE: 单机演示基于文件实现分布式锁,生产项目请替换成其他实现
// NOTICE:
// 单机演示基于文件实现分布式锁,生产项目请替换成其他实现。
return new FileDistributedLock(_lockFileDirectory, lockName);
}
}

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.HttpClients
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients
{
public interface IWechatApiHttpClientFactory
{

View File

@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
using Flurl.Http.Configuration;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.HttpClients.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements
{
partial class WechatApiHttpClientFactory : IWechatApiHttpClientFactory
{
@ -26,6 +24,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.HttpClients.Imple
public WechatApiClient Create(string appId)
{
// NOTICE:
// 这里的工厂方法是为了演示多租户而存在的,可根据 AppId 生成不同的 API 客户端。
// 如果你的项目只存在唯一一个租户,那么直接注入 `WechatApiClient` 即可。
var wechatAccountOptions = _wechatOptions.Accounts?.FirstOrDefault(e => string.Equals(appId, e.AppId));
if (wechatAccountOptions == null)
throw new Exception("未在配置项中找到该 AppId 对应的微信账号。");

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.Repositories
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.Repositories
{
public interface IWechatAccessTokenEntityRepository : IEnumerable<Models.WechatAccessTokenEntity>
{

View File

@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NMemory;
using NMemory;
using NMemory.Tables;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.Repositories.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.Repositories.Implements
{
internal class GlobalDatabase
{

View File

@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.Repositories.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.Repositories.Implements
{
public class WechatAccessTokenEntityRepository : IWechatAccessTokenEntityRepository
{

View File

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample
{
public class Startup
{
@ -23,7 +23,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
{
services.AddControllers();
// ×¢ÈëÅäÖÃÏî
// 注入配置项(内容见 `appsettings.json` 文件)
services.AddOptions();
services.Configure<Options.WechatOptions>(Configuration.GetSection(nameof(Options.WechatOptions)));

View File

@ -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 = "验签失败" });
}

View File

@ -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()
{

View File

@ -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()
{

View File

@ -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; }
}
}

View File

@ -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; }

View File

@ -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;
}
}
}
}

View File

@ -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>();

View File

@ -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>

View File

@ -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 天轮询刷新
}

View File

@ -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;

View File

@ -1,4 +1,4 @@
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.HttpClients
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients
{
public interface IWechatTenpayHttpClientFactory
{

View File

@ -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());
}

View File

@ -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)

View File

@ -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();
// ×¢ÈëÅäÖÃÏî
// 注入配置项(内容见 `appsettings.json` 文件)
services.AddOptions();
services.Configure<Options.TenpayOptions>(Configuration.GetSection(nameof(Options.TenpayOptions)));

View File

@ -13,7 +13,7 @@
"Merchants": [
{
"MerchantId": "填写商户号",
"SecretV3": "ĚîĐ´ V3 API ĂÜÔż",
"SecretV3": "填写商户 V3 API 密钥",
"CertSerialNumber": "填写商户证书序列号",
"CertPrivateKey": "填写商户证书文件内容"
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using Hangfire;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
{
public class AutofacInitializer
{
public static void Init()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().AsSelf();
builder.RegisterType<Services.HttpClients.Implements.WechatTenpayCertificateManagerFactory>()
.As<Services.HttpClients.IWechatTenpayCertificateManagerFactory>()
.SingleInstance();
builder.RegisterType<Services.HttpClients.Implements.WechatTenpayHttpClientFactory>()
.As<Services.HttpClients.IWechatTenpayHttpClientFactory>()
.SingleInstance();
builder.RegisterType<Services.BackgroundJobs.TenpayCertificateRefreshingBackgroundJob>()
.AsSelf()
.InstancePerBackgroundJob();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
}

View File

@ -0,0 +1,12 @@
using System.Web.Mvc;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
{
public class FilterRegistration
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using Hangfire;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
{
public class HangfireInitializer
{
public static BackgroundJobServer Init()
{
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseInMemoryStorage();
var server = new BackgroundJobServer();
HangfireAspNet.Use(() => new BackgroundJobServer[] { server });
BackgroundJob.Enqueue<Services.BackgroundJobs.TenpayCertificateRefreshingBackgroundJob>(job => job.ExecuteAsync());
RecurringJob.AddOrUpdate<Services.BackgroundJobs.TenpayCertificateRefreshingBackgroundJob>(job => job.ExecuteAsync(), Cron.Daily);
return server;
}
}
}

View File

@ -0,0 +1,13 @@
using System.Web.Mvc;
using System.Web.Routing;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
{
public class RouteRegistration
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
}
}
}

View File

@ -0,0 +1,18 @@
using System.Web.Http;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
{
public static class WebApiConfiguration
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "{controller}/{action}",
defaults: new { }
);
}
}
}

View File

@ -0,0 +1,61 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
[RoutePrefix("api/notify")]
public class TenpayNotifyController : ApiController
{
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
public TenpayNotifyController(
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
{
_tenpayHttpClientFactory = tenpayHttpClientFactory;
}
[HttpPost]
[Route("m-{merchant_id}/message-push")]
public async Task<IHttpActionResult> ReceiveMessage([FromUri(Name = "merchant_id")] string merchantId, CancellationToken cancellationToken)
{
using (var stream = await Request.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
string timestamp = Request.Headers.TryGetValues("Wechatpay-Timestamp", out _) ? Request.Headers.GetValues("Wechatpay-Timestamp").First() : null;
string nonce = Request.Headers.TryGetValues("Wechatpay-Nonce", out _) ? Request.Headers.GetValues("Wechatpay-Nonce").First() : null;
string signature = Request.Headers.TryGetValues("Wechatpay-Signature", out _) ? Request.Headers.GetValues("Wechatpay-Signature").First() : null;
string serialNumber = Request.Headers.TryGetValues("Wechatpay-Serial", out _) ? Request.Headers.GetValues("Wechatpay-Serial").First() : null;
string content = await reader.ReadToEndAsync();
Debug.WriteLine("接收到微信支付推送的数据:{0}", content);
var client = _tenpayHttpClientFactory.Create(merchantId);
bool valid = client.VerifyEventSignature(
callbackTimestamp: timestamp,
callbackNonce: nonce,
callbackBody: content,
callbackSignature: signature,
callbackSerialNumber: serialNumber
);
if (!valid)
{
// NOTICE:
// 需提前注入 CertificateManager、并添加平台证书才可以使用扩展方法执行验签操作。
// 有关 CertificateManager 的用法请参阅《开发文档 / 高级技巧 / 如何验证回调通知事件签名?》。
// 后续如何解密并反序列化,请参阅《开发文档 / 高级技巧 / 如何解密回调通知事件中的敏感数据?》。
return Json(new { code = "FAIL", message = "验签失败" });
}
return Json(new { code = "SUCCESS", message = "成功" });
}
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
[RoutePrefix("api/order")]
public class TenpayOrderController : ApiController
{
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
public TenpayOrderController(
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
{
_tenpayHttpClientFactory = tenpayHttpClientFactory;
}
[HttpPost]
[Route("jsapi")]
public async Task<IHttpActionResult> CreateOrderByJsapi([FromBody] Models.CreateOrderByJsapiRequest requestModel, CancellationToken cancellationToken)
{
if (requestModel == null)
return BadRequest();
var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId);
var request = new CreatePayTransactionJsapiRequest()
{
OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"),
AppId = requestModel.AppId,
Description = "演示订单",
NotifyUrl = Options.TenpayOptions.Instance.Value.NotifyUrl,
Amount = new CreatePayTransactionJsapiRequest.Types.Amount() { Total = requestModel.Amount },
Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { OpenId = requestModel.OpenId }
};
var response = await client.ExecuteCreatePayTransactionJsapiAsync(request, cancellationToken);
if (!response.IsSuccessful())
{
Debug.WriteLine(
"JSAPI 下单失败(状态码:{0},错误代码:{1},错误描述:{2})。",
response.RawStatus, response.ErrorCode, response.ErrorMessage
);
}
return Json(response);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
[RoutePrefix("api/refund")]
public class TenpayRefundController : ApiController
{
private readonly Services.HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
public TenpayRefundController(
Services.HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
{
_tenpayHttpClientFactory = tenpayHttpClientFactory;
}
[HttpPost]
[Route("")]
public async Task<IHttpActionResult> CreateRefund([FromBody] Models.CreateRefundRequest requestModel, CancellationToken cancellationToken)
{
if (requestModel == null)
return BadRequest();
var client = _tenpayHttpClientFactory.Create(requestModel.MerchantId);
var request = new CreateRefundDomesticRefundRequest()
{
TransactionId = requestModel.TransactionId,
OutRefundNumber = "SAMPLE_ORN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"),
Amount = new CreateRefundDomesticRefundRequest.Types.Amount()
{
Total = requestModel.OrderAmount,
Refund = requestModel.RefundAmount
},
Reason = "示例退款",
NotifyUrl = Options.TenpayOptions.Instance.Value.NotifyUrl
};
var response = await client.ExecuteCreateRefundDomesticRefundAsync(request, cancellationToken);
if (!response.IsSuccessful())
{
Debug.WriteLine(
"申请退款失败(状态码:{0},错误代码:{1},错误描述:{2})。",
response.RawStatus, response.ErrorCode, response.ErrorMessage
);
}
return Json(response);
}
}
}

View File

@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.WebApiApplication" Language="C#" %>

View File

@ -0,0 +1,38 @@
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample
{
public class WebApiApplication : HttpApplication
{
private Hangfire.BackgroundJobServer _backgroundJobServer;
protected void Application_Start()
{
// 注册 WebAPI 区域
AreaRegistration.RegisterAllAreas();
// 注册 WebAPI 配置
GlobalConfiguration.Configure(WebApiConfiguration.Register);
// 注册全局过滤器
FilterRegistration.RegisterGlobalFilters(GlobalFilters.Filters);
// 注册路由表
RouteRegistration.RegisterRoutes(RouteTable.Routes);
// 初始化 Autofac 依赖注入容器
AutofacInitializer.Init();
// 初始化 Hangfire 定时任务
_backgroundJobServer = HangfireInitializer.Init();
}
protected void Application_End()
{
_backgroundJobServer?.Dispose();
}
}
}

View File

@ -0,0 +1,15 @@
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Models
{
public class CreateOrderByJsapiRequest
{
public string MerchantId { get; set; }
public string AppId { get; set; }
public string OpenId { get; set; }
// NOTICE:
// 单机演示时金额来源于客户端请求,生产项目请改为服务端计算生成,切勿依赖客户端提供的金额结果。
public int Amount { get; set; }
}
}

View File

@ -0,0 +1,15 @@
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Models
{
public class CreateRefundRequest
{
public string MerchantId { get; set; }
public string TransactionId { get; set; }
// NOTICE:
// 单机演示时金额来源于客户端请求,生产项目请改为服务端计算生成,切勿依赖客户端提供的金额结果。
public int OrderAmount { get; set; }
public int RefundAmount { get; set; }
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Configuration;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Options
{
public partial class TenpayOptions
{
public static readonly Lazy<TenpayOptions> Instance = new Lazy<TenpayOptions>(() =>
{
var configMerchantRegex = new Regex("^TenpayOptions_Merchant_(\\d+)_MerchantId$");
var configMerchantIndexes = WebConfigurationManager.AppSettings.AllKeys
.Where(key => configMerchantRegex.IsMatch(key))
.Select(key => configMerchantRegex.Matches(key)[0].Groups[1].Value)
.ToArray();
return new TenpayOptions()
{
Merchants = configMerchantIndexes
.Select(i => new Types.WechatMerchant()
{
MerchantId = WebConfigurationManager.AppSettings[$"TenpayOptions_Merchant_{i}_MerchantId"],
SecretV3 = WebConfigurationManager.AppSettings[$"TenpayOptions_Merchant_{i}_SecretV3"],
CertSerialNumber = WebConfigurationManager.AppSettings[$"TenpayOptions_Merchant_{i}_CertSerialNumber"],
CertPrivateKey = WebConfigurationManager.AppSettings[$"TenpayOptions_Merchant_{i}_CertPrivateKey"],
})
.ToArray(),
NotifyUrl = WebConfigurationManager.AppSettings[$"TenpayOptions_NotifyUrl"]
};
}, isThreadSafe: true);
}
partial class TenpayOptions
{
public Types.WechatMerchant[] Merchants { get; set; } = Array.Empty<Types.WechatMerchant>();
public string NotifyUrl { get; set; } = string.Empty;
}
partial class TenpayOptions
{
public static class Types
{
public class WechatMerchant
{
public string MerchantId { get; set; } = string.Empty;
public string SecretV3 { get; set; } = string.Empty;
public string CertSerialNumber { get; set; } = string.Empty;
public string CertPrivateKey { get; set; } = string.Empty;
}
}
}
}

View File

@ -0,0 +1,18 @@
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("41c93671-99b5-44ad-b0d4-6e2d8f5688ec")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,252 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{7667F0D3-B41D-43C2-B69D-A68FE230EBF7}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample</RootNamespace>
<AssemblyName>SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress>
</Use64BitIISExpress>
<IISExpressSSLPort>
</IISExpressSSLPort>
<IISExpressAnonymousAuthentication>
</IISExpressAnonymousAuthentication>
<IISExpressWindowsAuthentication>
</IISExpressWindowsAuthentication>
<IISExpressUseClassicPipelineMode>
</IISExpressUseClassicPipelineMode>
<UseGlobalApplicationHostFile>
</UseGlobalApplicationHostFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Core" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Diagnostics.DiagnosticSource.6.0.0\lib\net461\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Text.Encodings.Web, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Text.Encodings.Web.6.0.0\lib\net461\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Abstractions" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.Helpers.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Core.5.2.7\lib\net45\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http.WebHost, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.7\lib\net45\System.Web.Http.WebHost.dll</HintPath>
</Reference>
<Reference Include="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\..\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.Razor.3.2.7\lib\net45\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.Routing" />
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Deployment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Autofac, Version=6.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\packages\Autofac.6.3.0\lib\netstandard2.0\Autofac.dll</HintPath>
</Reference>
<Reference Include="Autofac.Integration.Mvc, Version=6.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\packages\Autofac.Mvc5.6.0.0\lib\net472\Autofac.Integration.Mvc.dll</HintPath>
</Reference>
<Reference Include="Autofac.Integration.WebApi, Version=6.1.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\packages\Autofac.WebApi2.6.1.0\lib\net472\Autofac.Integration.WebApi.dll</HintPath>
</Reference>
<Reference Include="Hangfire.AspNet, Version=0.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Hangfire.AspNet.0.2.0\lib\net45\Hangfire.AspNet.dll</HintPath>
</Reference>
<Reference Include="Hangfire.Autofac, Version=2.3.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Hangfire.Autofac.2.3.1\lib\net45\Hangfire.Autofac.dll</HintPath>
</Reference>
<Reference Include="Hangfire.Core, Version=1.7.28.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Hangfire.Core.1.7.28\lib\net46\Hangfire.Core.dll</HintPath>
</Reference>
<Reference Include="Hangfire.InMemory, Version=0.3.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Hangfire.InMemory.0.3.4\lib\net45\Hangfire.InMemory.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Html.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNetCore.Html.Abstractions.2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Html.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Razor, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNetCore.Razor.2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Razor.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform">
<HintPath>..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Owin, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.3.1.0\lib\net45\Microsoft.Owin.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Host.SystemWeb, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Owin.Host.SystemWeb.3.1.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebHelpers.3.2.7\lib\net45\Microsoft.Web.Helpers.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<HintPath>..\..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
</Reference>
<Reference Include="SKIT.FlurlHttpClient.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Reference Include="WebMatrix.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.Data.3.2.7\lib\net45\WebMatrix.Data.dll</HintPath>
</Reference>
<Reference Include="WebMatrix.WebData, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebPages.WebData.3.2.7\lib\net45\WebMatrix.WebData.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\HangfireInitializer.cs" />
<Compile Include="App_Start\AutofacInitializer.cs" />
<Compile Include="App_Start\FilterRegistration.cs" />
<Compile Include="App_Start\RouteRegistration.cs" />
<Compile Include="App_Start\WebApiConfiguration.cs" />
<Compile Include="Controllers\TenpayNotifyController.cs" />
<Compile Include="Controllers\TenpayOrderController.cs" />
<Compile Include="Controllers\TenpayRefundController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\CreateOrderByJsapiRequest.cs" />
<Compile Include="Models\CreateRefundRequest.cs" />
<Compile Include="Options\TenpayOptions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\BackgroundJobs\TenpayCertificateRefreshingBackgroundJob.cs" />
<Compile Include="Services\HttpClients\Implements\WechatTenpayCertificateManagerFactory.cs" />
<Compile Include="Services\HttpClients\Implements\WechatTenpayHttpClientFactory.cs" />
<Compile Include="Services\HttpClients\IWechatTenpayCertificateManagerFactory.cs" />
<Compile Include="Services\HttpClients\IWechatTenpayHttpClientFactory.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Global.asax" />
<Content Include="Web.config" />
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
<Content Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="Packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\SKIT.FlurlHttpClient.Wechat.TenpayV3\SKIT.FlurlHttpClient.Wechat.TenpayV3.csproj">
<Project>{6fe502d4-c43d-49c9-9e57-d1ee566fd1c3}</Project>
<Name>SKIT.FlurlHttpClient.Wechat.TenpayV3</Name>
</ProjectReference>
</ItemGroup>
<Import Project="..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
</Target>
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>8080</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:8080/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>

View File

@ -0,0 +1,57 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.BackgroundJobs
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
class TenpayCertificateRefreshingBackgroundJob
{
private readonly HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
public TenpayCertificateRefreshingBackgroundJob(
HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
{
_tenpayHttpClientFactory = tenpayHttpClientFactory;
}
public async Task ExecuteAsync()
{
foreach (var tenpayMerchantOptions in Options.TenpayOptions.Instance.Value.Merchants)
{
try
{
var client = _tenpayHttpClientFactory.Create(tenpayMerchantOptions.MerchantId);
var request = new QueryCertificatesRequest();
var response = await client.ExecuteQueryCertificatesAsync(request);
if (response.IsSuccessful())
{
// NOTICE:
// 如果启用了 `AutoDecryptResponseSensitiveProperty` 配置项,则无需再手动执行下面被注释的解密方法:
// response = client.DecryptResponseSensitiveProperty(response);
foreach (var certificateModel in response.CertificateList)
{
client.CertificateManager.AddEntry(new CertificateEntry(certificateModel));
}
Debug.WriteLine("刷新微信商户平台证书成功。");
}
else
{
Debug.WriteLine(
"刷新微信商户平台证书失败(状态码:{0},错误代码:{1},错误描述:{2})。",
response.RawStatus, response.ErrorCode, response.ErrorMessage
);
}
}
catch (Exception ex)
{
Debug.WriteLine("刷新微信商户平台证书遇到异常。\r\n{0}", ex);
}
}
}
}
}

View File

@ -0,0 +1,9 @@
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
public interface IWechatTenpayCertificateManagerFactory
{
CertificateManager Create(string merchantId);
}
}

View File

@ -0,0 +1,7 @@
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients
{
public interface IWechatTenpayHttpClientFactory
{
WechatTenpayClient Create(string merchantId);
}
}

View File

@ -0,0 +1,25 @@
using System.Collections.Concurrent;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.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)
{
// NOTICE:
// 这里的工厂方法是为了演示多租户而存在的,可根据商户号生成不同的证书管理器。
// 如果你的项目只存在唯一一个租户,那么直接注入 `CertificateManager` 即可。
return _dict.GetOrAdd(merchantId, new InMemoryCertificateManager());
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Linq;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Implements
{
partial class WechatTenpayHttpClientFactory : IWechatTenpayHttpClientFactory
{
private readonly IWechatTenpayCertificateManagerFactory _tenpayCertificateManagerFactory;
public WechatTenpayHttpClientFactory(
IWechatTenpayCertificateManagerFactory tenpayCertificateManagerFactory)
{
_tenpayCertificateManagerFactory = tenpayCertificateManagerFactory;
}
public WechatTenpayClient Create(string merchantId)
{
// NOTICE:
// 这里的工厂方法是为了演示多租户而存在的,可根据商户号生成不同的 API 客户端。
// 如果你的项目只存在唯一一个租户,那么直接注入 `WechatTenpayClient` 即可。
var tenpayMerchantOptions = Options.TenpayOptions.Instance.Value.Merchants?.FirstOrDefault(e => string.Equals(merchantId, e.MerchantId));
if (tenpayMerchantOptions == null)
throw new Exception("未在配置项中找到该 MerchantId 对应的微信商户号。");
return new WechatTenpayClient(new WechatTenpayClientOptions()
{
MerchantId = tenpayMerchantOptions.MerchantId,
MerchantV3Secret = tenpayMerchantOptions.SecretV3,
MerchantCertSerialNumber = tenpayMerchantOptions.CertSerialNumber,
MerchantCertPrivateKey = tenpayMerchantOptions.CertPrivateKey,
CertificateManager = _tenpayCertificateManagerFactory.Create(tenpayMerchantOptions.MerchantId),
AutoEncryptRequestSensitiveProperty = true,
AutoDecryptResponseSensitiveProperty = true
});
}
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
</system.web>
</configuration>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>

View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="TenpayOptions_Merchant_0_MerchantId" value="填写商户号" />
<add key="TenpayOptions_Merchant_0_SecretV3" value="填写商户 V3 API 密钥" />
<add key="TenpayOptions_Merchant_0_CertSerialNumber" value="填写商户证书序列号" />
<add key="TenpayOptions_Merchant_0_CertPrivateKey" value="填写商户证书文件内容" />
<add key="TenpayOptions_NotifyUrl" value="https://localhost:5001" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.3.0.0" newVersion="6.3.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
</system.web>
<system.webServer>
<handlers>
<remove name="TRACEVerbHandler" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="6.3.0" targetFramework="net472" />
<package id="Autofac.Mvc5" version="6.0.0" targetFramework="net472" />
<package id="Autofac.WebApi2" version="6.1.0" targetFramework="net472" />
<package id="Hangfire.AspNet" version="0.2.0" targetFramework="net472" />
<package id="Hangfire.Autofac" version="2.3.1" targetFramework="net472" />
<package id="Hangfire.Core" version="1.7.28" targetFramework="net472" />
<package id="Hangfire.InMemory" version="0.3.4" targetFramework="net472" />
<package id="Microsoft.AspNet.Mvc" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebHelpers" version="3.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebPages.Data" version="3.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebPages.WebData" version="3.2.7" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Html.Abstractions" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.AspNetCore.Razor" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net472" />
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net472" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.1.0" targetFramework="net472" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net472" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
<package id="Owin" version="1.0" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Diagnostics.DiagnosticSource" version="6.0.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.4" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
</packages>