94 lines
3.3 KiB
PHP
94 lines
3.3 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'Method not allowed']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$name = trim($input['name'] ?? '');
|
|
$phone = trim($input['phone'] ?? '');
|
|
|
|
if (empty($name)) {
|
|
echo json_encode(['error' => 'Name is required']);
|
|
exit;
|
|
}
|
|
|
|
// Relaxed phone validation: 8 to 15 digits
|
|
if (!preg_match('/^\d{8,15}$/', $phone)) {
|
|
echo json_encode(['error' => 'Phone number must be between 8 and 15 digits']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Check if phone already exists
|
|
$stmt = $pdo->prepare("SELECT id FROM customers WHERE phone = ?");
|
|
$stmt->execute([$phone]);
|
|
if ($stmt->fetch()) {
|
|
echo json_encode(['error' => 'Customer with this phone number already exists']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO customers (name, phone, points) VALUES (?, ?, 0)");
|
|
if ($stmt->execute([$name, $phone])) {
|
|
$id = $pdo->lastInsertId();
|
|
|
|
// Fetch settings for consistency (though new customer is 0 points)
|
|
$settingsStmt = $pdo->query("SELECT points_for_free_meal FROM loyalty_settings WHERE id = 1");
|
|
$settings = $settingsStmt->fetch(PDO::FETCH_ASSOC);
|
|
$threshold = $settings ? intval($settings['points_for_free_meal']) : 70;
|
|
|
|
// --- Send Welcome WhatsApp Message via Wablas ---
|
|
try {
|
|
require_once __DIR__ . '/../includes/WablasService.php';
|
|
$wablas = new WablasService($pdo);
|
|
|
|
$companyStmt = $pdo->query("SELECT company_name FROM company_settings LIMIT 1");
|
|
$companyName = $companyStmt->fetchColumn() ?: 'Our Restaurant';
|
|
|
|
// Fetch welcome template
|
|
$templateStmt = $pdo->query("SELECT setting_value FROM integration_settings WHERE provider='wablas' AND setting_key='welcome_template'");
|
|
$welcomeTemplate = $templateStmt->fetchColumn();
|
|
|
|
if (empty($welcomeTemplate)) {
|
|
$welcomeTemplate = "Welcome *{customer_name}* to *{company_name}*! 🎉\n\nThank you for registering. You can now earn loyalty points with every order!\n\nYou currently have 0 points. Collect {points_threshold} points to earn a free meal!";
|
|
}
|
|
|
|
$welcomeMsg = str_replace(
|
|
['{customer_name}', '{company_name}', '{points_threshold}'],
|
|
[$name, $companyName, $threshold],
|
|
$welcomeTemplate
|
|
);
|
|
|
|
|
|
$wablas->sendMessage($phone, $welcomeMsg);
|
|
} catch (Exception $w) {
|
|
error_log("Wablas Welcome Msg Exception: " . $w->getMessage());
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'customer' => [
|
|
'id' => $id,
|
|
'name' => $name,
|
|
'phone' => $phone,
|
|
'email' => '',
|
|
'points' => 0,
|
|
'eligible_for_free_meal' => false,
|
|
'points_needed' => $threshold
|
|
]
|
|
]);
|
|
} else {
|
|
echo json_encode(['error' => 'Failed to create customer']);
|
|
}
|
|
} catch (Exception $e) {
|
|
error_log("Create Customer Error: " . $e->getMessage());
|
|
echo json_encode(['error' => 'Database error']);
|
|
}
|