namespace App\Services\Api\Providers; use App\Services\Api\BaseApiProvider; class AdvancedApiProvider extends BaseApiProvider { protected function getDataFormat(): string { return 'json'; } protected function authenticate($request) { return $request->withHeaders([ 'X-API-Key' => $this->config['api_key'], 'X-API-Secret' => $this->config['api_secret'], ]); } public function checkConnection(): array { return $this->makeRequest('GET', 'api/v2/health'); } public function getBalance(): array { $response = $this->makeRequest('GET', 'api/v2/wallet/balance'); if ($response['success']) { return [ 'success' => true, 'balance' => (float) $response['data']['available_balance'], 'currency' => $response['data']['currency'] ?? 'USD', 'reserved' => (float) ($response['data']['reserved_balance'] ?? 0), ]; } return [ 'success' => false, 'error' => $response['error'] ?? 'Failed to get balance', ]; } public function getServices(): array { $response = $this->makeRequest('GET', 'api/v2/services'); if ($response['success']) { $services = []; foreach ($response['data']['items'] as $service) { $services[] = $this->mapService($service); } return [ 'success' => true, 'services' => $services, ]; } return [ 'success' => false, 'error' => $response['error'] ?? 'Failed to get services', ]; } public function placeOrder(array $params): array { $response = $this->makeRequest('POST', 'api/v2/orders', [ 'service_code' => $params['service_id'], 'target_url' => $params['link'], 'quantity' => $params['quantity'], 'custom_parameters' => $params['custom_fields'] ?? [], 'webhook_url' => route('api.webhook.provider', $this->provider->id), ]); if ($response['success']) { return [ 'success' => true, 'order_id' => $response['data']['reference_id'], 'status' => $this->mapStatus($response['data']['current_status']), 'start_count' => $response['data']['initial_count'] ?? 0, 'remains' => $response['data']['remaining'] ?? 0, 'estimated_time' => $response['data']['estimated_completion'] ?? null, ]; } return [ 'success' => false, 'error' => $response['error']['message'] ?? 'Failed to place order', 'error_code' => $response['error']['code'] ?? null, ]; } public function getOrderStatus(string $orderId): array { $response = $this->makeRequest('GET', "api/v2/orders/{$orderId}"); if ($response['success']) { return [ 'success' => true, 'status' => $this->mapStatus($response['data']['status']), 'start_count' => $response['data']['processed_count'] ?? 0, 'remains' => $response['data']['remaining'] ?? 0, 'progress' => $response['data']['progress_percentage'] ?? 0, 'completed_at' => $response['data']['completed_at'] ?? null, ]; } return [ 'success' => false, 'error' => $response['error']['message'] ?? 'Failed to get order status', ]; } public function requestRefill(string $orderId): array { $response = $this->makeRequest('POST', "api/v2/orders/{$orderId}/refill"); if ($response['success']) { return [ 'success' => true, 'refill_id' => $response['data']['refill_request_id'], 'estimated_time' => $response['data']['estimated_completion'] ?? null, ]; } return [ 'success' => false, 'error' => $response['error']['message'] ?? 'Failed to request refill', ]; } public function cancelOrder(string $orderId): array { $response = $this->makeRequest('POST', "api/v2/orders/{$orderId}/cancel"); if ($response['success']) { return [ 'success' => true, 'refund_amount' => $response['data']['refund_amount'] ?? 0, 'refund_currency' => $response['data']['currency'] ?? 'USD', ]; } return [ 'success' => false, 'error' => $response['error']['message'] ?? 'Failed to cancel order', ]; } public function formatResponse(array $response, string $action): array { return $response; } protected function mapService(array $providerService): array { return [ 'provider_service_id' => $providerService['code'], 'name' => $providerService['display_name'], 'category' => $providerService['category']['name'] ?? null, 'price' => (float) $providerService['price']['amount'], 'min_order' => (int) ($providerService['limits']['min'] ?? 1), 'max_order' => (int) ($providerService['limits']['max'] ?? null), 'description' => $providerService['description'] ?? null, 'type' => $providerService['service_type'] ?? 'default', 'required_fields' => $providerService['required_parameters'] ?? [], 'our_price' => $this->calculateProfit((float) $providerService['price']['amount']), ]; } protected function mapStatus(string $providerStatus): string { $statusMap = [ 'created' => 'pending', 'queued' => 'pending', 'processing' => 'processing', 'running' => 'in_progress', 'finished' => 'completed', 'partially_completed' => 'partial', 'failed' => 'error', 'cancelled' => 'cancelled', 'refunded' => 'refunded', ]; return $statusMap[strtolower($providerStatus)] ?? 'pending'; } }