<?php
// API configuration loaded from database

// Include database connection
require_once 'config.php';

// Load global API settings from database
$global_api_enabled = false;
$stmt = $conn->prepare("SELECT config_value FROM payment_config WHERE config_key = 'api_enabled'");
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
    $global_api_enabled = (bool)$row['config_value'];
}
$stmt->close();

// Set global constant
define('EXTERNAL_API_ENABLED', $global_api_enabled);

// Optional logging flags
define('API_LOG_ENABLED', true);
define('API_LOG_FILE', 'api_purchase.log');

// Load network configurations from database
$network_mapping = [];
$external_api_networks = [];
$api_endpoints = [];

if ($global_api_enabled) {
    $stmt = $conn->prepare("SELECT network_type, api_endpoint FROM api_config WHERE is_active = 1");
    $stmt->execute();
    $result = $stmt->get_result();
    
    while ($row = $result->fetch_assoc()) {
        $network_mapping[$row['network_type']] = $row['network_type'];
        $external_api_networks[] = $row['network_type'];
        $api_endpoints[$row['network_type']] = $row['api_endpoint'];
    }
    $stmt->close();
}

// Helper function to get network mapping
function getExternalNetworkCode($internal_network) {
    global $network_mapping;
    return $network_mapping[$internal_network] ?? null;
}

// Helper function to check if network should use external API
function shouldUseExternalAPI($network) {
    global $external_api_networks;
    return EXTERNAL_API_ENABLED && in_array($network, $external_api_networks);
}

// Helper function to get API endpoint for a network
function getAPIEndpoint($network) {
    global $api_endpoints;
    return $api_endpoints[$network] ?? null;
}

// Helper function to get API configuration for a network
function getAPIConfig($network) {
    global $conn;
    
    $stmt = $conn->prepare("SELECT * FROM api_config WHERE network_type = ? AND is_active = 1");
    $stmt->bind_param("s", $network);
    $stmt->execute();
    $result = $stmt->get_result();
    $config = $rehsult->fetch_assoc();
    $stmt->close();
    
    return $config;
}

?>







