<?php
// RSPay Configuration

// Your RSPay Credentials
$rspay_merchant_id = 'INR222836';
$rspay_key = 'rspay_token_1763975285802'; // This acts as your Secret Key for signing

// Base API URL
$rspay_base_url = 'https://api.rs-pay.cc';

// Callback URLs (Adjust domain as needed)
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
$domain = $_SERVER['SERVER_NAME'];
$callback_url = $protocol . $domain . '/partner/rspay/callback.php';
$payout_callback_url = $protocol . $domain . '/partner/rspay/payout_callback.php';

// Function to generate SHA256 Signature
function generateRspaySign($params, $key) {
    // 1. Sort parameters by key (ASCII ascending) manually if needed, 
    // but the doc says "Sort the values except for the parameter key" -> wait, 
    // actually standard practice and the doc example implies sorting keys alphabetically usually.
    // Let's look at the doc example carefully:
    // amount=100.00&ext=10132031&merchantId=...&key=...
    // This looks like alphabetical sort of KEYS.
    
    ksort($params);

    $signString = '';
    foreach ($params as $k => $v) {
        // "value is null or empty string does not participate"
        if ($v !== null && $v !== '') {
            $signString .= $k . '=' . $v . '&';
        }
    }

    // Append key
    $signString .= 'key=' . $key;

    // Generate SHA256
    return hash('sha256', $signString);
}


// Function to send POST request
function callRspayApi($endpoint, $data) {
    global $rspay_base_url;
    
    $url = $rspay_base_url . $endpoint;
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Enable in production ideally
    
    $response = curl_exec($ch);
    
    if(curl_errno($ch)){
        return ['status' => 'error', 'message' => curl_error($ch)];
    }
    
    curl_close($ch);
    return json_decode($response, true);
}
?>
