status = $status; $this->errorCode = $code; $this->requestId = $requestId; $this->body = $body; } } /** Dependency-light GateProxy reseller API v1.2 client. */ class Client { private $apiKey; private $baseUrl; private $timeout; private $maxRetries; public function __construct(string $apiKey, string $baseUrl = 'https://api.gateproxy.store', int $timeout = 100, int $maxRetries = 2) { $this->apiKey = $apiKey; $this->baseUrl = rtrim($baseUrl, '/'); $this->timeout = $timeout; $this->maxRetries = max(0, $maxRetries); } private function key(?string $value = null): string { if ($value === null) $value = bin2hex(random_bytes(16)); if (!preg_match('/^[A-Za-z0-9_\\-:.]{8,64}$/', $value)) { throw new \InvalidArgumentException('idempotencyKey must be 8-64 allowed characters'); } return $value; } private static function clean(array $values): array { return array_filter($values, static function ($value) { return $value !== null; }); } private function request(string $method, string $path, array $query = [], $body = null, ?string $idempotencyKey = null, bool $auth = true, bool $text = false) { $query = self::clean($query); foreach ($query as &$value) { if (is_bool($value)) $value = $value ? 'true' : 'false'; } unset($value); if ($query) $path .= '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986); $headers = ['Accept: ' . ($text ? 'text/csv, text/plain' : 'application/json')]; if ($auth) $headers[] = 'Authorization: Bearer ' . $this->apiKey; if ($idempotencyKey !== null) $headers[] = 'Idempotency-Key: ' . $this->key($idempotencyKey); $payload = null; if ($body !== null) { $payload = json_encode($body); if ($payload === false) throw new \InvalidArgumentException('Request body is not JSON-encodable'); $headers[] = 'Content-Type: application/json'; } $canRetry = $method === 'GET' || ($method === 'POST' && $idempotencyKey !== null); $attempts = $canRetry ? $this->maxRetries + 1 : 1; for ($attempt = 0; $attempt < $attempts; $attempt++) { $ch = curl_init($this->baseUrl . $path); curl_setopt_array($ch, [ CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_POSTFIELDS => $payload, ]); $raw = curl_exec($ch); if ($raw === false) { $message = curl_error($ch); curl_close($ch); throw new GateProxyException(0, 'network_error', $message); } $status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); $headerSize = (int) curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headerText = substr($raw, 0, $headerSize); $content = substr($raw, $headerSize); curl_close($ch); $parsed = null; if ($content !== '') { $parsed = json_decode($content, true); if (json_last_error() !== JSON_ERROR_NONE) $parsed = ['raw' => $content]; } if ($status >= 200 && $status < 300) return $text ? $content : $parsed; if (in_array($status, [429, 502, 503, 504], true) && $attempt + 1 < $attempts) { $delay = 2 ** $attempt; if (preg_match('/^Retry-After:\\s*([0-9.]+)/mi', $headerText, $match)) $delay = (float) $match[1]; usleep((int) (min(30, max(0, $delay)) * 1000000)); continue; } $error = is_array($parsed) && isset($parsed['error']) ? $parsed['error'] : []; throw new GateProxyException($status, $error['code'] ?? 'http_error', $error['message'] ?? ('HTTP ' . $status), $error['request_id'] ?? null, $parsed); } return null; } private static function id($value): string { return rawurlencode((string) $value); } public function health() { return $this->request('GET', '/v1/health', [], null, null, false); } public function status() { return $this->request('GET', '/v1/status', [], null, null, false); } public function me() { return $this->request('GET', '/v1/me'); } public function balance() { return $this->request('GET', '/v1/balance'); } public function products() { return $this->request('GET', '/v1/products'); } public function product(string $product) { return $this->request('GET', '/v1/products/' . self::id($product)); } public function createOrder(string $product, string $plan, array $options = []) { $key = $this->key($options['idempotencyKey'] ?? null); $body = self::clean(['product' => $product, 'plan' => $plan, 'max_price_usd' => $options['maxPriceUsd'] ?? null, 'quote_id' => $options['quoteId'] ?? null, 'client_reference' => $options['clientReference'] ?? null, 'metadata' => $options['metadata'] ?? null]); return $this->request('POST', '/v1/orders', ['wait' => $options['wait'] ?? true], $body, $key); } public function createBatch(string $product, string $plan, int $quantity, array $options = []) { $body = self::clean(['product' => $product, 'plan' => $plan, 'quantity' => $quantity, 'max_price_usd' => $options['maxPriceUsd'] ?? null, 'client_reference' => $options['clientReference'] ?? null, 'metadata' => $options['metadata'] ?? null]); return $this->request('POST', '/v1/orders/batch', [], $body, $this->key($options['idempotencyKey'] ?? null)); } public function listOrders(array $filters = []) { return $this->request('GET', '/v1/orders', $filters); } public function iterOrders(array $filters = []): \Generator { do { $page = $this->listOrders($filters); foreach ($page['orders'] ?? [] as $order) yield $order; $cursor = $page['next_before'] ?? null; if ($cursor) $filters['before'] = $cursor; } while ($cursor); } public function getOrder(string $id) { return $this->request('GET', '/v1/orders/' . self::id($id)); } public function orderUsage(string $id) { return $this->request('GET', '/v1/orders/' . self::id($id) . '/usage'); } public function resetOrderPassword(string $id) { return $this->request('POST', '/v1/orders/' . self::id($id) . '/reset-password', [], []); } public function disableOrder(string $id) { return $this->request('POST', '/v1/orders/' . self::id($id) . '/disable', [], []); } public function enableOrder(string $id) { return $this->request('POST', '/v1/orders/' . self::id($id) . '/enable', [], []); } public function orderProxies(string $id, array $params = []) { return $this->request('GET', '/v1/orders/' . self::id($id) . '/proxies', $params, null, null, true, ($params['output'] ?? '') === 'text'); } public function createQuote(string $product, string $plan, ?int $quantity = null) { return $this->request('POST', '/v1/quotes', [], self::clean(compact('product', 'plan', 'quantity'))); } public function getQuote(string $id) { return $this->request('GET', '/v1/quotes/' . self::id($id)); } public function listTransactions(array $filters = []) { return $this->request('GET', '/v1/transactions', $filters); } public function exportOrdersCsv(array $filters = []): string { return $this->request('GET', '/v1/orders/export.csv', $filters, null, null, true, true); } public function exportTransactionsCsv(array $filters = []): string { return $this->request('GET', '/v1/transactions/export.csv', $filters, null, null, true, true); } public function stats(array $filters = []) { return $this->request('GET', '/v1/stats', $filters); } public function statement(string $month) { return $this->request('GET', '/v1/statements/' . self::id($month)); } public function listKeys() { return $this->request('GET', '/v1/keys'); } public function createKey(string $mode, string $name, ?int $expiresInDays = null) { return $this->request('POST', '/v1/keys', [], self::clean(['mode' => $mode, 'name' => $name, 'expires_in_days' => $expiresInDays])); } public function rotateKey(string $id, ?int $graceHours = null) { return $this->request('POST', '/v1/keys/' . self::id($id) . '/rotate', [], self::clean(['grace_hours' => $graceHours])); } public function deleteKey(string $id) { return $this->request('DELETE', '/v1/keys/' . self::id($id)); } public function getWebhook() { return $this->request('GET', '/v1/webhook'); } public function setWebhook(string $url) { return $this->request('POST', '/v1/webhook', [], ['url' => $url]); } public function updateWebhook(array $options = []) { return $this->request('PATCH', '/v1/webhook', [], self::clean(['events' => $options['events'] ?? null, 'low_balance_usd' => $options['lowBalanceUsd'] ?? null, 'low_traffic_percent' => $options['lowTrafficPercent'] ?? null])); } public function deleteWebhook() { return $this->request('DELETE', '/v1/webhook'); } public function testWebhook() { return $this->request('POST', '/v1/webhook/test', [], []); } public function listWebhookDeliveries(array $filters = []) { return $this->request('GET', '/v1/webhook/deliveries', $filters); } public function resendWebhookDelivery(string $id) { return $this->request('POST', '/v1/webhook/deliveries/' . self::id($id) . '/resend', [], []); } public function depositInfo() { return $this->request('GET', '/v1/deposits/info'); } public function verifyDeposit(string $reference, string $method = 'binance') { return $this->request('POST', '/v1/deposits/verify', [], compact('method', 'reference')); } public function createRestockAlert(string $product, string $plan) { return $this->request('POST', '/v1/restock-alerts', [], compact('product', 'plan')); } /** Verify the signature against the unmodified request body. */ public static function verifyWebhook(string $secret, string $rawBody, string $header, int $tolerance = 300): bool { $parts = []; foreach (explode(',', $header) as $part) { $pair = explode('=', $part, 2); if (count($pair) === 2) $parts[$pair[0]] = $pair[1]; } if (!isset($parts['t'], $parts['v1']) || !ctype_digit($parts['t'])) return false; if (abs(time() - (int) $parts['t']) > $tolerance) return false; $expected = hash_hmac('sha256', $parts['t'] . '.' . $rawBody, $secret); return hash_equals($expected, $parts['v1']); } }