From 0fe8498dda69626eb502eb52b8ee7033fa076bb5 Mon Sep 17 00:00:00 2001 From: Femi Novia Lina <77434812+FemiNoviaLina@users.noreply.github.com> Date: Tue, 17 May 2022 03:25:58 +0700 Subject: [PATCH] complete rent process --- app/Http/Controllers/RentController.php | 116 ++++++++++++++++++ .../2022_05_16_174019_update_orders_table.php | 32 +++++ public/css/app.css | 4 + public/images/logo-permata.png | Bin 0 -> 2470 bytes public/js/navbar_script.js | 6 +- resources/js/navbar_script.js | 6 +- resources/views/components/navbar.blade.php | 6 +- resources/views/confirm-payment.blade.php | 22 ++++ resources/views/payment-details.blade.php | 25 ++-- routes/web.php | 3 + 10 files changed, 199 insertions(+), 21 deletions(-) create mode 100644 database/migrations/2022_05_16_174019_update_orders_table.php create mode 100644 public/images/logo-permata.png create mode 100644 resources/views/confirm-payment.blade.php 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 0000000000000000000000000000000000000000..0d122d021446c6b985bf0d8e2eef1457e1dc25e6 GIT binary patch literal 2470 zcmV;X30d}uP)&=idE@I5;>sI5;>sI5;>sI5;>sI5;>sJ_Ug|#>BCVoakx2KC4$ViWbrY(PR(@ zRIhB%{IZao=$}!4s(GxrGI5NFX3#R)TO zPIZqqmm78*2(o_keZ}Q!GD_A$F@|DdEz~V|$=TIIq-h`KHq>;FHCH5#F|od(!6oW+ zx>r}I+vg!yS2y(yxJcJ^5_RG5cyqa7KL;jFIKZe=Qa?SQ=|{$!$5X**DAC2(Y1L0B z+fr3p|D|_D(a<$WdcyE9Zv58AuGQ2&_3ErGX{lzL9wF}7y5f98s0i+{p(rU4X;0ZV z-t7;Sg#B`AF>a z2znxJF3qYa)gHG{#QZWU#!6ll=Y{sq-!=P1lHG zk>T2(%mYZ$V*4{~--Hul`7M0)T zh7HA=Kif`{ypwLT`;}1>M zjKWG7H_+mS4afGCZZb)0slUI0dV6731M6K`)Y#~a){W0D`M9=uki>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4s7Z&E(a0mVHNt=0kG831is~{Pe9(fgbN`GW5q1wv61~4TZ9c{ zL!OL2CjSAfbCkVD(f=ea&kpoeU~CO|(pWfS%nTx>q@>hhK6@S|V9q_X(_#1((B&v1 z?IiRRaCmuGQ^6b+b8;c$24p?LA`sD1i;FP@^a-@xIAA7s8P+UA1YHO3Y?eY`lj??s z20h$ZvmdojkCr_>yEj~ai|z-qva$?xAHlqAjCEMv3wTxuzK!1RbvZaVI5;>sI5;>s kI5;>sI5;>sIR1zDCwD$TTS*~`d;kCd07*qoM6N<$f|-hyHUIzs literal 0 HcmV?d00001 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))