取消订单
parent
dba3c80d85
commit
2557525384
|
|
@ -7,6 +7,7 @@ use App\Endpoint\Api\Http\Resources\OrderResource;
|
||||||
use App\Endpoint\Api\Http\Resources\OrderResourceCollection;
|
use App\Endpoint\Api\Http\Resources\OrderResourceCollection;
|
||||||
use App\Exceptions\BizException;
|
use App\Exceptions\BizException;
|
||||||
use App\Helpers\Paginator as PaginatorHelper;
|
use App\Helpers\Paginator as PaginatorHelper;
|
||||||
|
use App\Models\Order;
|
||||||
use App\Services\OrderService;
|
use App\Services\OrderService;
|
||||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
@ -142,7 +143,33 @@ class OrderController extends Controller
|
||||||
*/
|
*/
|
||||||
public function cancel($id, Request $request)
|
public function cancel($id, Request $request)
|
||||||
{
|
{
|
||||||
// todo 取消订单
|
$user = $request->user();
|
||||||
|
|
||||||
|
try {
|
||||||
|
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([
|
||||||
|
'amount' => $order->total_amount,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$order->update([
|
||||||
|
'status' => Order::STATUS_CANCELLED,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
} catch (BizException $e) {
|
||||||
|
throw $e;
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
report($e);
|
||||||
|
|
||||||
|
throw new BizException('订单支付失败,请重试');
|
||||||
|
}
|
||||||
|
|
||||||
return response()->noContent();
|
return response()->noContent();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,15 @@ class Order extends Model
|
||||||
'completed_at',
|
'completed_at',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 仅查询支付过期的订单
|
||||||
|
*/
|
||||||
|
public function scopeExpired()
|
||||||
|
{
|
||||||
|
return $this->where('status', static::STATUS_PENDING)
|
||||||
|
->where('created_at', '<=', now()->subSeconds(config('settings.order_payment_expires_at')));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下单人
|
* 下单人
|
||||||
*
|
*
|
||||||
|
|
@ -114,12 +123,11 @@ class Order extends Model
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 仅查询支付过期的订单
|
* 属于此订单的退款任务
|
||||||
*/
|
*/
|
||||||
public function scopeExpired()
|
public function refundTasks()
|
||||||
{
|
{
|
||||||
return $this->where('status', static::STATUS_PENDING)
|
return $this->hasMany(OrderRefundTask::class);
|
||||||
->where('created_at', '<=', now()->subSeconds(config('settings.order_payment_expires_at')));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class OrderRefundTask extends Model
|
||||||
|
{
|
||||||
|
public const STATUS_PENDING = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $attributes = [
|
||||||
|
'status' => self::STATUS_PENDING,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'order_id',
|
||||||
|
'after_sale_id',
|
||||||
|
'amount',
|
||||||
|
'status',
|
||||||
|
'failed_reason',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateOrderRefundTasksTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('order_refund_tasks', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->unsignedBigInteger('order_id')->comment('订单ID');
|
||||||
|
$table->unsignedBigInteger('after_sale_id')->nullable()->comment('售后ID');
|
||||||
|
$table->unsignedBigInteger('amount')->comment('退款金额');
|
||||||
|
$table->tinyInteger('status')->default(0)->comment('状态');
|
||||||
|
$table->string('failed_reason')->nullable()->comment('失败原因');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['order_id', 'after_sale_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('order_refund_tasks');
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue