122 lines
4.3 KiB
PHP
122 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* CheckoutController.php
|
|
*
|
|
* @copyright 2022 beikeshop.com - All Rights Reserved
|
|
* @link https://beikeshop.com
|
|
* @author Edward Yang <yangjin@guangda.work>
|
|
* @created 2022-06-28 16:47:57
|
|
* @modified 2022-06-28 16:47:57
|
|
*/
|
|
|
|
namespace Beike\Shop\Http\Controllers;
|
|
|
|
use Beike\Models\Product;
|
|
use Beike\Repositories\OrderRepo;
|
|
use Beike\Shop\Services\CheckoutService;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CheckoutController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
try {
|
|
$data = (new CheckoutService)->checkoutData();
|
|
$data = hook_filter('checkout.index.data', $data);
|
|
// 判断:当前购物车商品是否符合下单条件
|
|
$carts = $data['carts']['carts'] ?? [];
|
|
$cartQuantity = collect($carts)->groupBy('product_id')->map(function($productGroup){
|
|
$first = $productGroup->first();
|
|
return [
|
|
'product_id' => $first['product_id'],
|
|
'total_quantity' => $productGroup->sum('quantity'),
|
|
'name_format' => $first['name_format'],
|
|
'minimum_order' => $first['minimum_order'],
|
|
'sales_method' => $first['sales_method'],
|
|
'piece_to_batch' => $first['piece_to_batch'],
|
|
];
|
|
})->toArray();
|
|
foreach($carts as $cartItem){
|
|
$productInfo = $cartQuantity[$cartItem['product_id']];
|
|
// 判断:起订量 如果大于购买数量 不符合下单条件
|
|
$minimumOrder = $productInfo['sales_method'] == 'batches' ? ($productInfo['minimum_order'] * $productInfo['piece_to_batch']) : $productInfo['minimum_order'];// 起订量
|
|
if($minimumOrder > $productInfo['total_quantity']){
|
|
throw new \Exception(trans('product.quantity_error_mini',[
|
|
'goods_name'=>$productInfo['name_format'],
|
|
'num'=>$minimumOrder,
|
|
]));
|
|
break;
|
|
}
|
|
// 判断:批量销售商品 购买总数量必须是N的倍数 否则不符合下单条件
|
|
$pieceToBatch = $productInfo['sales_method'] == 'batches' ? $productInfo['piece_to_batch'] : 1;// 倍数
|
|
if(($productInfo['total_quantity'] % $pieceToBatch) != 0){
|
|
throw new \Exception(trans('product.quantity_error_multiple',[
|
|
'goods_name'=>$productInfo['name_format'],
|
|
'num'=>$pieceToBatch,
|
|
]));
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
return view('checkout', $data);
|
|
} catch (\Exception $e) {
|
|
return redirect(shop_route('carts.index'))->withErrors(['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更改结算信息
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function update(Request $request): mixed
|
|
{
|
|
try {
|
|
$requestData = $request->all();
|
|
|
|
$data = (new CheckoutService)->update($requestData);
|
|
|
|
return hook_filter('checkout.update.data', $data);
|
|
} catch (\Exception $e) {
|
|
return json_fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 确认提交订单
|
|
*
|
|
* @return mixed
|
|
* @throws \Throwable
|
|
*/
|
|
public function confirm()
|
|
{
|
|
try {
|
|
$checkoutService = new CheckoutService;
|
|
$selectedProducts = $checkoutService->selectedProducts->toArray();
|
|
foreach($selectedProducts as $product){
|
|
if($product['product']['active'] == FALSE){
|
|
return json_fail(trans('common.product_active_false'));
|
|
}
|
|
}
|
|
$data = $checkoutService->confirm();
|
|
|
|
return hook_filter('checkout.confirm.data', $data);
|
|
} catch (\Exception $e) {
|
|
return json_fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function success()
|
|
{
|
|
$order_number = request('order_number');
|
|
|
|
$customer = current_customer();
|
|
$order = OrderRepo::getOrderByNumber($order_number, $customer);
|
|
$data = hook_filter('account.order.show.data', ['order' => $order, 'html_items' => []]);
|
|
|
|
return view('checkout/success', $data);
|
|
}
|
|
}
|