PagarmeOrderResponseData.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Data\Pagarme\Response\PagarmeOrderResponseData;
  3. use App\Enums\PaymentStatusEnum;
  4. /**
  5. * @param array<int, array> $items Itens do pedido (code, amount, quantity, description, etc.)
  6. * @param array<string, mixed>|null $customer Dados do cliente retornados pela API
  7. * @param array<int, array> $charges Cobrancas do pedido (status, amount, payment_method, last_transaction, etc.)
  8. * @param array<int, array> $checkouts Checkouts do pedido
  9. * @param array<string, mixed> $metadata Metadados retornados pela API
  10. */
  11. final readonly class PagarmeOrderResponseData
  12. {
  13. public function __construct(
  14. public ?string $id,
  15. public ?string $code,
  16. public ?int $amount,
  17. public ?string $currency,
  18. public ?bool $closed,
  19. public ?string $status,
  20. public array $items,
  21. public ?array $customer,
  22. public array $charges,
  23. public array $checkouts,
  24. public array $metadata,
  25. public ?string $createdAt = null,
  26. public ?string $updatedAt = null,
  27. public ?string $closedAt = null,
  28. ) {}
  29. public static function fromArray(array $payload): self
  30. {
  31. return new self(
  32. id: $payload['id'] ?? null,
  33. code: $payload['code'] ?? null,
  34. amount: isset($payload['amount']) ? (int) $payload['amount'] : null,
  35. currency: $payload['currency'] ?? null,
  36. closed: $payload['closed'] ?? null,
  37. status: $payload['status'] ?? null,
  38. items: $payload['items'] ?? [],
  39. customer: ! empty($payload['customer']) ? $payload['customer'] : null,
  40. charges: $payload['charges'] ?? [],
  41. checkouts: $payload['checkouts'] ?? [],
  42. metadata: $payload['metadata'] ?? [],
  43. createdAt: $payload['created_at'] ?? null,
  44. updatedAt: $payload['updated_at'] ?? null,
  45. closedAt: $payload['closed_at'] ?? null,
  46. );
  47. }
  48. public function id(): ?string
  49. {
  50. return $this->id;
  51. }
  52. public function requireId(): string
  53. {
  54. if (! $this->id) {
  55. throw new \RuntimeException('Pagar.me order creation returned an empty id.');
  56. }
  57. return $this->id;
  58. }
  59. public function firstCharge(): array
  60. {
  61. return $this->charges[0] ?? [];
  62. }
  63. public function lastTransaction(): array
  64. {
  65. return $this->firstCharge()['last_transaction'] ?? [];
  66. }
  67. public function gatewayEntityReference(): ?string
  68. {
  69. $charge = $this->firstCharge();
  70. return $charge['id'] ?? $this->id;
  71. }
  72. public function gatewayEntityLabel(): string
  73. {
  74. return isset($this->firstCharge()['id']) ? 'charge' : 'order';
  75. }
  76. public function gatewayOperationReference(): ?string
  77. {
  78. $charge = $this->firstCharge();
  79. $transaction = $this->lastTransaction();
  80. return $transaction['id'] ?? $charge['id'] ?? $this->id;
  81. }
  82. public function gatewayOperationLabel(): string
  83. {
  84. $charge = $this->firstCharge();
  85. $transaction = $this->lastTransaction();
  86. return isset($transaction['id']) ? 'transaction' : (isset($charge['id']) ? 'charge' : 'order');
  87. }
  88. public function paymentStatus(): PaymentStatusEnum
  89. {
  90. $charge = $this->firstCharge();
  91. $transaction = $this->lastTransaction();
  92. $status = strtolower((string) (($transaction['status'] ?? null) ?: ($charge['status'] ?? null)));
  93. return match ($status) {
  94. 'captured', 'paid', 'overpaid' => PaymentStatusEnum::PAID,
  95. 'authorized_pending_capture', 'waiting_capture' => PaymentStatusEnum::AUTHORIZED,
  96. 'pending', 'waiting_payment' => PaymentStatusEnum::PENDING,
  97. 'processing' => PaymentStatusEnum::PROCESSING,
  98. 'not_authorized', 'with_error', 'failed',
  99. 'underpaid', 'chargedback' => PaymentStatusEnum::FAILED,
  100. 'voided', 'partial_void', 'canceled',
  101. 'cancelled', 'refunded', 'partial_refunded',
  102. 'partial_canceled' => PaymentStatusEnum::CANCELLED,
  103. default => PaymentStatusEnum::PENDING,
  104. };
  105. }
  106. public function paidAt(): ?string
  107. {
  108. return $this->filledArrayValue($this->firstCharge(), 'paid_at');
  109. }
  110. public function authorizedAt(): ?string
  111. {
  112. $transaction = $this->lastTransaction();
  113. $transactionStatus = $transaction['status'] ?? null;
  114. if (in_array($transactionStatus, ['authorized_pending_capture', 'captured', 'partial_capture'], true)) {
  115. return $this->filledArrayValue($transaction, 'created_at');
  116. }
  117. return null;
  118. }
  119. public function failureCode(): ?string
  120. {
  121. return $this->filledArrayValue($this->lastTransaction()['gateway_response'] ?? [], 'code');
  122. }
  123. public function failureMessage(): ?string
  124. {
  125. $transaction = $this->lastTransaction();
  126. $acquirerMessage = $this->filledArrayValue($transaction, 'acquirer_message');
  127. if ($acquirerMessage) {
  128. return $acquirerMessage;
  129. }
  130. $gatewayErrors = $transaction['gateway_response']['errors'] ?? [];
  131. if (! is_array($gatewayErrors) || empty($gatewayErrors)) {
  132. return null;
  133. }
  134. $message = collect($gatewayErrors)
  135. ->pluck('message')
  136. ->filter()
  137. ->implode('; ') ?: null;
  138. if ($message && str_contains($message, 'Sem ambiente configurado')) {
  139. return 'Pix não esta habilitado ou configurado neste ambiente do Pagar.me.';
  140. }
  141. return $message;
  142. }
  143. public function toArray(): array
  144. {
  145. return [
  146. 'id' => $this->id,
  147. 'code' => $this->code,
  148. 'amount' => $this->amount,
  149. 'currency' => $this->currency,
  150. 'closed' => $this->closed,
  151. 'items' => $this->items,
  152. 'customer' => $this->customer,
  153. 'status' => $this->status,
  154. 'created_at' => $this->createdAt,
  155. 'updated_at' => $this->updatedAt,
  156. 'closed_at' => $this->closedAt,
  157. 'charges' => $this->charges,
  158. 'checkouts' => $this->checkouts,
  159. 'metadata' => $this->metadata,
  160. ];
  161. }
  162. private function filledArrayValue(array $data, string $field): ?string
  163. {
  164. if (! array_key_exists($field, $data) || $data[$field] === null || $data[$field] === '' || $data[$field] === []) {
  165. return null;
  166. }
  167. return (string) $data[$field];
  168. }
  169. }