diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Order/OrderBLL.cs b/src/0-core/HuanMeng.MiaoYu.Code/Order/OrderBLL.cs index 5193ee1..37915cc 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Order/OrderBLL.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Order/OrderBLL.cs @@ -89,7 +89,7 @@ namespace HuanMeng.MiaoYu.Code.Order } catch (Exception ex) { - RedisCache.KeyDelete(redisLock); + RedisCache.KeyDelete(redisLock); throw new Exception("创建订单失败"); } diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs b/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs index 8a0ac8e..044c92d 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Payment/PaymentExtend.cs @@ -132,6 +132,20 @@ namespace HuanMeng.MiaoYu.Code.Payment var _weChatConfig = miaoYuBase.AppConfig.Payment.WeChatConfig ?? weChatConfig; var _wxClient = miaoYuBase.AppConfig.Payment.WxClient ?? wxClient; + if (payment == "xcx") + { + if (miaoYuBase._UserId == 0) + { + throw new Exception("请先登录"); + } + var userAccount = miaoYuBase.Dao.daoDbMiaoYu.context.T_User_MiniProgram_Account.FirstOrDefault(it => it.UserId == miaoYuBase._UserId); + if (userAccount == null) + { + throw new Exception("未找到人员数据,请先登录"); + } + + return new WeChatMiniProgram(_wxClient, _weChatConfig, miaoYuBase.TenantInfo, miaoYuBase._UserId, userAccount.OpenId); + } return new WeChatPayment(_wxClient, _weChatConfig, miaoYuBase.TenantInfo, miaoYuBase._UserId); } diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatMiniProgram.cs b/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatMiniProgram.cs new file mode 100644 index 0000000..58f85fd --- /dev/null +++ b/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatMiniProgram.cs @@ -0,0 +1,76 @@ +using HuanMeng.DotNetCore.MultiTenant.Contract; +using HuanMeng.MiaoYu.Code.Payment.Contract; +using Newtonsoft.Json; +using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models; +using SKIT.FlurlHttpClient.Wechat.TenpayV3; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HuanMeng.MiaoYu.Code.Payment.WeChat +{ + /// + /// + /// + /// + /// + /// + /// + /// + public class WeChatMiniProgram(WechatTenpayClient client, WeChatConfig weChatConfig, ITenantInfo tenantInfo, int userId, string openId) : IPayment + { + public async Task<(string orderId, string order)> CreateOrder(int productId, string productName, decimal price, params object[] args) + { + //var orderId = GenerateTimestampIdWithOffset(); + var orderId = PaymentExtend.GenerateCustomString("WMP", userId, productId, "002"); + //var client = new WechatTenpayClient(wechatTenpayClientOptions); + /* 以 JSAPI 统一下单接口为例 */ + string sign = $"{tenantInfo.Identifier}{orderId}{userId}"; + sign = MD5Encryption.ComputeMD5Hash(sign); + var notifyUrl = weChatConfig.NotifyUrl.Replace("${pay}", "wx").Replace("${orderId}", orderId).Replace("${tenant}", tenantInfo.Identifier).Replace("${sign}", sign); + + var request1 = new CreatePayTransactionAppRequest() + { + OutTradeNumber = orderId, + AppId = weChatConfig.AppId, + Description = productName, + ExpireTime = DateTimeOffset.Now.AddMinutes(20), + NotifyUrl = notifyUrl, + Amount = new CreatePayTransactionJsapiRequest.Types.Amount() + { + Total = (int)(price * 100) + }, + }; + + /* 以 JSAPI 统一下单接口为例 */ + var request = new CreatePayTransactionJsapiRequest() + { + + OutTradeNumber = orderId, + AppId = weChatConfig.AppId, + Description = productName, + ExpireTime = DateTimeOffset.Now.AddMinutes(20), + NotifyUrl = notifyUrl, + Amount = new CreatePayTransactionJsapiRequest.Types.Amount() + { + Total = (int)(price * 100) + }, + Payer = new CreatePayTransactionJsapiRequest.Types.Payer() + { + OpenId = openId + } + }; + var response = await client.ExecuteCreatePayTransactionJsapiAsync(request); + 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("微信下单失败"); + } + } +} 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 9059c2b..03a8c5a 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatPayment.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Payment/WeChat/WeChatPayment.cs @@ -9,7 +9,7 @@ using System.Security.Cryptography; namespace HuanMeng.MiaoYu.Code.Payment.WeChat { /// - /// 微信支付 + /// 微信支付 /// public class WeChatPayment(WechatTenpayClient client, WeChatConfig weChatConfig, ITenantInfo tenantInfo, int userId) : IPayment { @@ -35,6 +35,7 @@ namespace HuanMeng.MiaoYu.Code.Payment.WeChat Total = (int)(price * 100) }, }; + var response = await client.ExecuteCreatePayTransactionAppAsync(request); if (response.IsSuccessful()) {