154 lines
4.6 KiB
PHP
154 lines
4.6 KiB
PHP
<?php
|
|
class Gateway extends MY_Controller {
|
|
function __construct()
|
|
{
|
|
return parent::__construct();
|
|
}
|
|
function email_config() {
|
|
header('Content-Type: application/json');
|
|
|
|
$sql = "SELECT
|
|
M_EmailConfigSender,
|
|
M_EmailConfigUsername,
|
|
M_EmailConfigPassword,
|
|
M_EmailConfigServer,
|
|
M_EmailConfigMaxRetry
|
|
FROM m_emailconfig
|
|
LIMIT 1";
|
|
$query = $this->db->query($sql);
|
|
if ($query === false) {
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Failed to fetch email config', 'config' => array()));
|
|
return;
|
|
}
|
|
|
|
$row = $query->row_array();
|
|
echo json_encode(array('status' => 'OK', 'message' => 'Email config fetched', 'config' => empty($row) ? array() : $row));
|
|
return;
|
|
}
|
|
function get_order($limit) {
|
|
header('Content-Type: application/json');
|
|
|
|
$limit = (int) $limit;
|
|
if ($limit < 1) {
|
|
$limit = 1;
|
|
}
|
|
|
|
$rows = array();
|
|
$this->db->trans_begin();
|
|
|
|
$selectSql = "SELECT *
|
|
FROM t_send_email
|
|
WHERE T_SendEmailStatus = 'S'
|
|
ORDER BY T_SendEmailID ASC
|
|
LIMIT {$limit}
|
|
FOR UPDATE";
|
|
$query = $this->db->query($selectSql);
|
|
|
|
if ($query === false) {
|
|
$this->db->trans_rollback();
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Failed to fetch sending queue', 'data' => array()));
|
|
return;
|
|
}
|
|
|
|
$rows = $query->result_array();
|
|
|
|
$remaining = $limit - count($rows);
|
|
if ($remaining > 0) {
|
|
$selectRetrySql = "SELECT *
|
|
FROM t_send_email
|
|
WHERE T_SendEmailStatus = 'E'
|
|
AND T_SendEmailCount <= 5
|
|
ORDER BY T_SendEmailID ASC
|
|
LIMIT {$remaining}
|
|
FOR UPDATE";
|
|
$retryQuery = $this->db->query($selectRetrySql);
|
|
|
|
if ($retryQuery === false) {
|
|
$this->db->trans_rollback();
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Failed to fetch retry queue', 'data' => array()));
|
|
return;
|
|
}
|
|
|
|
$retryRows = $retryQuery->result_array();
|
|
if (!empty($retryRows)) {
|
|
$rows = array_merge($rows, $retryRows);
|
|
}
|
|
}
|
|
|
|
if (!empty($rows)) {
|
|
$ids = array();
|
|
foreach ($rows as $row) {
|
|
$ids[] = (int) $row['T_SendEmailID'];
|
|
}
|
|
|
|
$updateSql = "UPDATE t_send_email
|
|
SET T_SendEmailStatus = 'P',
|
|
T_SendEmailLastUpdated = NOW()
|
|
WHERE T_SendEmailID IN (" . implode(',', $ids) . ")";
|
|
$updated = $this->db->query($updateSql);
|
|
|
|
if ($updated === false) {
|
|
$this->db->trans_rollback();
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Failed to update queue status', 'data' => array()));
|
|
return;
|
|
}
|
|
}
|
|
|
|
if ($this->db->trans_status() === false) {
|
|
$this->db->trans_rollback();
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Transaction failed', 'data' => array()));
|
|
return;
|
|
}
|
|
|
|
$this->db->trans_commit();
|
|
echo json_encode(array('status' => 'OK', 'message' => 'Data fetched', 'data' => $rows, 'total' => count($rows)));
|
|
return;
|
|
}
|
|
function update() {
|
|
header('Content-Type: application/json');
|
|
|
|
$raw = file_get_contents('php://input');
|
|
$payload = json_decode($raw, true);
|
|
if (!is_array($payload)) {
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Invalid JSON payload'));
|
|
return;
|
|
}
|
|
|
|
$id = isset($payload['id']) ? (int) $payload['id'] : 0;
|
|
$status = isset($payload['status']) ? strtoupper(trim($payload['status'])) : '';
|
|
$message = isset($payload['message']) ? $payload['message'] : '';
|
|
|
|
if ($id < 1) {
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Invalid id'));
|
|
return;
|
|
}
|
|
|
|
if ($status !== 'OK' && $status !== 'ERR') {
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Invalid status'));
|
|
return;
|
|
}
|
|
|
|
$escapedMessage = $this->db->escape($message);
|
|
$setSql = "T_SendEmailResponse = {$escapedMessage}, T_SendEmailLastUpdated = NOW()";
|
|
if ($status === 'OK') {
|
|
$setSql .= ", T_SendEmailStatus = 'R', T_SendEmailReceived = NOW()";
|
|
} else {
|
|
$setSql .= ", T_SendEmailStatus = 'E', T_SendEmailCount = T_SendEmailCount + 1";
|
|
}
|
|
|
|
$this->db->trans_begin();
|
|
$sql = "UPDATE t_send_email SET {$setSql} WHERE T_SendEmailID = {$id}";
|
|
$updated = $this->db->query($sql);
|
|
|
|
if ($updated === false || $this->db->trans_status() === false) {
|
|
$this->db->trans_rollback();
|
|
echo json_encode(array('status' => 'ERR', 'message' => 'Failed to update data'));
|
|
return;
|
|
}
|
|
|
|
$this->db->trans_commit();
|
|
echo json_encode(array('status' => 'OK', 'message' => 'Data updated', 'id' => $id, 'result_status' => $status));
|
|
return;
|
|
}
|
|
}
|