toArray() : $payload; $endpoint = $this->pagarmeUrl($path); try { $response = $this->pagarmeHttp($idempotencyKey) ->send($method, $endpoint, ['json' => $payload]) ->throw(); return $response->json() ?? []; } catch (Throwable $e) { Log::channel('pagarme')->error('Pagar.me request failed', [ 'method' => strtoupper($method), 'endpoint' => $endpoint, 'payload' => $payload, 'exception' => $e->getMessage(), ]); } } protected function pagarmeHttp(string $idempotencyKey) { $secretKey = config('services.pagarme.secret_key'); if (empty($secretKey)) { Log::channel('pagarme')->error('PAGARME_SECRET_KEY is not configured.'); throw new \RuntimeException('PAGARME_SECRET_KEY is not configured.'); } return Http::withBasicAuth($secretKey, '') ->withHeaders([ 'Idempotency-Key' => $idempotencyKey, 'Content-Type' => 'application/json', 'Accept' => 'application/json', ]); } protected function pagarmeUrl(string $path): string { return rtrim(config('services.pagarme.base_url'), '/').'/'.ltrim($path, '/'); } }