PaymentData.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Data\Pagarme\Order\Parts\Request;
  3. use App\Data\Pagarme\PagarmeData;
  4. final readonly class PaymentData extends PagarmeData
  5. {
  6. /**
  7. * @param SplitData[]|null $split
  8. */
  9. public function __construct(
  10. public string $paymentMethod,
  11. public ?array $split = null,
  12. public ?CreditCardData $creditCard = null,
  13. public ?PixData $pix = null,
  14. ) {
  15. self::requireIn($this->paymentMethod, ['credit_card', 'pix'], 'payments.payment_method');
  16. if ($this->paymentMethod === 'credit_card' && ! $this->creditCard) {
  17. throw new \InvalidArgumentException('payments.credit_card e obrigatorio para credit_card.');
  18. }
  19. if ($this->paymentMethod === 'pix' && ! $this->pix) {
  20. throw new \InvalidArgumentException('payments.pix e obrigatorio para pix.');
  21. }
  22. }
  23. /**
  24. * @param SplitData[]|null $split
  25. */
  26. public static function creditCard(CreditCardData $creditCard, ?array $split = null): self
  27. {
  28. return new self(
  29. paymentMethod: 'credit_card',
  30. creditCard: $creditCard,
  31. split: $split,
  32. );
  33. }
  34. /**
  35. * @param SplitData[]|null $split
  36. */
  37. public static function pix(PixData $pix, ?array $split = null): self
  38. {
  39. return new self(
  40. paymentMethod: 'pix',
  41. pix: $pix,
  42. split: $split,
  43. );
  44. }
  45. //
  46. public function toArray(): array
  47. {
  48. $payload = $this->filterFilledRecursive([
  49. 'payment_method' => $this->paymentMethod,
  50. 'split' => $this->split,
  51. ]);
  52. if ($this->creditCard) {
  53. $payload['credit_card'] = $this->creditCard->toArray();
  54. }
  55. if ($this->pix) {
  56. $payload['pix'] = $this->pix->toArray();
  57. }
  58. return $payload;
  59. }
  60. }