Files
flash-call-otp/backend/api/whatsapp-webhook.php
2026-06-24 15:01:02 +03:00

146 lines
4.5 KiB
PHP

<?php
/**
* WhatsApp Gateway Webhook Receiver & QR Code Viewer (6 Slots)
*/
require_once __DIR__ . '/../includes/Redis.php';
require_once __DIR__ . '/../includes/Auth.php';
$redis = RedisClient::getInstance();
// Handle Gateway POST requests (state changes, QR code delivery)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$secret = $_SERVER['HTTP_X_WEBHOOK_SECRET'] ?? '';
$expectedSecret = $_ENV['WHATSAPP_WEBHOOK_SECRET'] ?? $_SERVER['WHATSAPP_WEBHOOK_SECRET'] ?? getenv('WHATSAPP_WEBHOOK_SECRET') ?: 'flash_call_otp_webhook_secret_key';
if ($secret !== $expectedSecret) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'unauthorized']);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
if ($input && isset($input['state']) && isset($input['session_key'])) {
$state = $input['state'];
$sessionKey = $input['session_key'];
$redis->set("whatsapp:{$sessionKey}:status", $state);
if ($state === 'waiting_qr' && isset($input['qr_code'])) {
$redis->setex("whatsapp:{$sessionKey}:qr", 60, $input['qr_code']);
} elseif ($state === 'connected') {
$redis->del("whatsapp:{$sessionKey}:qr");
if (isset($input['phone'])) {
$redis->set("whatsapp:{$sessionKey}:phone", $input['phone']);
}
} elseif ($state === 'disconnected') {
$redis->del("whatsapp:{$sessionKey}:qr");
$redis->del("whatsapp:{$sessionKey}:phone");
}
}
echo json_encode(['success' => true]);
exit;
}
// Prepare slots data for UI
$slots = [];
for ($i = 1; $i <= 6; $i++) {
$sk = "slot-{$i}";
$slots[$sk] = [
'status' => $redis->get("whatsapp:{$sk}:status") ?: 'disconnected',
'qr' => $redis->get("whatsapp:{$sk}:qr"),
'phone' => $redis->get("whatsapp:{$sk}:phone")
];
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>إعداد بوابة الواتساب — Flash Call OTP</title>
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<style>
body {
font-family: 'Cairo', sans-serif;
background-color: #f0f4f8;
color: #333;
margin: 0;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 30px;
}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
gap: 20px;
max-width: 1200px;
margin: 0 auto;
}
.card {
background: #fff;
padding: 25px;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(0,0,0,0.05);
text-align: center;
}
.slot-title {
font-size: 18px;
font-weight: 700;
margin-bottom: 15px;
color: #2c3e50;
}
.status-badge {
display: inline-block;
padding: 6px 16px;
border-radius: 20px;
font-weight: 600;
font-size: 14px;
margin-bottom: 15px;
}
.status-connected { background-color: #d1e7dd; color: #0f5132; }
.status-waiting_qr { background-color: #fff3cd; color: #664d03; }
.status-disconnected { background-color: #f8d7da; color: #842029; }
.qrcode-container {
display: flex;
justify-content: center;
margin: 15px 0;
padding: 10px;
background: #fff;
border: 1px solid #eee;
border-radius: 8px;
min-height: 200px;
align-items: center;
}
.refresh-btn {
background-color: #0d6efd;
color: white;
border: none;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-family: 'Cairo', sans-serif;
font-weight: 600;
margin-top: 10px;
transition: 0.2s;
}
.refresh-btn:hover { background-color: #0b5ed7; }
.global-refresh {
display: block;
margin: 20px auto;
font-size: 16px;
padding: 12px 24px;
}
</style>
</head>
<body>
</div>
</div>
</body>
</html>