This commit is contained in:
南浔
2025-08-05 16:09:57 +08:00
5 changed files with 93 additions and 8 deletions
@@ -17,16 +17,17 @@ namespace Apimanager_backend.Controllers
private IOrderService _orderService;
private IPaymentConfigService _paymentService;
private IPayService _payService;
public PayController(ILogger<PayController> logger,IOrderService order,IPaymentConfigService paymentConfigService,IPayService payService)
public PayController(ILogger<PayController> logger, IOrderService order, IPaymentConfigService paymentConfigService, IPayService payService)
{
_logger = logger;
_orderService = order;
_paymentService = paymentConfigService;
_payService = payService;
}
//创建支付订单
[HttpPost]
[Authorize(Roles = "User")]
public async Task<IActionResult> CreatePayment([FromBody]CreatePaymentDto dto)
public async Task<IActionResult> CreatePayment([FromBody] CreatePaymentDto dto)
{
var userId = User.Claims.First(x => x.Type == "userId").Value;
//获取支付接口信息
@@ -41,17 +42,18 @@ namespace Apimanager_backend.Controllers
switch (paymentConfig.PayType)
{
case PayType.None:
throw new BaseException(4001,"当前支付方式未配置");
throw new BaseException(4001, "当前支付方式未配置");
case PayType.Epay:
var epayRes = await _payService.CreateEpay(orderRes, paymentConfig,dto.ReturnUrl);
var epayRes = await _payService.CreateEpay(orderRes, paymentConfig, dto.ReturnUrl);
return Ok(epayRes);
default:
throw new BaseException(4001, "不支持的支付方式");
}
}
//支付完成通知
[HttpGet]
public async Task<IActionResult> Notice(
int pid, string trade_no,string out_trade_no
int pid, string trade_no, string out_trade_no
, string type, string name, decimal money
, string trade_status, string sign, string sign_type
)
@@ -66,8 +68,9 @@ namespace Apimanager_backend.Controllers
param["trade_status"] = trade_status;
param["sign"] = sign;
param["sign_type"] = sign_type;
//获取支付配置key,用于签名校验
PaymentConfig paymentConfig = await _paymentService.GetPaymentConfigInfoByTypeAsync(type);
bool verifyRes = EpayHelper.VerifySign(param,paymentConfig.SecretKey);
bool verifyRes = EpayHelper.VerifySign(param, paymentConfig.SecretKey);
if (!verifyRes)
{
throw new BaseException(4001, "签名校验失败");
@@ -79,5 +82,16 @@ namespace Apimanager_backend.Controllers
await _orderService.UpdateOrderAsync(orderDto);
return Ok("Success");
}
[HttpPost]
[Authorize( Roles = "User")]
public async Task<IActionResult> Buy([FromBody]BuyDto dto)
{
var userId = User.Claims.First(x => x.Type == "userId").Value;
dto.UserId = int.Parse(userId);
await _payService.BuyAsync(dto);
var res = new ResponseBase<object?>(1000,"订购成功",null);
return Ok(res);
}
}
}