Skip to content

Commit

Permalink
新增:订单取消API
Browse files Browse the repository at this point in the history
  • Loading branch information
xxx committed Apr 1, 2024
1 parent f3adf4e commit 5034a98
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
26 changes: 5 additions & 21 deletions app/Console/Commands/OrderTimeoutHandlerCommand.php
Expand Up @@ -12,55 +12,39 @@
use Illuminate\Console\Command;
use App\Services\Order\Services\OrderService;
use App\Services\Order\Interfaces\OrderServiceInterface;
use Symfony\Component\Console\Command\Command as CommandAlias;

class OrderTimeoutHandlerCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'order:pay:timeout';

/**
* The console command description.
*
* @var string
*/
protected $description = '订单超时处理(自动置为已取消=无法继续支付)';

/**
* @var OrderService
*/
protected $orderService;

/**
* OrderTimeoutHandlerCommand constructor.
*
* @param OrderServiceInterface $orderService
*/
public function __construct(OrderServiceInterface $orderService)
{
parent::__construct();
$this->orderService = $orderService;
}

/**
* @throws \App\Exceptions\ServiceException
*/

public function handle()
{
// 超时一个小时未支付订单
$now = Carbon::now()->subMinutes(60);
$orders = $this->orderService->getTimeoutOrders($now->toDateTimeString());
if (!$orders) {
return;
return CommandAlias::SUCCESS;
}

foreach ($orders as $order) {
$this->line($order['order_id']);
$this->orderService->cancel($order['id']);
}

return 0;
return CommandAlias::SUCCESS;
}
}
32 changes: 30 additions & 2 deletions app/Http/Controllers/Backend/Api/V1/OrderController.php
Expand Up @@ -21,7 +21,9 @@
use App\Services\Order\Models\Order;
use App\Services\Order\Models\OrderGoods;
use App\Services\Order\Models\OrderRefund;
use App\Services\Order\Services\OrderService;
use App\Services\Order\Models\OrderPaidRecord;
use App\Services\Order\Interfaces\OrderServiceInterface;

class OrderController extends BaseController
{
Expand Down Expand Up @@ -160,12 +162,15 @@ public function detail($id)
public function finishOrder($id)
{
$order = Order::query()->where('id', $id)->firstOrFail();
event(new PaymentSuccessEvent($order->toArray()));

AdministratorLog::storeLog(
AdministratorLog::MODULE_ORDER,
AdministratorLog::OPT_UPDATE,
compact('id')
);

event(new PaymentSuccessEvent($order->toArray()));

return $this->success();
}

Expand Down Expand Up @@ -359,7 +364,7 @@ public function refundOrders(Request $request)
public function deleteRefundOrder($id)
{
$refundOrder = OrderRefund::query()->where('id', $id)->firstOrFail();
$orderAllRefundOrdersCount = (int)OrderRefund::query()->where('order_id', $refundOrder['order_id'])->count();
$orderAllRefundOrdersCount = OrderRefund::query()->where('order_id', $refundOrder['order_id'])->count();
DB::transaction(function () use ($refundOrder, $orderAllRefundOrdersCount) {
AdministratorLog::storeLog(
AdministratorLog::MODULE_ORDER,
Expand All @@ -379,4 +384,27 @@ public function deleteRefundOrder($id)
});
return $this->success();
}

public function cancel(OrderServiceInterface $orderService, $id)
{

/**
* @var OrderService $orderService
*/

$order = Order::query()->where('id', $id)->firstOrFail();
if (!in_array($order['status'], [Order::STATUS_UNPAY, Order::STATUS_PAYING])) {
return $this->error(__('订单状态异常'));
}

AdministratorLog::storeLog(
AdministratorLog::MODULE_ORDER,
AdministratorLog::OPT_UPDATE,
compact('id')
);

$orderService->cancel($order['id']);

return $this->success();
}
}
6 changes: 6 additions & 0 deletions database/seeders/AdministratorPermissionSeeder.php
Expand Up @@ -574,6 +574,12 @@ public function run()
'method' => 'DELETE',
'url' => 'order/refund/\d+',
],
[
'display_name' => '订单-取消订单',
'slug' => 'order.cancel',
'method' => 'GET',
'url' => 'order/\d+/cancel',
],
],
],

Expand Down
1 change: 1 addition & 0 deletions routes/backend-v1.php
Expand Up @@ -252,6 +252,7 @@
Route::get('/', 'OrderController@index');
Route::get('/{id}', 'OrderController@detail');
Route::get('/{id}/finish', 'OrderController@finishOrder');
Route::get('/{id}/cancel', 'OrderController@cancel');
Route::post('/{id}/refund', 'OrderController@submitRefund');
Route::get('/refund/list', 'OrderController@refundOrders');
Route::delete('/refund/{id}', 'OrderController@deleteRefundOrder');
Expand Down

0 comments on commit 5034a98

Please sign in to comment.