diff --git a/src/0-core/HuanMeng.MiaoYu.Code/HuanMeng.MiaoYu.Code.csproj b/src/0-core/HuanMeng.MiaoYu.Code/HuanMeng.MiaoYu.Code.csproj index b637826..2340ba4 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/HuanMeng.MiaoYu.Code.csproj +++ b/src/0-core/HuanMeng.MiaoYu.Code/HuanMeng.MiaoYu.Code.csproj @@ -27,10 +27,7 @@ - - - - + diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs b/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs index 1ae7b71..87961ea 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs @@ -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 /// public static class PaymentExtend { - //public static WXPayTradeApi WxApi { get; set; } + private static WechatTenpayClientOptions wechatTenpayClientOptions { get; set; } = new WechatTenpayClientOptions(); + private static WeChatConfig weChatConfig { get; set; } /// /// 支付 /// @@ -27,13 +34,24 @@ namespace HuanMeng.MiaoYu.Code.Payment { AddAlipay(builder.Configuration); + weChatConfig = builder.Configuration.GetSection("Payment:WeChatConfig").Get(); + 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(); - - 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); } /// diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatConfig.cs b/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatConfig.cs index 0ba9124..6933ec7 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatConfig.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatConfig.cs @@ -23,10 +23,26 @@ namespace HuanMeng.MiaoYu.Code.Payment.WeChat /// /// public string Key { get; set; } + /// + /// + /// public string MchId { get; set; } + /// + /// + /// public string NotifyUrl { get; set; } + + /// + /// + /// + public string MerchantCertificateSerialNumber { get; set; } + + /// + /// + /// + public string MerchantCertificatePrivateKey { get; set; } } - + } diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatPayment.cs b/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatPayment.cs index 6fd05a0..467a970 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatPayment.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatPayment.cs @@ -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 { /// /// 微信支付 /// - 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}"; } } }