From 9ba902a5731dfa3732d50d48662ff9f5c28858f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?old=E6=98=93?= <156663459@qq.com> Date: Thu, 14 Dec 2023 14:11:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=94=AF=E4=BB=98=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Front/OrderController.cs | 6 + .../Controllers/Front/PayQrCodeController.cs | 36 +++ .../Controllers/Front/WeixinPayController.cs | 114 +------ New_College.Api/New_College.Model.xml | 5 - New_College.Api/New_College.xml | 32 +- New_College.Api/Startup.cs | 10 +- New_College.Api/appsettings.Development.json | 33 ++ New_College.Api/appsettings.json | 32 ++ New_College.Common/Helper/QrCodeHelper.cs | 78 +++++ New_College.Common/Helper/WeChatPayHelper.cs | 78 ----- .../Helper/WeChatPayV3Helper.cs | 198 ++++++++++-- New_College.Common/Helper/WeixinHelper.cs | 1 - New_College.Common/New_College.Common.csproj | 5 +- New_College.IServices/IV_OrderInfoServices.cs | 30 +- .../IWeiChatPayServicesV3.cs | 16 - .../Models/OccupationMapJobInfo.cs | 3 +- .../ViewModels/AliyunSmsSenderConfig.cs | 25 -- .../BASE/D_UniversityRankRepository.cs | 3 +- New_College.Services/D_LongIdMapServices.cs | 1 - New_College.Services/V_OrderInfoServices.cs | 305 +----------------- New_College.Services/WeiChatPayServicesV3.cs | 83 ----- 21 files changed, 398 insertions(+), 696 deletions(-) create mode 100644 New_College.Api/Controllers/Front/PayQrCodeController.cs create mode 100644 New_College.Common/Helper/QrCodeHelper.cs delete mode 100644 New_College.Common/Helper/WeChatPayHelper.cs delete mode 100644 New_College.IServices/IWeiChatPayServicesV3.cs delete mode 100644 New_College.Services/WeiChatPayServicesV3.cs diff --git a/New_College.Api/Controllers/Front/OrderController.cs b/New_College.Api/Controllers/Front/OrderController.cs index fd9e908..0af4271 100644 --- a/New_College.Api/Controllers/Front/OrderController.cs +++ b/New_College.Api/Controllers/Front/OrderController.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using New_College.Common.Helper; using New_College.IServices; using New_College.Model; using New_College.Model.ViewModels; @@ -20,6 +22,10 @@ namespace New_College.Api.Controllers.Front v_OrderInfoServices = IV_OrderInfoServices; } + + + + /// /// 下订单 /// diff --git a/New_College.Api/Controllers/Front/PayQrCodeController.cs b/New_College.Api/Controllers/Front/PayQrCodeController.cs new file mode 100644 index 0000000..6047d5c --- /dev/null +++ b/New_College.Api/Controllers/Front/PayQrCodeController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Mvc; +using New_College.Common.Helper; +using New_College.IServices; +using New_College.Services; +using System.Threading.Tasks; + +namespace New_College.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class PayQrCodeController : Controller + { + private readonly IV_OrderInfoServices v_OrderInfoServices; + public PayQrCodeController(IV_OrderInfoServices OrderInfoServices) + { + + v_OrderInfoServices = OrderInfoServices; + } + + + /// + /// 生成支付图片 + /// + /// + /// + [HttpGet] + [Route("/payqrcode/create")] + public async Task Create([FromQuery] WeChatPayTransactionsNativeQuery query) + { + + var filestream = await v_OrderInfoServices.QrCodePay(query); + + return File(filestream, "image/png"); + } + } +} diff --git a/New_College.Api/Controllers/Front/WeixinPayController.cs b/New_College.Api/Controllers/Front/WeixinPayController.cs index c84b56c..f180e58 100644 --- a/New_College.Api/Controllers/Front/WeixinPayController.cs +++ b/New_College.Api/Controllers/Front/WeixinPayController.cs @@ -72,7 +72,7 @@ namespace New_College.Api.Controllers.Front data.SetValue("openid", query.OpenId); //可以将用户Id和订单Id同时封装在attach中 data.SetValue("attach", string.Format("{0}|{1}", query.OpenId, payNum)); - WxPayData result = UnifiedOrder(data); + WxPayData result = WeChatPayV3.UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { resp.err_code_des = result.GetValue("err_code_des").ToString(); @@ -240,116 +240,6 @@ namespace New_College.Api.Controllers.Front } - /** - * - * 统一下单 - * @param WxPaydata inputObj 提交给统一下单API的参数 - * @param int timeOut 超时时间 - * @throws WePayException - * @return 成功时返回,其他抛异常 - */ - public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 60) - { - string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; - //检测必填参数 - if (!inputObj.IsSet("out_trade_no")) - { - throw new Exception("缺少统一支付接口必填参数out_trade_no!"); - } - else if (!inputObj.IsSet("body")) - { - throw new Exception("缺少统一支付接口必填参数body!"); - } - else if (!inputObj.IsSet("total_fee")) - { - throw new Exception("缺少统一支付接口必填参数total_fee!"); - } - else if (!inputObj.IsSet("trade_type")) - { - throw new Exception("缺少统一支付接口必填参数trade_type!"); - } - - //关联参数 - if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) - { - throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); - } - if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) - { - throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); - } - - //异步通知url未设置,则使用配置文件中的url - if (!inputObj.IsSet("notify_url")) - { - inputObj.SetValue("notify_url", WeixinConfig.NotifyUrl);//异步通知url - } - - inputObj.SetValue("appid", WeixinConfig.Appid);//公众账号ID - inputObj.SetValue("mch_id", WeixinConfig.MCHID);//商户号 - inputObj.SetValue("spbill_create_ip", WePayConfig.IP);//终端ip - inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 - - //签名 - inputObj.SetValue("sign", inputObj.MakeSign()); - string xml = inputObj.ToXml(); - - var start = DateTime.Now; - - // Log.Info("XcxPayApi", "UnfiedOrder request : " + xml); - string response = HttpPost(xml, url, "application/xml", timeOut); - //Log.Info("XcxPayApi", "UnfiedOrder response : " + response); - // WebHookHelper.WebHookmarkdownSend(response); - - var end = DateTime.Now; - int timeCost = (int)((end - start).TotalMilliseconds); - - WxPayData result = new WxPayData(); - result.FromXml(response); - // ReportCostTime(url, timeCost, result);//测速上报网络不好时使用 - return result; - } - /// - /// 生成随机数 - /// - /// - public static string GenerateNonceStr() - { - return Guid.NewGuid().ToString().Replace("-", ""); - } - /// - /// POST请求 - /// - /// - /// - /// application/xml、application/json、application/text、application/x-www-form-urlencoded - /// - /// - /// - public static string HttpPost(string postData, string url, string contentType = null, int timeOut = 30, Dictionary headers = null) - { - postData = postData ?? ""; - - var httpClientHandler = new HttpClientHandler - { - ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true - }; - using (HttpClient httpClient = new HttpClient(httpClientHandler)) - { - if (headers != null) - { - foreach (var header in headers) - httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); - } - using (HttpContent client = new StringContent(postData, Encoding.UTF8)) - { - if (contentType != null) - client.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); - - HttpResponseMessage response = httpClient.PostAsync(url, client).Result; - return response.Content.ReadAsStringAsync().Result; - } - } - } + } } diff --git a/New_College.Api/New_College.Model.xml b/New_College.Api/New_College.Model.xml index 42beb8a..f0e0941 100644 --- a/New_College.Api/New_College.Model.xml +++ b/New_College.Api/New_College.Model.xml @@ -3495,11 +3495,6 @@ 支付结果回调URL - - - - - 本科/专科大类 1本科 2 专科 diff --git a/New_College.Api/New_College.xml b/New_College.Api/New_College.xml index 0f86df7..59bf8bf 100644 --- a/New_College.Api/New_College.xml +++ b/New_College.Api/New_College.xml @@ -970,31 +970,6 @@ - - - 统一下单 - @param WxPaydata inputObj 提交给统一下单API的参数 - @param int timeOut 超时时间 - @throws WePayException - @return 成功时返回,其他抛异常 - - - - 生成随机数 - - - - - - POST请求 - - - - application/xml、application/json、application/text、application/x-www-form-urlencoded - - - - 服务器接口,因为是模板生成,所以首字母是大写的,自己可以重构下 @@ -1019,6 +994,13 @@ + + + 生成支付图片 + + + + 服务器接口,因为是模板生成,所以首字母是大写的,自己可以重构下 diff --git a/New_College.Api/Startup.cs b/New_College.Api/Startup.cs index 1739132..922fa52 100644 --- a/New_College.Api/Startup.cs +++ b/New_College.Api/Startup.cs @@ -19,8 +19,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Reflection; using New_College.Model.ViewModels; -using Essensoft.AspNetCore.Payment.WeChatPay; -using Essensoft.AspNetCore.Payment.Alipay; + using Microsoft.AspNetCore.Http; using IdentityModel; using Microsoft.AspNetCore.Authentication.Cookies; @@ -30,6 +29,7 @@ using System.Text.Encodings.Web; using System.Text.Unicode; using Microsoft.AspNetCore.ResponseCompression; using System.Linq; +using Senparc.Weixin.RegisterServices; namespace New_College { @@ -83,7 +83,6 @@ namespace New_College CasdoorConfig.CallbackPath = Appsettings.app(new string[] { "Casdoor", "CallbackPath" }); - services.AddMemoryCacheSetup(); services.AddSqlsugarSetup(); services.AddDbSetup(); @@ -95,8 +94,9 @@ namespace New_College services.AddHttpContextSetup(); services.AddAppConfigSetup(); services.AddHttpApi(); - + //Senparc.Weixin 注册(必须) + services.AddSenparcWeixinServices(Configuration); @@ -215,6 +215,8 @@ namespace New_College // 封装Swagger展示 app.UseSwaggerMildd(() => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("New_College.Api.index.html")); + + // ↓↓↓↓↓↓ 注意下边这些中间件的顺序,很重要 ↓↓↓↓↓↓ // CORS跨域 diff --git a/New_College.Api/appsettings.Development.json b/New_College.Api/appsettings.Development.json index 5a43355..55fb186 100644 --- a/New_College.Api/appsettings.Development.json +++ b/New_College.Api/appsettings.Development.json @@ -216,5 +216,38 @@ "ClientSecret": "cc385a5d0efd5f7bf693d8c4574126158f7b0a8f", "CallbackPath": "http://test.sso.ycymedu.com/login/oauth/authorize?client_id=4c6d761417dbbdd665be&response_type=code&redirect_uri=http://192.168.100.129:8081/Home/FirstPage&scope=read&state=casdoor", "RequireHttpsMetadata": false + }, + "WeChatPay": { + // 应用号 + // 如:微信公众平台AppId、微信开放平台AppId、微信小程序AppId、企业微信CorpId等 + "AppId": "wx1bfbd8233a2d3261", + "AppSecret": "c12d2bf676a3b11d542efd7f477a72f3", + // 商户号 + // 为微信支付商户平台的商户号 + "MchId": "1614987630", + + // 商户API密钥 + // 为微信支付商户平台的API密钥,请注意不是APIv3密钥 + "APIKey": "", + + // 商户APIv3密钥 + // 为微信支付商户平台的APIv3密钥,请注意不是API密钥,v3接口必填 + "APIv3Key": "brapuk6fon0wachiMlth2t3lb4a0h7ji", + + // 商户API证书 + // 使用V2退款、付款等接口时必填 + // 使用V3接口时必填 + // 可为证书文件路径 / 证书文件的base64字符串 + "Certificate": "MIID9jCCAt6gAwIBAgIUX7aJQ2iVIFJNjycVSZIGI+dcI10wDQYJKoZIhvcNAQELBQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsTFFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3QgQ0EwHhcNMjIxMjEyMDUxMjE1WhcNMjcxMjExMDUxMjE1WjCBhzETMBEGA1UEAwwKMTYxNDk4NzYzMDEbMBkGA1UECgwS5b6u5L+h5ZWG5oi357O757ufMTMwMQYDVQQLDCrkuIrmtbfkuIDojYnkuIDmnKjmlZnogrLnp5HmioDmnInpmZDlhazlj7gxCzAJBgNVBAYMAkNOMREwDwYDVQQHDAhTaGVuWmhlbjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALlWoiOOEEecInVwlNxl78d7xkk+Q1ZMZHaVyiyrBbY0ik55fRwg76s3u3q67IYv/UDgik8thWRc0KvL/rhiyb+h9F9/aTWZ2LXoCXiFbtzNBV40aHshi4QNOfmdEPHmMytwohRJB/MUPo8lJpG1hB/6OgNJjsqbBNXTm7HtU4A9okqSaUUgzgbaWuDOON8BXHqBsR36gHAWCfr2ecFw0QBh/9mwn/DkF25vbR/QLXCsKIw3LF6BkWOdBUNvnJYHxF2yMvMV5cflsutkf63bfSF5qznVNAiMfMmhhfM3OqtOvk48PtLkHDcfC0XJuud9VFAxp8GaLSU6IJr2vM6KDjkCAwEAAaOBgTB/MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgP4MGUGA1UdHwReMFwwWqBYoFaGVGh0dHA6Ly9ldmNhLml0cnVzLmNvbS5jbi9wdWJsaWMvaXRydXNjcmw/Q0E9MUJENDIyMEU1MERCQzA0QjA2QUQzOTc1NDk4NDZDMDFDM0U4RUJEMjANBgkqhkiG9w0BAQsFAAOCAQEAU39wePx6nBiHGKsRkfIDgQH1VEcoyXDIdORkyV2UvhX5MPo9fCOSU2vT4HiuyTKs5W74I7+4jEmqLtS5UzUhAu6K5Bl80yYmXErHRZIaF0fpxT+LAHocsCIC5B8grVWNDztu6F9UqB7Ngr8bfHm1j2esnb0x48UvWm2Uqn2Z38RT4BPty048yYdcVVndYRPigL+I4QfkVZZVJmxAi/wZXiKBSOb9Rp2du8NyNrEPiBDZWXRF5HLxgJoJtXY8tLJPUY5Lm6M1TlhnCvNbVSode8HazI6paCOvQHYN73D27Ie+G5SEic8iR0MFfsXNeKol+sF8iAeDN94EpvPD0ZkfPg==", + // 商户API私钥 + // 当配置了P12格式证书时,已包含私钥,不必再单独配置API私钥。 + // PEM格式证书,需要单独配置。 + "APIPrivateKey": "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC5VqIjjhBHnCJ1cJTcZe/He8ZJPkNWTGR2lcosqwW2NIpOeX0cIO+rN7t6uuyGL/1A4IpPLYVkXNCry/64Ysm/ofRff2k1mdi16Al4hW7czQVeNGh7IYuEDTn5nRDx5jMrcKIUSQfzFD6PJSaRtYQf+joDSY7KmwTV05ux7VOAPaJKkmlFIM4G2lrgzjjfAVx6gbEd+oBwFgn69nnBcNEAYf/ZsJ/w5Bdub20f0C1wrCiMNyxegZFjnQVDb5yWB8RdsjLzFeXH5bLrZH+t230heas51TQIjHzJoYXzNzqrTr5OPD7S5Bw3HwtFybrnfVRQMafBmi0lOiCa9rzOig45AgMBAAECggEABiuyPkjOlfPG2e4jpqrmLtgaI/okt8zXU3NtRctz5zscl7xHxBX8pNC4Nz/d/CHA5tYTBMvZMxw7/lxOYMk5Ol4kCsHtONGke3n8Rrz6kfNpiFdqWx8kzAK8uWjXVZZ6ganAW7iKwTcdVhrAi/1lJsNoBw/95xT+NjhTaLskGpLkQPEwct1Ygc7Ce+5d/UCphdk351sRzhEf1dgaAbn+/fHD599fpo87YCO1QPChTy+jp7e+d6B3QxspNCeTcpCdxevM1EVAYlDYOivONM9RQSAPWjsErIxKyN5nvusFhO4xrSIfg6uRkGEVKiDMIU6C8O3XVcwH9vIw3az5SUFRAQKBgQDcYcRZP+GNU3YpFj2Zcn2I9WNXwSU32Wp1wNEgKwvTv9fXDxYiuK6OjIM76DhcsuO1ew7dTtFXfSrmEkIXRBf4XmMes0I6RD/Ds1tB+hCo71b3OX28C4bzCd4IicY/9JpG8cYRReFKd42XX/9N7qxxZ8vefSq5xNzbDOtKRnBJCQKBgQDXSvPpiDRMTDuQ+hkyW4s+dYt9Mj9xSKiKQiGc84yPcboLgMYwf3A0P6eDCHFN1G63MKuwg36cgI8uhkNjv757NzR5195jDx56tvnx1s+DqPKLX5Cd0UOQLtjFSxTyRaOAHz4Fc06zQBQ0Zp3P+xZQFZe8K2cIBy4hM0FNqknXsQKBgE9pDzZVtXGWVGqDsBidz6xNN8nEOCQCvMDHhNOBj1Zyt1SwA/ro70VATn2EtOJ6N2EKgZWvM/HBkXart5Lon3aIlFDYqRGTzCI5e7WvZNdqvryAw9TBlpP2x0HuWzhqbDkKH6PTuVV69NNJ7RXo3cxTaXu3QHEGBcsMwa/W59j5AoGBALvzhWj1LaMGUmJI1MJvxOosODB+M4C0i97XyPx6atW31VHBhERGnTAf+h+cuT3XHSj0Zo7c3SXH3jghoqdsd+Cd20QbvtwAEP3Uy/PjMpqgXc5rCZedXzx6iWgEGi8/uHNb0wtzFNvML1dn1pIBhAM6NpNh2LrbedwmIPaqmXihAoGASTRxC8cOvjEwGrwxHkN9ZPOIK3OTFGkPrJxF3byskQEpc7zOKRx+kxeRvZ5jKacMqW2ns3yMR+JZO9devWotpx+mj5mlWMUC5bQB9BnwForuD4vKDjb5Y8IEAQTopUxWApzK2CQCLETUhCDl1+eQ+EUUoISFHO99qBT6Vpu7qNE=", + // RSA公钥 + // 目前仅调用"企业付款到银行卡API [V2]"时使用,执行本示例中的"获取RSA加密公钥API [V2]"即可获取。 + "RsaPublicKey": "/apiclient_cert.p12", + + //回调地址 + "NotifyUrl": "https://pay.ycymedu.com/api/notify/transactions" } } diff --git a/New_College.Api/appsettings.json b/New_College.Api/appsettings.json index 37c2ef4..d2f0672 100644 --- a/New_College.Api/appsettings.json +++ b/New_College.Api/appsettings.json @@ -216,5 +216,37 @@ "ClientSecret": "d556f0692052a2df56614282dd86895b04783262", "CallbackPath": "http://192.168.103.119:8000/login/oauth/authorize?client_id=ae6bdccc3a7821232b31&response_type=code&redirect_uri=http://192.168.103.100:8083/callback&scope=read&state=casdoor", "RequireHttpsMetadata": false + }, + "SenparcWeixinSetting": { + //以下为 Senparc.Weixin 的 SenparcWeixinSetting 微信配置 + //注意:所有的字符串值都可能被用于字典索引,因此请勿留空字符串(但可以根据需要,删除对应的整条设置)! + + //微信全局 + "IsDebug": true, + + //以下不使用的参数可以删除,key 修改后将会失效 + + //公众号 + "Token": "#{Token}#", //说明:字符串内两侧#和{}符号为 Azure DevOps 默认的占位符格式,如果您有明文信息,请删除同占位符,修改整体字符串,不保留#和{},如:{"Token": "MyFullToken"} + "EncodingAESKey": "#{EncodingAESKey}#", + "WeixinAppId": "#{WeixinAppId}#", + "WeixinAppSecret": "#{WeixinAppSecret}#", + + //微信支付V3 + "TenPayV3_AppId": "#{TenPayV3_AppId}#", + "TenPayV3_AppSecret": "#{TenPayV3_AppSecret}#", + "TenPayV3_SubAppId": "#{TenPayV3_SubAppId}#", + "TenPayV3_SubAppSecret": "#{TenPayV3_SubAppSecret}#", + "TenPayV3_MchId": "#{TenPayV3_MchId}#", + "TenPayV3_SubMchId": "#{TenPayV3_SubMchId}#", //子商户,没有可留空 + "TenPayV3_Key": "#{TenPayV3_Key}#", + "TenPayV3_TenpayNotify": "#{TenPayV3_TenpayNotify}#", //https://YourDomainName/TenpayApiV3/PayNotifyUrl + /* 支付证书私钥 + * 1、支持明文私钥(无换行字符) + * 2、私钥文件路径(如:~/App_Data/cert/apiclient_key.pem),注意:必须放在 App_Data 等受保护的目录下,避免泄露 + */ + "TenPayV3_PrivateKey": "#{TenPayV3_PrivateKey}#", //(新)证书私钥 + "TenPayV3_SerialNumber": "#{TenPayV3_SerialNumber}#", //(新)证书序列号 + "TenPayV3_ApiV3Key": "#{TenPayV3_APIv3Key}#" //(新)APIv3 密钥 } } diff --git a/New_College.Common/Helper/QrCodeHelper.cs b/New_College.Common/Helper/QrCodeHelper.cs new file mode 100644 index 0000000..0d57d0a --- /dev/null +++ b/New_College.Common/Helper/QrCodeHelper.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading.Tasks; +using ZXing; +using ZXing.Common; + +namespace New_College.Common +{ + public static class QrCodeHelper + { + /// + /// 生成二维码 + /// + /// + /// + public static MemoryStream GerQrCodeStream(string url) + { + BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300); + var bw = new ZXing.BarcodeWriterPixelData(); + + var pixelData = bw.Write(bitMatrix); + var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); + var fileStream = new MemoryStream(); + var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); + try + { + // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image + System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length); + } + finally + { + bitmap.UnlockBits(bitmapData); + } + + fileStream.Flush();//.net core 必须要加 + fileStream.Position = 0;//.net core 必须要加 + + bitmap.Save(fileStream, System.Drawing.Imaging.ImageFormat.Png); + + fileStream.Seek(0, SeekOrigin.Begin); + return fileStream; + } + + /// + /// 获取文字图片信息 + /// + /// + /// + public static MemoryStream GetTextImageStream(string text) + { + MemoryStream fileStream = new MemoryStream(); + var fontSize = 14; + var wordLength = 0; + for (int i = 0; i < text.Length; i++) + { + byte[] bytes = Encoding.Default.GetBytes(text.Substring(i, 1)); + wordLength += bytes.Length > 1 ? 2 : 1; + } + using (var bitmap = new System.Drawing.Bitmap(wordLength * fontSize + 20, 14 + 40, System.Drawing.Imaging.PixelFormat.Format32bppRgb)) + { + using (Graphics g = Graphics.FromImage(bitmap)) + { + g.ResetTransform();//重置图像 + g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + g.DrawString(text, new Font("宋体", fontSize, FontStyle.Bold), Brushes.White, 10, 10); + bitmap.Save(fileStream, System.Drawing.Imaging.ImageFormat.Png); + } + } + fileStream.Seek(0, SeekOrigin.Begin); + return fileStream; + } + } +} \ No newline at end of file diff --git a/New_College.Common/Helper/WeChatPayHelper.cs b/New_College.Common/Helper/WeChatPayHelper.cs deleted file mode 100644 index 4107b20..0000000 --- a/New_College.Common/Helper/WeChatPayHelper.cs +++ /dev/null @@ -1,78 +0,0 @@ -using Essensoft.AspNetCore.Payment.WeChatPay; -using Essensoft.AspNetCore.Payment.WeChatPay.V2; -using Essensoft.AspNetCore.Payment.WeChatPay.V2.Request; -using Essensoft.AspNetCore.Payment.WeChatPay.V2.Response; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; - - -namespace New_College.Common.Helper -{ - public class WeChatPay - { - /// - /// 订单查询 - /// - /// - /// - /// - /// - public async Task OrderQuery(IWeChatPayClient _client, - IOptions _optionsAccessor, WeChatPayOrderQuery viewModel) - { - var request = new WeChatPayOrderQueryRequest - { - TransactionId = viewModel.TransactionId, - OutTradeNo = viewModel.OutTradeNo - }; - return await _client.ExecuteAsync(request, _optionsAccessor.Value); - } - - /// - /// 返回微信支付链接 - /// - /// - /// - public static async Task GetMwebUrl(IWeChatPayClient _client, IOptions _optionsAccessor, - WeChatPayPayRequest viewModel) - { - var request = new WeChatPayUnifiedOrderRequest - { - Body = viewModel.Body, - OutTradeNo = viewModel.OutTradeNo, - TotalFee = viewModel.TotalFee, - SpBillCreateIp = viewModel.SpBillCreateIp, - NotifyUrl = viewModel.NotifyUrl, - TradeType = viewModel.TradeType - }; - return await _client.ExecuteAsync(request, _optionsAccessor.Value); - } - /// - /// 退款 - /// - /// - /// - /// - /// - public static async Task Refund(IWeChatPayClient _client, IOptions _optionsAccessor, - WeChatPayRefundQuery query) - { - var request = new WeChatPayRefundRequest - { - OutRefundNo = query.OutRefundNo, - TransactionId = query.TransactionId, - OutTradeNo = query.OutTradeNo, - TotalFee = query.TotalFee, - RefundFee = query.RefundFee, - RefundDesc = query.RefundDesc, - NotifyUrl = query.NotifyUrl - }; - var response = await _client.ExecuteAsync(request, _optionsAccessor.Value); - return response; - } - } -} diff --git a/New_College.Common/Helper/WeChatPayV3Helper.cs b/New_College.Common/Helper/WeChatPayV3Helper.cs index eb27fda..1e62ff4 100644 --- a/New_College.Common/Helper/WeChatPayV3Helper.cs +++ b/New_College.Common/Helper/WeChatPayV3Helper.cs @@ -1,44 +1,198 @@ -using Essensoft.AspNetCore.Payment.WeChatPay; -using Essensoft.AspNetCore.Payment.WeChatPay.V3; -using Essensoft.AspNetCore.Payment.WeChatPay.V3.Domain; -using Essensoft.AspNetCore.Payment.WeChatPay.V3.Request; + using Microsoft.Extensions.Options; +using NPOI.SS.Formula.Functions; +using Senparc.Weixin.TenPayV3; +using Senparc.Weixin.TenPayV3.Apis; +using Senparc.Weixin.TenPayV3.Apis.BasePay; +using Senparc.Weixin.TenPayV3.Entities; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net.Http; +using System.Text; +using System.Threading; using System.Threading.Tasks; namespace New_College.Common.Helper { - public class WeChatPayV3 + public static class WeChatPayV3 { + + + /// - /// 扫码支付-Native下单API + /// 扫码支付-Native下单API /// - /// - /// - /// /// - public static async Task QrCodePay(IWeChatPayClient _client, - IOptions _optionsAccessor, WeChatPayTransactionsNativeQuery viewModel) + public static async Task QrCodePay(int Price, string title) { - var model = new WeChatPayTransactionsNativeBodyModel + ///native + string url = "https://api.mch.weixin.qq.com/v3/pay/transactions/native"; + + //使用 Native 支付,输出二维码并展示 + MemoryStream fileStream = null;//输出图片的URL + var price = (int)(Price * 100); + var name = title + " - Native 支付"; + var sp_billno = string.Format("{0}{1}{2}", WeixinConfig.MCHID, SystemTime.Now.ToString("yyyyMMddHHmmss"), + TenPayV3Util.BuildRandomStr(6)); + var notifyUrl = WeixinConfig.NotifyUrl; + TransactionsRequestData requestData = new(WeixinConfig.Appid, WeixinConfig.MCHID, name, sp_billno, new TenpayDateTime(DateTime.Now.AddHours(1)), null, notifyUrl, null, new() { currency = "CNY", total = price }, null, null, null, null); + BasePayApis basePayApis = new BasePayApis(); + var result = await basePayApis.NativeAsync(requestData); + //进行安全签名验证 + if (result.VerifySignSuccess == true) { - AppId = _optionsAccessor.Value.AppId, - MchId = _optionsAccessor.Value.MchId, - Amount = new Essensoft.AspNetCore.Payment.WeChatPay.V3.Domain.Amount { Total = viewModel.Total, Currency = "CNY" }, - Description = viewModel.Description, - NotifyUrl = viewModel.NotifyUrl, - OutTradeNo = viewModel.OutTradeNo, - }; - var request = new WeChatPayTransactionsNativeRequest(); - request.SetBodyModel(model); - return await _client.ExecuteAsync(request, _optionsAccessor.Value); + fileStream = QrCodeHelper.GerQrCodeStream(result.code_url); + } + else + { + fileStream = QrCodeHelper.GetTextImageStream("Native Pay 未能通过签名验证,无法显示二维码"); + } + return fileStream; } + /** + * + * 统一下单 + * @param WxPaydata inputObj 提交给统一下单API的参数 + * @param int timeOut 超时时间 + * @throws WePayException + * @return 成功时返回,其他抛异常 + */ + public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 60) + { + string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; + //检测必填参数 + if (!inputObj.IsSet("out_trade_no")) + { + throw new Exception("缺少统一支付接口必填参数out_trade_no!"); + } + else if (!inputObj.IsSet("body")) + { + throw new Exception("缺少统一支付接口必填参数body!"); + } + else if (!inputObj.IsSet("total_fee")) + { + throw new Exception("缺少统一支付接口必填参数total_fee!"); + } + else if (!inputObj.IsSet("trade_type")) + { + throw new Exception("缺少统一支付接口必填参数trade_type!"); + } + //关联参数 + if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) + { + throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); + } + if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) + { + throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); + } + + //异步通知url未设置,则使用配置文件中的url + if (!inputObj.IsSet("notify_url")) + { + inputObj.SetValue("notify_url", WeixinConfig.NotifyUrl);//异步通知url + } + + inputObj.SetValue("appid", WeixinConfig.Appid);//公众账号ID + inputObj.SetValue("mch_id", WeixinConfig.MCHID);//商户号 + inputObj.SetValue("spbill_create_ip", WePayConfig.IP);//终端ip + inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 + + //签名 + inputObj.SetValue("sign", inputObj.MakeSign()); + string xml = inputObj.ToXml(); + + var start = DateTime.Now; + + // Log.Info("XcxPayApi", "UnfiedOrder request : " + xml); + string response = HttpPost(xml, url, "application/xml", timeOut); + //Log.Info("XcxPayApi", "UnfiedOrder response : " + response); + // WebHookHelper.WebHookmarkdownSend(response); + + var end = DateTime.Now; + int timeCost = (int)((end - start).TotalMilliseconds); + + WxPayData result = new WxPayData(); + result.FromXml(response); + // ReportCostTime(url, timeCost, result);//测速上报网络不好时使用 + return result; + } + /// + /// 生成随机数 + /// + /// + public static string GenerateNonceStr() + { + return Guid.NewGuid().ToString().Replace("-", ""); + } + /// + /// POST请求 + /// + /// + /// + /// application/xml、application/json、application/text、application/x-www-form-urlencoded + /// + /// + /// + public static string HttpPost(string postData, string url, string contentType = null, int timeOut = 30, Dictionary headers = null) + { + postData = postData ?? ""; + + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true + }; + using (HttpClient httpClient = new HttpClient(httpClientHandler)) + { + if (headers != null) + { + foreach (var header in headers) + httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); + } + using (HttpContent client = new StringContent(postData, Encoding.UTF8)) + { + if (contentType != null) + client.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + + HttpResponseMessage response = httpClient.PostAsync(url, client).Result; + return response.Content.ReadAsStringAsync().Result; + } + } + } + } + + + + /// + /// + /// + public class WePayConfig + { + //=======【商户系统后台机器IP】===================================== + /* 此参数可手动配置也可在程序中自动获取 + */ + public const string IP = "8.8.8.8"; + + + //=======【代理服务器设置】=================================== + /* 默认IP和端口号分别为0.0.0.0和0,此时不开启代理(如有需要才设置) + */ + public const string PROXY_URL = ""; + + //=======【上报信息配置】=================================== + /* 测速上报等级,0.关闭上报; 1.仅错误时上报; 2.全量上报 + */ + public const int REPORT_LEVENL = 1; + + //=======【日志级别】=================================== + /* 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息 + */ + public const int LOG_LEVENL = 3; } } diff --git a/New_College.Common/Helper/WeixinHelper.cs b/New_College.Common/Helper/WeixinHelper.cs index ad3da18..86586dc 100644 --- a/New_College.Common/Helper/WeixinHelper.cs +++ b/New_College.Common/Helper/WeixinHelper.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Net.Http; using System.Security.Cryptography; using System.Text; -using Essensoft.AspNetCore.Payment.Alipay.Domain; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; diff --git a/New_College.Common/New_College.Common.csproj b/New_College.Common/New_College.Common.csproj index aeef837..ab60b73 100644 --- a/New_College.Common/New_College.Common.csproj +++ b/New_College.Common/New_College.Common.csproj @@ -13,9 +13,6 @@ - - - @@ -25,6 +22,7 @@ + @@ -32,6 +30,7 @@ + diff --git a/New_College.IServices/IV_OrderInfoServices.cs b/New_College.IServices/IV_OrderInfoServices.cs index 2d48ca4..4421291 100644 --- a/New_College.IServices/IV_OrderInfoServices.cs +++ b/New_College.IServices/IV_OrderInfoServices.cs @@ -1,8 +1,10 @@ using New_College.Common; +using New_College.Common.Helper; using New_College.IServices.BASE; using New_College.Model; using New_College.Model.Models; using New_College.Model.ViewModels; +using System.IO; using System.Threading.Tasks; namespace New_College.IServices @@ -13,31 +15,16 @@ namespace New_College.IServices public interface IV_OrderInfoServices : IBaseServices { + + Task QrCodePay(WeChatPayTransactionsNativeQuery viewModel); + Task CreateOrder(UniOrderQuery query); Task UpdateOrderStatus(string orderNo, EnumPayType payType, string trade_no); - /// - /// 唤醒微信H5支付 - /// - /// - /// - Task> WechatPubPay(WeChatPayQuery query); - /// - /// 微信订单查询确认 传用户Id - /// - /// - /// - Task> WeChatPayStatus(IdQuery query); - /// - /// 支付宝 订单确认 - /// - /// - /// - Task> AliPayStatus(IdQuery query); /// /// 获取vip信息(分页) @@ -53,12 +40,7 @@ namespace New_College.IServices /// Task> PaySuccess(V_OrderInfo query); - /// - /// 支付宝H5支付 - /// - /// - /// - Task> AliH5Pay(WeChatPayQuery query); + } } \ No newline at end of file diff --git a/New_College.IServices/IWeiChatPayServicesV3.cs b/New_College.IServices/IWeiChatPayServicesV3.cs deleted file mode 100644 index 8a02dfb..0000000 --- a/New_College.IServices/IWeiChatPayServicesV3.cs +++ /dev/null @@ -1,16 +0,0 @@ -using New_College.Common.Helper; -using New_College.IServices.BASE; -using New_College.Model.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace New_College.IServices -{ - public interface IWeiChatPayServicesV3 : IBaseServices - { - Task QrCodePay(WeChatPayTransactionsNativeQuery viewModel); - } -} diff --git a/New_College.Model/Models/OccupationMapJobInfo.cs b/New_College.Model/Models/OccupationMapJobInfo.cs index 74717fc..6de26c9 100644 --- a/New_College.Model/Models/OccupationMapJobInfo.cs +++ b/New_College.Model/Models/OccupationMapJobInfo.cs @@ -1,5 +1,4 @@ -using Essensoft.AspNetCore.Payment.Alipay.Domain; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/New_College.Model/ViewModels/AliyunSmsSenderConfig.cs b/New_College.Model/ViewModels/AliyunSmsSenderConfig.cs index 7627d5a..71ab1bc 100644 --- a/New_College.Model/ViewModels/AliyunSmsSenderConfig.cs +++ b/New_College.Model/ViewModels/AliyunSmsSenderConfig.cs @@ -40,30 +40,5 @@ namespace New_College.Model.ViewModels public static string NotifyUrl { get; set; } } - /// - /// - /// - public class WePayConfig - { - //=======【商户系统后台机器IP】===================================== - /* 此参数可手动配置也可在程序中自动获取 - */ - public const string IP = "8.8.8.8"; - - //=======【代理服务器设置】=================================== - /* 默认IP和端口号分别为0.0.0.0和0,此时不开启代理(如有需要才设置) - */ - public const string PROXY_URL = ""; - - //=======【上报信息配置】=================================== - /* 测速上报等级,0.关闭上报; 1.仅错误时上报; 2.全量上报 - */ - public const int REPORT_LEVENL = 1; - - //=======【日志级别】=================================== - /* 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息 - */ - public const int LOG_LEVENL = 3; - } } diff --git a/New_College.Repository/BASE/D_UniversityRankRepository.cs b/New_College.Repository/BASE/D_UniversityRankRepository.cs index 1e3ebcb..2c5c7e1 100644 --- a/New_College.Repository/BASE/D_UniversityRankRepository.cs +++ b/New_College.Repository/BASE/D_UniversityRankRepository.cs @@ -1,5 +1,4 @@ -using Essensoft.AspNetCore.Payment.Alipay.Domain; -using New_College.IRepository; +using New_College.IRepository; using New_College.IRepository.UnitOfWork; using New_College.Model.Models; using New_College.Model.ViewModels; diff --git a/New_College.Services/D_LongIdMapServices.cs b/New_College.Services/D_LongIdMapServices.cs index 4634be2..23d2987 100644 --- a/New_College.Services/D_LongIdMapServices.cs +++ b/New_College.Services/D_LongIdMapServices.cs @@ -20,7 +20,6 @@ using System.Text; using SqlSugar; using System.Linq.Expressions; using Org.BouncyCastle.Asn1.Ocsp; -using Essensoft.AspNetCore.Payment.Alipay.Domain; using StackExchange.Redis; using New_College.Repository; using NPOI.Util.ArrayExtensions; diff --git a/New_College.Services/V_OrderInfoServices.cs b/New_College.Services/V_OrderInfoServices.cs index cb458d3..acfb31a 100644 --- a/New_College.Services/V_OrderInfoServices.cs +++ b/New_College.Services/V_OrderInfoServices.cs @@ -13,31 +13,16 @@ using YIJIYI.Core.Common.Helper; using System; using New_College.Model; using New_College.Common.Helper; -using Microsoft.Extensions.Options; -using Essensoft.AspNetCore.Payment.WeChatPay; -using Essensoft.AspNetCore.Payment.Alipay; using Microsoft.Extensions.Logging; -using Essensoft.AspNetCore.Payment.Alipay.Domain; -using Essensoft.AspNetCore.Payment.Alipay.Request; using System.Collections.Generic; using LinqKit; -using System.Collections.Specialized; -using Org.BouncyCastle.Asn1.Ocsp; -using Microsoft.AspNetCore.Http; +using System.IO; using Microsoft.AspNetCore.Mvc; -using NPOI.SS.Formula.Functions; -using Essensoft.AspNetCore.Payment.WeChatPay.V2.Request; -using Essensoft.AspNetCore.Payment.WeChatPay.V2; namespace New_College.Services { public class V_OrderInfoServices : BaseServices, IV_OrderInfoServices { - private readonly IWeChatPayClient _client; - private readonly IOptions _optionsAccessor; - private readonly IAlipayClient alipayClient; - private readonly IOptions alipayoptions; - private readonly IBaseRepository _dal; private readonly IUnitOfWork _unitOfWork; private readonly IV_CustomerInfoRepository v_CustomerInfoRepository; @@ -45,23 +30,16 @@ namespace New_College.Services private readonly IV_VipCardTypeRepository v_VipCardTypeRepository; private readonly ILogger logger; - private readonly IWeiChatPayServicesV3 _weiChatPayServicesV3; public int Nums = 10; - public V_OrderInfoServices(IWeChatPayClient client, IOptions optionsAccessor - , IAlipayClient IAlipayClient, IOptions alipayopt - , IBaseRepository dal + public V_OrderInfoServices( IBaseRepository dal , IUnitOfWork IUnitOfWork , IV_CustomerInfoRepository IV_CustomerInfoRepository , IV_VipCardInfoRepository IV_VipCardInfoRepository , IV_VipCardTypeRepository IV_VipCardTypeRepository - , ILogger loggers, IWeiChatPayServicesV3 weiChatPayServicesV3 + , ILogger loggers ) { - _client = client; - _optionsAccessor = optionsAccessor; - alipayClient = IAlipayClient; - alipayoptions = alipayopt; this._dal = dal; _unitOfWork = IUnitOfWork; v_CustomerInfoRepository = IV_CustomerInfoRepository; @@ -69,7 +47,6 @@ namespace New_College.Services v_VipCardTypeRepository = IV_VipCardTypeRepository; logger = loggers; base.BaseDal = dal; - _weiChatPayServicesV3 = weiChatPayServicesV3; } @@ -77,9 +54,15 @@ namespace New_College.Services /// 扫码支付-Native下单API /// /// - public async Task QrCodePay(WeChatPayTransactionsNativeQuery viewModel) + public async Task QrCodePay(WeChatPayTransactionsNativeQuery viewModel) { - return await _weiChatPayServicesV3.QrCodePay(viewModel); + viewModel.OutTradeNo = OrderGenerateHelper.GenerateOrderNo("ZY"); + viewModel.Total = 99 * 100; + + + return await WeChatPayV3.QrCodePay(viewModel.Total.Value,"demo"); + + } /// @@ -285,194 +268,8 @@ namespace New_College.Services return status; } - /// - /// 唤醒微信H5支付 - /// - /// - /// - public async Task> WechatPubPay(WeChatPayQuery query) - { - string out_trade_no = DateTime.Now.ToString("yyyyMMddHHmmssfff"); - logger.LogInformation(out_trade_no + "唤醒支付"); - WeChatPayPayRequest viewModel = new WeChatPayPayRequest() { }; - //viewModel.NotifyUrl = string.Format("{0}/api/front/WeixinPay/PayNotify", PayInfoQuery.ApiUrl); - viewModel.NotifyUrl = string.Format("{0}/api/front/HFivePay/WeChartOrderConfirm", PayInfoQuery.ApiUrl); - //viewModel.NotifyUrl = string.Format("{0}/api/front/HFivePay/WeChartOrderConfirm?out_trade_no={1}", PayInfoQuery.ApiUrl, out_trade_no); - viewModel.SpBillCreateIp = PayInfoQuery.CreateIp; - viewModel.OutTradeNo = out_trade_no; - viewModel.TotalFee = Convert.ToInt32(query.Money * 100); - viewModel.Body = "六纬志愿VIP"; - viewModel.TradeType = "MWEB"; - - var response = await WeChatPay.GetMwebUrl(_client, _optionsAccessor, viewModel); - if (response.ReturnCode == "SUCCESS" && response.ReturnMsg == "OK") - { - try - { - //_unitOfWork.BeginTran(); - var order = await _dal.Add(new V_OrderInfo - { - out_trade_no = out_trade_no, - PayType = EnumPayType.WeiPay, - CardTypeId = query.CardTypeId, - Status = EnumOrderType.payment, - CustomerId = query.CustomerId, - Price = query.Money, - PayPrice = query.Money, - Name = "六纬志愿VIP开通", - CardNo = "0", - CardId = 0 - }); - if (order > 0) - { - logger.LogInformation(out_trade_no + "唤醒成功"); - //_unitOfWork.CommitTran(); - return new MessageModel() - { - success = true, - msg = "成功", - response = string.Format("{0}&redirect_url={1}", response.MwebUrl, string.Format("{0}?out_trade_no={1}", string.Format("{0}/api/front/HFivePay/WeChartOrderConfirm", PayInfoQuery.ApiUrl), out_trade_no)) - }; - } - } - catch (Exception ex) - { - //_unitOfWork.RollbackTran(); - return new MessageModel() { success = false, msg = "唤醒微信失败,请重试" }; - } - } - return new MessageModel() { success = false, msg = "唤醒微信失败,请重试" }; - } - - /// - /// 微信订单查询确认 传用户Id - /// - /// - /// - public async Task> WeChatPayStatus(IdQuery query) - { - var orderinfo = await _dal.Query(x => x.CustomerId == query.Id && x.IsDelete == false && x.PayType == EnumPayType.WeiPay); - if (orderinfo.Count <= 0) - return new MessageModel() { success = false, msg = "未查询到用户订单" }; - var result = orderinfo.OrderByDescending(x => x.Id).FirstOrDefault(); - if (result.Status == EnumOrderType.payoff) - { - return new MessageModel() { success = true, msg = "已支付成功" }; - } - var request = new WeChatPayOrderQueryRequest - { - OutTradeNo = result.out_trade_no - }; - //如果订单状态是 - var response = await _client.ExecuteAsync(request, _optionsAccessor.Value); - if (response.TradeState == "NOTPAY") - { - return new MessageModel() { success = false, msg = "您还未支付订单,请重新支付" }; - } - if (response.ReturnCode == "SUCCESS" && response.ReturnMsg == "OK") - { - var payresult = await PaySuccess(result); - if (payresult.success == true) - { - logger.LogInformation("SUCCESS:" + result.out_trade_no); - return new MessageModel() { success = true, msg = "支付成功" }; - } - else - { - return new MessageModel() { success = false, msg = "支付成功,业务处理错误,请联系客服" }; - } - } - return new MessageModel() { success = false, msg = "您还未支付订单,请重新支付或联系客服" }; - } - - /// - /// 支付宝 订单确认 - /// - /// - /// - public async Task> AliPayStatus(IdQuery query) - { - logger.LogInformation("订单确认(支付宝)"); - var orderinfo = await _dal.Query(x => x.CustomerId == query.Id && x.IsDelete == false && x.PayType == EnumPayType.AliPay); - if (orderinfo.Count <= 0) - return new MessageModel() { success = false, msg = "未查询到用户订单" }; - var result = orderinfo.OrderByDescending(x => x.CreateTime).FirstOrDefault(); - if (result.Status == EnumOrderType.payoff) - { - return new MessageModel() { success = true, msg = "已支付成功" }; - } - else - { - return new MessageModel() { success = false, msg = "未支付,如有疑问请联系客服" }; - } - - //var model = new AlipayTradeWapPayModel - //{ - // OutTradeNo = query.out_trade_no, - //}; - //var req = new AlipayTradeWapPayRequest(); - //req.SetBizModel(model); - //var response = await alipayClient.PageExecuteAsync(req, alipayoptions.Value); - //logger.LogInformation(query.out_trade_no + "返回参数" + response.Body); - ////response.Body - ////根绝回调的返回 确认是否支付成功 此处还存在问题---------------------------------- - ////if(response.) - //var payresult = await PaySuccess(result); - //if (payresult.success == true) - //{ - // logger.LogInformation("SUCCESS(支付宝订单确认成功):" + result.out_trade_no); - // return new MessageModel() { success = true, msg = "支付成功" }; - //} - //else - //{ - // return new MessageModel() { success = false, msg = "支付成功,业务处理错误,请联系客服" }; - //} - } - - ///// - ///// 微信订单回调确认 - ///// - ///// - ///// - //public async Task> WeChartOrderConfirm(XmlQuery query) - //{ - // string xmlstr = ""; - // try - // { - // var response = XmlSerializeHelper.XMLToObject(query.xml); - // if (response == null) - // return new MessageModel() { success = false, msg = "订单确认失败", response = xmlstr }; - // var result = (await _dal.Query(x => x.out_trade_no == response.out_trade_no && x.PayType == EnumPayType.WeiPay)).FirstOrDefault(); - // if (result != null && result.Status == EnumOrderType.payoff) - // { - // xmlstr = "< return_code >< ![CDATA[SUCCESS]] >< return_msg >< ![CDATA[OK]] >"; - // return new MessageModel() { success = true, msg = "成功", response = xmlstr }; - // } - // if (response.result_code == "SUCCESS") - // { - // //记录流水 - // var payresult = await PaySuccess(result); - // if (payresult.success == true) - // { - // xmlstr = "< return_code >< ![CDATA[SUCCESS]] >< return_msg >< ![CDATA[OK]] >"; - // return new MessageModel() { success = true, msg = "成功", response = xmlstr }; - // } - // else - // { - // payresult.response = xmlstr; - // return payresult; - // } - - // } - // return new MessageModel() { success = false, msg = "失败", response = xmlstr }; - // } - // catch (Exception ex) - // { - // return new MessageModel() { success = false, msg = "失败", response = xmlstr }; - // } - //} /// /// 支付成功后操作 @@ -586,85 +383,7 @@ namespace New_College.Services return code; } - /// - /// 支付宝H5支付 - /// - /// - /// - public async Task> AliH5Pay(WeChatPayQuery query) - { - logger.LogInformation("唤醒支付宝" + query.CustomerId); - AliOrderResult aliOrder = new AliOrderResult(); - try - { - string out_trade_no = query.CustomerId + "1" + DateTime.Now.ToString("yyyyMMddHHmmssfff"); - - AlipayTradeWapPayQuery viewMode = new AlipayTradeWapPayQuery() { }; - string NotifyUrl = string.Format("{0}/api/front/HFivePay/AliOrderConfirm", PayInfoQuery.ApiUrl); - //生成订单 - //或者失败 - var model = new AlipayTradeWapPayModel - { - Body = "六纬志愿VIP", - Subject = "六纬志愿VIP", - TotalAmount = query.Money.ToString(), - OutTradeNo = out_trade_no, - ProductCode = "QUICK_WAP_WAY", - TimeoutExpress = "90m" - }; - var req = new AlipayTradeWapPayRequest(); - req.SetBizModel(model); - req.SetNotifyUrl(NotifyUrl); - req.SetReturnUrl("https://payment.yrtsedu.cn/alipay.html"); - var response = await alipayClient.PageExecuteAsync(req, alipayoptions.Value); - if (response == null) - { - logger.LogInformation("唤醒失败" + out_trade_no); - return new MessageModel() { success = false, msg = "支付宝唤醒失败" }; - } - logger.LogInformation("唤醒成功" + out_trade_no); - //_unitOfWork.BeginTran(); - var order = await _dal.Add(new V_OrderInfo - { - out_trade_no = out_trade_no, - PayType = EnumPayType.AliPay, - CardTypeId = query.CardTypeId, - Status = EnumOrderType.payment, - CustomerId = query.CustomerId, - Price = query.Money, - PayPrice = query.Money, - Name = "六纬志愿VIP开通", - CardNo = "0", - CardId = 0 - }); - if (order > 0) - { - logger.LogInformation(out_trade_no + "唤醒成功"); - //_unitOfWork.CommitTran(); - return new MessageModel() - { - //?payType='alipay'&&success=true&&customerId=" + query.CustomerId + "&&out_trade_no=" + out_trade_no - success = true, - msg = "获取成功", - response = new AliOrderResult() - { - Body = response.Body, - CustomerId = query.CustomerId, - out_trade_no = out_trade_no, - PayType = "alipay", - Success = true - } - }; - } - return new MessageModel() { success = false, msg = "订单添加失败" }; - } - catch (Exception ex) - { - //_unitOfWork.RollbackTran(); - logger.LogInformation("唤醒异常" + ex.ToString()); - return new MessageModel() { success = false, msg = "支付宝唤醒失败" }; - } - } + } diff --git a/New_College.Services/WeiChatPayServicesV3.cs b/New_College.Services/WeiChatPayServicesV3.cs deleted file mode 100644 index 950b7f7..0000000 --- a/New_College.Services/WeiChatPayServicesV3.cs +++ /dev/null @@ -1,83 +0,0 @@ - -using New_College.IServices; -using New_College.Model.Models; -using New_College.Services.BASE; -using New_College.IRepository.Base; -using New_College.IRepository.UnitOfWork; -using New_College.Model.ViewModels; -using System.Threading.Tasks; -using New_College.Common; -using New_College.IRepository; -using System.Linq; -using YIJIYI.Core.Common.Helper; -using System; -using New_College.Model; -using New_College.Common.Helper; -using Microsoft.Extensions.Options; -using Essensoft.AspNetCore.Payment.WeChatPay; -using Essensoft.AspNetCore.Payment.Alipay; -using Microsoft.Extensions.Logging; -using Essensoft.AspNetCore.Payment.Alipay.Domain; -using Essensoft.AspNetCore.Payment.Alipay.Request; -using System.Collections.Generic; -using LinqKit; -using System.Collections.Specialized; -using Org.BouncyCastle.Asn1.Ocsp; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using NPOI.SS.Formula.Functions; -using Essensoft.AspNetCore.Payment.WeChatPay.V3; - -namespace New_College.Services -{ - public class WeiChatPayServicesV3 : BaseServices, IWeiChatPayServicesV3 - { - private readonly IWeChatPayClient _client; - private readonly IOptions _optionsAccessor; - private readonly IAlipayClient alipayClient; - private readonly IOptions alipayoptions; - - private readonly IBaseRepository _dal; - private readonly IUnitOfWork _unitOfWork; - private readonly IV_CustomerInfoRepository v_CustomerInfoRepository; - private readonly IV_VipCardInfoRepository v_VipCardInfoRepository; - private readonly IV_VipCardTypeRepository v_VipCardTypeRepository; - - private readonly ILogger logger; - - public int Nums = 10; - public WeiChatPayServicesV3(IWeChatPayClient client, IOptions optionsAccessor - , IAlipayClient IAlipayClient, IOptions alipayopt - , IBaseRepository dal - , IUnitOfWork IUnitOfWork - , IV_CustomerInfoRepository IV_CustomerInfoRepository - , IV_VipCardInfoRepository IV_VipCardInfoRepository - , IV_VipCardTypeRepository IV_VipCardTypeRepository - , ILogger loggers - ) - { - _client = client; - _optionsAccessor = optionsAccessor; - alipayClient = IAlipayClient; - alipayoptions = alipayopt; - this._dal = dal; - _unitOfWork = IUnitOfWork; - v_CustomerInfoRepository = IV_CustomerInfoRepository; - v_VipCardInfoRepository = IV_VipCardInfoRepository; - v_VipCardTypeRepository = IV_VipCardTypeRepository; - logger = loggers; - base.BaseDal = dal; - } - - - /// - /// PC端扫描支付生成URL - /// - /// - /// - public async Task QrCodePay(WeChatPayTransactionsNativeQuery viewModel) - { - return await WeChatPayV3.QrCodePay(_client, _optionsAccessor, viewModel); - } - } -} \ No newline at end of file