$payload */ public function createCheckout(array $payload): array { $res = Http::withToken($this->token())->acceptJson()->timeout(15)->post($this->base().'/checkouts', $payload); return $this->jsonOrFail($res, 'Could not start checkout. Please try again.'); } public function verify(string $reference): array { $res = Http::withToken($this->token())->acceptJson()->timeout(15)->post($this->base().'/checkouts/verify', [ 'reference' => $reference, ]); return $this->jsonOrFail($res, 'Could not verify payment. Please try again.'); } public function show(string $reference): array { $res = Http::withToken($this->token())->acceptJson()->timeout(10)->get($this->base().'/orders/'.$reference); return $this->jsonOrFail($res, 'Could not load payment order.'); } /** @return array */ private function jsonOrFail(Response $res, string $fallback): array { if ($res->failed()) { throw new RuntimeException($this->errorMessage($res, $fallback)); } return (array) $res->json(); } private function errorMessage(Response $res, string $fallback): string { $message = $res->json('error') ?? $res->json('message'); if (is_array($message)) { $message = collect($message)->flatten()->first(); } $message = trim((string) ($message ?? '')); return $message !== '' ? $message : $fallback; } }