【新增】实现支付宝小程序适配。

This commit is contained in:
jianweie
2024-04-22 23:04:58 +08:00
parent 8a4fe681cb
commit a6ad9274e1
53 changed files with 13032 additions and 107 deletions

View File

@@ -18,19 +18,19 @@ using System.Threading.Tasks;
using NLog;
using System;
using System.Globalization;
using CoreCms.Net.Model.Options;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.Alipay.Request;
using Microsoft.Extensions.DependencyInjection;
using Essensoft.Paylink.WeChatPay;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using static Aliyun.OSS.Model.LiveChannelStat;
using Essensoft.Paylink.WeChatPay.V2.Request;
using Essensoft.Paylink.WeChatPay.V2;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ShopCouponGetResponse.Types.Result.Types.Coupon.Types.CouponDetail.Types.Discount.Types.DiscountCondidtion.Types;
using Aop.Api;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.WxaICPApplyICPFilingRequest.Types;
using Microsoft.IdentityModel.Tokens;
namespace CoreCms.Net.Services
{
@@ -42,17 +42,19 @@ namespace CoreCms.Net.Services
private readonly IAlipayClient _client;
private readonly IServiceProvider _serviceProvider;
private readonly IAlipayConfigServices _alipayConfigServices;
private readonly AliPayOptions _options;
public AliPayServices(IWeChatPayRepository dal, IServiceProvider serviceProvider, IAlipayClient client, IAlipayConfigServices alipayConfigServices)
public AliPayServices(IWeChatPayRepository dal, IServiceProvider serviceProvider, IAlipayClient client, IAlipayConfigServices alipayConfigServices, IOptions<AliPayOptions> options)
{
_serviceProvider = serviceProvider;
_client = client;
_alipayConfigServices = alipayConfigServices;
_options = options.Value;
BaseDal = dal;
}
#region
/// <summary>
/// 发起支付宝支付
/// </summary>
@@ -63,7 +65,14 @@ namespace CoreCms.Net.Services
var jm = new WebApiCallBack();
using var container = _serviceProvider.CreateScope();
var billPaymentsServices = container.ServiceProvider.GetService<ICoreCmsBillPaymentsServices>();
//var payment = await billPaymentsServices.QueryByClauseAsync(p => p.paymentId == entity.paymentId);
var _aliPayUserInfoServices = container.ServiceProvider.GetService<ICoreCmsAliPayUserInfoServices>();
var aliUserInfo = await _aliPayUserInfoServices.QueryByClauseAsync(p => p.userInfoId == entity.userId);
if (aliUserInfo == null)
{
jm.msg = "支付宝下单用户获取失败";
return jm;
}
var tradeType = GlobalEnumVars.AliPayPayTradeType.ScanQRCodes.ToString();
if (!string.IsNullOrEmpty(entity.parameters))
@@ -73,8 +82,6 @@ namespace CoreCms.Net.Services
tradeType = PayHelper.GetAliPayPayTradeType(value.ObjectToString());
}
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appType == tradeType);
if (config == null)
{
@@ -90,7 +97,6 @@ namespace CoreCms.Net.Services
var notifyUrl = config.notifyUrl.EndsWith("/") ? config.notifyUrl + "m-" + config.appId : config.notifyUrl + "/m-" + config.appId;
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
@@ -132,30 +138,43 @@ namespace CoreCms.Net.Services
jm.status = !response.IsError;
}
//支付宝小程序支付
else if (tradeType == GlobalEnumVars.AliPayPayTradeType.JSAPI.ToString())
{
var model = new AlipayTradePagePayModel
Aop.Api.IAopClient alipayClient = new Aop.Api.DefaultAopClient(_options.AliPublicApi, _options.AppId, _options.AppSecret, "json", "1.0", "RSA2", _options.AliPublicKey, "utf-8", false);
Aop.Api.Request.AlipayTradeCreateRequest request = new Aop.Api.Request.AlipayTradeCreateRequest();
Aop.Api.Domain.AlipayTradeCreateModel model = new Aop.Api.Domain.AlipayTradeCreateModel();
model.OutTradeNo = entity.paymentId;
model.TotalAmount = entity.money.ToString(CultureInfo.InvariantCulture);
model.Subject = entity.payTitle.Length > 40 ? entity.payTitle[..40] : entity.payTitle;
model.ProductCode = "JSAPI_PAY";
model.OpAppId = _options.AppId;
model.Body = entity.payTitle.Length > 40 ? entity.payTitle[..40] : entity.payTitle;
if (!string.IsNullOrEmpty(aliUserInfo.userId))
{
OutTradeNo = entity.paymentId,
Subject = entity.payTitle.Length > 40 ? entity.payTitle[..40] : entity.payTitle,
ProductCode = "JSAPI_PAY",
TotalAmount = entity.money.ToString(CultureInfo.InvariantCulture),
Body = entity.payTitle.Length > 40 ? entity.payTitle[..40] : entity.payTitle
model.BuyerId = aliUserInfo.userId;
}
if (!string.IsNullOrEmpty(aliUserInfo.openId))
{
model.OpBuyerOpenId = aliUserInfo.openId;
}
request.SetBizModel(model);
request.SetNotifyUrl(notifyUrl);
Aop.Api.Response.AlipayTradeCreateResponse response = alipayClient.Execute(request);
jm.data = new
{
entity.paymentId,
response.TradeNo
};
var req = new AlipayTradePagePayRequest();
req.SetBizModel(model);
req.SetNotifyUrl(notifyUrl);
//req.SetReturnUrl(config.jumpUrl);
Loging.NLogUtil.WriteAll(NLog.LogLevel.Trace, Loging.LogType.Order, "支付宝JSAPI支付拼接支付宝小程序入参", JsonConvert.SerializeObject(model));
var response = await _client.PageExecuteAsync(req, payOptions);
Loging.NLogUtil.WriteAll(NLog.LogLevel.Trace, Loging.LogType.Order, "支付宝JSAPI支付返回数据", JsonConvert.SerializeObject(response));
jm.data = response.Body;
//response.TradeNo = entity.paymentId;
jm.otherData = response;
jm.status = !response.IsError;
jm.msg = response.IsError ? response.SubMsg : "支付宝小程序支付参数构建成功。";
}
//PC网站支付
else if (tradeType == GlobalEnumVars.AliPayPayTradeType.JSAPI_PC.ToString())
@@ -179,7 +198,7 @@ namespace CoreCms.Net.Services
jm.data = response.Body;
//response.TradeNo = entity.paymentId;
response.TradeNo = entity.paymentId;
jm.otherData = response;
jm.status = !response.IsError;
@@ -245,8 +264,9 @@ namespace CoreCms.Net.Services
return jm;
}
#endregion
#region 退
/// <summary>
/// 用户退款
/// </summary>
@@ -315,6 +335,47 @@ namespace CoreCms.Net.Services
return jm;
}
#endregion
#region 访(alipay.system.oauth.token)
/// <summary>
/// 根据票据返回app_auth_token相关信息
/// </summary>
/// <param name="code">票据</param>
/// <returns></returns>
public Aop.Api.Response.AlipaySystemOauthTokenResponse GetAliPayAppAuthTokenBYCode(string code)
{
Aop.Api.IAopClient alipayClient = new Aop.Api.DefaultAopClient(_options.AliPublicApi, _options.AppId, _options.AppSecret, "json", "1.0", "RSA2", _options.AliPublicKey, "utf-8", false);
Aop.Api.Request.AlipaySystemOauthTokenRequest request = new Aop.Api.Request.AlipaySystemOauthTokenRequest();
request.GrantType = "authorization_code";
request.Code = code;
request.RefreshToken = _options.AliAccessTokenRefresh;
Aop.Api.Response.AlipaySystemOauthTokenResponse response = alipayClient.Execute(request);
return response;
}
#endregion
#region (alipay.user.info.share)
/// <summary>
/// 根据Token返回支付宝用户基本信息
/// </summary>
/// <param name="token">Token</param>
public Aop.Api.Response.AlipayUserInfoShareResponse GetAliPayUserInfoByToken(string token)
{
Aop.Api.IAopClient alipayClient = new Aop.Api.DefaultAopClient(_options.AliPublicApi, _options.AppId, _options.AppSecret, "json", "1.0", "RSA2", _options.AliPublicKey, "utf-8", false);
Aop.Api.Request.AlipayUserInfoShareRequest request = new Aop.Api.Request.AlipayUserInfoShareRequest();
Aop.Api.Response.AlipayUserInfoShareResponse response = alipayClient.Execute(request, token);
return response;
}
#endregion
}