160 lines
5.2 KiB
PHP
160 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Address;
|
|
use App\Models\Location;
|
|
use App\Repositories\Member\Cart\MemberCartRepository;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CheckoutController extends Controller
|
|
{
|
|
public function index(Request $request, MemberCartRepository $memberCartRepository)
|
|
{
|
|
$request->merge(['location_id' => $request->input('location_id', session('location_id', 22))]);
|
|
|
|
$store = Location::find($request->input('location_id'));
|
|
|
|
$subtotal = $memberCartRepository->getSubtotal($request->input('location_id'));
|
|
|
|
$address_list = Address::where('user_id', $request->user()->id)->orderBy('is_primary','desc')->get();
|
|
|
|
|
|
$total = $subtotal;
|
|
return view('checkout.v1-delivery-1', [
|
|
'carts' => $request->user()->carts,
|
|
'subtotal' => $subtotal,
|
|
'total' => $total,
|
|
'store' => $store,
|
|
'address_list' => $address_list,
|
|
]);
|
|
}
|
|
|
|
public function indexProcess(Request $request)
|
|
{
|
|
$delivery_method = $request->input('delivery_method') ?? 'shipping';
|
|
$address_id = $request->input('address_id');
|
|
|
|
if ($address_id == null) {
|
|
$address_list = Address::where('user_id', $request->user()->id)->orderBy('is_primary','desc')->get();
|
|
$address_id = $address_list->first()->id;
|
|
}
|
|
|
|
if ($delivery_method == null || $address_id == null) {
|
|
return redirect()->back()->with('error', 'Delivery method or address is required');
|
|
}
|
|
|
|
|
|
if ($delivery_method == 'shipping') {
|
|
session(['checkout_delivery_method' => $delivery_method]);
|
|
session(['checkout_address_id' => $address_id]);
|
|
return redirect()->route('checkout.shipping');
|
|
}
|
|
|
|
if ($delivery_method == 'pickup') {
|
|
session(['checkout_delivery_method' => $delivery_method]);
|
|
session(['checkout_address_id' => null]);
|
|
return redirect()->route('checkout.payment');
|
|
}
|
|
|
|
return redirect()->back()->with('error', 'Delivery method is not valid');
|
|
}
|
|
|
|
|
|
public function chooseShipping(Request $request, MemberCartRepository $memberCartRepository)
|
|
{
|
|
try {
|
|
$delivery_method = $request->input('delivery_method');
|
|
$address_id = $request->input('address_id');
|
|
|
|
$subtotal = $memberCartRepository->getSubtotal($request->input('location_id'));
|
|
$total = $subtotal;
|
|
|
|
|
|
|
|
$shipping_list = [
|
|
[
|
|
'courier' => 'JNE',
|
|
'service' => 'REG',
|
|
'title' => 'JNE Regular',
|
|
'cost' => 10000,
|
|
],
|
|
[
|
|
'courier' => 'JNE',
|
|
'service' => 'YES',
|
|
'title' => 'JNE YES (Same Day)',
|
|
'cost' => 25000,
|
|
],
|
|
[
|
|
'courier' => 'J&T',
|
|
'service' => 'EZ',
|
|
'title' => 'J&T Express',
|
|
'cost' => 12000,
|
|
],
|
|
[
|
|
'courier' => 'SiCepat',
|
|
'service' => 'REG',
|
|
'title' => 'SiCepat Regular',
|
|
'cost' => 11000,
|
|
],
|
|
[
|
|
'courier' => 'Gojek',
|
|
'service' => 'Same Day',
|
|
'title' => 'Gojek Same Day',
|
|
'cost' => 20000,
|
|
],
|
|
[
|
|
'courier' => 'Grab',
|
|
'service' => 'Instant',
|
|
'title' => 'Grab Instant',
|
|
'cost' => 22000,
|
|
],
|
|
];
|
|
|
|
return view('checkout.v1-delivery-1-shipping', [
|
|
'carts' => $request->user()->carts,
|
|
'subtotal' => $subtotal,
|
|
'total' => $total,
|
|
'delivery_method' => $delivery_method,
|
|
'address_id' => $address_id,
|
|
'address' => Address::find($address_id),
|
|
'shipping_list' => $shipping_list,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return redirect()->route('checkout.delivery')->with('error', 'Invalid checkout data');
|
|
}
|
|
}
|
|
|
|
public function chooseShippingProcess(Request $request)
|
|
{
|
|
$shipping_option = $request->input('shipping_option');
|
|
|
|
// Parse shipping option (format: courier|service|cost)
|
|
$shipping_data = explode('|', $shipping_option);
|
|
$courier = $shipping_data[0] ?? '';
|
|
$service = $shipping_data[1] ?? '';
|
|
$cost = $shipping_data[2] ?? 0;
|
|
|
|
session(['checkout_courier' => $courier]);
|
|
session(['checkout_service' => $service]);
|
|
session(['checkout_shipping_cost' => $cost]);
|
|
|
|
return redirect()->route('checkout.payment');
|
|
}
|
|
|
|
public function choosePayment(Request $request)
|
|
{
|
|
try {
|
|
|
|
// proses checkout
|
|
|
|
|
|
// proses payment
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
return redirect()->route('checkout.delivery')->with('error', 'Invalid checkout data');
|
|
}
|
|
}
|
|
}
|