|
@@ -4,21 +4,21 @@
|
|
|
/// 微信支付服务
|
|
/// 微信支付服务
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
[ApiDescriptionSettings(Order = 99)]
|
|
[ApiDescriptionSettings(Order = 99)]
|
|
|
-public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
|
|
|
|
+public class SysWechatPayService : IDynamicApiController, ITransient
|
|
|
{
|
|
{
|
|
|
- private readonly SqlSugarRepository<WeChatPay> _weChatPayUserRep;
|
|
|
|
|
|
|
+ private readonly SqlSugarRepository<SysWechatPay> _sysWechatPayUserRep;
|
|
|
|
|
|
|
|
- private readonly WeChatPayOptions _weChatPayOptions;
|
|
|
|
|
- public readonly WechatTenpayClient WeChatTenpayClient;
|
|
|
|
|
|
|
+ private readonly WechatPayOptions _wechatPayOptions;
|
|
|
|
|
+ public readonly WechatTenpayClient WechatTenpayClient;
|
|
|
private readonly PayCallBackOptions _payCallBackOptions;
|
|
private readonly PayCallBackOptions _payCallBackOptions;
|
|
|
|
|
|
|
|
- public WeChatPayService(SqlSugarRepository<WeChatPay> weChatPayUserRep,
|
|
|
|
|
- IOptions<WeChatPayOptions> weChatPayOptions,
|
|
|
|
|
|
|
+ public SysWechatPayService(SqlSugarRepository<SysWechatPay> sysWechatPayUserRep,
|
|
|
|
|
+ IOptions<WechatPayOptions> wechatPayOptions,
|
|
|
IOptions<PayCallBackOptions> payCallBackOptions)
|
|
IOptions<PayCallBackOptions> payCallBackOptions)
|
|
|
{
|
|
{
|
|
|
- _weChatPayUserRep = weChatPayUserRep;
|
|
|
|
|
- _weChatPayOptions = weChatPayOptions.Value;
|
|
|
|
|
- WeChatTenpayClient = CreateTenpayClient();
|
|
|
|
|
|
|
+ _sysWechatPayUserRep = sysWechatPayUserRep;
|
|
|
|
|
+ _wechatPayOptions = wechatPayOptions.Value;
|
|
|
|
|
+ WechatTenpayClient = CreateTenpayClient();
|
|
|
_payCallBackOptions = payCallBackOptions.Value;
|
|
_payCallBackOptions = payCallBackOptions.Value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -30,10 +30,10 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
{
|
|
{
|
|
|
var tenpayClientOptions = new WechatTenpayClientOptions()
|
|
var tenpayClientOptions = new WechatTenpayClientOptions()
|
|
|
{
|
|
{
|
|
|
- MerchantId = _weChatPayOptions.MerchantId,
|
|
|
|
|
- MerchantV3Secret = _weChatPayOptions.MerchantV3Secret,
|
|
|
|
|
- MerchantCertificateSerialNumber = _weChatPayOptions.MerchantCertificateSerialNumber,
|
|
|
|
|
- MerchantCertificatePrivateKey = File.ReadAllText(App.WebHostEnvironment.ContentRootPath + _weChatPayOptions.MerchantCertificatePrivateKey),
|
|
|
|
|
|
|
+ MerchantId = _wechatPayOptions.MerchantId,
|
|
|
|
|
+ MerchantV3Secret = _wechatPayOptions.MerchantV3Secret,
|
|
|
|
|
+ MerchantCertificateSerialNumber = _wechatPayOptions.MerchantCertificateSerialNumber,
|
|
|
|
|
+ MerchantCertificatePrivateKey = File.ReadAllText(App.WebHostEnvironment.ContentRootPath + _wechatPayOptions.MerchantCertificatePrivateKey),
|
|
|
PlatformCertificateManager = new InMemoryCertificateManager()
|
|
PlatformCertificateManager = new InMemoryCertificateManager()
|
|
|
};
|
|
};
|
|
|
return new WechatTenpayClient(tenpayClientOptions);
|
|
return new WechatTenpayClient(tenpayClientOptions);
|
|
@@ -44,38 +44,38 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
|
- [HttpPost("/weChatPay/genPayPara")]
|
|
|
|
|
- public dynamic GenerateParametersForJsapiPay(WeChatPayParaInput input)
|
|
|
|
|
|
|
+ [HttpPost("/sysWechatPay/genPayPara")]
|
|
|
|
|
+ public dynamic GenerateParametersForJsapiPay(WechatPayParaInput input)
|
|
|
{
|
|
{
|
|
|
- return WeChatTenpayClient.GenerateParametersForJsapiPayRequest(_weChatPayOptions.AppId, input.PrepayId);
|
|
|
|
|
|
|
+ return WechatTenpayClient.GenerateParametersForJsapiPayRequest(_wechatPayOptions.AppId, input.PrepayId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 微信支付统一下单获取Id(商户直连)
|
|
/// 微信支付统一下单获取Id(商户直连)
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- [HttpPost("/weChatPay/payTransaction")]
|
|
|
|
|
- public async Task<dynamic> CreatePayTransaction([FromBody] WeChatPayTransactionInput input)
|
|
|
|
|
|
|
+ [HttpPost("/sysWechatPay/payTransaction")]
|
|
|
|
|
+ public async Task<dynamic> CreatePayTransaction([FromBody] WechatPayTransactionInput input)
|
|
|
{
|
|
{
|
|
|
var request = new CreatePayTransactionJsapiRequest()
|
|
var request = new CreatePayTransactionJsapiRequest()
|
|
|
{
|
|
{
|
|
|
OutTradeNumber = DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next(100, 1000), // 订单号
|
|
OutTradeNumber = DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next(100, 1000), // 订单号
|
|
|
- AppId = _weChatPayOptions.AppId,
|
|
|
|
|
|
|
+ AppId = _wechatPayOptions.AppId,
|
|
|
Description = input.Description,
|
|
Description = input.Description,
|
|
|
Attachment = input.Attachment,
|
|
Attachment = input.Attachment,
|
|
|
GoodsTag = input.GoodsTag,
|
|
GoodsTag = input.GoodsTag,
|
|
|
ExpireTime = DateTimeOffset.Now.AddMinutes(10),
|
|
ExpireTime = DateTimeOffset.Now.AddMinutes(10),
|
|
|
- NotifyUrl = _payCallBackOptions.WeChatPayUrl,
|
|
|
|
|
|
|
+ NotifyUrl = _payCallBackOptions.WechatPayUrl,
|
|
|
Amount = new CreatePayTransactionJsapiRequest.Types.Amount() { Total = input.Total },
|
|
Amount = new CreatePayTransactionJsapiRequest.Types.Amount() { Total = input.Total },
|
|
|
Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { OpenId = input.OpenId }
|
|
Payer = new CreatePayTransactionJsapiRequest.Types.Payer() { OpenId = input.OpenId }
|
|
|
};
|
|
};
|
|
|
- var response = await WeChatTenpayClient.ExecuteCreatePayTransactionJsapiAsync(request);
|
|
|
|
|
|
|
+ var response = await WechatTenpayClient.ExecuteCreatePayTransactionJsapiAsync(request);
|
|
|
if (!response.IsSuccessful())
|
|
if (!response.IsSuccessful())
|
|
|
throw Oops.Oh(response.ErrorMessage);
|
|
throw Oops.Oh(response.ErrorMessage);
|
|
|
|
|
|
|
|
// 保存订单信息
|
|
// 保存订单信息
|
|
|
- var wechatPay = new WeChatPay()
|
|
|
|
|
|
|
+ var wechatPay = new SysWechatPay()
|
|
|
{
|
|
{
|
|
|
- MerchantId = _weChatPayOptions.MerchantId,
|
|
|
|
|
|
|
+ MerchantId = _wechatPayOptions.MerchantId,
|
|
|
OutTradeNumber = request.OutTradeNumber,
|
|
OutTradeNumber = request.OutTradeNumber,
|
|
|
Description = input.Description,
|
|
Description = input.Description,
|
|
|
Attachment = input.Attachment,
|
|
Attachment = input.Attachment,
|
|
@@ -83,7 +83,7 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
Total = input.Total,
|
|
Total = input.Total,
|
|
|
OpenId = input.OpenId
|
|
OpenId = input.OpenId
|
|
|
};
|
|
};
|
|
|
- await _weChatPayUserRep.InsertAsync(wechatPay);
|
|
|
|
|
|
|
+ await _sysWechatPayUserRep.InsertAsync(wechatPay);
|
|
|
|
|
|
|
|
return new
|
|
return new
|
|
|
{
|
|
{
|
|
@@ -95,35 +95,35 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 微信支付统一下单获取Id(服务商模式)
|
|
/// 微信支付统一下单获取Id(服务商模式)
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- [HttpPost("/weChatPay/payPartnerTransaction")]
|
|
|
|
|
- public async Task<dynamic> CreatePayPartnerTransaction([FromBody] WeChatPayTransactionInput input)
|
|
|
|
|
|
|
+ [HttpPost("/sysWechatPay/payPartnerTransaction")]
|
|
|
|
|
+ public async Task<dynamic> CreatePayPartnerTransaction([FromBody] WechatPayTransactionInput input)
|
|
|
{
|
|
{
|
|
|
var request = new CreatePayPartnerTransactionJsapiRequest()
|
|
var request = new CreatePayPartnerTransactionJsapiRequest()
|
|
|
{
|
|
{
|
|
|
OutTradeNumber = DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next(100, 1000), // 订单号
|
|
OutTradeNumber = DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next(100, 1000), // 订单号
|
|
|
- AppId = _weChatPayOptions.AppId,
|
|
|
|
|
- MerchantId = _weChatPayOptions.MerchantId,
|
|
|
|
|
- SubAppId = _weChatPayOptions.AppId,
|
|
|
|
|
- SubMerchantId = _weChatPayOptions.MerchantId,
|
|
|
|
|
|
|
+ AppId = _wechatPayOptions.AppId,
|
|
|
|
|
+ MerchantId = _wechatPayOptions.MerchantId,
|
|
|
|
|
+ SubAppId = _wechatPayOptions.AppId,
|
|
|
|
|
+ SubMerchantId = _wechatPayOptions.MerchantId,
|
|
|
Description = input.Description,
|
|
Description = input.Description,
|
|
|
Attachment = input.Attachment,
|
|
Attachment = input.Attachment,
|
|
|
GoodsTag = input.GoodsTag,
|
|
GoodsTag = input.GoodsTag,
|
|
|
ExpireTime = DateTimeOffset.Now.AddMinutes(10),
|
|
ExpireTime = DateTimeOffset.Now.AddMinutes(10),
|
|
|
- NotifyUrl = _payCallBackOptions.WeChatPayUrl,
|
|
|
|
|
|
|
+ NotifyUrl = _payCallBackOptions.WechatPayUrl,
|
|
|
Amount = new CreatePayPartnerTransactionJsapiRequest.Types.Amount() { Total = input.Total },
|
|
Amount = new CreatePayPartnerTransactionJsapiRequest.Types.Amount() { Total = input.Total },
|
|
|
Payer = new CreatePayPartnerTransactionJsapiRequest.Types.Payer() { OpenId = input.OpenId }
|
|
Payer = new CreatePayPartnerTransactionJsapiRequest.Types.Payer() { OpenId = input.OpenId }
|
|
|
};
|
|
};
|
|
|
- var response = await WeChatTenpayClient.ExecuteCreatePayPartnerTransactionJsapiAsync(request);
|
|
|
|
|
|
|
+ var response = await WechatTenpayClient.ExecuteCreatePayPartnerTransactionJsapiAsync(request);
|
|
|
if (!response.IsSuccessful())
|
|
if (!response.IsSuccessful())
|
|
|
throw Oops.Oh(response.ErrorMessage);
|
|
throw Oops.Oh(response.ErrorMessage);
|
|
|
|
|
|
|
|
// 保存订单信息
|
|
// 保存订单信息
|
|
|
- var wechatPay = new WeChatPay()
|
|
|
|
|
|
|
+ var wechatPay = new SysWechatPay()
|
|
|
{
|
|
{
|
|
|
- AppId = _weChatPayOptions.AppId,
|
|
|
|
|
- MerchantId = _weChatPayOptions.MerchantId,
|
|
|
|
|
- SubAppId = _weChatPayOptions.AppId,
|
|
|
|
|
- SubMerchantId = _weChatPayOptions.MerchantId,
|
|
|
|
|
|
|
+ AppId = _wechatPayOptions.AppId,
|
|
|
|
|
+ MerchantId = _wechatPayOptions.MerchantId,
|
|
|
|
|
+ SubAppId = _wechatPayOptions.AppId,
|
|
|
|
|
+ SubMerchantId = _wechatPayOptions.MerchantId,
|
|
|
OutTradeNumber = request.OutTradeNumber,
|
|
OutTradeNumber = request.OutTradeNumber,
|
|
|
Description = input.Description,
|
|
Description = input.Description,
|
|
|
Attachment = input.Attachment,
|
|
Attachment = input.Attachment,
|
|
@@ -131,7 +131,7 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
Total = input.Total,
|
|
Total = input.Total,
|
|
|
OpenId = input.OpenId
|
|
OpenId = input.OpenId
|
|
|
};
|
|
};
|
|
|
- await _weChatPayUserRep.InsertAsync(wechatPay);
|
|
|
|
|
|
|
+ await _sysWechatPayUserRep.InsertAsync(wechatPay);
|
|
|
|
|
|
|
|
return new
|
|
return new
|
|
|
{
|
|
{
|
|
@@ -145,32 +145,32 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <param name="tradeId"></param>
|
|
/// <param name="tradeId"></param>
|
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
|
- [HttpGet("/weChatPay/payInfo")]
|
|
|
|
|
- public async Task<WeChatPay> GetWeChatPayInfo(string tradeId)
|
|
|
|
|
|
|
+ [HttpGet("/sysWechatPay/payInfo")]
|
|
|
|
|
+ public async Task<SysWechatPay> GetWechatPayInfo(string tradeId)
|
|
|
{
|
|
{
|
|
|
- return await _weChatPayUserRep.GetFirstAsync(u => u.OutTradeNumber == tradeId);
|
|
|
|
|
|
|
+ return await _sysWechatPayUserRep.GetFirstAsync(u => u.OutTradeNumber == tradeId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 微信支付成功回调(商户直连)
|
|
/// 微信支付成功回调(商户直连)
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
|
- [HttpPost("/notify/weChatPay/payCallBack")]
|
|
|
|
|
|
|
+ [HttpPost("/sysWechatPay/notify/payCallBack")]
|
|
|
[AllowAnonymous]
|
|
[AllowAnonymous]
|
|
|
- public async Task<WeChatPayOutput> WeChatPayCallBack()
|
|
|
|
|
|
|
+ public async Task<WechatPayOutput> WechatPayCallBack()
|
|
|
{
|
|
{
|
|
|
using var ms = new MemoryStream();
|
|
using var ms = new MemoryStream();
|
|
|
await App.HttpContext.Request.Body.CopyToAsync(ms);
|
|
await App.HttpContext.Request.Body.CopyToAsync(ms);
|
|
|
var b = ms.ToArray();
|
|
var b = ms.ToArray();
|
|
|
var callbackJson = Encoding.UTF8.GetString(b);
|
|
var callbackJson = Encoding.UTF8.GetString(b);
|
|
|
|
|
|
|
|
- var callbackModel = WeChatTenpayClient.DeserializeEvent(callbackJson);
|
|
|
|
|
|
|
+ var callbackModel = WechatTenpayClient.DeserializeEvent(callbackJson);
|
|
|
if ("TRANSACTION.SUCCESS".Equals(callbackModel.EventType))
|
|
if ("TRANSACTION.SUCCESS".Equals(callbackModel.EventType))
|
|
|
{
|
|
{
|
|
|
- var callbackResource = WeChatTenpayClient.DecryptEventResource<TransactionResource>(callbackModel);
|
|
|
|
|
|
|
+ var callbackResource = WechatTenpayClient.DecryptEventResource<TransactionResource>(callbackModel);
|
|
|
|
|
|
|
|
// 修改订单支付状态
|
|
// 修改订单支付状态
|
|
|
- var wechatPay = await _weChatPayUserRep.GetFirstAsync(u => u.OutTradeNumber == callbackResource.OutTradeNumber
|
|
|
|
|
|
|
+ var wechatPay = await _sysWechatPayUserRep.GetFirstAsync(u => u.OutTradeNumber == callbackResource.OutTradeNumber
|
|
|
&& u.MerchantId == callbackResource.MerchantId);
|
|
&& u.MerchantId == callbackResource.MerchantId);
|
|
|
if (wechatPay == null) return null;
|
|
if (wechatPay == null) return null;
|
|
|
//wechatPay.OpenId = callbackResource.Payer.OpenId; // 支付者标识
|
|
//wechatPay.OpenId = callbackResource.Payer.OpenId; // 支付者标识
|
|
@@ -185,9 +185,9 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
wechatPay.PayerTotal = callbackResource.Amount.PayerTotal; // 用户支付金额
|
|
wechatPay.PayerTotal = callbackResource.Amount.PayerTotal; // 用户支付金额
|
|
|
wechatPay.SuccessTime = callbackResource.SuccessTime; // 支付完成时间
|
|
wechatPay.SuccessTime = callbackResource.SuccessTime; // 支付完成时间
|
|
|
|
|
|
|
|
- await _weChatPayUserRep.AsUpdateable(wechatPay).IgnoreColumns(true).ExecuteCommandAsync();
|
|
|
|
|
|
|
+ await _sysWechatPayUserRep.AsUpdateable(wechatPay).IgnoreColumns(true).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
- return new WeChatPayOutput()
|
|
|
|
|
|
|
+ return new WechatPayOutput()
|
|
|
{
|
|
{
|
|
|
Total = wechatPay.Total,
|
|
Total = wechatPay.Total,
|
|
|
Attachment = wechatPay.Attachment,
|
|
Attachment = wechatPay.Attachment,
|
|
@@ -202,22 +202,22 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
/// 微信支付成功回调(服务商模式)
|
|
/// 微信支付成功回调(服务商模式)
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
|
- [HttpPost("/notify/weChatPay/payPartnerCallback")]
|
|
|
|
|
|
|
+ [HttpPost("/sysWechatPay/notify/payPartnerCallback")]
|
|
|
[AllowAnonymous]
|
|
[AllowAnonymous]
|
|
|
- public async Task WeChatPayPartnerCallBack()
|
|
|
|
|
|
|
+ public async Task WechatPayPartnerCallBack()
|
|
|
{
|
|
{
|
|
|
using var ms = new MemoryStream();
|
|
using var ms = new MemoryStream();
|
|
|
await App.HttpContext.Request.Body.CopyToAsync(ms);
|
|
await App.HttpContext.Request.Body.CopyToAsync(ms);
|
|
|
var b = ms.ToArray();
|
|
var b = ms.ToArray();
|
|
|
var callbackJson = Encoding.UTF8.GetString(b);
|
|
var callbackJson = Encoding.UTF8.GetString(b);
|
|
|
|
|
|
|
|
- var callbackModel = WeChatTenpayClient.DeserializeEvent(callbackJson);
|
|
|
|
|
|
|
+ var callbackModel = WechatTenpayClient.DeserializeEvent(callbackJson);
|
|
|
if ("TRANSACTION.SUCCESS".Equals(callbackModel.EventType))
|
|
if ("TRANSACTION.SUCCESS".Equals(callbackModel.EventType))
|
|
|
{
|
|
{
|
|
|
- var callbackResource = WeChatTenpayClient.DecryptEventResource<PartnerTransactionResource>(callbackModel);
|
|
|
|
|
|
|
+ var callbackResource = WechatTenpayClient.DecryptEventResource<PartnerTransactionResource>(callbackModel);
|
|
|
|
|
|
|
|
// 修改订单支付状态
|
|
// 修改订单支付状态
|
|
|
- var wechatPay = await _weChatPayUserRep.GetFirstAsync(u => u.OutTradeNumber == callbackResource.OutTradeNumber
|
|
|
|
|
|
|
+ var wechatPay = await _sysWechatPayUserRep.GetFirstAsync(u => u.OutTradeNumber == callbackResource.OutTradeNumber
|
|
|
&& u.MerchantId == callbackResource.MerchantId);
|
|
&& u.MerchantId == callbackResource.MerchantId);
|
|
|
if (wechatPay == null) return;
|
|
if (wechatPay == null) return;
|
|
|
//wechatPay.OpenId = callbackResource.Payer.OpenId; // 支付者标识
|
|
//wechatPay.OpenId = callbackResource.Payer.OpenId; // 支付者标识
|
|
@@ -232,7 +232,7 @@ public class WeChatPayService : IDynamicApiController, ITransient
|
|
|
wechatPay.PayerTotal = callbackResource.Amount.PayerTotal; // 用户支付金额
|
|
wechatPay.PayerTotal = callbackResource.Amount.PayerTotal; // 用户支付金额
|
|
|
wechatPay.SuccessTime = callbackResource.SuccessTime; // 支付完成时间
|
|
wechatPay.SuccessTime = callbackResource.SuccessTime; // 支付完成时间
|
|
|
|
|
|
|
|
- await _weChatPayUserRep.AsUpdateable(wechatPay).IgnoreColumns(true).ExecuteCommandAsync();
|
|
|
|
|
|
|
+ await _sysWechatPayUserRep.AsUpdateable(wechatPay).IgnoreColumns(true).ExecuteCommandAsync();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|