namespace App\Services\Api\Providers; use App\Services\Api\BaseApiProvider; class StandardApiProvider extends BaseApiProvider { protected function getDataFormat(): string { return 'form_params'; } protected function authenticate($request) { return $request->withHeaders([ 'Authorization' => 'Bearer ' . $this->config['api_key'], ]); } public function checkConnection(): array { return $this->makeRequest('GET', 'api/v1/status'); } public function getBalance(): array { $response = $this->makeRequest('GET', 'api/v1/balance'); if ($response['success'] && isset($response['data']['balance'])) { return [ 'success' => true, 'balance' => (float) $response['data']['balance'], 'currency' => $response['data']['currency'] ?? 'USD', ]; } return [ 'success' => false, 'error' => $response['error'] ?? 'Failed to get balance', ]; } public function getServices(): array { $response = $this->makeRequest('GET', 'api/v1/services'); if ($response['success'] && isset($response['data']['services'])) { $services = []; foreach ($response['data']['services'] 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/v1/order', [ 'service_id' => $params['service_id'], 'link' => $params['link'], 'quantity' => $params['quantity'], 'custom_data' => $params['custom_fields'] ?? null, ]); if ($response['success'] && isset($response['data']['order_id'])) { return [ 'success' => true, 'order_id' => $response['data']['order_id'], 'status' => $this->mapStatus($response['data']['status'] ?? 'pending'), 'start_count' => $response['data']['start_count'] ?? 0, 'remains' => $response['data']['remains'] ?? 0, ]; } return [ 'success' => false, 'error' => $response['error'] ?? 'Failed to place order', ]; } public function getOrderStatus(string $orderId): array { $response = $this->makeRequest('GET', "api/v1/order/{$orderId}"); if ($response['success']) { return [ 'success' => true, 'status' => $this->mapStatus($response['data']['status'] ?? 'pending'), 'start_count' => $response['data']['start_count'] ?? 0, 'remains' => $response['data']['remains'] ?? 0, 'completed_at' => $response['data']['completed_at'] ?? null, ]; } return [ 'success' => false, 'error' => $response['error'] ?? 'Failed to get order status', ]; } public function requestRefill(string $orderId): array { $response = $this->makeRequest('POST', 'api/v1/refill', [ 'order_id' => $orderId, ]); if ($response['success']) { return [ 'success' => true, 'refill_id' => $response['data']['refill_id'] ?? null, 'message' => $response['data']['message'] ?? 'Refill requested', ]; } return [ 'success' => false, 'error' => $response['error'] ?? 'Failed to request refill', ]; } public function cancelOrder(string $orderId): array { $response = $this->makeRequest('POST', 'api/v1/cancel', [ 'order_id' => $orderId, ]); if ($response['success']) { return [ 'success' => true, 'message' => $response['data']['message'] ?? 'Order cancelled', 'refund_amount' => $response['data']['refund'] ?? 0, ]; } return [ 'success' => false, 'error' => $response['error'] ?? 'Failed to cancel order', ]; } public function formatResponse(array $response, string $action): array { // Format based on provider's specific response structure return $response; } protected function mapService(array $providerService): array { return [ 'provider_service_id' => $providerService['id'], 'name' => $providerService['name'], 'category' => $providerService['category'] ?? null, 'price' => (float) $providerService['price'], 'min_order' => (int) ($providerService['min'] ?? 1), 'max_order' => (int) ($providerService['max'] ?? null), 'description' => $providerService['description'] ?? null, 'type' => $providerService['type'] ?? 'default', 'our_price' => $this->calculateProfit((float) $providerService['price']), ]; } protected function mapStatus(string $providerStatus): string { $statusMap = [ 'pending' => 'pending', 'processing' => 'processing', 'in_progress' => 'in_progress', 'completed' => 'completed', 'partial' => 'partial', 'canceled' => 'cancelled', 'error' => 'error', ]; return $statusMap[strtolower($providerStatus)] ?? 'pending'; } }