$items Itens do pedido (code, amount, quantity, description, etc.) * @param array|null $customer Dados do cliente retornados pela API * @param array $charges Cobrancas do pedido (status, amount, payment_method, last_transaction, etc.) * @param array $checkouts Checkouts do pedido * @param array $metadata Metadados retornados pela API */ final readonly class PagarmeOrderResponseData { public function __construct( public ?string $id, public ?string $code, public ?int $amount, public ?string $currency, public ?bool $closed, public ?string $status, public array $items, public ?array $customer, public array $charges, public array $checkouts, public array $metadata, public ?string $createdAt = null, public ?string $updatedAt = null, public ?string $closedAt = null, ) {} public function authorizedAt(): ?string { $transaction = $this->lastTransaction(); $transactionStatus = $transaction['status'] ?? null; if (in_array($transactionStatus, ['authorized_pending_capture', 'captured', 'partial_capture'], true)) { return $this->filledArrayValue($transaction, 'created_at'); } return null; } public function failureCode(): ?string { return $this->filledArrayValue($this->lastTransaction()['gateway_response'] ?? [], 'code'); } public function failureMessage(): ?string { $transaction = $this->lastTransaction(); $acquirerMessage = $this->filledArrayValue($transaction, 'acquirer_message'); if ($acquirerMessage) { return $acquirerMessage; } $gatewayErrors = $transaction['gateway_response']['errors'] ?? []; if (! is_array($gatewayErrors) || empty($gatewayErrors)) { return null; } $message = collect($gatewayErrors) ->pluck('message') ->filter() ->implode('; ') ?: null; if ($message && str_contains($message, 'Sem ambiente configurado')) { return 'Pix não esta habilitado ou configurado neste ambiente do Pagar.me.'; } return $message; } public function firstCharge(): array { return $this->charges[0] ?? []; } public function gatewayEntityLabel(): string { return isset($this->firstCharge()['id']) ? 'charge' : 'order'; } public function gatewayEntityReference(): ?string { $charge = $this->firstCharge(); return $charge['id'] ?? $this->id; } public function gatewayOperationLabel(): string { $charge = $this->firstCharge(); $transaction = $this->lastTransaction(); return isset($transaction['id']) ? 'transaction' : (isset($charge['id']) ? 'charge' : 'order'); } public function gatewayOperationReference(): ?string { $charge = $this->firstCharge(); $transaction = $this->lastTransaction(); return $transaction['id'] ?? $charge['id'] ?? $this->id; } public function id(): ?string { return $this->id; } public function lastTransaction(): array { return $this->firstCharge()['last_transaction'] ?? []; } public function paymentStatus(): PaymentStatusEnum { $charge = $this->firstCharge(); $transaction = $this->lastTransaction(); $status = strtolower((string) (($transaction['status'] ?? null) ?: ($charge['status'] ?? null))); return match ($status) { 'captured', 'paid', 'overpaid' => PaymentStatusEnum::PAID, 'authorized_pending_capture', 'waiting_capture' => PaymentStatusEnum::AUTHORIZED, 'pending', 'waiting_payment' => PaymentStatusEnum::PENDING, 'processing' => PaymentStatusEnum::PROCESSING, 'not_authorized', 'with_error', 'failed', 'underpaid', 'chargedback' => PaymentStatusEnum::FAILED, 'voided', 'partial_void', 'canceled', 'cancelled', 'refunded', 'partial_refunded', 'partial_canceled' => PaymentStatusEnum::CANCELLED, default => PaymentStatusEnum::PENDING, }; } public function paidAt(): ?string { return $this->filledArrayValue($this->firstCharge(), 'paid_at'); } public function requireId(): string { if (! $this->id) { throw new \RuntimeException('Pagar.me order creation returned an empty id.'); } return $this->id; } // public static function fromArray(array $payload): self { return new self( id: $payload['id'] ?? null, code: $payload['code'] ?? null, amount: isset($payload['amount']) ? (int) $payload['amount'] : null, currency: $payload['currency'] ?? null, closed: $payload['closed'] ?? null, status: $payload['status'] ?? null, items: $payload['items'] ?? [], customer: ! empty($payload['customer']) ? $payload['customer'] : null, charges: $payload['charges'] ?? [], checkouts: $payload['checkouts'] ?? [], metadata: $payload['metadata'] ?? [], createdAt: $payload['created_at'] ?? null, updatedAt: $payload['updated_at'] ?? null, closedAt: $payload['closed_at'] ?? null, ); } public function toArray(): array { return [ 'id' => $this->id, 'code' => $this->code, 'amount' => $this->amount, 'currency' => $this->currency, 'closed' => $this->closed, 'items' => $this->items, 'customer' => $this->customer, 'status' => $this->status, 'created_at' => $this->createdAt, 'updated_at' => $this->updatedAt, 'closed_at' => $this->closedAt, 'charges' => $this->charges, 'checkouts' => $this->checkouts, 'metadata' => $this->metadata, ]; } // private function filledArrayValue(array $data, string $field): ?string { if (! array_key_exists($field, $data) || $data[$field] === null || $data[$field] === '' || $data[$field] === []) { return null; } return (string) $data[$field]; } }