增加微信支付

This commit is contained in:
zpc 2024-08-25 19:43:52 +08:00
parent 436c24ea8c
commit 90e4e32b22
4 changed files with 72 additions and 28 deletions

View File

@ -27,10 +27,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Senparc.Weixin" Version="6.19.1" />
<PackageReference Include="Senparc.Weixin.Cache.CsRedis" Version="1.0.0" />
<PackageReference Include="Senparc.Weixin.Cache.Redis" Version="2.19.1" />
<PackageReference Include="Senparc.Weixin.TenPay" Version="1.16.1" />
<PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="3.7.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.2" />
<PackageReference Include="TencentCloudSDK.Common" Version="3.0.1042" />

View File

@ -9,6 +9,12 @@ using HuanMeng.MiaoYu.Model.Dto.Cache;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
namespace HuanMeng.MiaoYu.Code.Payment
{
@ -17,7 +23,8 @@ namespace HuanMeng.MiaoYu.Code.Payment
/// </summary>
public static class PaymentExtend
{
//public static WXPayTradeApi WxApi { get; set; }
private static WechatTenpayClientOptions wechatTenpayClientOptions { get; set; } = new WechatTenpayClientOptions();
private static WeChatConfig weChatConfig { get; set; }
/// <summary>
/// 支付
/// </su0mmary>
@ -27,13 +34,24 @@ namespace HuanMeng.MiaoYu.Code.Payment
{
AddAlipay(builder.Configuration);
weChatConfig = builder.Configuration.GetSection("Payment:WeChatConfig").Get<WeChatConfig>();
if (weChatConfig == null)
{
throw new Exception("微信支付失败");
}
var manager = new InMemoryCertificateManager();
/* 仅列出必须配置项。也包含一些诸如超时时间、UserAgent 等的配置项 */
wechatTenpayClientOptions = new WechatTenpayClientOptions()
{
MerchantId = weChatConfig.MchId,
MerchantV3Secret = weChatConfig.Key,
MerchantCertificateSerialNumber = weChatConfig.MerchantCertificateSerialNumber,
MerchantCertificatePrivateKey = weChatConfig.MerchantCertificatePrivateKey,
PlatformCertificateManager = manager
};
var weChatConfig = builder.Configuration.GetSection("Payment:WeChatConfig").Get<WeChatConfig>();
string key = Path.GetFullPath("DataStorage/1680394019/apiclient_key.pem");
string key1 = Path.GetFullPath("DataStorage/1680394019/apiclient_cert.pem");
return builder;
}
@ -81,7 +99,7 @@ namespace HuanMeng.MiaoYu.Code.Payment
{
return new AlipayPayment();
}
return new WeChatPayment();
return new WeChatPayment(wechatTenpayClientOptions, weChatConfig);
}
/// <summary>

View File

@ -23,10 +23,26 @@ namespace HuanMeng.MiaoYu.Code.Payment.WeChat
///
/// </summary>
public string Key { get; set; }
/// <summary>
///
/// </summary>
public string MchId { get; set; }
/// <summary>
///
/// </summary>
public string NotifyUrl { get; set; }
/// <summary>
///
/// </summary>
public string MerchantCertificateSerialNumber { get; set; }
/// <summary>
///
/// </summary>
public string MerchantCertificatePrivateKey { get; set; }
}
}

View File

@ -1,34 +1,47 @@
using HuanMeng.MiaoYu.Code.Payment.Contract;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
using Newtonsoft.Json;
namespace HuanMeng.MiaoYu.Code.Payment.WeChat
{
/// <summary>
/// 微信支付
/// </summary>
public class WeChatPayment() : IPayment
public class WeChatPayment(WechatTenpayClientOptions wechatTenpayClientOptions, WeChatConfig weChatConfig) : IPayment
{
public async Task<(string orderId, string order)> CreateOrder(string productName, decimal price, params object[] args)
{
// WechatPayHelper.pay_config = new WechatPayConfig()
// {
// app_id = "xxxxxx",
// mch_id = "xxxxxx",
// api_key = "xxxxxx",
// cert_path = "E:\\xxxxxx\\apiclient_cert.p12",
// cert_password = "xxxxxx"
// };
var orderId = GenerateTimestampIdWithOffset();
return new(orderId, null);
var client = new WechatTenpayClient(wechatTenpayClientOptions);
/* 以 JSAPI 统一下单接口为例 */
var request = new CreatePayTransactionAppRequest()
{
OutTradeNumber = orderId,
AppId = weChatConfig.AppId,
Description = productName,
ExpireTime = DateTimeOffset.Now.AddMinutes(20),
NotifyUrl = weChatConfig.NotifyUrl,
Amount = new CreatePayTransactionJsapiRequest.Types.Amount()
{
Total = (int)(price * 100)
},
};
var response = client.ExecuteCreatePayTransactionAppAsync(request).Result;
if (response.IsSuccessful())
{
var paramMap = client.GenerateParametersForAppPayRequest(request.AppId, response.PrepayId);
Console.WriteLine("PrepayId" + response.PrepayId);
return new(orderId, JsonConvert.SerializeObject(paramMap));
}
throw new Exception("微信下单失败");
}
private string GenerateTimestampIdWithOffset()
{
var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); // 获取Unix时间戳毫秒
var random = new Random().Next(1000, 9999); // 生成四位随机数
return $"WX0{timestamp}J001Z{random}";
return $"WX0{timestamp}J001C{random}";
}
}
}