diff --git a/app/Http/Controllers/RentController.php b/app/Http/Controllers/RentController.php index 9733e2d..ae508a2 100644 --- a/app/Http/Controllers/RentController.php +++ b/app/Http/Controllers/RentController.php @@ -6,6 +6,8 @@ use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Http; + use App\Models\Vehicle; use App\Models\Order; @@ -134,6 +136,120 @@ public function getPaymentDetails($id) $method = request()->input('method') ? request()->input('method') : 'BCA Transfer'; $order = Order::find($id); + if($order->transaction_id) { + return redirect()->route('confirm-payment', [$order->id])->with('order', $order); + } + return view('payment-details', ['order' => $order, 'selected_method' => $method]); } + + public function getVirtualAccount($id) + { + $method = request()->input('method'); + $order = Order::find($id); + + $AUTH_STRING = "Basic " . base64_encode(env('MIDTRANS_SERVER_KEY') . ":"); + + $payment_type = array( + "BCA Transfer" => "bank_transfer", + "BNI Transfer" => "bank_transfer", + "BRI Transfer" => "bank_transfer", + "Mandiri Transfer" => "echannel", + "Permata Transfer" => "permata", + "Shopee Pay" => "gopay", + "Dana" => "gopay", + "OVO" => "gopay", + "GoPay" => "gopay" + ); + + $req_body = [ + "payment_type" => $payment_type[$method], + "transaction_details" => [ + "order_id" => $order->id, + "gross_amount" => $order->total_price, + ] + ]; + + if($payment_type[$method] == 'bank_transfer') { + $req_body['bank_transfer']['bank'] = explode(' ', strtolower($method))[0]; + } + + if($payment_type[$method] == 'echannel') { + $req_body['echannel']['bill_info1'] = $order->id; + $req_body['echannel']['bill_info2'] = $order->total_price; + } + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Authorization' => $AUTH_STRING + ]) + ->withBody(json_encode($req_body), 'application/json') + ->post('https://api.sandbox.midtrans.com/v2/charge'); + + $response = $response->json(); + + if($response['status_code'] == '201') { + if($payment_type[$method] == 'bank_transfer') { + $order->virtual_account = $response['va_numbers'][0]['va_number']; + } else if ($payment_type[$method] == 'echannel') { + $order->virtual_account = $response['bill_key'] . ' ' . $response['bill_code']; + } else if ($payment_type[$method] == 'gopay') { + $order->qr_link = $response['actions'][0]['url']; + $order->deep_link = $response['actions'][1]['url']; + } else { + $order->virtual_account = $response->json()['permata_va_number']; + } + + $order->transaction_id = $response['transaction_id']; + $payment_expiry_time = new DateTime(); + $payment_expiry_time->modify('+1 day'); + $order->payment_expiry_time = $payment_expiry_time->format('Y-m-d H:i:s'); + $order->payment_method = $method; + } + + $order->save(); + + if($order->transaction_id) { + return redirect()->route('confirm-payment', ['id' => $order->id]); + } else { + return redirect()->route('payment-details', ['id' => $order->id])->with('error', 'Something went wrong. Please try again later.'); + } + } + + public function getConfirmPayment($id) + { + $order = Order::find($id); + + if($order->transaction_id) { + return view('confirm-payment', ['order' => $order]); + } else { + return redirect()->route('payment-details', ['id' => $order->id]); + } + } + + public function checkPayment($id) { + $order = Order::find($id); + + $AUTH_STRING = "Basic " . base64_encode(env('MIDTRANS_SERVER_KEY') . ":"); + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Authorization' => $AUTH_STRING + ]) + ->get('https://api.sandbox.midtrans.com/v2/' . $order->transaction_id . '/status'); + + $response = $response->json(); + + if($response['status_code'] == '200') { + if($response['transaction_status'] == 'settlement') { + $order->order_status = 'PAYMENT_DONE'; + } + } + + $order->save(); + + return redirect()->route('user-orders'); + } } diff --git a/database/migrations/2022_05_16_174019_update_orders_table.php b/database/migrations/2022_05_16_174019_update_orders_table.php new file mode 100644 index 0000000..1924b98 --- /dev/null +++ b/database/migrations/2022_05_16_174019_update_orders_table.php @@ -0,0 +1,32 @@ +timestamp('payment_expiry_time')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('orders', function (Blueprint $table) { + $table->dropColumn('payment_expiry_time'); + }); + } +}; diff --git a/public/css/app.css b/public/css/app.css index 4a01c4e..57900b5 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -704,6 +704,10 @@ select { margin-top: 5rem; margin-bottom: 5rem; } +.my-24 { + margin-top: 6rem; + margin-bottom: 6rem; +} .ml-3 { margin-left: 0.75rem; } diff --git a/public/images/logo-permata.png b/public/images/logo-permata.png new file mode 100644 index 0000000..0d122d0 Binary files /dev/null and b/public/images/logo-permata.png differ diff --git a/public/js/navbar_script.js b/public/js/navbar_script.js index 09bea96..deeada8 100644 --- a/public/js/navbar_script.js +++ b/public/js/navbar_script.js @@ -7,7 +7,7 @@ var showButton = document.getElementById('rent-button'); var showMenu = document.getElementById('rent-menu'); var toggleMenu = document.getElementById('menu-toggle'); var menuList = document.getElementById('menu-list'); -var menuOption = document.getElementsByClassName('rent-menu-option'); +var menuOption = document.getElementsByClassName('menu-option'); toggleMenu.onclick = function (event) { event.preventDefault(); @@ -17,7 +17,7 @@ toggleMenu.onclick = function (event) { menuOption[0].onclick = function (event) { event.preventDefault(); - if (menuOption[0].innerHTML == 'Rent Car') { + if (menuOption[0].innerHTML.trim() == 'Rent Car') { window.location.href = window.location.origin + '/find/car'; } else { window.location.href = window.location.origin + '/dashboard/vehicles/car'; @@ -27,7 +27,7 @@ menuOption[0].onclick = function (event) { menuOption[1].onclick = function (event) { event.preventDefault(); - if (menuOption[0].innerHTML == 'Rent Car') { + if (menuOption[1].innerHTML.trim() == 'Rent Motorcycle') { window.location.href = window.location.origin + '/find/motor'; } else { window.location.href = window.location.origin + '/dashboard/vehicles/motor'; diff --git a/resources/js/navbar_script.js b/resources/js/navbar_script.js index 78431d7..3bcc20f 100644 --- a/resources/js/navbar_script.js +++ b/resources/js/navbar_script.js @@ -2,7 +2,7 @@ const showButton = document.getElementById('rent-button'); const showMenu = document.getElementById('rent-menu'); const toggleMenu = document.getElementById('menu-toggle'); const menuList = document.getElementById('menu-list'); -const menuOption = document.getElementsByClassName('rent-menu-option'); +const menuOption = document.getElementsByClassName('menu-option'); toggleMenu.onclick = (event) => { event.preventDefault(); @@ -11,7 +11,7 @@ toggleMenu.onclick = (event) => { menuOption[0].onclick = (event) => { event.preventDefault(); - if(menuOption[0].innerHTML == 'Rent Car') { + if(menuOption[0].innerHTML.trim() == 'Rent Car') { window.location.href = window.location.origin + '/find/car'; } else { window.location.href = window.location.origin + '/dashboard/vehicles/car'; @@ -20,7 +20,7 @@ menuOption[0].onclick = (event) => { menuOption[1].onclick = (event) => { event.preventDefault(); - if(menuOption[0].innerHTML == 'Rent Car') { + if(menuOption[1].innerHTML.trim() == 'Rent Motorcycle') { window.location.href = window.location.origin + '/find/motor'; } else { window.location.href = window.location.origin + '/dashboard/vehicles/motor'; diff --git a/resources/views/components/navbar.blade.php b/resources/views/components/navbar.blade.php index c1ae174..445db6c 100644 --- a/resources/views/components/navbar.blade.php +++ b/resources/views/components/navbar.blade.php @@ -21,10 +21,10 @@ @if($isRent($key) || $isProduct($key))