<?php
/* =========================================================
   PAYMENT GATEWAY CONFIGURATION
   ========================================================= */
define('PG_BASE_URL', 'https://getaway.winbigplay6.site/api'); // আপনার API মূল URL
define('PG_API_KEY_ID', 'pk_fbc6109b3ad854f2324e8372');          // মার্চেন্ট API Key
define('PG_API_SECRET', '39ced892f4c4bfaa9468e3a9da2d783082ba310225693a97');       // মার্চেন্ট Secret Key

class CV111GatewayClient {
    private $baseUrl;
    private $apiKeyId;
    private $apiSecret;

    public function __construct($baseUrl, $apiKeyId, $apiSecret) {
        $this->baseUrl = rtrim($baseUrl, '/');
        $this->apiKeyId = $apiKeyId;
        $this->apiSecret = $apiSecret;
    }

    private function generateSignature($timestamp, $httpMethod, $requestPath, $payloadString = '') {
        $prehash = $timestamp . strtoupper($httpMethod) . $requestPath . $payloadString;
        return hash_hmac('sha256', $prehash, $this->apiSecret);
    }

    private function makeRequest($endpoint, $method = 'GET', $body = null) {
        $url = $this->baseUrl . $endpoint;
        $parsedUrl = parse_url($url);
        $requestPath = $parsedUrl['path'] . (isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '');
        $timestamp = time();
        $payloadString = (!empty($body) && is_array($body)) ? json_encode($body, JSON_UNESCAPED_SLASHES) : '';

        $signature = $this->generateSignature($timestamp, $method, $requestPath, $payloadString);

        $headers = [
            'Content-Type: application/json',
            'X-PG-KEY: ' . $this->apiKeyId,
            'X-PG-TIMESTAMP: ' . $timestamp,
            'X-PG-SIGNATURE: ' . $signature
        ];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        if (!empty($payloadString)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadString);
        }

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return ['code' => $httpCode, 'data' => json_decode($response, true)];
    }

    public function getWalletNumber($method = 'bkash') {
        $res = $this->makeRequest('/merchant/get-wallet?method=' . urlencode($method), 'GET');
        if ($res['code'] === 200 && isset($res['data']['status']) && $res['data']['status'] === 'success') {
            return $res['data']['wallet_number'];
        }
        return false;
    }

    public function checkByTrxId($trxId, $amount) {
        $res = $this->makeRequest('/merchant/check-trx', 'POST', ['trx_id' => $trxId, 'amount' => (float)$amount]);
        return ($res['code'] === 200 && isset($res['data']['status']) && $res['data']['status'] === 'success');
    }

    public function claimPayment($trxId, $claimedBy) {
        $res = $this->makeRequest('/merchant/claim-payment', 'POST', ['trx_id' => $trxId, 'claimed_by' => $claimedBy]);
        return ($res['code'] === 200 && isset($res['data']['status']) && $res['data']['status'] === 'success');
    }
}
?>