Files
hyperf_service/app/Service/Api/Order/RefundOrderService.php
2025-03-31 16:55:21 +08:00

86 lines
2.3 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\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\RedisCache;
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;
/**
* @var RedisCache
*/
#[Inject]
protected RedisCache $redis;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
// 考虑是否枷锁
$orderId = (int)$this->request->input('order_id');
$refundLockKey = ApiRedisKey::refundLock($this->userId);
if (0 == ($this->redis->addLock($refundLockKey))){
throw new ErrException('请勿重复点击');
}
$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();
}catch (Exception $e) {
throw new ErrException($e->getMessage());
}
$this->redis->delLock($refundLockKey);
return $this->return->success();
}
}