db->trans_begin(); $prm = $this->sys_input; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://api.krmpesan.app/tokens?refresh_token=$this->REFRESH_TOKEN&device_key=$this->DEVICE_kEY", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', )); $response = curl_exec($curl); curl_close($curl); if ($response === false) { return false; } $resp = json_decode($response, true); $idtoken = $resp['IdToken']; // hapus data sebelumnya $sqldelete = "DELETE FROM one_hs.hs_token"; $qrydelete = $this->db->query($sqldelete); if (!$qrydelete) { $this->db->trans_rollback(); $this->sys_error_db("delete hs_token error", $this->db); exit; } // insert data ke dalam database $sql = "INSERT INTO one_hs.hs_token( HS_TokenDate, HS_TokenToken, HS_TokenCreated, HS_TokenLastUpdated) VALUES(NOW(),?,NOW(),NOW())"; $qry = $this->db->query($sql, array($idtoken)); if (!$qry) { $this->db->trans_rollback(); $this->sys_error_db("insert hs_token error", $this->db); exit; } $this->db->trans_commit(); echo $idtoken; } function get_refreshtoken() { $sql = "SELECT HS_TokenID, HS_TokenDate, HS_TokenToken FROM one_hs.hs_token"; $qry = $this->db->query($sql); if ($qry) { $rows = $qry->result_array(); } else { $this->sys_error_db("hs_token select", $this->db); exit; } foreach ($rows as $k => $v) { $tokenhs = $v["HS_TokenToken"]; } echo $tokenhs; } function get_token() { $urltoken = 'http://devone.aplikasi.web.id/one-api/hs/send_whatsappv2/get_refreshtoken'; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $urltoken); $result = curl_exec($ch); curl_close($ch); return $result; } function post($url, $data, $token) { $prm = $this->sys_input; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $data, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Accept: application/json', 'Authorization: Bearer ' . $token, ] )); $response = curl_exec($curl); if (curl_error($curl) != "") { echo json_encode([ "status" => "ERR", "message" => "Http Error : " . curl_error($curl), ]); curl_close($curl); exit(); } curl_close($curl); return $response; } function send_new($template_name = "geths") { $sql = "SELECT HS_WhatsAppID, HS_WhatsAppName, HS_WhatsAppNoHP, HS_WhatsAppIsSent, HS_WhatsAppType, DATE_FORMAT(T_OrderDate, '%d-%m-%Y') as orderDate, T_OrderTime FROM one_hs.hs_whatsapp JOIN one_hs.t_order ON T_OrderT_TransactionID = HS_WhatsAppT_TransactionID AND T_OrderIsActive = 'Y' WHERE HS_WhatsAppIsActive = 'Y' AND HS_WhatsAppType = 'NEW' AND (HS_WhatsAppIsSent = 'N' OR HS_WhatsAppIsSent = 'R') ORDER BY HS_WhatsAppID"; $token = $this->get_token(); $qry = $this->db->query($sql); if ($qry) { $rows = $qry->result_array(); } else { $this->sys_error_db("select whatsapp error", $this->db); exit; } $url = "https://api.krmpesan.app/messages"; // print_r($rows); foreach ($rows as $key => $value) { // print_r($value); $waId = $value["HS_WhatsAppID"]; $phone = $value["HS_WhatsAppNoHP"]; $name = $value["HS_WhatsAppName"]; $orderdate = $value["orderDate"]; $ordertime = $value["T_OrderTime"]; $data = json_encode([ "phone" => $phone, "template_name" => $template_name, "template_language" => "id", "template" => [ "body" => [ $name, $orderdate . ' ' . 'jam' . ' ' . $ordertime ] ] ]); $messages = $orderdate . ' ' . $ordertime; // echo $data; $resp = $this->post($url, $data, $token); $j_response = json_decode($resp, true); if ($j_response["code"] == 200) { $queueid = $j_response["data"]["id"]; $sql_update = "UPDATE one_hs.hs_whatsapp SET HS_WhatsAppMessage = ?, HS_WhatsAppIsSent = 'Y', HS_WhatsAppSentDate = NOW(), queue_id = ? WHERE HS_WhatsAppID = ?"; $qry_update = $this->db->query($sql_update, array( $messages, $queueid, $waId )); if (!$qry_update) { $this->sys_error_db("update whatsapp error", $this->db); exit; } $result = [ "status" => "OK" ]; } else { $sql_update = "UPDATE one_hs.hs_whatsapp SET HS_WhatsAppIsSent = if(HS_WhatsAppRetry >= 3, 'E', 'N'), HS_WhatsAppRetry = HS_WhatsAppRetry + 1 WHERE HS_WhatsAppID = ?"; $qry_update = $this->db->query($sql_update, array( $waId )); if (!$qry_update) { $this->sys_error_db("update whatsapp error", $this->db); exit; } $result = [ "status" => "ERR", "message" => $resp ]; } } return $result; } function send_process($template_name = "homeservice2") { $sql = "SELECT HS_WhatsAppID, HS_WhatsAppName, HS_WhatsAppNoHP, HS_WhatsAppIsSent, HS_WhatsAppType, T_OrderDate, T_OrderTime, M_StaffName FROM one_hs.hs_whatsapp JOIN one_hs.t_order ON HS_WhatsAppT_TransactionID = T_OrderT_TransactionID AND T_OrderIsActive = 'Y' JOIN one_hs.hs_deliveryorderdetail ON HS_WhatsAppT_TransactionID = HS_DeliveryOrderDetailT_TransactionID AND HS_DeliveryOrderDetailIsActive = 'Y' JOIN one_hs.hs_deliveryorder ON HS_DeliveryOrderDetailHS_DeliveryOrderID = HS_DeliveryOrderID AND HS_DeliveryOrderIsActive = 'Y' JOIN one.m_user ON HS_DeliveryOrderM_UserID = M_UserID AND M_UserIsActive = 'Y' JOIN one.m_staff ON M_UserM_StaffID = M_StaffID AND M_StaffIsActive = 'Y' WHERE HS_WhatsAppIsActive = 'Y' AND HS_WhatsAppType = 'PROCESS' AND (HS_WhatsAppIsSent = 'N' OR HS_WhatsAppIsSent = 'R') ORDER BY HS_WhatsAppID"; $qry = $this->db->query($sql); if ($qry) { $rows = $qry->result_array(); } else { $this->sys_error_db("select whatsapp error", $this->db); exit; } $token = $this->get_token(); // print_r($rows); $url = "https://api.krmpesan.app/messages"; foreach ($rows as $key => $value) { // print_r($value); $waId = $value["HS_WhatsAppID"]; $phone = $value["HS_WhatsAppNoHP"]; $name = $value["HS_WhatsAppName"]; $staff = $value["M_StaffName"]; $data = json_encode([ "phone" => $phone, "template_name" => $template_name, "template_language" => "id", "template" => [ "body" => [ $name, $staff ] ] ]); // echo $data; $resp = $this->post($url, $data, $token); $j_response = json_decode($resp, true); // print_r($j_response); if ($j_response["code"] == 200) { $queueid = $j_response["data"]["id"]; $sql_update = "UPDATE one_hs.hs_whatsapp SET HS_WhatsAppMessage = ?, HS_WhatsAppIsSent = 'Y', HS_WhatsAppSentDate = NOW(), queue_id = ? WHERE HS_WhatsAppID = ?"; $qry_update = $this->db->query($sql_update, array( $staff, $queueid, $waId )); if (!$qry_update) { $this->sys_error_db("update whatsapp error", $this->db); exit; } $result = [ "status" => "OK" ]; } else { $sql_update = "UPDATE one_hs.hs_whatsapp SET HS_WhatsAppIsSent = if(HS_WhatsAppRetry >= 3, 'E', 'N'), HS_WhatsAppRetry = HS_WhatsAppRetry + 1 WHERE HS_WhatsAppID = ?"; $qry_update = $this->db->query($sql_update, array( $waId )); if (!$qry_update) { $this->sys_error_db("update whatsapp error", $this->db); exit; } $result = [ "status" => "ERR", "message" => $resp ]; } } return $result; } function send() { // send new $resultnew = $this->send_new(); if ($resultnew === false) { echo json_encode(array( 'status' => 'ERR', 'message' => 'Send Message Error' )); exit; } // send process $resultprocess = $this->send_process(); if ($resultprocess === false) { echo json_encode(array( 'status' => 'ERR', 'message' => 'Send Message Error' )); exit; } echo json_encode(array( 'status' => 'OK', 'wa_message_new' => $resultnew, 'wa_message_process' => $resultprocess )); } function sendv2() { $prm = $this->sys_input; $template_new = $prm['template_new']; $template_process = $prm['template_process']; // send new $resultnew = $this->send_new($template_new); if ($resultnew === false) { echo json_encode(array( 'status' => 'ERR', 'message' => 'Send Message Error' )); exit; } // send process $resultprocess = $this->send_process($template_process); if ($resultprocess === false) { echo json_encode(array( 'status' => 'ERR', 'message' => 'Send Message Error' )); exit; } echo json_encode(array( 'status' => 'OK', 'wa_message_new' => $resultnew, 'wa_message_process' => $resultprocess )); } }