129 lines
3.8 KiB
C#
129 lines
3.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using PetWash.Api.Models;
|
|
using PetWash.Api.Services;
|
|
|
|
namespace PetWash.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class OrdersController : ControllerBase
|
|
{
|
|
private readonly OrderService _orderService;
|
|
private readonly WeChatPayService _weChatPayService;
|
|
|
|
public OrdersController(OrderService orderService, WeChatPayService weChatPayService)
|
|
{
|
|
_orderService = orderService;
|
|
_weChatPayService = weChatPayService;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateOrder([FromBody] CreateOrderRequest request)
|
|
{
|
|
try
|
|
{
|
|
var order = await _orderService.CreateOrderAsync(request.PackageId);
|
|
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")]
|
|
public async Task<IActionResult> ConfirmPayment(int id)
|
|
{
|
|
var order = await _orderService.ConfirmPaymentAsync(id);
|
|
if (order == null)
|
|
return NotFound();
|
|
|
|
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)
|
|
{
|
|
var order = await _orderService.GetOrderAsync(id);
|
|
if (order == null)
|
|
return NotFound();
|
|
|
|
return Ok(order);
|
|
}
|
|
|
|
[HttpPut("{id}/status")]
|
|
public async Task<IActionResult> UpdateStatus(int id, [FromBody] UpdateStatusRequest request)
|
|
{
|
|
var order = await _orderService.UpdateOrderStatusAsync(id, request.Status);
|
|
if (order == null)
|
|
return NotFound();
|
|
|
|
return Ok(order);
|
|
}
|
|
}
|
|
|
|
public record CreateOrderRequest(int PackageId);
|
|
public record UpdateStatusRequest(OrderStatus Status);
|