<?php
class Exchange
{
    
    private $baseUrl = 'https://api.toobit.com';
    private $apiKey = '1YOAsjZhe4eKCwfNFOhFRwsOThxpOdfiPiaxhodU9jmTSQLIpBsLn6mJUxitb9VS';
    private $apiSecret = 'NAouL44SmJ1BJt6RWj6RYdoDGzj59KvqEVGlCfF6RLHOmnGbXxzXKHd1OTpEYPU6';

    

    public function getDepositDetails($uid)
    {
        // Endpoint
        $endpoint = '/api/v1/agent/depositDetailList';

        // Parameters for the request
        $startTime = (time() - 30 * 24 * 60 * 60) * 1000; // 30 days ago in milliseconds
        $endTime = time() * 1000; // Current time in milliseconds
        $pageIndex = 1;
        $pageSize = 100;
        $recvWindow = 5000;
        $timestamp = time() * 1000;

        $params = [
            'startTime' => $startTime,
            'endTime' => $endTime,
            'uid' => $uid,
            'pageIndex' => $pageIndex,
            'pageSize' => $pageSize,
            'recvWindow' => $recvWindow,
            'timestamp' => $timestamp,
        ];

        // Generate the query string
        $queryString = http_build_query($params);

        // Create the HMAC SHA256 signature
        $signature = hash_hmac('sha256', $queryString, $this->apiSecret);

        // Add the signature to the query parameters
        $params['signature'] = $signature;

        // Final query string with the signature
        $queryStringWithSignature = http_build_query($params);

        // Full URL with query string
        $fullUrl = $this->baseUrl . $endpoint . '?' . $queryStringWithSignature;

        // Initialize cURL
        $ch = curl_init($fullUrl);

        // Set cURL options
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'X-BB-APIKEY: ' . $this->apiKey,
        ]);

        // Execute the GET request
        $response = curl_exec($ch);

        // Check for cURL errors
        if (curl_errno($ch)) {
            throw new Exception('Request Error: ' . curl_error($ch));
        }

        // Get the HTTP status code
        $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        // Close cURL
        curl_close($ch);

        // Process the response
        if ($httpStatus == 200) {
            $data = json_decode($response, true);
            if (isset($data['data']['list']) && !empty($data['data']['list'])) {
                return $data['data']['list'];
            } else {
                return [];
            }
        } else {
            throw new Exception("Error: HTTP Status $httpStatus\nResponse: $response");
        }
    }
}

// try {
    

//     $exchange = new Exchange();
//     $uid = 218868372; // Replace with a valid UID

//     $depositDetails = $exchange->getDepositDetails($uid);

//     if (!empty($depositDetails)) {
//         echo "Deposit Details for UID $uid:\n";
//         print_r($depositDetails);
//     } else {
//         echo "No deposit details found for UID $uid.\n";
//     }
// } catch (Exception $e) {
//     echo $e->getMessage();
// }
