Files
2026-08-20 18:39:37 +07:00

73 lines
1.7 KiB
PHP

<?php
// webhook_receiver.php
include __DIR__ . "/../config/connect.php";
// =========================
// GET : Verifikasi Webhook
// =========================
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$mode = $_GET['hub_mode'] ?? $_GET['hub.mode'] ?? null;
$token = $_GET['hub_verify_token'] ?? $_GET['hub.verify_token'] ?? null;
$challenge = $_GET['hub_challenge'] ?? $_GET['hub.challenge'] ?? null;
$phone_number_id = $_GET['phone_number_id'] ?? null;
if (!$phone_number_id) {
http_response_code(403);
exit;
}
$stmt = $pdo->prepare("
SELECT verify_token
FROM whatsapp_cloud_api
WHERE number_id = :number_id
LIMIT 1
");
$stmt->execute(['number_id' => $phone_number_id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (
$mode === 'subscribe' &&
$row &&
hash_equals($row['verify_token'], (string)$token)
) {
http_response_code(200);
echo $challenge;
} else {
http_response_code(403);
}
exit;
}
// =========================
// POST : Terima Webhook
// =========================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$raw = file_get_contents('php://input');
$json = json_decode($raw, true);
if (!$json || !isset($json['entry'])) {
http_response_code(400);
exit;
}
// Simpan ke queue
$stmt = $pdo->prepare("
INSERT INTO whatsapp_webhook_queue
(payload, source, status, created_at)
VALUES (:payload, 'meta', 'new', NOW())
");
$stmt->execute([
'payload' => json_encode($json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
]);
http_response_code(200);
echo "EVENT_RECEIVED";
exit;
}
http_response_code(404);