更新20260316
This commit is contained in:
@@ -9,10 +9,12 @@ namespace PetWash.Api.Controllers;
|
||||
public class OrdersController : ControllerBase
|
||||
{
|
||||
private readonly OrderService _orderService;
|
||||
private readonly WeChatPayService _weChatPayService;
|
||||
|
||||
public OrdersController(OrderService orderService)
|
||||
public OrdersController(OrderService orderService, WeChatPayService weChatPayService)
|
||||
{
|
||||
_orderService = orderService;
|
||||
_weChatPayService = weChatPayService;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -21,12 +23,24 @@ public class OrdersController : ControllerBase
|
||||
try
|
||||
{
|
||||
var order = await _orderService.CreateOrderAsync(request.PackageId);
|
||||
return Ok(order);
|
||||
var payment = await _weChatPayService.CreateNativePayAsync(order, HttpContext.RequestAborted);
|
||||
|
||||
return Ok(new CreateOrderResponse
|
||||
{
|
||||
Order = order,
|
||||
CodeUrl = payment.CodeUrl,
|
||||
OutTradeNo = payment.OutTradeNo,
|
||||
ExpiresAt = payment.ExpiresAt
|
||||
});
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status502BadGateway, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("{id}/payment")]
|
||||
@@ -39,6 +53,56 @@ public class OrdersController : ControllerBase
|
||||
return Ok(order);
|
||||
}
|
||||
|
||||
[HttpGet("{id}/payment-status")]
|
||||
public async Task<IActionResult> GetPaymentStatus(int id, [FromQuery] string outTradeNo)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(outTradeNo))
|
||||
{
|
||||
return BadRequest("outTradeNo is required.");
|
||||
}
|
||||
|
||||
var order = await _orderService.GetOrderAsync(id);
|
||||
if (order == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (order.IsPaid)
|
||||
{
|
||||
return Ok(new PaymentStatusResponse
|
||||
{
|
||||
Order = order,
|
||||
IsPaid = true,
|
||||
TradeState = "SUCCESS",
|
||||
OutTradeNo = outTradeNo,
|
||||
Message = "Order already confirmed."
|
||||
});
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var payment = await _weChatPayService.QueryOrderByOutTradeNoAsync(outTradeNo, HttpContext.RequestAborted);
|
||||
if (string.Equals(payment.TradeState, "SUCCESS", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
order = await _orderService.ConfirmPaymentAsync(id) ?? order;
|
||||
}
|
||||
|
||||
return Ok(new PaymentStatusResponse
|
||||
{
|
||||
Order = order,
|
||||
IsPaid = order.IsPaid,
|
||||
TradeState = payment.TradeState,
|
||||
OutTradeNo = payment.OutTradeNo,
|
||||
TransactionId = payment.TransactionId,
|
||||
Message = order.IsPaid ? "Payment confirmed." : "Waiting for payment."
|
||||
});
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status502BadGateway, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetOrder(int id)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user