baseUrl = rtrim($baseUrl, "/"); } /** Rechnung anlegen und finalisieren. Ohne $idempotencyKey wird eine UUID erzeugt. */ public function createInvoice(array $invoice, ?string $idempotencyKey = null): array { return $this->request("POST", "/api/v1/invoices", json: $invoice, idempotencyKey: $idempotencyKey ?? self::uuid()); } /** Rechnungsliste; $params z. B. ["page" => 1, "pageSize" => 50, "status" => "finalized"]. */ public function listInvoices(array $params = []): array { $query = http_build_query(array_filter($params, fn($v) => $v !== null && $v !== "")); return $this->request("GET", "/api/v1/invoices" . ($query !== "" ? "?" . $query : "")); } public function getInvoice(string $invoiceId): array { return $this->request("GET", "/api/v1/invoices/" . rawurlencode($invoiceId)); } /** * Rechnung per E-Mail versenden. Nicht wiederholungssicher: jeder erfolgreiche Aufruf verschickt * eine E-Mail. Nach einem Timeout zuerst mit getInvoice() prüfen, ob sentAt gesetzt ist. */ public function sendInvoice(string $invoiceId, string $recipientEmail, ?string $subject = null, ?string $message = null): array { return $this->request( "POST", "/api/v1/invoices/" . rawurlencode($invoiceId) . "/send", json: ["recipientEmail" => $recipientEmail, "subject" => $subject, "message" => $message] ); } /** PDF als Binärstring. */ public function getInvoicePdf(string $invoiceId): string { return $this->request("GET", "/api/v1/invoices/" . rawurlencode($invoiceId) . "/pdf", accept: "application/pdf"); } /** E-Rechnung prüfen (Pfad zu XML/PDF). Liefert das Prüfergebnis mit Befunden. */ public function validate(string $filePath, bool $preview = false): array { $file = new CURLFile($filePath, mime_content_type($filePath) ?: "application/octet-stream", basename($filePath)); return $this->request("POST", "/api/v1/validate" . ($preview ? "?preview=true" : ""), form: ["file" => $file]); } private function request(string $method, string $path, ?array $json = null, ?array $form = null, ?string $idempotencyKey = null, string $accept = "application/json"): mixed { $headers = ["Authorization: Bearer " . $this->token, "Accept: " . $accept]; if ($idempotencyKey !== null) { $headers[] = "Idempotency-Key: " . $idempotencyKey; } $ch = curl_init($this->baseUrl . $path); $options = [ CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $this->timeoutSeconds, CURLOPT_FOLLOWLOCATION => false, ]; if ($json !== null) { $headers[] = "Content-Type: application/json"; $options[CURLOPT_POSTFIELDS] = json_encode($json, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR); } elseif ($form !== null) { $options[CURLOPT_POSTFIELDS] = $form; // curl setzt multipart/form-data selbst } $options[CURLOPT_HTTPHEADER] = $headers; curl_setopt_array($ch, $options); $body = curl_exec($ch); if ($body === false) { $fehler = curl_error($ch); curl_close($ch); throw new BillvoxException(0, "NETWORK", "Billvox nicht erreichbar: " . $fehler); } $status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); $contentType = (string) curl_getinfo($ch, CURLINFO_CONTENT_TYPE); curl_close($ch); if ($status >= 400) { $fehler = json_decode((string) $body, true) ?: []; throw new BillvoxException( $status, (string) ($fehler["code"] ?? "UNKNOWN"), (string) ($fehler["message"] ?? "HTTP " . $status), (array) ($fehler["details"] ?? []) ); } if (str_contains($contentType, "application/json")) { return json_decode((string) $body, true, 512, JSON_THROW_ON_ERROR); } return (string) $body; } private static function uuid(): string { $bytes = random_bytes(16); $bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x40); $bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80); return vsprintf("%s%s-%s-%s-%s-%s%s%s", str_split(bin2hex($bytes), 4)); } }