diff --git a/system/plugin/initiatePaybillStk.php b/system/plugin/initiatePaybillStk.php new file mode 100644 index 0000000..24348ee --- /dev/null +++ b/system/plugin/initiatePaybillStk.php @@ -0,0 +1,137 @@ +toastr.error('Invalid input data');"; + return; + } + + // Normalize phone number + $phone = preg_replace(['/^\+/', '/^0/', '/^7/', '/^1/'], ['', '254', '2547', '2541'], $phone); + + // Retrieve bank details from the database + $bankaccount = ORM::for_table('tbl_appconfig')->where('setting', 'PaybillAcc')->find_one(); + $bankname = ORM::for_table('tbl_appconfig')->where('setting', 'PaybillName')->find_one(); + $bankaccount = $bankaccount ? $bankaccount->value : null; + $bankname = $bankname ? $bankname->value : null; + + if (!$bankaccount || !$bankname) { + echo ""; + return; + } + + // Check for existing user details + $CheckId = ORM::for_table('tbl_customers')->where('username', $username)->order_by_desc('id')->find_one(); + $CheckUser = ORM::for_table('tbl_customers')->where('phonenumber', $phone)->find_many(); + $UserId = $CheckId ? $CheckId->id : null; + + if ($CheckUser) { + ORM::for_table('tbl_customers')->where('phonenumber', $phone)->where_not_equal('id', $UserId)->delete_many(); + } + + // Retrieve payment gateway record + $PaymentGatewayRecord = ORM::for_table('tbl_payment_gateway') + ->where('username', $username) + ->where('status', 1) + ->order_by_desc('id') + ->find_one(); + + if (!$PaymentGatewayRecord) { + echo ""; + return; + } + + // Update user phone number + $ThisUser = ORM::for_table('tbl_customers')->where('username', $username)->order_by_desc('id')->find_one(); + if ($ThisUser) { + $ThisUser->phonenumber = $phone; + $ThisUser->save(); + } + + $amount = $PaymentGatewayRecord->price; + + // Safaricom API credentials + $consumerKey = 'YOUR_CONSUMER_KEY'; + $consumerSecret = 'YOUR_CONSUMER_SECRET'; + + // Get access token + $access_token_url = 'https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; + $curl = curl_init($access_token_url); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/json; charset=utf8']); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_USERPWD, "$consumerKey:$consumerSecret"); + $result = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + + if ($status !== 200) { + echo ""; + return; + } + + $result = json_decode($result); + $access_token = $result->access_token; + + // Initiate Paybill payment + $paybill_url = 'https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest'; + $Timestamp = date("YmdHis"); + $BusinessShortCode = 'YOUR_BUSINESS_SHORTCODE'; + $Passkey = 'YOUR_PASSKEY'; + $Password = base64_encode($BusinessShortCode . $Passkey . $Timestamp); + $CallBackURL = U . 'callback/PaybillCallback'; + + $curl_post_data = [ + 'BusinessShortCode' => $BusinessShortCode, + 'Password' => $Password, + 'Timestamp' => $Timestamp, + 'TransactionType' => 'CustomerPayBillOnline', + 'Amount' => $amount, + 'PartyA' => $phone, + 'PartyB' => $BusinessShortCode, + 'PhoneNumber' => $phone, + 'CallBackURL' => $CallBackURL, + 'AccountReference' => $bankaccount, + 'TransactionDesc' => 'PayBill Payment' + ]; + + $curl = curl_init($paybill_url); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/json', 'Authorization:Bearer ' . $access_token]); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data)); + $curl_response = curl_exec($curl); + curl_close($curl); + + $mpesaResponse = json_decode($curl_response); + $responseCode = $mpesaResponse->ResponseCode; + $resultDesc = $mpesaResponse->resultDesc; + $MerchantRequestID = $mpesaResponse->MerchantRequestID; + $CheckoutRequestID = $mpesaResponse->CheckoutRequestID; + + if ($responseCode == "0") { + date_default_timezone_set('Africa/Nairobi'); + $now = date("Y-m-d H:i:s"); + + $PaymentGatewayRecord->pg_paid_response = $resultDesc; + $PaymentGatewayRecord->username = $username; + $PaymentGatewayRecord->checkout = $CheckoutRequestID; + $PaymentGatewayRecord->payment_method = 'Mpesa PayBill'; + $PaymentGatewayRecord->payment_channel = 'Mpesa PayBill'; + $PaymentGatewayRecord->save(); + + if (!empty($_POST['channel'])) { + echo json_encode(["status" => "success", "message" => "Enter Pin to complete"]); + } else { + echo ""; + } + } else { + echo ""; + } +} + +?> diff --git a/system/plugin/initiatebankstk.php b/system/plugin/initiatebankstk.php new file mode 100644 index 0000000..4de0c5a --- /dev/null +++ b/system/plugin/initiatebankstk.php @@ -0,0 +1,304 @@ +where('setting', 'Stkbankacc') + ->find_one(); + + $bankname = ORM::for_table('tbl_appconfig') + ->where('setting', 'Stkbankname') + ->find_one(); + + $bankaccount = ($bankaccount) ? $bankaccount->value : null; + $bankname = ($bankname) ? $bankname->value : null; + + // echo $bankname; + + + $CheckId = ORM::for_table('tbl_customers') + ->where('username', $username) + ->order_by_desc('id') + ->find_one(); + + $CheckUser = ORM::for_table('tbl_customers') + ->where('phonenumber', $phone) + ->find_many(); + + $UserId=$CheckId->id; + + + + + + + if (empty($bankaccount) || empty($bankname)) { + + + echo $error=""; + + + die(); + } + + + + + + $getpaybill = ORM::for_table('tbl_banks') + ->where('name', $bankname) + ->find_one(); + + + + + + + $paybill=$getpaybill->paybill; + + + + // echo $paybill; + + + + + + + + + + $cburl = U . 'callback/BankStkPush' ; + + + $PaymentGatewayRecord = ORM::for_table('tbl_payment_gateway') + ->where('username', $username) + ->where('status', 1) // Add this line to filter by status + ->order_by_desc('id') + ->find_one(); + + + + $ThisUser= ORM::for_table('tbl_customers') + ->where('username', $username) + ->order_by_desc('id') + ->find_one(); + + + + $ThisUser->phonenumber=$phone; + // $ThisUser->username=$phone; + $ThisUser->save(); + + + + + + $amount=$PaymentGatewayRecord->price; + + if(!$PaymentGatewayRecord){ + + echo $error=""; + + die(); + } + + + + + + + + + $consumerKey = '3AmVP1WFDQn7GrDH8GcSSKxcAvnJdZGC'; //Fill with your app Consumer Key + $consumerSecret = '71Lybl6jUtxM0F35'; // Fill with your app Secret + + $headers = ['Content-Type:application/json; charset=utf8']; + + $access_token_url = 'https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; + + $curl = curl_init($access_token_url); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_HEADER, FALSE);(''); + + curl_setopt($curl, CURLOPT_USERPWD, $consumerKey.':'.$consumerSecret); + $result = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $result = json_decode($result); + + $access_token = $result->access_token; + + // echo $access_token; + + curl_close($curl); + + +// Initiate Stk push + +$stk_url = 'https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest'; +$PartyA = $phone; // This is your phone number, + $AccountReference = $bankaccount; + $TransactionDesc = 'TestMapayment'; + $Amount = $amount; + $BusinessShortCode='4122323'; + $Passkey='aaebecea73082fa56af852606106b1316d5b4dfa2f12d0088800b0b88e4bb6e3'; + $Timestamp = date("YmdHis",time()); + $Password = base64_encode($BusinessShortCode.$Passkey.$Timestamp); + $CallBackURL = $cburl; + + +$curl = curl_init(); +curl_setopt($curl, CURLOPT_URL, $stk_url); +curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.$access_token)); //setting custom header + + +$curl_post_data = array( + //Fill in the request parameters with valid values + 'BusinessShortCode' => $BusinessShortCode, + 'Password' => $Password, + 'Timestamp' => $Timestamp, + 'TransactionType' => 'CustomerPayBillOnline', + 'Amount' => $Amount, + 'PartyA' => $PartyA, + 'PartyB' => $paybill, + 'PhoneNumber' => $PartyA, + 'CallBackURL' => $CallBackURL, + 'AccountReference' => $AccountReference, + 'TransactionDesc' => $TransactionDesc +); + +$data_string = json_encode($curl_post_data); + +curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); +curl_setopt($curl, CURLOPT_POST, true); +curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); + +$curl_response = curl_exec($curl); +//print_r($curl_response); + +// echo $curl_response; +// die; + +$mpesaResponse = json_decode($curl_response); + + + + +$responseCode = $mpesaResponse->ResponseCode; +$resultDesc = $mpesaResponse->resultDesc; +$MerchantRequestID = $mpesaResponse->MerchantRequestID; +$CheckoutRequestID = $mpesaResponse->CheckoutRequestID; + + + if($responseCode=="0"){ + date_default_timezone_set('Africa/Nairobi'); + $now=date("Y-m-d H:i:s"); + +// $username=$phone; + + $PaymentGatewayRecord->pg_paid_response = $resultDesc; + $PaymentGatewayRecord->username = $username; + $PaymentGatewayRecord->checkout = $CheckoutRequestID; + $PaymentGatewayRecord->payment_method = 'Mpesa Stk Push'; + $PaymentGatewayRecord->payment_channel = 'Mpesa Stk Push'; + $PaymentGatewayRecord->save(); + + + + if(!empty($_POST['channel'])){ + + echo json_encode(["status" => "success", "message" => "Enter Pin to complete"]); + + }else{ + echo $error=""; + + } + + }else{ + + echo $error=""; + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + +?> diff --git a/system/plugin/initiatempesa.php b/system/plugin/initiatempesa.php new file mode 100644 index 0000000..6670855 --- /dev/null +++ b/system/plugin/initiatempesa.php @@ -0,0 +1,148 @@ +where('username', $username) + ->order_by_desc('id') + ->find_one(); + $CheckUser = ORM::for_table('tbl_customers') + ->where('phonenumber', $phone) + ->find_many(); + $UserId = $CheckId->id; + + $CallBackURL = U . 'callback/mpesa'; + $PaymentGatewayRecord = ORM::for_table('tbl_payment_gateway') + ->where('username', $username) + ->where('status', 1) // Add this line to filter by status + ->order_by_desc('id') + ->find_one(); + $ThisUser = ORM::for_table('tbl_customers') + ->where('username', $username) + ->order_by_desc('id') + ->find_one(); + $ThisUser->phonenumber = $phone; + $ThisUser->save(); + $amount = $PaymentGatewayRecord->price; + if (!$PaymentGatewayRecord) { + echo json_encode(["status" => "error", "message" => "Could not complete the payment req, please contact administrator"]); + } + // Get the M-Pesa mpesa_env + $mpesa_env = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_env') + ->find_one(); + $mpesa_env = ($mpesa_env) ? $mpesa_env->value : null; + // Get the M-Pesa consumer key + $mpesa_consumer_key = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_consumer_key') + ->find_one(); + $mpesa_consumer_key = ($mpesa_consumer_key) ? $mpesa_consumer_key->value : null; + // Get the M-Pesa consumer secret + $mpesa_consumer_secret = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_consumer_secret') + ->find_one(); + $mpesa_consumer_secret = ($mpesa_consumer_secret) ? $mpesa_consumer_secret->value : null; + $mpesa_business_code = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_business_code') + ->find_one(); + $mpesa_business_code = ($mpesa_business_code) ? $mpesa_business_code->value : null; + $mpesa_shortcode_type = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_shortcode_type') + ->find_one(); + if ($mpesa_shortcode_type == 'BuyGoods') { + $mpesa_buygoods_till_number = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_buygoods_till_number') + ->find_one(); + $mpesa_buygoods_till_number = ($mpesa_buygoods_till_number) ? $mpesa_buygoods_till_number->value : null; + $PartyB = $mpesa_buygoods_till_number; + $Type_of_Transaction = 'CustomerBuyGoodsOnline'; + } else { + $PartyB = $mpesa_business_code; + $Type_of_Transaction = 'CustomerPayBillOnline'; + } + $Passkey = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_pass_key') + ->find_one(); + $Passkey = ($Passkey) ? $Passkey->value : null; + $Time_Stamp = date("Ymdhis"); + $password = base64_encode($mpesa_business_code . $Passkey . $Time_Stamp); + if ($mpesa_env == "live") { + $OnlinePayment = 'https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest'; + $Token_URL = 'https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; + } elseif ($mpesa_env == "sandbox") { + $OnlinePayment = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest'; + $Token_URL = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; + } else { + return json_encode(["Message" => "invalid application status"]); + }; + $headers = ['Content-Type:application/json; charset=utf8']; + $curl = curl_init($Token_URL); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_HEADER, FALSE); + curl_setopt($curl, CURLOPT_USERPWD, $mpesa_consumer_key . ':' . $mpesa_consumer_secret); + $result = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $result = json_decode($result); + $access_token = $result->access_token; + curl_close($curl); + $password = base64_encode($mpesa_business_code . $Passkey . $Time_Stamp); + $stkpushheader = ['Content-Type:application/json', 'Authorization:Bearer ' . $access_token]; + //INITIATE CURL + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $OnlinePayment); + curl_setopt($curl, CURLOPT_HTTPHEADER, $stkpushheader); //setting custom header + $curl_post_data = array( + //Fill in the request parameters with valid values + 'BusinessShortCode' => $mpesa_business_code, + 'Password' => $password, + 'Timestamp' => $Time_Stamp, + 'TransactionType' => $Type_of_Transaction, + 'Amount' => $amount, + 'PartyA' => $phone, + 'PartyB' => $PartyB, + 'PhoneNumber' => $phone, + 'CallBackURL' => $CallBackURL, + 'AccountReference' => $username, + 'TransactionDesc' => 'Payment for ' . $username + ); + $data_string = json_encode($curl_post_data); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); + $curl_response = curl_exec($curl); + $curl_Tranfer2_response = json_decode($curl_response); + if (isset($curl_Tranfer2_response->ResponseCode) && $curl_Tranfer2_response->ResponseCode == "0") { + $resultDesc = $curl_Tranfer2_response->resultDesc; + $CheckoutRequestID = $curl_Tranfer2_response->CheckoutRequestID; + date_default_timezone_set('Africa/Nairobi'); + $now = date("Y-m-d H:i:s"); + // $username=$phone; + $PaymentGatewayRecord->pg_paid_response = $resultDesc; + $PaymentGatewayRecord->username = $username; + $PaymentGatewayRecord->checkout = $CheckoutRequestID; + $PaymentGatewayRecord->payment_method = 'Mpesa Stk Push'; + $PaymentGatewayRecord->payment_channel = 'Mpesa Stk Push'; + $saveGateway = $PaymentGatewayRecord->save(); + if ($saveGateway) { + if (!empty($_POST['channel'])) { + echo json_encode(["status" => "success", "message" => "Enter Mpesa Pin to complete $mpesa_business_code $Type_of_Transaction , Party B: $PartyB, Amount: $amount, Phone: $phone, CheckoutRequestID: $CheckoutRequestID"]); + } else { + echo ""; + } + } else { + echo json_encode(["status" => "error", "message" => "Failed to save the payment gateway record"]); + } + } else { + $errorMessage = $curl_Tranfer2_response->errorMessage; + echo json_encode(["status" => "error", "message" => $errorMessage]); + } +} diff --git a/system/plugin/initiatetillstk.php b/system/plugin/initiatetillstk.php new file mode 100644 index 0000000..51238a5 --- /dev/null +++ b/system/plugin/initiatetillstk.php @@ -0,0 +1,232 @@ +where('setting', 'mpesa_till_consumer_key') + ->find_one(); + + $consumer_secret = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_till_consumer_secret') + ->find_one(); + + $consumer_secret = ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_till_consumer_secret') + ->find_one(); + + $BusinessShortCode= ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_till_shortcode_code') + ->find_one(); + + $PartyB= ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_till_partyb') + ->find_one(); + + + $LipaNaMpesaPasskey= ORM::for_table('tbl_appconfig') + ->where('setting', 'mpesa_till_pass_key') + ->find_one(); + + + + $consumer_key = ($consumer_key) ? $consumer_key->value : null; + $consumer_secret = ($consumer_secret) ? $consumer_secret->value : null; + $BusinessShortCode = ($BusinessShortCode) ? $BusinessShortCode->value : null; + $PartyB = ($PartyB) ? $PartyB->value : null; + $LipaNaMpesaPasskey = ($LipaNaMpesaPasskey) ? $LipaNaMpesaPasskey->value : null; + + + + + + $cburl = U . 'callback/MpesatillStk' ; + + + // + + $CheckId = ORM::for_table('tbl_customers') + ->where('username', $username) + ->order_by_desc('id') + ->find_one(); + + $CheckUser = ORM::for_table('tbl_customers') + ->where('phonenumber', $phone) + ->find_many(); + + $UserId=$CheckId->id; + + + + $PaymentGatewayRecord = ORM::for_table('tbl_payment_gateway') + ->where('username', $username) + ->where('status', 1) // Add this line to filter by status + ->order_by_desc('id') + ->find_one(); + + $ThisUser= ORM::for_table('tbl_customers') + ->where('username', $username) + ->order_by_desc('id') + ->find_one(); + + + + $ThisUser->phonenumber=$phone; + // $ThisUser->username=$phone; + $ThisUser->save(); + + + $amount=$PaymentGatewayRecord->price; + + if(!$PaymentGatewayRecord){ + + echo $error=""; + die(); + + } + + + $TransactionType = 'CustomerBuyGoodsOnline'; + $tokenUrl = 'https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; + $phone= $phone; + $lipaOnlineUrl = 'https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest'; + // $amount= '1'; + $CallBackURL = $cburl; + date_default_timezone_set('Africa/Nairobi'); + $timestamp = date("YmdHis"); + $password = base64_encode($BusinessShortCode . $LipaNaMpesaPasskey . $timestamp); + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $tokenUrl); + $credentials = base64_encode($consumer_key . ':' . $consumer_secret); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $credentials)); + curl_setopt($curl, CURLOPT_HEADER, false); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + $curl_response = curl_exec($curl); + + $token = json_decode($curl_response)->access_token; + $curl2 = curl_init(); + curl_setopt($curl2, CURLOPT_URL, $lipaOnlineUrl); + curl_setopt($curl2, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization:Bearer ' . $token)); + + + + $curl2_post_data = [ + 'BusinessShortCode' => $BusinessShortCode, + 'Password' => $password, + 'Timestamp' => $timestamp, + 'TransactionType' => $TransactionType, + 'Amount' => $amount, + 'PartyA' => $phone, + 'PartyB' => $PartyB, + 'PhoneNumber' => $phone, + 'CallBackURL' => $CallBackURL, + 'AccountReference' => 'Payment For Goods', + 'TransactionDesc' => 'Payment for goods', + ]; + + $data2_string = json_encode($curl2_post_data); + + curl_setopt($curl2, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl2, CURLOPT_POST, true); + curl_setopt($curl2, CURLOPT_POSTFIELDS, $data2_string); + curl_setopt($curl2, CURLOPT_HEADER, false); + curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($curl2, CURLOPT_SSL_VERIFYHOST, 0); + $curl_response = curl_exec($curl2); + +$curl_response1 = curl_exec($curl); +//($curl_response); + +//echo $curl_response; + + $mpesaResponse = json_decode($curl_response); + + +//echo $phone; + +$responseCode = $mpesaResponse->ResponseCode; +$MerchantRequestID = $mpesaResponse->MerchantRequestID; +$CheckoutRequestID = $mpesaResponse->CheckoutRequestID; +$resultDesc = $mpesaResponse->CustomerMessage; + // file_put_contents('stk.log',$curl_response,FILE_APPEND); + + + + + + + + + + + + +// echo $cburl; + + $responseCode = $responseCode; + if($responseCode=="0"){ + date_default_timezone_set('Africa/Nairobi'); + $now=date("Y-m-d H:i:s"); + + + // $username=$phone; + + + $PaymentGatewayRecord->pg_paid_response = $resultDesc; + $PaymentGatewayRecord->checkout = $CheckoutRequestID; + $PaymentGatewayRecord->username = $username; + $PaymentGatewayRecord->payment_method = 'Mpesa Stk Push'; + $PaymentGatewayRecord->payment_channel = 'Mpesa Stk Push'; + $PaymentGatewayRecord->save(); + + if(!empty($_POST['channel'])){ + + echo json_encode(["status" => "success", "message" => "Enter Pin to complete","phone"=> $phone]); + + }else{ + echo $error=""; + + } + + + + }else{ + + echo "There is an issue with the transaction, please wait for 0 seconds then try again"; + } + + + + + + + + + + + + + + + + + + } diff --git a/system/plugin/log.php b/system/plugin/log.php new file mode 100644 index 0000000..7fd8661 --- /dev/null +++ b/system/plugin/log.php @@ -0,0 +1,46 @@ +assign('_title', 'Log Mikrotik'); + $ui->assign('_system_menu', 'Log Mikrotik'); + $admin = Admin::_info(); + $ui->assign('_admin', $admin); + $routers = ORM::for_table('tbl_routers')->where('enabled', '1')->find_many(); + $routerId = $routes['2'] ?? ($routers ? $routers[0]['id'] : null); // Memastikan ada router yang aktif + $logs = fetchLogs($routerId); // Mengambil log dari router yang dipilih + $ui->assign('logs', $logs); + + $ui->display('log.tpl'); +} + +// Fungsi untuk mengambil logs dari MikroTik +function fetchLogs($routerId) { + if (!$routerId) { + return []; // Mengembalikan array kosong jika router tidak tersedia + } + + $mikrotik = ORM::for_table('tbl_routers')->where('enabled', '1')->find_one($routerId); + if (!$mikrotik) { + return []; // Mengembalikan array kosong jika router tidak ditemukan + } + + $client = Mikrotik::getClient($mikrotik['ip_address'], $mikrotik['username'], $mikrotik['password']); + $request = new Request('/log/print'); + $response = $client->sendSync($request); + + $logs = []; + foreach ($response as $entry) { + $logs[] = $entry->getIterator()->getArrayCopy(); // Mengumpulkan data dari setiap entry + } + + return $logs; +} \ No newline at end of file