<?php
// getway/sketpay/config.php

require_once __DIR__ . '/../../common/config.php'; 

if (!isset($conn)) {
    die("Error: Database connection (\$conn) not found in root config.php");
}

// Fetch settings from key-value structure
$settings_query = $conn->query("SELECT name, value FROM settings");
$settings = [];
if ($settings_query) {
    while ($row = $settings_query->fetch_assoc()) {
        $settings[$row['name']] = $row['value'];
    }
}

$db_api_key = $settings['np_api_key'] ?? '8QFIJJNzDGGw1qmhdCJcK5xcvuh8PwRXviUfDlLatVhgkXlZv';
$db_base_url = 'https://pay.sketpay.top';

// Site Domain
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https://" : "http://";
$host = $_SERVER['HTTP_HOST'];
$current_domain = $protocol . $host;
$db_domain = $settings['site_domain'] ?? $current_domain;

$db_domain = rtrim($db_domain, '/');

define('NP_API_KEY', $db_api_key);
define('NP_BASE_URL', $db_base_url);
define('BASE_URL', $db_domain);

define('SUCCESS_URL', BASE_URL . '/getway/sketpay/success.php');
define('CANCEL_URL', BASE_URL . '/getway/sketpay/cancel.php');
define('WEBHOOK_URL', BASE_URL . '/getway/sketpay/webhook.php');

if (!function_exists('redirect')) {
    function redirect($url) {
        if (!headers_sent()) {
            header("Location: $url");
        } else {
            echo "<script>window.location.href='$url';</script>";
        }
        exit;
    }
}
?>
