Files
hyperf_service/app/Service/Api/Order/RefundOrderService.php
2025-03-27 10:47:31 +08:00

73 lines
1.9 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Order;
use App\Constants\Common\OrderCode;
use App\Constants\Common\RefundCode;
use App\Exception\ErrException;
use App\Model\Order;
use App\Model\PayOrder;
use App\Service\Amqp\Refund\FullRefundOrderService;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Common\OrderChangeStatusTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class RefundOrderService extends BaseService
{
use OrderChangeStatusTrait;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var PayOrder
*/
#[Inject]
protected PayOrder $payOrderModel;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
//todo 考虑是否枷锁
$orderId = (int)$this->request->input('order_id');
$orderInfo = $this->orderModel->getInfoById($orderId);
if ($orderInfo->user_id != $this->userId) throw new ErrException('该订单不是您的订单,请勿操作');
if ($orderInfo->status != OrderCode::PAYED) throw new ErrException('该订单状态已变更,请勿重复操作,刷新后无法退款请联系客服');
try {
$orderId = (int)$this->request->input('order_id');
$service = new FullRefundOrderService();
$service->orderId = $orderId;
$service->reason = '用户主动退款订单';
$service->type = RefundCode::FULL_GOOD_REFUND;
$service->handle();
return $this->return->success();
}catch (Exception $e) {
throw new ErrException($e->getMessage());
}
}
}