6
0
Fork 0

优化取消订单

release
李静 2021-12-20 16:31:22 +08:00
parent f0a6ceecd4
commit e53be8ff12
2 changed files with 27 additions and 18 deletions

View File

@ -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;

View File

@ -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,
]);
}
}