222 lines
11 KiB
PHP
222 lines
11 KiB
PHP
<?php
|
|
require_once __DIR__ . "/../includes/functions.php";
|
|
require_permission("settings_view");
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../includes/WablasService.php';
|
|
|
|
$pdo = db();
|
|
$wablasTestResult = null;
|
|
$message = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!has_permission('settings_add')) {
|
|
header("Location: integrations.php?error=permission_denied");
|
|
exit;
|
|
}
|
|
|
|
$provider = $_POST['provider'] ?? '';
|
|
$action = $_POST['action'] ?? 'save';
|
|
|
|
// Thawani
|
|
if ($provider === 'thawani') {
|
|
$keys = ['public_key', 'secret_key', 'environment'];
|
|
foreach ($keys as $k) {
|
|
$val = $_POST[$k] ?? '';
|
|
$stmt = $pdo->prepare("INSERT INTO integration_settings (provider, setting_key, setting_value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
|
|
$stmt->execute(['thawani', $k, $val]);
|
|
}
|
|
$message = 'saved';
|
|
|
|
if ($action === 'save') {
|
|
header("Location: integrations.php?msg=saved");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Wablas
|
|
if ($provider === 'wablas') {
|
|
$keys = ['domain', 'token', 'secret_key', 'order_template', 'is_enabled'];
|
|
foreach ($keys as $k) {
|
|
$val = $_POST[$k] ?? '0';
|
|
if ($k === 'is_enabled' && !isset($_POST[$k])) {
|
|
$val = '0';
|
|
}
|
|
$stmt = $pdo->prepare("INSERT INTO integration_settings (provider, setting_key, setting_value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
|
|
$stmt->execute(['wablas', $k, $val]);
|
|
}
|
|
|
|
if ($action === 'save') {
|
|
header("Location: integrations.php?msg=saved");
|
|
exit;
|
|
} elseif ($action === 'test') {
|
|
// Instantiate service (loads from DB, which we just updated)
|
|
$wablasService = new WablasService($pdo);
|
|
$testPhone = $_POST['test_phone'] ?? '';
|
|
|
|
if (!empty($testPhone)) {
|
|
$wablasTestResult = $wablasService->sendMessage($testPhone, "Test message from Flatlogic POS. Connection verified!");
|
|
} else {
|
|
$wablasTestResult = $wablasService->testConnection();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch current settings
|
|
$stmt = $pdo->query("SELECT provider, setting_key, setting_value FROM integration_settings");
|
|
$allSettings = $stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC);
|
|
|
|
function getSetting($settings, $provider, $key) {
|
|
if (isset($settings[$provider])) {
|
|
foreach ($settings[$provider] as $s) {
|
|
if ($s['setting_key'] === $key) return $s['setting_value'];
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
$thawaniEnv = getSetting($allSettings, 'thawani', 'environment');
|
|
$thawaniPub = getSetting($allSettings, 'thawani', 'public_key');
|
|
$thawaniSec = getSetting($allSettings, 'thawani', 'secret_key');
|
|
|
|
$wablasDom = getSetting($allSettings, 'wablas', 'domain');
|
|
$wablasTok = getSetting($allSettings, 'wablas', 'token');
|
|
$wablasSecKey = getSetting($allSettings, 'wablas', 'secret_key');
|
|
$wablasTemplate = getSetting($allSettings, 'wablas', 'order_template');
|
|
$wablasEnabled = getSetting($allSettings, 'wablas', 'is_enabled');
|
|
|
|
// Default template if empty
|
|
if (empty($wablasTemplate)) {
|
|
$wablasTemplate = "Dear *{customer_name}*,
|
|
|
|
Thank you for dining with *{company_name}*! 🍽️
|
|
|
|
*Order Details:*
|
|
{order_details}
|
|
|
|
Total: *{total_amount}* OMR
|
|
|
|
You've earned *{points_earned} points* with this order.
|
|
💰 *Current Balance: {new_balance} points*";
|
|
}
|
|
|
|
require_once __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2 class="h3 mb-0 text-gray-800">Integrations</h2>
|
|
</div>
|
|
|
|
<?php if (isset($_GET['error']) && $_GET['error'] == 'permission_denied'): ?>
|
|
<div class="alert alert-danger border-0 shadow-sm rounded-3">Access Denied: You do not have permission to perform this action.</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($_GET['msg']) && $_GET['msg'] == 'saved'): ?>
|
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
|
Settings saved successfully.
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($wablasTestResult): ?>
|
|
<div class="alert alert-<?= $wablasTestResult['success'] ? 'success' : 'danger' ?> alert-dismissible fade show" role="alert">
|
|
<strong>Wablas Test Result:</strong> <?= htmlspecialchars($wablasTestResult['message']) ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="row">
|
|
<!-- Thawani -->
|
|
<div class="col-md-6 mb-4">
|
|
<div class="card shadow h-100">
|
|
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
|
|
<h6 class="m-0 fw-bold text-primary">Thawani Payments</h6>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<input type="hidden" name="provider" value="thawani">
|
|
<div class="mb-3">
|
|
<label class="form-label">Environment</label>
|
|
<select class="form-select" name="environment" <?= !has_permission('settings_add') ? 'disabled' : '' ?>>
|
|
<option value="sandbox" <?= $thawaniEnv == 'sandbox' ? 'selected' : '' ?>>Sandbox</option>
|
|
<option value="production" <?= $thawaniEnv == 'production' ? 'selected' : '' ?>>Production</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Public Key</label>
|
|
<input type="text" class="form-control" name="public_key" value="<?= htmlspecialchars($thawaniPub) ?>" <?= !has_permission('settings_add') ? 'readonly' : '' ?>>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Secret Key</label>
|
|
<input type="password" class="form-control" name="secret_key" value="<?= htmlspecialchars($thawaniSec) ?>" <?= !has_permission('settings_add') ? 'readonly' : '' ?>>
|
|
</div>
|
|
<?php if (has_permission('settings_add')): ?>
|
|
<button type="submit" name="action" value="save" class="btn btn-primary">Save Thawani Settings</button>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Wablas -->
|
|
<div class="col-md-6 mb-4">
|
|
<div class="card shadow h-100">
|
|
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
|
|
<h6 class="m-0 fw-bold text-success">Wablas WhatsApp</h6>
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" name="is_enabled" id="is_enabled_switch" form="wablas_form" value="1" <?= $wablasEnabled === '1' ? 'checked' : '' ?> <?= !has_permission('settings_add') ? 'disabled' : '' ?>>
|
|
<label class="form-check-label" for="is_enabled_switch">Enabled</label>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST" id="wablas_form">
|
|
<input type="hidden" name="provider" value="wablas">
|
|
|
|
<!-- Also keep a hidden input to send '0' if checkbox is unchecked (handled in PHP POST block too) -->
|
|
<div class="mb-3">
|
|
<label class="form-label">Domain</label>
|
|
<input type="text" class="form-control" name="domain" placeholder="https://..." value="<?= htmlspecialchars($wablasDom) ?>" <?= !has_permission('settings_add') ? 'readonly' : '' ?>>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Token</label>
|
|
<input type="password" class="form-control" name="token" value="<?= htmlspecialchars($wablasTok) ?>" <?= !has_permission('settings_add') ? 'readonly' : '' ?>>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Secret Key</label>
|
|
<input type="password" class="form-control" name="secret_key" value="<?= htmlspecialchars($wablasSecKey) ?>" <?= !has_permission('settings_add') ? 'readonly' : '' ?>>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Order Notification Template</label>
|
|
<textarea class="form-control font-monospace" name="order_template" rows="8" <?= !has_permission('settings_add') ? 'readonly' : '' ?>><?= htmlspecialchars($wablasTemplate) ?></textarea>
|
|
<div class="form-text mt-2">
|
|
<strong>Available Variables:</strong><br>
|
|
<code>{customer_name}</code>, <code>{company_name}</code>, <code>{order_id}</code>,
|
|
<code>{order_details}</code> (list of items), <code>{total_amount}</code>,
|
|
<code>{points_earned}</code>, <code>{points_redeemed}</code>, <code>{new_balance}</code>.
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (has_permission('settings_add')): ?>
|
|
<div class="mb-3 border-top pt-3">
|
|
<label class="form-label text-muted small">Test Configuration</label>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" name="test_phone" placeholder="e.g. 62812345678" value="<?= htmlspecialchars($_POST['test_phone'] ?? '') ?>">
|
|
<button type="submit" name="action" value="test" class="btn btn-info text-white">Test & Send Message</button>
|
|
</div>
|
|
<small class="text-muted">Enter a phone number to send a real test message.</small>
|
|
</div>
|
|
|
|
<div class="d-flex justify-content-end">
|
|
<button type="submit" name="action" value="save" class="btn btn-success">Save Settings</button>
|
|
</div>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/includes/header.php'; ?>
|