<?php
function safe_str($data) {
    return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
}

function paymentapi($key) {
    // Replace this with your actual token & URL logic
    $map = [
        'akupi_token' => 'e4df63a705d5fffa3cb4d675ce88a9c0',
        'gateway_Url' => 'upi.mastpe.in'
    ];
    return $map[$key] ?? '';
}

function logPaymentResponse($order_id, $user_id, $response, $request_data = [], $raw_curl_response = '', $curl_error = '') {
    $log_file = __DIR__ . '/logs/payment_response.log';
    $timestamp = date('Y-m-d H:i:s');

    $log_data = "==========================\n";
    $log_data .= "[$timestamp]\n";
    $log_data .= "ORDER_ID: $order_id\n";
    $log_data .= "USER_ID: $user_id\n";

    if (!empty($curl_error)) {
        $log_data .= "CURL ERROR: $curl_error\n";
    }

    $log_data .= "REQUEST:\n" . json_encode($request_data, JSON_PRETTY_PRINT) . "\n";
    $log_data .= "RAW CURL RESPONSE:\n" . $raw_curl_response . "\n";

    if (!empty($response)) {
        $log_data .= "PARSED RESPONSE:\n" . json_encode($response, JSON_PRETTY_PRINT) . "\n";
    } else {
        $log_data .= "PARSED RESPONSE: NULL (Failed to decode JSON)\n";
    }

    $log_data .= "==========================\n\n";

    file_put_contents($log_file, $log_data, FILE_APPEND);
}

?>
