diff --git a/app/Http/Controllers/CartController.php b/app/Http/Controllers/CartController.php index b2e0136..592c29a 100644 --- a/app/Http/Controllers/CartController.php +++ b/app/Http/Controllers/CartController.php @@ -35,7 +35,7 @@ class CartController extends Controller public function add(MemberCartRequest $request, MemberCartRepository $repository) { - Log::info($request->all()); + // Log::info($request->all()); $data = $request->validated(); $item = $repository->create($data); diff --git a/app/Http/Controllers/CheckoutController.php b/app/Http/Controllers/CheckoutController.php index 065cdce..80d9530 100644 --- a/app/Http/Controllers/CheckoutController.php +++ b/app/Http/Controllers/CheckoutController.php @@ -9,7 +9,6 @@ use App\Repositories\Member\ShippingRepository; use App\Repositories\Member\Transaction\TransactionRepository; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; - use Illuminate\Validation\ValidationException; class CheckoutController extends Controller @@ -22,22 +21,18 @@ class CheckoutController extends Controller $subtotal = $memberCartRepository->getSubtotal($request->input('location_id')); - $address_list = Address::where('user_id', auth()->user()->id)->orderBy('is_primary','desc')->get(); - + $address_list = Address::where('user_id', auth()->user()->id)->orderBy('is_primary', 'desc')->get(); $total = $subtotal; - - $carts = $memberCartRepository->getList($request); - return view('checkout.v1-delivery-1', [ 'carts' => $carts, 'subtotal' => $subtotal, 'total' => $total, 'store' => $store, - 'address_list' => $address_list, + 'address_list' => $address_list, ]); } @@ -46,33 +41,33 @@ class CheckoutController extends Controller $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_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, ShippingRepository $shippingRepository) { try { @@ -81,7 +76,6 @@ class CheckoutController extends Controller $location_id = session('location_id', 22); - if ($delivery_method == null || $address_id == null || $location_id == null) { return redirect()->route('checkout.delivery'); @@ -90,37 +84,43 @@ class CheckoutController extends Controller $subtotal = $memberCartRepository->getSubtotal($location_id); $total = $subtotal; - $request->merge(['location_id' => $location_id]); $carts = $memberCartRepository->getList($request); - - try{ + try { $shipping_list = collect($shippingRepository->getList([ - "location_id" => $location_id, - "address_id" => $address_id, - "items" => $carts, - ])['pricing'] ?? [])->map(function($row){ + 'location_id' => $location_id, + 'address_id' => $address_id, + 'items' => $carts, + ])['pricing'] ?? [])->map(function ($row) { return [ - 'courier' => $row['courier_code'], - 'service' => $row['courier_service_code'], - 'title' => $row['courier_name']." - ".$row['courier_service_name'], + 'courier' => $row['courier_code'], + 'service' => $row['courier_service_code'], + 'title' => $row['courier_name'].' - '.$row['courier_service_name'], 'description' => $row['duration'], - 'cost' => $row['shipping_fee'], + 'cost' => $row['shipping_fee'], ]; }); if (count($shipping_list) == 0) { throw ValidationException::withMessages([ - "message" => "Tidak dapat menghitung ongkir" + 'message' => 'Tidak dapat menghitung ongkir', ]); } - }catch(\Exception $e){ + } catch (\Exception $e) { + $message = $e->getMessage(); + + // if contain Failed due to invalid or missing postal code + if (str_contains($message, 'Failed due to invalid or missing postal code')) { + $message = 'Kode pos tidak valid atau tidak ditemukan'; + } else if (str_contains($message, 'support@biteship.com')) { + $message = 'Layanan pengiriman tidak tersedia untuk alamat ini'; + } throw ValidationException::withMessages([ - "message" => $e->getMessage() + 'message' => $message, ]); } - + return view('checkout.v1-delivery-1-shipping', [ 'carts' => $request->user()->carts, 'subtotal' => $subtotal, @@ -132,6 +132,7 @@ class CheckoutController extends Controller ]); } catch (\Exception $e) { Log::info($e); + return redirect()->route('checkout.delivery')->with('error', $e->getMessage() ?? 'Invalid checkout data'); } } @@ -164,6 +165,8 @@ class CheckoutController extends Controller $location_id = session('location_id', 22); + $use_point = session('use_point') ?? 0; + $items = []; $request->merge(['location_id' => $location_id]); @@ -172,25 +175,27 @@ class CheckoutController extends Controller if (count($carts) == 0) { return redirect()->route('checkout.delivery')->with('error', 'No items in cart'); } - + foreach ($carts as $cart) { $items[] = [ - "item_reference_id" => $cart->item_reference_id, - "qty" => $cart->qty + 'item_reference_id' => $cart->item_reference_id, + 'qty' => $cart->qty, ]; } $data = [ - "address_id" => $address_id, - "note" => "", - "courier_company" => $courier, - "courier_type" => $service, - "location_id" => $location_id, - "items" => $items, - "vouchers" => [], - "use_customer_points" => 0, + 'address_id' => $address_id, + 'note' => '', + 'courier_company' => $courier, + 'courier_type' => $service, + 'location_id' => $location_id, + 'items' => $items, + 'vouchers' => [], + 'use_customer_points' => $use_point ?? 0, ]; - + + dd($data); + $item = $repository->create($data); $notification = new \App\Notifications\Member\Transaction\OrderWaitPayment($item); @@ -201,19 +206,20 @@ class CheckoutController extends Controller // proses payment - - $payment = $item->payments()->where("method_type",'App\Models\XenditLink') - ->where("status",'PENDING') - ->first(); - $invoice_url = $payment ? @$payment->method->invoice_url: ""; - - return redirect()->to($invoice_url)->withHeaders([ - 'Cache-Control' => 'no-cache, no-store, must-revalidate', - 'Pragma' => 'no-cache', - 'Expires' => '0' - ]); + $payment = $item->payments()->where('method_type', 'App\Models\XenditLink') + ->where('status', 'PENDING') + ->first(); + $invoice_url = $payment ? @$payment->method->invoice_url : ''; + // reset state session + session()->forget(['checkout_delivery_method', 'checkout_address_id', 'checkout_courier', 'checkout_service', 'use_point']); + + return redirect()->to($invoice_url)->withHeaders([ + 'Cache-Control' => 'no-cache, no-store, must-revalidate', + 'Pragma' => 'no-cache', + 'Expires' => '0', + ]); } catch (\Exception $e) { @@ -222,4 +228,92 @@ class CheckoutController extends Controller return redirect()->route('checkout.delivery')->with('error', 'Invalid checkout data'); } } + + /** + * Apply points to session + */ + public function applyPoint(Request $request) + { + try { + $usePoint = $request->input('use_point'); + + // Validate input + if (!$usePoint || !is_numeric($usePoint) || $usePoint < 0) { + return response()->json([ + 'success' => false, + 'message' => 'Invalid points value' + ]); + } + + $usePoint = (int) $usePoint; + + // Get user's available points + $userPoints = auth()->user()->customer->point ?? 0; + + // Check if user has enough points + if ($usePoint > $userPoints) { + return response()->json([ + 'success' => false, + 'message' => 'You cannot use more points than available' + ]); + } + + // Set points in session + session(['use_point' => $usePoint]); + + return response()->json([ + 'success' => true, + 'message' => 'Points applied successfully', + 'points_applied' => $usePoint, + 'remaining_points' => $userPoints - $usePoint + ]); + + } catch (\Exception $e) { + Log::error('Error applying points: ' . $e->getMessage()); + + return response()->json([ + 'success' => false, + 'message' => 'An error occurred while applying points' + ]); + } + } + + /** + * Remove points from session + */ + public function removePoint(Request $request) + { + try { + // Check if points are currently applied + $currentPoints = session('use_point', 0); + + if ($currentPoints <= 0) { + return response()->json([ + 'success' => false, + 'message' => 'No points are currently applied' + ]); + } + + // Remove points from session + session()->forget('use_point'); + + // Get user's available points + $userPoints = auth()->user()->customer->point ?? 0; + + return response()->json([ + 'success' => true, + 'message' => 'Points removed successfully', + 'points_removed' => $currentPoints, + 'available_points' => $userPoints + ]); + + } catch (\Exception $e) { + Log::error('Error removing points: ' . $e->getMessage()); + + return response()->json([ + 'success' => false, + 'message' => 'An error occurred while removing points' + ]); + } + } } diff --git a/app/Http/Controllers/VoucherEventController.php b/app/Http/Controllers/VoucherEventController.php index 2dc23ce..02ea22d 100644 --- a/app/Http/Controllers/VoucherEventController.php +++ b/app/Http/Controllers/VoucherEventController.php @@ -39,7 +39,7 @@ class VoucherEventController extends Controller // Call the repository's redeem method $result = $this->voucherEventRepository->redeem($voucherEvent, $user); - if ($result['success']) { + if ($result) { return response()->json([ 'success' => true, 'message' => $result['message'] ?? 'Voucher redeemed successfully!', diff --git a/lang/en/checkout.php b/lang/en/checkout.php index fafb736..c943daf 100644 --- a/lang/en/checkout.php +++ b/lang/en/checkout.php @@ -56,4 +56,16 @@ return [ 'terms_and_conditions' => 'Terms and Conditions', 'close' => 'Close', 'redeem' => 'Redeem', + 'use_point' => 'Use Points', + 'your_points' => 'Your Points', + 'available_points' => 'Available Points', + 'input_point' => 'Enter points to use', + 'apply_point' => 'Apply Points', + 'remove_point' => 'Remove Points', + 'points_applied' => 'Points Applied', + 'points_used' => 'points used', + 'saving' => 'Discount:', + 'shipping' => 'Shipping:', + 'estimated_total' => 'Estimated total:', + 'proceed_to_checkout' => 'Proceed to checkout', ]; diff --git a/lang/id/checkout.php b/lang/id/checkout.php index 2020e48..04e4c3c 100644 --- a/lang/id/checkout.php +++ b/lang/id/checkout.php @@ -50,10 +50,22 @@ return [ 'enter_valid_promo_code' => 'Masukkan kode promo yang valid!', 'apply' => 'Gunakan', 'available_vouchers' => 'Voucher Tersedia', - 'available_voucher_events' => 'Event Voucher Tersedia', + 'available_voucher_events' => 'Tukar Poin', 'points_required' => 'Poin Diperlukan', 'valid_period' => 'Periode Berlaku', 'terms_and_conditions' => 'Syarat dan Ketentuan', 'close' => 'Tutup', 'redeem' => 'Tukar', + 'use_point' => 'Gunakan Poin', + 'your_points' => 'Poin Anda', + 'available_points' => 'Poin Tersedia', + 'input_point' => 'Masukkan poin yang akan digunakan', + 'apply_point' => 'Gunakan Poin', + 'remove_point' => 'Hapus Poin', + 'points_applied' => 'Poin Diterapkan', + 'points_used' => 'poin digunakan', + 'saving' => 'Diskon:', + 'shipping' => 'Pengiriman:', + 'estimated_total' => 'Total estimasi:', + 'proceed_to_checkout' => 'Lanjut ke checkout', ]; diff --git a/resources/views/checkout/v1-cart.blade.php b/resources/views/checkout/v1-cart.blade.php index 3aee0c1..8a2a074 100644 --- a/resources/views/checkout/v1-cart.blade.php +++ b/resources/views/checkout/v1-cart.blade.php @@ -186,26 +186,26 @@ Rp 0
+ {{ number_format(session('use_point'), 0, ",", ".") }} {{ __('checkout.points_used') }} +
+