DotNetCore.SKIT.FlurlHttpCl.../samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayRefundController.cs

60 lines
2.3 KiB
C#
Raw Normal View History

2024-01-29 23:12:37 +08:00
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
[ApiController]
[Route("api/refund")]
2021-12-04 16:11:49 +08:00
public class TenpayRefundController : ControllerBase
{
private readonly ILogger _logger;
2021-12-04 16:11:49 +08:00
private readonly Options.TenpayOptions _tenpayOptions;
2024-01-29 23:12:37 +08:00
private readonly Services.HttpClients.IWechatTenpayClientFactory _wechatTenpayClientFactory;
2021-12-04 16:11:49 +08:00
public TenpayRefundController(
ILoggerFactory loggerFactory,
2021-12-04 16:11:49 +08:00
IOptions<Options.TenpayOptions> tenpayOptions,
2024-01-29 23:12:37 +08:00
Services.HttpClients.IWechatTenpayClientFactory wechatTenpayClientFactory)
{
_logger = loggerFactory.CreateLogger(GetType());
2021-12-04 16:11:49 +08:00
_tenpayOptions = tenpayOptions.Value;
2024-01-29 23:12:37 +08:00
_wechatTenpayClientFactory = wechatTenpayClientFactory;
}
[HttpPost]
[Route("")]
public async Task<IActionResult> CreateRefund([FromBody] Models.CreateRefundRequest requestModel)
{
2024-02-06 14:25:41 +08:00
var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId);
var request = new CreateRefundDomesticRefundRequest()
{
TransactionId = requestModel.TransactionId,
OutRefundNumber = "SAMPLE_ORN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"),
Amount = new CreateRefundDomesticRefundRequest.Types.Amount()
{
Total = requestModel.OrderAmount,
Refund = requestModel.RefundAmount
},
Reason = "示例退款",
2021-12-04 16:11:49 +08:00
NotifyUrl = _tenpayOptions.NotifyUrl
};
var response = await client.ExecuteCreateRefundDomesticRefundAsync(request, cancellationToken: HttpContext.RequestAborted);
if (!response.IsSuccessful())
{
_logger.LogWarning(
"申请退款失败(状态码:{0},错误代码:{1},错误描述:{2})。",
2024-01-29 23:12:37 +08:00
response.GetRawStatus(), response.ErrorCode, response.ErrorMessage
);
}
return new JsonResult(response);
}
}
}