using System.Net.Cache; using System.Security.Cryptography.X509Certificates; using System; using Aop.Api; using WingServerCommon.Config; using WingServerCommon.Config.Parameters; using Aop.Api.Request; using Newtonsoft.Json; using Aop.Api.Domain; using System.Collections.Generic; using WingServerCommon.Log; namespace WingPaymentService.Common { /// /// 支付宝支付 /// public class AlipayCommon { private readonly string _protocol = ConfigurationManager.GetParammeter("Alipay", "Protocol").Value; private readonly string _gatewayHost = ConfigurationManager.GetParammeter("Alipay", "GatewayHost").Value; private readonly string _signType = ConfigurationManager.GetParammeter("Alipay", "SignType").Value; private readonly string _appId = ConfigurationManager.GetParammeter("Alipay", "AppId").Value; private readonly string _merchantPrivateKey = ConfigurationManager.GetParammeter("Alipay", "MerchantPrivateKey").Value; private readonly string _alipayPublicKey = ConfigurationManager.GetParammeter("Alipay", "AlipayPublicKey").Value; private readonly string _merchantCertPath = ConfigurationManager.GetParammeter("Alipay", "MerchantCertPath").Value; private readonly string _certPath = ConfigurationManager.GetParammeter("Alipay", "AlipayCertPath").Value; private readonly string _rootCertPath = ConfigurationManager.GetParammeter("Alipay", "AlipayRootCertPath").Value; private readonly string _notifyUrl = ConfigurationManager.GetParammeter("Alipay", "NotifyUrl").Value; /// /// 支付宝网站支付 /// /// /// /// /// public (string, string) AlipayPagePay(string subject, double totalAmount, string orderCode, string recordCode) { try { var serverUrl = $"{_protocol}://{_gatewayHost}/gateway.do"; // 组装业务参数model AlipayTradePagePayModel model = new AlipayTradePagePayModel { Subject = subject, TotalAmount = totalAmount.ToString("0.00"), OutTradeNo = orderCode, ProductCode = "QUICK_WAP_WAY", StoreId = recordCode }; // 1. 创建IAopClient实例 IAopClient client = new DefaultAopClient ( serverUrl, _appId, //请更换为您的AppId _merchantPrivateKey, //请更换为您的PKCS1格式的应用私钥 "json", "1.0", _signType, _alipayPublicKey, "utf-8" //请更换为您使用的字符集编码,推荐采用utf-8 ); Aop.Api.Request.AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); // 设置异步通知接收地址 request.SetNotifyUrl(_notifyUrl); request.SetProdCode("FAST_INSTANT_TRADE_PAY"); request.SetBizModel(model); // 3. 发起请求并处理响应 Aop.Api.Response.AlipayTradePagePayResponse response = client.SdkExecute(request); if (!response.IsError) { var url = $"{serverUrl}?{response.Body}"; Logger.WriteLineInfo($"AlipayPagePay success pay url:{url}"); return (url, JsonConvert.SerializeObject(request)); } else { Logger.WriteLineError($"AlipayPagePay error:{response.Msg},{response.SubMsg}"); return (string.Empty, JsonConvert.SerializeObject(request)); } } catch (Exception ex) { Logger.WriteLineError($"AlipayPagePay error:{ex}"); return (string.Empty, $"AlipayPagePay error:{ex}"); } } /// /// 支付宝手机网站支付 /// /// /// /// /// public (string, string) AlipayWapPay(string subject, double totalAmount, string orderCode, string recordCode) { try { var serverUrl = $"{_protocol}://{_gatewayHost}/gateway.do"; // 组装业务参数model AlipayTradeWapPayModel model = new AlipayTradeWapPayModel { Subject = subject, TotalAmount = totalAmount.ToString("0.00"), OutTradeNo = orderCode, ProductCode = "QUICK_WAP_WAY", StoreId = recordCode }; // 1. 创建IAopClient实例 IAopClient client = new DefaultAopClient ( _gatewayHost, _appId, //请更换为您的AppId _merchantPrivateKey, //请更换为您的PKCS1格式的应用私钥 "json", "1.0", _signType, _alipayPublicKey, "utf-8" //请更换为您使用的字符集编码,推荐采用utf-8 ); Aop.Api.Request.AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); // 设置异步通知接收地址 request.SetNotifyUrl(_notifyUrl); request.SetProdCode("QUICK_WAP_WAY"); request.SetBizModel(model); // 3. 发起请求并处理响应 Aop.Api.Response.AlipayTradeWapPayResponse response = client.SdkExecute(request); if (!response.IsError) { var url = $"{serverUrl}?{response.Body}"; Logger.WriteLineInfo($"AlipayWapPay success pay url:{url}"); return (url, JsonConvert.SerializeObject(request)); } else { Logger.WriteLineError($"AlipayWapPay error:{response.Msg},{response.SubMsg}"); return (string.Empty, JsonConvert.SerializeObject(request)); } } catch (Exception ex) { Logger.WriteLineError($"AlipayWapPay error:{ex}"); return (string.Empty, $"AlipayWapPay error:{ex}"); } } } }