order-pre
parent
e3041d1401
commit
580088a561
|
|
@ -4,6 +4,7 @@ namespace App\Endpoint\Api\Http\Controllers\Order;
|
|||
|
||||
use App\Endpoint\Api\Http\Controllers\Controller;
|
||||
use App\Endpoint\Api\Http\Resources\OrderPreResource;
|
||||
use App\Endpoint\Api\Http\Resources\OrderResource;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\{Order, OrderPre, ProductSku};
|
||||
use App\Models\Store\Store;
|
||||
|
|
@ -109,27 +110,36 @@ class OrderPreController extends Controller
|
|||
|
||||
public function storeOrder(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'id' => 'required'
|
||||
]);
|
||||
$id = $request->input('id');
|
||||
$order_pre = OrderPre::findOrFail($id);
|
||||
$user = $request->user();
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$service = new OrderService();
|
||||
// 如果还有未支付的订单, 则不生成新订单
|
||||
$order = $user->orders()->where([
|
||||
'source_type' => OrderPre::class,
|
||||
'source_id' => $order_pre->id,
|
||||
'status' => Order::STATUS_PENDING,
|
||||
])->first();
|
||||
if (!$order) {
|
||||
$order = $service->createOrderByPre($user, $order_pre);
|
||||
if ($request->filled('coupon_id')) {
|
||||
$coupon = $user->coupons()->find($request->input('coupon_id'));
|
||||
if (!$coupon) {
|
||||
throw new BizException('优惠券不存在');
|
||||
}
|
||||
if ($coupon->is_use) {
|
||||
throw new BizException('优惠券已使用');
|
||||
}
|
||||
|
||||
if (!now()->between($coupon->use_start_at, $coupon->use_end_at)) {
|
||||
throw new BizException('优惠券已过期');
|
||||
}
|
||||
$others = $order_pre->others;
|
||||
$others['coupon_id'] = $coupon->id;
|
||||
$order_pre->update(['others' => $others]);
|
||||
}
|
||||
$service = new OrderService();
|
||||
$order = $service->createOrderByPre($user, $order_pre);
|
||||
|
||||
DB::commit();
|
||||
return response()->json([
|
||||
'order_id' => $order->id
|
||||
]);
|
||||
return OrderResource::make($order);
|
||||
} catch (Throwable $e) {
|
||||
DB::rollBack();
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ class OrderProfitResource extends JsonResource
|
|||
'order' => $this->when($this->whenLoaded('order'), [
|
||||
'id' => $this->order?->id,
|
||||
'sn' => $this->order?->sn,
|
||||
'total_amount' => $this->order ? $this->order->total_amount / 100 : 0
|
||||
'total_amount' => $this->order ? $this->order->total_amount / 100 : 0,
|
||||
'created_at' => $this->order?->created_at->timestamp,
|
||||
]),
|
||||
'from_user_id' => $this->from_user_id,
|
||||
'user_id' => $this->user_id,
|
||||
|
|
|
|||
Loading…
Reference in New Issue