mahjong_group/server/CoreCms.Net.Web.WebApi/Controllers/PayNotify/WeChatPayController.cs
2026-01-01 14:35:52 +08:00

148 lines
6.5 KiB
C#

/***********************************************************************
* Project: CoreCms
* ProjectName: 核心内容管理系统
* Web: https://www.corecms.net
* Author: 大灰灰
* Email: jianweie@163.com
* CreateTime: 2021/1/31 21:45:10
* Description: 暂无
***********************************************************************/
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using NLog;
using System;
using System.Threading.Tasks;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using Essensoft.Paylink.WeChatPay;
using Essensoft.Paylink.WeChatPay.V2;
using Essensoft.Paylink.WeChatPay.V2.Notify;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Text;
namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
{
/// <summary>
/// 微信支付异步通知
/// </summary>
[Route("Notify/[controller]/[action]")]
public class WeChatPayController : ControllerBase
{
private readonly ICoreCmsBillPaymentsServices _billPaymentsServices;
private readonly ICoreCmsBillRefundServices _billRefundServices;
private readonly IWeChatPayNotifyClient _client;
private readonly IOptions<WeChatPayOptions> _optionsAccessor;
private readonly IRedisOperationRepository _redisOperationRepository;
/// <summary>
/// 构造函数
/// </summary>
public WeChatPayController(
IWeChatPayNotifyClient client
, IOptions<WeChatPayOptions> optionsAccessor
, ICoreCmsBillPaymentsServices billPaymentsServices, ICoreCmsBillRefundServices billRefundServices, IRedisOperationRepository redisOperationRepository)
{
_client = client;
_optionsAccessor = optionsAccessor;
_billPaymentsServices = billPaymentsServices;
_billRefundServices = billRefundServices;
_redisOperationRepository = redisOperationRepository;
}
/// <summary>
/// 统一下单支付结果通知
/// </summary>
[HttpPost]
public async Task<IActionResult> Unifiedorder()
{
try
{
// 先读取原始请求 Body
//Request.EnableBuffering(); // 允许多次读取流
//string body = "";
//using (var reader = new StreamReader(Request.Body, Encoding.UTF8, leaveOpen: true))
//{
// body = await reader.ReadToEndAsync();
// Request.Body.Position = 0; // 重置流指针,保证后续能再次读取
//}
// 打印原始请求内容(方便定位问题)
// NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "微信支付成功回调原始请求", body);
var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
// 打印解析后的对象
NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "微信支付成功回调解析结果", JsonConvert.SerializeObject(notify));
if (notify.ReturnCode == WeChatPayCode.Success)
{
await _redisOperationRepository.ListLeftPushAsync(RedisMessageQueueKey.WeChatPayNotice, JsonConvert.SerializeObject(notify));
return WeChatPayNotifyResult.Success;
}
NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "微信支付成功回调", JsonConvert.SerializeObject(notify));
return NoContent();
}
catch (Exception ex)
{
NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "微信支付成功回调", "统一下单支付结果通知" + $"异常: {ex.Message}", ex);
return NoContent();
}
}
/// <summary>
/// Retrieves the current WeChat Pay configuration options.
/// </summary>
/// <remarks>Use this method to obtain the active WeChat Pay settings configured for the
/// application. The returned options can be used to verify or display payment configuration details.</remarks>
/// <returns>An <see cref="IActionResult"/> containing the WeChat Pay options. The result has an HTTP 200 status code and
/// includes the options in the response body.</returns>
[HttpGet]
public IActionResult GetWeChatPayOptions()
{
return Ok(_optionsAccessor.Value);
}
/// <summary>
/// 退款结果通知
/// </summary>
[HttpPost]
public async Task<IActionResult> Refund()
{
try
{
Request.EnableBuffering(); // 允许多次读取流
string body = "";
using (var reader = new StreamReader(Request.Body, Encoding.UTF8, leaveOpen: true))
{
body = await reader.ReadToEndAsync();
Request.Body.Position = 0; // 重置流指针,保证后续能再次读取
}
//打印原始请求内容(方便定位问题)
NLogUtil.WriteAll(LogLevel.Trace, LogType.Refund, "退款收到消息", body);
var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
NLogUtil.WriteAll(LogLevel.Trace, LogType.Refund, "订单已全额退款", JsonConvert.SerializeObject(notify));
if (notify.ReturnCode == WeChatPayCode.Success)
if (notify.RefundStatus == WeChatPayCode.Success)
{
//Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
var memo = JsonConvert.SerializeObject(notify);
await _billRefundServices.UpdateAsync(p => new CoreCmsBillRefund { memo = memo }, p => p.refundId == notify.OutTradeNo);
return WeChatPayNotifyResult.Success;
}
return NoContent();
}
catch (Exception ex)
{
NLogUtil.WriteAll(LogLevel.Trace, LogType.Refund, "微信退款结果通知", "退款结果通知", ex);
return NoContent();
}
}
}
}