|
|
@@ -30,38 +30,50 @@ final readonly class PagarmeOrderResponseData
|
|
|
public ?string $closedAt = null,
|
|
|
) {}
|
|
|
|
|
|
- public static function fromArray(array $payload): self
|
|
|
+ public function authorizedAt(): ?string
|
|
|
{
|
|
|
- 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,
|
|
|
- );
|
|
|
+ $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 id(): ?string
|
|
|
+ public function failureCode(): ?string
|
|
|
{
|
|
|
- return $this->id;
|
|
|
+ return $this->filledArrayValue($this->lastTransaction()['gateway_response'] ?? [], 'code');
|
|
|
}
|
|
|
|
|
|
- public function requireId(): string
|
|
|
+ public function failureMessage(): ?string
|
|
|
{
|
|
|
- if (! $this->id) {
|
|
|
- throw new \RuntimeException('Pagar.me order creation returned an empty id.');
|
|
|
+ $transaction = $this->lastTransaction();
|
|
|
+
|
|
|
+ $acquirerMessage = $this->filledArrayValue($transaction, 'acquirer_message');
|
|
|
+
|
|
|
+ if ($acquirerMessage) {
|
|
|
+ return $acquirerMessage;
|
|
|
}
|
|
|
|
|
|
- return $this->id;
|
|
|
+ $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
|
|
|
@@ -69,9 +81,9 @@ final readonly class PagarmeOrderResponseData
|
|
|
return $this->charges[0] ?? [];
|
|
|
}
|
|
|
|
|
|
- public function lastTransaction(): array
|
|
|
+ public function gatewayEntityLabel(): string
|
|
|
{
|
|
|
- return $this->firstCharge()['last_transaction'] ?? [];
|
|
|
+ return isset($this->firstCharge()['id']) ? 'charge' : 'order';
|
|
|
}
|
|
|
|
|
|
public function gatewayEntityReference(): ?string
|
|
|
@@ -81,31 +93,40 @@ final readonly class PagarmeOrderResponseData
|
|
|
return $charge['id'] ?? $this->id;
|
|
|
}
|
|
|
|
|
|
- public function gatewayEntityLabel(): string
|
|
|
+ public function gatewayOperationLabel(): string
|
|
|
{
|
|
|
- return isset($this->firstCharge()['id']) ? 'charge' : 'order';
|
|
|
+ $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 gatewayOperationLabel(): string
|
|
|
+ public function id(): ?string
|
|
|
{
|
|
|
- $charge = $this->firstCharge();
|
|
|
- $transaction = $this->lastTransaction();
|
|
|
+ return $this->id;
|
|
|
+ }
|
|
|
|
|
|
- return isset($transaction['id']) ? 'transaction' : (isset($charge['id']) ? 'charge' : 'order');
|
|
|
+ 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) {
|
|
|
@@ -127,48 +148,35 @@ final readonly class PagarmeOrderResponseData
|
|
|
return $this->filledArrayValue($this->firstCharge(), 'paid_at');
|
|
|
}
|
|
|
|
|
|
- public function authorizedAt(): ?string
|
|
|
+ public function requireId(): 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');
|
|
|
+ if (! $this->id) {
|
|
|
+ throw new \RuntimeException('Pagar.me order creation returned an empty id.');
|
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
+ return $this->id;
|
|
|
}
|
|
|
|
|
|
- public function failureCode(): ?string
|
|
|
- {
|
|
|
- return $this->filledArrayValue($this->lastTransaction()['gateway_response'] ?? [], 'code');
|
|
|
- }
|
|
|
+ //
|
|
|
|
|
|
- public function failureMessage(): ?string
|
|
|
+ public static function fromArray(array $payload): self
|
|
|
{
|
|
|
- $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;
|
|
|
+ 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
|
|
|
@@ -191,6 +199,8 @@ final readonly class PagarmeOrderResponseData
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ //
|
|
|
+
|
|
|
private function filledArrayValue(array $data, string $field): ?string
|
|
|
{
|
|
|
if (! array_key_exists($field, $data) || $data[$field] === null || $data[$field] === '' || $data[$field] === []) {
|