TransactionData.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Data\Pagarme\Order\Parts\Response;
  3. use App\Data\Pagarme\PagarmeResponseData;
  4. final readonly class TransactionData extends PagarmeResponseData
  5. {
  6. public function __construct(
  7. public ?string $id,
  8. public ?string $status,
  9. public ?int $amount,
  10. public ?int $cost,
  11. public ?string $createdAt,
  12. public ?string $acquirerMessage,
  13. public array $gatewayResponse,
  14. public ?string $qrCode = null,
  15. public ?string $qrCodeUrl = null,
  16. public ?string $expiresAt = null,
  17. ) {}
  18. public static function fromArray(array $payload): static
  19. {
  20. return new self(
  21. id: static::arrString($payload, 'id'),
  22. status: static::arrString($payload, 'status'),
  23. amount: static::arrInt($payload, 'amount'),
  24. cost: static::arrInt($payload, 'cost'),
  25. createdAt: static::arrString($payload, 'created_at'),
  26. acquirerMessage: static::arrString($payload, 'acquirer_message'),
  27. gatewayResponse: static::arrArray($payload, 'gateway_response'),
  28. qrCode: static::arrString($payload, 'qr_code'),
  29. qrCodeUrl: static::arrString($payload, 'qr_code_url'),
  30. expiresAt: static::arrString($payload, 'expires_at'),
  31. );
  32. }
  33. public function toArray(): array
  34. {
  35. return array_filter([
  36. 'id' => $this->id,
  37. 'status' => $this->status,
  38. 'amount' => $this->amount,
  39. 'cost' => $this->cost,
  40. 'created_at' => $this->createdAt,
  41. 'acquirer_message' => $this->acquirerMessage,
  42. 'gateway_response' => $this->gatewayResponse,
  43. 'qr_code' => $this->qrCode,
  44. 'qr_code_url' => $this->qrCodeUrl,
  45. 'expires_at' => $this->expiresAt,
  46. ], static fn ($v) => $v !== null);
  47. }
  48. }