J

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // ======================= // ⚙️ الإعدادات // ======================= function getSetting($key) { global $db; $stmt = $db->prepare("SELECT value FROM settings WHERE key = ?"); $stmt->execute([$key]); return $stmt->fetchColumn(); } // ======================= // 📲 واتساب API // ======================= function sendWhatsApp($to, $message) { $url = getSetting('wa_api_url'); $token = getSetting('wa_api_token'); $project = getSetting('wa_project'); $data = [ "to" => $to, "message" => $message, "project_name" => $project ]; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, CURLOPT_HTTPHEADER => [ "Authorization: Bearer $token", "Content-Type: application/json" ], CURLOPT_POSTFIELDS => json_encode($data, JSON_UNESCAPED_UNICODE) ]); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); } // ======================= // 🔑 نظام الأكواد // ======================= function getProductCode($product_id) { global $db; try { $db->beginTransaction(); $stmt = $db->prepare(" SELECT * FROM product_codes WHERE product_id = ? AND is_used = 0 LIMIT 1 "); $stmt->execute([$product_id]); $code = $stmt->fetch(PDO::FETCH_ASSOC); if (!$code) { $db->rollBack(); return false; } $update = $db->prepare(" UPDATE product_codes SET is_used = 1, used_at = datetime('now') WHERE id = ? "); $update->execute([$code['id']]); $db->commit(); return $code['code']; } catch (Exception $e) { $db->rollBack(); return false; } } // ======================= // 📦 المنتجات // ======================= function getProduct($id) { global $db; $stmt = $db->prepare("SELECT * FROM products WHERE id = ?"); $stmt->execute([$id]); return $stmt->fetch(PDO::FETCH_ASSOC); } // ======================= // 💳 بعد الدفع (تشغيل أساسي) // ======================= function handlePurchase($product_id, $phone) { $product = getProduct($product_id); if (!$product) { return "المنتج غير موجود"; } $code = getProductCode($product_id); if (!$code) { sendWhatsApp($phone, "❌ لا توجد أكواد متوفرة حالياً"); return "No codes"; } $message = "✅ تم شراء: " . $product['name'] . "\n\n"; $message .= "🔑 كود التفعيل:\n" . $code; sendWhatsApp($phone, $message); return $code; } // ======================= // 📄 الصفحات // ======================= function getPage($slug) { global $db; $stmt = $db->prepare("SELECT * FROM pages WHERE slug = ?"); $stmt->execute([$slug]); return $stmt->fetch(PDO::FETCH_ASSOC); } // ======================= // 🎨 الثيمات // ======================= function loadTheme($page) { $theme = getSetting('active_theme') ?? 'default'; echo ""; echo ""; echo "
"; echo $page['content']; echo "
"; echo ""; }
العودة للمتجر
});