<?php
// File: admin/phonepe_helper.php

// ✅ FIX: Include the MAIN config file from the root directory
require_once __DIR__ . '/../phonepe_config.php';

/**
 * Makes a cURL request to the PhonePe API.
 *
 * @param string $payloadBase64 The base64 encoded JSON payload.
 * @param string $endpoint The API endpoint (e.g., /pg/v1/pay or /pg/v1/status/{merchantId}/{merchantTransactionId})
 * @return array The decoded JSON response.
 */
function phonepe_make_request($payloadBase64, $endpoint) {
    
    // ✅ Use constants from the included config file
    $hash_string = $payloadBase64 . $endpoint . $PHONEPE_SALT_KEY;
    $sha256_hash = hash('sha256', $hash_string);
    $x_verify_header = $sha256_hash . '###' . $PHONEPE_SALT_INDEX;

    $ch = curl_init();
    // ✅ Use constant from the included config file
    curl_setopt($ch, CURLOPT_URL, $PHONEPE_HOST_URL . $endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['request' => $payloadBase64]));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'X-VERIFY: ' . $x_verify_header,
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}
?>