diff --git a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php index 2cde7480..545b8710 100644 --- a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php +++ b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php @@ -7,10 +7,8 @@ use App\Endpoint\Api\Http\Resources\OrderPackageResource; use App\Endpoint\Api\Http\Resources\OrderResource; use App\Endpoint\Api\Http\Resources\OrderResourceCollection; use App\Exceptions\BizException; -use App\Helpers\Order as OrderHelper; use App\Helpers\Paginator as PaginatorHelper; use App\Models\KuaidiLog; -use App\Models\Order; use App\Services\OrderService; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; @@ -167,21 +165,7 @@ class OrderController extends Controller return DB::transaction(function () use ($id, $user) { $order = $user->orders()->lockForUpdate()->findOrFail($id); - if (! $order->isPending() && ! $order->isWaitShipping()) { - throw new BizException('订单状态不是待付款或待发货'); - } - - if ($order->isWaitShipping()) { - $order->refundTasks()->create([ - 'sn' => OrderHelper::serialNumber(), - 'amount' => $order->total_amount, - 'reason' => '取消订单', - ]); - } - - $order->update([ - 'status' => Order::STATUS_CANCELLED, - ]); + (new OrderService())->cancel($order); }); } catch (ModelNotFoundException | BizException $e) { throw $e; diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 272bc433..744133b1 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -639,7 +639,7 @@ class OrderService } /** - * 订单确认收货 + * 确认订单 * * @param \App\Models\Order $order * @return void @@ -667,4 +667,29 @@ class OrderService 'completed_at' => $time, ]); } + + /** + * 取消订单 + * + * @param \App\Models\Order $order + * @return void + */ + public function cancel(Order $order) + { + if (! $order->isPending() && ! $order->isWaitShipping()) { + throw new BizException('订单状态不是待付款或待发货'); + } + + if ($order->isWaitShipping()) { + $order->refundTasks()->create([ + 'sn' => OrderHelper::serialNumber(), + 'amount' => $order->total_amount, + 'reason' => '取消订单', + ]); + } + + $order->update([ + 'status' => Order::STATUS_CANCELLED, + ]); + } }