mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 16:13:26 +08:00
138 lines
6.9 KiB
C#
138 lines
6.9 KiB
C#
/***********************************************************************
|
|
* Project: CoreCms.Net *
|
|
* Web: https://CoreCms.Net *
|
|
* ProjectName: 核心内容管理系统 *
|
|
* Author: 大灰灰 *
|
|
* Email: JianWeie@163.com *
|
|
* CreateTime: 2020-08-25 1:25:29
|
|
* Description: 暂无
|
|
***********************************************************************/
|
|
|
|
|
|
using CoreCms.Net.IServices;
|
|
using CoreCms.Net.Model.Entities;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
|
|
|
namespace CoreCms.Net.Task
|
|
{
|
|
/// <summary>
|
|
/// 定时更新微信支付平台证书
|
|
/// </summary>
|
|
public class AutoRefreshPlatformCertificateJob
|
|
{
|
|
|
|
private readonly IWeChatPayConfigServices _weChatPayConfigServices;
|
|
private readonly IWechatTenpayClientFactory _wechatTenpayClientFactory;
|
|
private readonly ICoreCmsWeChatPayPlatformCertificateServices _weChatPayPlatformCertificateServices;
|
|
private readonly ISysTaskLogServices _taskLogServices;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public AutoRefreshPlatformCertificateJob(IWeChatPayConfigServices weChatPayConfigServices, IWechatTenpayClientFactory wechatTenpayClientFactory, ICoreCmsWeChatPayPlatformCertificateServices weChatPayPlatformCertificateServices, ISysTaskLogServices taskLogServices)
|
|
{
|
|
_weChatPayConfigServices = weChatPayConfigServices;
|
|
_wechatTenpayClientFactory = wechatTenpayClientFactory;
|
|
_weChatPayPlatformCertificateServices = weChatPayPlatformCertificateServices;
|
|
_taskLogServices = taskLogServices;
|
|
}
|
|
|
|
public async System.Threading.Tasks.Task Execute()
|
|
{
|
|
var config = await _weChatPayConfigServices.QueryListByClauseAsync(p => p.isDefault == true && p.isEnable == true);
|
|
if (config != null)
|
|
{
|
|
foreach (var item in config)
|
|
{
|
|
try
|
|
{
|
|
const string algorithmType = "RSA";
|
|
var client = await _wechatTenpayClientFactory.Create(item.mchId);
|
|
var request = new QueryCertificatesRequest() { AlgorithmType = algorithmType };
|
|
var response = await client.ExecuteQueryCertificatesAsync(request);
|
|
if (response.IsSuccessful())
|
|
{
|
|
// NOTICE:
|
|
// 如果构造 Client 时启用了 `AutoDecryptResponseSensitiveProperty` 配置项,则无需再执行下面一行的手动解密方法:
|
|
response = client.DecryptResponseSensitiveProperty(response);
|
|
|
|
foreach (var certificate in response.CertificateList)
|
|
{
|
|
var entity = CertificateEntry.Parse(algorithmType, certificate);
|
|
|
|
// 将证书添加到平台证书管理器中
|
|
var model = await _weChatPayPlatformCertificateServices.QueryByClauseAsync(p =>
|
|
p.merchantId == item.mchId);
|
|
if (model != null)
|
|
{
|
|
model.algorithmType = entity.AlgorithmType;
|
|
model.serialNumber = entity.SerialNumber;
|
|
model.certificate = entity.Certificate;
|
|
model.effectiveTime = entity.EffectiveTime.DateTime;
|
|
model.expireTime = entity.ExpireTime.DateTime;
|
|
|
|
model.updataTime = DateTime.Now;
|
|
await _weChatPayPlatformCertificateServices.UpdateAsync(model);
|
|
}
|
|
else
|
|
{
|
|
model = new CoreCmsWeChatPayPlatformCertificate();
|
|
model.algorithmType = entity.AlgorithmType;
|
|
model.serialNumber = entity.SerialNumber;
|
|
model.certificate = entity.Certificate;
|
|
model.effectiveTime = entity.EffectiveTime.DateTime;
|
|
model.expireTime = entity.ExpireTime.DateTime;
|
|
model.merchantId = item.mchId;
|
|
model.createTime = DateTime.Now;
|
|
await _weChatPayPlatformCertificateServices.InsertAsync(model);
|
|
}
|
|
//client.PlatformCertificateManager.AddEntry(entity);
|
|
}
|
|
|
|
//插入日志
|
|
var log = new SysTaskLog
|
|
{
|
|
createTime = DateTime.Now,
|
|
isSuccess = true,
|
|
name = "刷新微信商户平台证书成功",
|
|
parameters = JsonConvert.SerializeObject(response)
|
|
};
|
|
await _taskLogServices.InsertAsync(log);
|
|
|
|
}
|
|
else
|
|
{
|
|
//插入日志
|
|
var log = new SysTaskLog
|
|
{
|
|
createTime = DateTime.Now,
|
|
isSuccess = true,
|
|
name = "刷新微信商户平台证书失败",
|
|
parameters = $"刷新微信商户平台证书失败(状态码:{response.GetRawStatus()},错误代码:{response.ErrorCode},错误描述:{response.ErrorMessage})。",
|
|
};
|
|
await _taskLogServices.InsertAsync(log);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
//插入日志
|
|
var log = new SysTaskLog
|
|
{
|
|
createTime = DateTime.Now,
|
|
isSuccess = true,
|
|
name = "刷新微信商户平台证书遇到异常",
|
|
parameters = JsonConvert.SerializeObject(ex)
|
|
};
|
|
await _taskLogServices.InsertAsync(log);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|